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

Нет описания правки
Нет описания правки
 
(не показаны 2 промежуточные версии этого же участника)
Строка 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


Строка 173: Строка 188:


     return tostring(v) == target
     return tostring(v) == target
end
local function is_nonempty_value(v)
    if v == nil then return false end
    if type(v) == "table" then
        return next(v) ~= nil
    end
    return true
end
end


Строка 382: Строка 405:
     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: Строка 414:
         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: Строка 424:
         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: Строка 477:
     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: Строка 485:
     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)
                 if v ~= nil then
                 if is_nonempty_value(v) then
                     matches[#matches + 1] = idKey
                     matches[#matches + 1] = idKey
                 end
                 end
Строка 462: Строка 497:
         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: Строка 507:
         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


Строка 498: Строка 544:
     local id = args[1] or ""
     local id = args[1] or ""
     local pagePath = args[2] or ""
     local pagePath = args[2] or ""
     local tplPath = args[3] or ""
     local tplPath = mw.text.unstripNoWiki(args[3] or "")


     if id == "" or pagePath == "" or tplPath == "" then
     if id == "" or pagePath == "" or tplPath == "" then
Строка 529: Строка 575:
     local kind = (args[2] or ""):lower()
     local kind = (args[2] or ""):lower()
     local generatorId = args[3] or ""
     local generatorId = args[3] or ""
     local tplPath = args[4] or ""
     local tplPath = mw.text.unstripNoWiki(args[4] or "")


     if searchId == "" or generatorId == "" or tplPath == "" then
     if searchId == "" or generatorId == "" or tplPath == "" then