Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 1049: | Строка 1049: | ||
end | end | ||
local function append_table_tree(parts, prefix, value) | local function append_table_tree(parts, prefix, value, isTopLevel) | ||
if type(value) ~= "table" or next(value) == nil then | if type(value) ~= "table" or next(value) == nil then | ||
return | return | ||
end | end | ||
local ok, json = pcall(mw.text.jsonEncode, value) | if isTopLevel then | ||
local ok, json = pcall(mw.text.jsonEncode, value) | |||
if ok and json then | |||
parts[#parts + 1] = "value=" .. to_nowiki(json) | |||
end | |||
end | end | ||
| Строка 1074: | Строка 1076: | ||
if type(v) == "table" then | if type(v) == "table" then | ||
if next(v) ~= nil then | if next(v) ~= nil then | ||
append_table_tree(parts, key, v) | append_table_tree(parts, key, v, false) | ||
end | end | ||
else | else | ||
| Строка 1109: | Строка 1111: | ||
return keys[1] | return keys[1] | ||
end | end | ||
| Строка 1186: | Строка 1139: | ||
return | return | ||
end | end | ||
local idKey = choose_id_key(obj) | local idKey = choose_id_key(obj) | ||
if not idKey then | if not idKey then | ||
return | return | ||
end | end | ||
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] = tostring(idKey) | parts[#parts + 1] = tostring(idKey) | ||
local keys = {} | local keys = {} | ||
for k in pairs(obj) do | for k in pairs(obj) do | ||
| Строка 1206: | Строка 1159: | ||
end | end | ||
end | end | ||
table.sort(keys, function(a, b) | table.sort(keys, function(a, b) | ||
return tostring(a) < tostring(b) | return tostring(a) < tostring(b) | ||
end) | end) | ||
for _, k in ipairs(keys) do | for _, k in ipairs(keys) do | ||
local v = obj[k] | local v = obj[k] | ||
if k == idKey then | if k == idKey then | ||
if type( | if type(v) == "table" then | ||
if | if next(v) ~= nil then | ||
local okJson, jsonVal = pcall(mw.text.jsonEncode, v) | local okJson, jsonVal = pcall(mw.text.jsonEncode, v) | ||
if okJson and jsonVal then | if okJson and jsonVal then | ||
parts[#parts + 1] = "value=" .. to_nowiki(jsonVal) | parts[#parts + 1] = "value=" .. to_nowiki(jsonVal) | ||
end | end | ||
append_table_tree(parts, k, v, true) | |||
append_table_tree(parts, k, v) | |||
end | end | ||
elseif v ~= nil then | elseif v ~= nil then | ||
| Строка 1238: | Строка 1183: | ||
if type(v) == "table" then | if type(v) == "table" then | ||
if next(v) ~= nil then | if next(v) ~= nil then | ||
append_table_tree(parts, k, v) | append_table_tree(parts, k, v, true) | ||
end | end | ||
elseif v ~= nil then | elseif v ~= nil then | ||
| Строка 1245: | Строка 1190: | ||
end | end | ||
end | end | ||
parts[#parts + 1] = "}}" | parts[#parts + 1] = "}}" | ||
calls[#calls + 1] = table.concat(parts, "|") | calls[#calls + 1] = table.concat(parts, "|") | ||