Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 1049: | Строка 1049: | ||
end | end | ||
local function | local function append_table_tree(parts, prefix, value) | ||
if value == nil then | if type(value) ~= "table" or next(value) == nil then | ||
return | return | ||
end | end | ||
local | local ok, json = pcall(mw.text.jsonEncode, value) | ||
if ok and json then | |||
parts[#parts + 1] = prefix .. "=" .. to_nowiki(json) | |||
end | |||
local keys = {} | |||
for k in pairs(value) do | |||
keys[#keys + 1] = k | |||
end | end | ||
if next( | table.sort(keys, function(a, b) | ||
return tostring(a) < tostring(b) | |||
end) | |||
for _, k in ipairs(keys) do | |||
local v = value[k] | |||
local key = prefix .. "." .. tostring(k) | |||
if type(v) == "table" then | |||
if next(v) ~= nil then | |||
append_table_tree(parts, key, v) | |||
end | |||
else | |||
parts[#parts + 1] = key .. "=" .. tostring(v) | |||
end | |||
end | end | ||
end | |||
local function choose_id_key(obj) | |||
local keys = {} | |||
for k in pairs(obj) do | |||
if type(k) == "string" then | |||
keys[#keys + 1] = k | |||
end | end | ||
end | end | ||
if #keys == 0 then | |||
return nil | |||
end | end | ||
table.sort(keys, function(a, b) | table.sort(keys, function(a, b) | ||
local av = obj[a] | |||
local bv = obj[b] | |||
local aPrimitive = type(av) ~= "table" | |||
local bPrimitive = type(bv) ~= "table" | |||
if aPrimitive ~= bPrimitive then | |||
return aPrimitive | |||
end | |||
return tostring(a) < tostring(b) | return tostring(a) < tostring(b) | ||
end) | end) | ||
return keys[1] | |||
end | end | ||
| Строка 1108: | Строка 1133: | ||
end | end | ||
local function makeCall( | local function makeCall(obj) | ||
if type( | if type(obj) ~= "table" then | ||
return | |||
end | |||
local idKey = choose_id_key(obj) | |||
if not idKey then | |||
return | return | ||
end | end | ||
| Строка 1119: | Строка 1149: | ||
end | end | ||
parts[#parts + 1] = | parts[#parts + 1] = tostring(idKey) | ||
local keys = {} | |||
for k in pairs(obj) do | |||
if type(k) == "string" then | |||
keys[#keys + 1] = k | |||
end | |||
end | |||
table.sort(keys, function(a, b) | |||
return tostring(a) < tostring(b) | |||
end) | |||
if type( | for _, k in ipairs(keys) do | ||
local v = obj[k] | |||
if k == idKey then | |||
if type(v) == "table" then | |||
if next(v) ~= nil then | |||
append_table_tree(parts, k, v) | |||
end | |||
elseif v ~= nil then | |||
parts[#parts + 1] = "value=" .. tostring(v) | |||
end | |||
else | |||
if type(v) == "table" then | |||
if next(v) ~= nil then | |||
append_table_tree(parts, k, v) | |||
end | |||
elseif v ~= nil then | |||
parts[#parts + 1] = k .. "=" .. tostring(v) | |||
end | |||
end | |||
end | end | ||
| Строка 1133: | Строка 1190: | ||
if is_array(data) then | if is_array(data) then | ||
for _, item in ipairs(data) do | for _, item in ipairs(data) do | ||
makeCall(item) | |||
end | end | ||
else | else | ||
makeCall(data) | |||
end | end | ||
| Строка 1149: | Строка 1200: | ||
end | end | ||
return frame:preprocess(table.concat(calls, " ")) | |||
end | end | ||