Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 70: | Строка 70: | ||
return tostring(v) | return tostring(v) | ||
end | end | ||
end | |||
local function json_flat(v) | |||
local ok, json = pcall(mw.text.jsonEncode, v) | |||
if not ok or not json then | |||
return nil | |||
end | |||
json = tostring(json):gsub("[\r\n]+", "") | |||
return json | |||
end | |||
local function is_array(tbl) | |||
local max = 0 | |||
local count = 0 | |||
for k in pairs(tbl) do | |||
if type(k) ~= "number" then | |||
return false | |||
end | |||
if k > max then max = k end | |||
count = count + 1 | |||
end | |||
return count > 0 and max == count | |||
end | end | ||
| Строка 196: | Строка 218: | ||
local key = (prefix == "" and tostring(k) or prefix .. "." .. tostring(k)) | local key = (prefix == "" and tostring(k) or prefix .. "." .. tostring(k)) | ||
if type(v) == "table" then | if type(v) == "table" then | ||
walk(v, key) | if is_array(v) then | ||
local json = json_flat(v) | |||
if json then | |||
parts[#parts + 1] = key .. "=" .. json | |||
end | |||
else | |||
local json = json_flat(v) | |||
if json then | |||
parts[#parts + 1] = key .. "=" .. json | |||
end | |||
walk(v, key) | |||
end | |||
else | else | ||
parts[#parts + 1] = key .. "=" .. tostring(v) | parts[#parts + 1] = key .. "=" .. tostring(v) | ||