Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) Нет описания правки Метка: отменено |
Pok (обсуждение | вклад) Отмена версии 213853, сделанной Pok (обсуждение) Метка: отмена |
||
| Строка 41: | Строка 41: | ||
return parsed | return parsed | ||
end | end | ||
| Строка 366: | Строка 289: | ||
local function find_matching_ids(idsTable, keyPath, searchValue) | local function find_matching_ids(idsTable, keyPath, searchValue) | ||
local parsedPath = parse_path(keyPath) | local parsedPath = parse_path(keyPath) | ||
local target = tostring(searchValue) | |||
local matches = {} | local matches = {} | ||
for idKey, entry in pairs(idsTable) do | for idKey, entry in pairs(idsTable) do | ||
if type(entry) == "table" | if type(entry) == "table" then | ||
local v = get_by_parsed_path(entry, parsedPath) | |||
if v ~= nil and contains_target(v, target) then | |||
matches[#matches + 1] = idKey | |||
end | |||
end | end | ||
end | end | ||
| Строка 729: | Строка 656: | ||
end | end | ||
local idsTable = type(data.id) == "table" and data.id or data | |||
local ids = collect_id_keys(data) | local ids = collect_id_keys(data) | ||
if #ids == 0 then | if #ids == 0 then | ||
| Строка 734: | Строка 662: | ||
end | end | ||
local matches = | local matches | ||
if searchType == "key" then | |||
local target = tostring(searchValue) | |||
matches = {} | |||
for _, idKey in ipairs(ids) do | for _, idKey in ipairs(ids) do | ||
local | local v = resolve_path_value(data, idKey, parsedPath) | ||
if type( | if type(v) == "table" and v[target] ~= nil then | ||
matches[#matches + 1] = idKey | matches[#matches + 1] = idKey | ||
end | end | ||
end | end | ||
else | else | ||
for _, idKey in ipairs(ids) do | local target = tostring(searchValue) | ||
matches = {} | |||
if idsTable == data then | |||
matches = find_matching_ids(idsTable, keyPath, searchValue) | |||
else | |||
for _, idKey in ipairs(ids) do | |||
local v = resolve_path_value(data, idKey, parsedPath) | |||
if v ~= nil and contains_target(v, target) then | |||
matches[#matches + 1] = idKey | |||
end | |||
end | end | ||
end | end | ||
| Строка 811: | Строка 745: | ||
end | end | ||
local matches | local matches | ||
if searchType == "path" then | if searchType == "path" then | ||
matches = {} | |||
for _, idKey in ipairs(ids) do | for _, idKey in ipairs(ids) do | ||
local | local v = resolve_entry_path_value(data, idKey, parsedPath) | ||
if | if is_nonempty_value(v) then | ||
matches[#matches + 1] = idKey | matches[#matches + 1] = idKey | ||
end | end | ||
end | end | ||
elseif searchType == "key" then | |||
local target = tostring(searchValue) | local target = tostring(searchValue) | ||
matches = {} | |||
for _, idKey in ipairs(ids) do | for _, idKey in ipairs(ids) do | ||
local | local v = resolve_path_value(data, idKey, parsedPath) | ||
if type( | if type(v) == "table" and v[target] ~= nil then | ||
matches[#matches + 1] = idKey | matches[#matches + 1] = idKey | ||
end | |||
end | |||
else | |||
local idsTable = data.id | |||
if type(idsTable) == "table" then | |||
local directMatches = find_matching_ids(idsTable, keyPath, searchValue) | |||
local defaultValue = nil | |||
local includeDefault = false | |||
local base = data["default"] | |||
if type(base) == "table" then | |||
defaultValue = get_by_parsed_path(base, parsedPath) | |||
includeDefault = defaultValue ~= nil and contains_target(defaultValue, tostring(searchValue)) | |||
end | |||
if includeDefault and #directMatches < #ids then | |||
local seen = {} | |||
for i = 1, #directMatches do | |||
seen[directMatches[i]] = true | |||
end | |||
matches = {} | |||
for _, idKey in ipairs(directMatches) do | |||
matches[#matches + 1] = idKey | |||
end | |||
for _, idKey in ipairs(ids) do | |||
if not seen[idKey] then | |||
local specific = idsTable[idKey] | |||
local specificValue = type(specific) == "table" and get_by_parsed_path(specific, parsedPath) or | |||
nil | |||
if specificValue == nil then | |||
matches[#matches + 1] = idKey | |||
end | |||
end | |||
end | |||
else | |||
matches = directMatches | |||
end | |||
else | |||
local target = tostring(searchValue) | |||
matches = {} | |||
for _, idKey in ipairs(ids) do | |||
local v = resolve_path_value(data, idKey, parsedPath) | |||
if v ~= nil and contains_target(v, target) then | |||
matches[#matches + 1] = idKey | |||
end | |||
end | end | ||
end | end | ||