将多个 Lua 参数合并成表深度

我目前有这段代码,但我觉得这可以更好地优化:

local config = {
    val = 1,
    background = {
        foo = 5,
        textures = {
            bar = 'okay',
            layers = {
                alpha = 1,
                color = {
                    red = 255,
                    blue = 0,
                    green = 100
                }
            }
        }
    },
    sub = {
        val = 5,
        foo = {
            val = true,
            bar = {
            }
        }
    }
}

local function set_config(...)
    local arg = {...}
    if type(arg[1]) == 'table' then
        table.extend(config, arg[1])
    elseif #arg == 2 then
        if type(arg[2]) == 'table' then
            table.extend(config[arg[1]], arg[2])
        else
            config[arg[1]] = arg[2]
        end
    elseif #arg == 3 then
        if type(arg[3]) == 'table' then
            table.extend(config[arg[1]][arg[2]], arg[3])
        else
            config[arg[1]][arg[2]] = arg[3]
        end
    -- 这是第 4 次。重复性越来越高
    elseif #arg == 4 then
        if type(arg[4]) == 'table' then
            table.extend(config[arg[1]][arg[2]][arg[3]], arg[4])
        else
            config[arg[1]][arg[2]][arg[3]] = arg[4]
        end
    -- 为了我想要实现的内容,我应该一直使用这种 elseif 直到无限
    end
end
-- 我可以尽可能地深入表中

现在我可以这样做:

set_config('val', 3)
set_config('sub', {
    foo = false
})

但我想深入表格而没有现在的 set_config 的限制

所以我可以这样做:

set_config('background', 'textures', 'layers', 'medium', 'color', 'red', 255)

为了澄清,table.extend 如下:

function table.extend(t1, t2)
    for k, v in pairs(t2) do
        if (type(v) == "table") and (type(t1[k] or false) == "table") then
            table.extend(t1[k], t2[k])
        else
            t1[k] = v
        end
    end
    return t1
end

我尝试了一些东西,但我的 Lua 知识相当有限。

lhf 的答案几乎正确,这是我想要的结果:

local function set_config(...)
    local a={...}
    local n=#a
    local t=config
    if #a == 1 and type(a[1]) == 'table' then
        table.extend(config, a[1])
        return
    end
    for i=1,n-2 do
        local k=a[i]
        t[k]=t[k] or {}
        t=t[k]
    end
    if type(t[a[n-2]]) == 'table' then
        table.extend(t[a[n-1]], a[n])
    else
        t[a[n-1]] = a[n]
    end
end
点赞
用户107090
用户107090

试试这个:

local config={}

function set_config(...)
    local a={...}
    local n=#a
    local t=config
    for i=1,n-2 do
        local k=a[i]
        t[k]=t[k] or {}
        t=t[k]
    end
    t[a[n-1]]=a[n]
end

set_config('background', 'textures', 'layers', 'medium', 'color', 'red', 255)

print(config.background.textures.layers.medium.color.red)
2013-10-29 03:43:53
用户1244588
用户1244588

使用一些元表的魔术,你可以完全摆脱set_config

local config, config_mt
config_mt = {
    __index = function(t, k)
        t[k] = setmetatable({}, config_mt)
        return t[k]
    end,
}
config = setmetatable({}, config_mt)

config.background.textures.layers.medium.color.red = 255
print(config.background.textures.layers.medium.color.red)

不过,有一个缺点:从存储中恢复您的配置后,您必须遍历结构体以再次应用元表。

2013-10-30 18:30:31