2

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

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
    -- And now for the 4th time. This is starting to get way to repetitive
    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
    -- And for what I try to achieve, I should continue this elseif's until indefinite
    end
end
-- And this should go as deep into the table as I want to

现在我可以做

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
4

2 回答 2

3

尝试这个:

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-29T03:43:53.583 回答
1

使用一些可转换的魔法,您可以完全摆脱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-30T18:30:31.513 回答