Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 72: | Строка 72: | ||
end | end | ||
function p. | local function deep_copy(src) | ||
local dst = {} | |||
for k, v in pairs(src) do | |||
if type(v) == "table" then | |||
dst[k] = deep_copy(v) | |||
else | |||
dst[k] = v | |||
end | |||
end | |||
return dst | |||
end | |||
local function deep_merge(dst, src) | |||
for k, v in pairs(src) do | |||
if type(v) == "table" and type(dst[k]) == "table" then | |||
deep_merge(dst[k], v) | |||
elseif type(v) == "table" then | |||
dst[k] = deep_copy(v) | |||
else | |||
dst[k] = v | |||
end | |||
end | |||
end | |||
function p.findInProto(frame) | |||
local args = frame.args or {} | local args = frame.args or {} | ||
local | local protoKey = args[1] or "" | ||
local | local fieldId = args[2] or "" | ||
if | if protoKey == "" or fieldId == "" then | ||
return "[]" | return "[]" | ||
end | end | ||
| Строка 93: | Строка 117: | ||
end | end | ||
local proto = data[ | local proto = data[protoKey] | ||
if type(proto) ~= "table" then | if type(proto) ~= "table" then | ||
return "[]" | return "[]" | ||
end | end | ||
local | local value = proto[fieldId] | ||
if value == nil then | |||
return "[]" | |||
end | |||
local out = {} | |||
local t = type(value) | |||
if t == "table" then | |||
for _, v in ipairs(value) do | |||
out[#out + 1] = v | |||
end | end | ||
else | |||
out[1] = value | |||
end | end | ||
return mw.text.jsonEncode( | return mw.text.jsonEncode(out) | ||
end | end | ||
| Строка 140: | Строка 157: | ||
end | end | ||
local entry = data[id] | local entry = data[id] | ||
if entry == nil then | |||
local idsTable = data.id | |||
if type(idsTable) == "table" then | |||
local specific = idsTable[id] | |||
if type(specific) == "table" then | |||
local base = data["default"] | |||
if type(base) == "table" then | |||
local merged = deep_copy(base) | |||
deep_merge(merged, specific) | |||
entry = merged | |||
else | |||
entry = deep_copy(specific) | |||
end | |||
end | |||
end | |||
end | |||
if entry == nil then | |||
local base = data["default"] | |||
if type(base) == "table" then | |||
entry = deep_copy(base) | |||
else | |||
entry = {} | |||
end | |||
end | |||
if type(entry) ~= "table" then return "" end | if type(entry) ~= "table" then return "" end | ||
| Строка 223: | Строка 267: | ||
local searchId = args[1] or "" | local searchId = args[1] or "" | ||
local protoId = args[2] or "" | local protoId = args[2] or "" | ||
local tplPath = args[ | local tplPath = args[3] or "" | ||
local pagePath = "prototype/" .. protoId .. ".json" | |||
if searchId == "" or protoId == "" or tplPath == "" then | if searchId == "" or protoId == "" or tplPath == "" then | ||
| Строка 229: | Строка 274: | ||
end | end | ||
local | local idsJson = p.findInProto({ args = { searchId, protoId } }) | ||
local ok, | local ok, ids = pcall(mw.text.jsonDecode, idsJson or "") | ||
if not ok or type( | if not ok or type(ids) ~= "table" or #ids == 0 then | ||
return "" | return "" | ||
end | end | ||
local out = {} | local out = {} | ||
for _ | for _, id in ipairs(ids) do | ||
local tpl = p.getTpl({ args = { | local tpl = p.getTpl({ args = { id, pagePath, tplPath } }) | ||
if tpl ~= "" then | if tpl ~= "" then | ||
out[#out + 1] = tpl | out[#out + 1] = tpl | ||