Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 120: | Строка 120: | ||
local direct = data[id] | local direct = data[id] | ||
if direct ~= nil then | if direct ~= nil then | ||
return direct | return direct | ||
end | end | ||
| Строка 151: | Строка 143: | ||
end | end | ||
return nil | return nil | ||
end | |||
local function collect_id_keys(data) | |||
if type(data) ~= "table" then | |||
return {} | |||
end | |||
local idsTable = data.id | |||
local ids = {} | |||
if type(idsTable) == "table" then | |||
for k in pairs(idsTable) do | |||
ids[#ids + 1] = k | |||
end | |||
return ids | |||
end | |||
for k in pairs(data) do | |||
if k ~= "default" and k ~= "id" then | |||
ids[#ids + 1] = k | |||
end | |||
end | |||
return ids | |||
end | end | ||
| Строка 382: | Строка 397: | ||
if not data then return "[]" end | if not data then return "[]" end | ||
local | local ids = collect_id_keys(data) | ||
if | if #ids == 0 then | ||
return "" | return "" | ||
end | end | ||
| Строка 391: | Строка 406: | ||
local target = tostring(searchValue) | local target = tostring(searchValue) | ||
matches = {} | matches = {} | ||
for idKey | for _, idKey in ipairs(ids) do | ||
local entry = resolve_entry(data, idKey) | |||
if type(entry) == "table" then | if type(entry) == "table" then | ||
local v = get_by_path(entry, keyPath) | local v = get_by_path(entry, keyPath) | ||
| Строка 400: | Строка 416: | ||
end | end | ||
else | else | ||
matches = | local target = tostring(searchValue) | ||
matches = {} | |||
for _, idKey in ipairs(ids) do | |||
local entry = resolve_entry(data, idKey) | |||
if type(entry) == "table" then | |||
local v = get_by_path(entry, keyPath) | |||
if v ~= nil and contains_target(v, target) then | |||
matches[#matches + 1] = idKey | |||
end | |||
end | |||
end | |||
end | end | ||
| Строка 443: | Строка 469: | ||
if not data then return "" end | if not data then return "" end | ||
local | local ids = collect_id_keys(data) | ||
if | if #ids == 0 then | ||
return "" | return "" | ||
end | end | ||
| Строка 451: | Строка 477: | ||
if searchType == "path" then | if searchType == "path" then | ||
matches = {} | matches = {} | ||
for idKey | for _, idKey in ipairs(ids) do | ||
local entry = resolve_entry(data, idKey) | |||
if type(entry) == "table" then | if type(entry) == "table" then | ||
local v = get_by_path(entry, keyPath) | local v = get_by_path(entry, keyPath) | ||
| Строка 462: | Строка 489: | ||
local target = tostring(searchValue) | local target = tostring(searchValue) | ||
matches = {} | matches = {} | ||
for idKey | for _, idKey in ipairs(ids) do | ||
local entry = resolve_entry(data, idKey) | |||
if type(entry) == "table" then | if type(entry) == "table" then | ||
local v = get_by_path(entry, keyPath) | local v = get_by_path(entry, keyPath) | ||
| Строка 471: | Строка 499: | ||
end | end | ||
else | else | ||
matches = | local target = tostring(searchValue) | ||
matches = {} | |||
for _, idKey in ipairs(ids) do | |||
local entry = resolve_entry(data, idKey) | |||
if type(entry) == "table" then | |||
local v = get_by_path(entry, keyPath) | |||
if v ~= nil and contains_target(v, target) then | |||
matches[#matches + 1] = idKey | |||
end | |||
end | |||
end | |||
end | end | ||