gookit / config

📝 Go configuration manage(load,get,set,export). support JSON, YAML, TOML, Properties, INI, HCL, ENV and Flags. Multi file load, data override merge, parse ENV var. Go应用配置加载管理,支持多种格式,多文件加载,远程文件加载,支持数据合并,解析环境变量名

Home Page:https://pkg.go.dev/github.com/gookit/config/v2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[FEAT] Support Set with Slice (Array) Index Using Square Brackets?

KiddoV opened this issue · comments

It would be nice to support slice index in .Set()
Example:

config.Set("parent.child[0]", "Test1")
config.Set("parent.child[1]", "Test2")

Currently, the result will be like:

{
    "parent": {
        "child[0]": "Test1",
        "child[1]": "Test1"
    }
}

==> Expecting result would look like:

{
    "parent": {
        "child": [
          "Test1",
          "Test2"
        ]
    }
}

Thanks,

Has been implemented, the next version will be released.

Glad to hear!!!

hi, new version has been released. https://github.com/gookit/config/releases/tag/v2.1.4

@inhere
Does the new version v2.2.1 also support array of object like:

{
    "parent": [
        {
            "child1Field1": 1,
            "child1Field2": 2
        },
        {
            "child2Field1": 3,
            "child2Field2": 4
        }
    ]
}

When I do config.Set("parent[0].child1Field1", 5) it doesn't work tho.
Result:

{
    "parent": [
        {
            "child1Field1": 1,
            "child1Field2": 2
        },
        {
            "child2Field1": 3,
            "child2Field2": 4
        }
    ],
    "parent[0]": {
        "child1Field1": 5
    }
}

Expecting result:

{
    "parent": [
        {
            "child1Field1": 5,
            "child1Field2": 2
        },
        {
            "child2Field1": 3,
            "child2Field2": 4
        }
    ]
}

Can you also implement to support this?
Thanks!

This must be a bug or something. I noticed we can do this, but only the second time on. (ONLY when the JSON file doesn't contain these config yet)
When call config.Set("parent[0].child1Field1", 5) the first time, data will be created like this:

{
    [...]
    "parent[0]": {
        "child1Field1": 5
    }
}

But call the second time it would be correct:

{
    [...]
    "parent": { //When calling the second time and so on...
        [...]
        {
            "child1Field1": 5,
            [...]
        }
    },
    "parent[0]": { //When calling the first time
        "child1Field1": 5
    }
}

And when I reload the program and keep those config in the JSON file to be loaded. I will get this error after call: config.Set("parent[0].child1Field1", 5):

key parent[0] elem must be map for set sub-value by remain path: parent[0].child1Field1

Any idea?

Any update on this?

:( I've been quite busy lately, so no updates.