Модуль:GetField: различия между версиями

Нет описания правки
Нет описания правки
Строка 120: Строка 120:
         local direct = data[id]
         local direct = data[id]
         if direct ~= nil then
         if direct ~= nil then
            if type(direct) == "table" then
                local base = data["default"]
                if type(base) == "table" then
                    local merged = deep_copy(base)
                    deep_merge(merged, direct)
                    return merged
                end
            end
             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 idsTable = data.id
     local ids = collect_id_keys(data)
     if type(idsTable) ~= "table" then
     if #ids == 0 then
         return ""
         return ""
     end
     end
Строка 391: Строка 406:
         local target = tostring(searchValue)
         local target = tostring(searchValue)
         matches = {}
         matches = {}
         for idKey, entry in pairs(idsTable) do
         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 = find_matching_ids(idsTable, keyPath, searchValue)
        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 idsTable = data.id
     local ids = collect_id_keys(data)
     if type(idsTable) ~= "table" then
     if #ids == 0 then
         return ""
         return ""
     end
     end
Строка 451: Строка 477:
     if searchType == "path" then
     if searchType == "path" then
         matches = {}
         matches = {}
         for idKey, entry in pairs(idsTable) do
         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, entry in pairs(idsTable) do
         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 = find_matching_ids(idsTable, keyPath, searchValue)
        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