Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 353: | Строка 353: | ||
end | end | ||
local function flatten_parts(entry | local function flatten_parts(entry) | ||
if type(entry) ~= "table" then | if type(entry) ~= "table" then | ||
return {} | return {} | ||
end | end | ||
local parts = {} | local parts = {} | ||
local function append_table_json(key, value) | local function append_table_json(key, value) | ||
| Строка 375: | Строка 367: | ||
end | end | ||
local function walk(tbl, | local function walk(tbl, prefix) | ||
local keys = {} | local keys = {} | ||
for k in pairs(tbl) do | for k in pairs(tbl) do | ||
| Строка 387: | Строка 379: | ||
local v = tbl[k] | local v = tbl[k] | ||
local kStr = tostring(k) | local kStr = tostring(k) | ||
local key = ( | local key = (prefix == "" and kStr or prefix .. "." .. kStr) | ||
if type(v) == "table" then | if type(v) == "table" then | ||
| Строка 407: | Строка 399: | ||
end | end | ||
walk(entry, | walk(entry, "") | ||
return parts | return parts | ||
end | end | ||
| Строка 905: | Строка 897: | ||
end | end | ||
function p.flattenParams(entry | function p.flattenParams(entry) | ||
return flatten_parts(entry | return flatten_parts(entry) | ||
end | end | ||
| Строка 1055: | Строка 1047: | ||
local result = table.concat(out, " ") | local result = table.concat(out, " ") | ||
return preprocess_or_return(frame, result) | return preprocess_or_return(frame, result) | ||
end | |||
local function append_param_tree(parts, prefix, value) | |||
if value == nil then | |||
return | |||
end | |||
local t = type(value) | |||
if t ~= "table" then | |||
parts[#parts + 1] = prefix .. "=" .. tostring(value) | |||
return | |||
end | |||
if next(value) == nil then | |||
return | |||
end | |||
if is_array(value) then | |||
for i, v in ipairs(value) do | |||
append_param_tree(parts, prefix .. "." .. i, v) | |||
end | |||
return | |||
end | |||
local keys = {} | |||
for k in pairs(value) do | |||
keys[#keys + 1] = k | |||
end | |||
table.sort(keys, function(a, b) | |||
return tostring(a) < tostring(b) | |||
end) | |||
for _, k in ipairs(keys) do | |||
append_param_tree(parts, prefix .. "." .. tostring(k), value[k]) | |||
end | |||
end | end | ||
| Строка 1085: | Строка 1114: | ||
local parts = { makeTemplatePrefix() } | local parts = { makeTemplatePrefix() } | ||
if tplArgs ~= "" then | if tplArgs ~= "" then | ||
parts[#parts + 1] = tplArgs | parts[#parts + 1] = tplArgs | ||
end | end | ||
parts[#parts + 1] = id | parts[#parts + 1] = id | ||
if type(obj) == "table" then | if type(obj) == "table" then | ||
append_param_tree(parts, id, obj) | |||
elseif obj ~= nil then | elseif obj ~= nil then | ||
parts[#parts + 1] = "value=" .. tostring(obj) | parts[#parts + 1] = "value=" .. tostring(obj) | ||