Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 788: | Строка 788: | ||
if not seen[idKey] then | if not seen[idKey] then | ||
local specific = idsTable[idKey] | local specific = idsTable[idKey] | ||
local specificValue = type(specific) == "table" and get_by_parsed_path(specific, parsedPath) or nil | local specificValue = type(specific) == "table" and get_by_parsed_path(specific, parsedPath) or | ||
nil | |||
if specificValue == nil then | if specificValue == nil then | ||
matches[#matches + 1] = idKey | matches[#matches + 1] = idKey | ||
| Строка 1049: | Строка 1050: | ||
end | end | ||
local function append_table_tree(parts, prefix, value | local function append_table_tree(parts, prefix, value) | ||
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 ok and json then | |||
parts[#parts + 1] = prefix .. "=" .. to_nowiki(json) | |||
end | end | ||
| Строка 1076: | Строка 1075: | ||
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) | ||
end | end | ||
else | else | ||
| Строка 1111: | Строка 1110: | ||
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 | |||
if is_array_of_primitives(value) 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 | ||
| Строка 1168: | Строка 1220: | ||
if k == idKey then | if k == idKey then | ||
if type(v) == "table" then | if type(k) == "string" and string.sub(k, 1, 6) == "!type:" then | ||
if type(v) == "table" 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 | |||
if next(v) ~= nil then | |||
append_table_tree(parts, k, v) | |||
end | end | ||
elseif v ~= nil then | elseif v ~= nil then | ||
| Строка 1183: | Строка 1243: | ||
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) | ||
end | end | ||
elseif v ~= nil then | elseif v ~= nil then | ||