Модуль:GetField: различия между версиями

Нет описания правки
Нет описания правки
Строка 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
if ok and json then
local ok, json = pcall(mw.text.jsonEncode, value)
parts[#parts + 1] = prefix .. "=" .. to_nowiki(json)
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
local function is_array_of_primitives(tbl)
if type(tbl) ~= "table" or not is_array(tbl) then
return false
end
for _, v in ipairs(tbl) do
if type(v) == "table" then
return false
end
end
return true
end
local function append_table_tree_raw(parts, value)
if type(value) ~= "table" or next(value) == nil then
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
local v = value[k]
local key = tostring(k)
if type(v) == "table" then
if is_array_of_primitives(v) then
local okJson, jsonVal = pcall(mw.text.jsonEncode, v)
if okJson and jsonVal then
parts[#parts + 1] = key .. "=" .. to_nowiki(jsonVal)
end
end
if next(v) ~= nil then
append_table_tree_raw(parts, v)
end
else
parts[#parts + 1] = key .. "=" .. tostring(v)
end
end
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(k) == "string" and string.sub(k, 1, 6) == "!type:" then
if type(v) == "table" then
if type(v) == "table" then
if next(v) ~= nil then
append_table_tree_raw(parts, v)
elseif v ~= nil then
parts[#parts + 1] = "value=" .. tostring(v)
end
elseif type(v) == "table" then
if is_array_of_primitives(v) 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
end
append_table_tree(parts, k, v, true)
if next(v) ~= nil then
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, "|")