Skip to content

Commit

Permalink
up: enhance the Set logic, allow change type of map item value. issues
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 22, 2022
1 parent 37bfe90 commit 46cb2f1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 29 deletions.
79 changes: 54 additions & 25 deletions issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/testutil"
"github.com/stretchr/testify/assert"
"github.com/gookit/goutil/testutil/assert"
)

// https://github.com/gookit/config/issues/37
Expand All @@ -30,7 +30,7 @@ func TestIssues_37(t *testing.T) {
}
}
`)
is.NoError(err)
is.NoErr(err)
dump.Println(c.Data())

is.Panics(func() {
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestIssues37_yaml_v3(t *testing.T) {
}
}
`)
is.NoError(err)
is.NoErr(err)
dump.Println(c.Data())

err = c.LoadStrings(config.Yaml, `
Expand All @@ -67,7 +67,7 @@ lang:
allowed:
en: "666"
`)
is.NoError(err)
is.NoErr(err)
dump.Println(c.Data())
}

Expand All @@ -85,35 +85,35 @@ func TestIssues_46(t *testing.T) {
}
`)

is.NoError(err)
is.NoErr(err)
dump.Println(c.Data())

val, _ := c.GetValue("http")
mp := val.(map[string]interface{})
dump.Println(mp)
is.Equal("${HTTP_PORT|8080}", mp["port"])
is.Eq("${HTTP_PORT|8080}", mp["port"])

smp := c.StringMap("http")
dump.Println(smp)
is.Contains(smp, "port")
is.Equal("8080", smp["port"])
is.Eq("8080", smp["port"])

type Http struct {
Port int
}

h := &Http{}
err = c.BindStruct("http", h)
is.NoError(err)
is.NoErr(err)
dump.Println(h)
is.Equal(8080, h.Port)
is.Eq(8080, h.Port)

testutil.MockEnvValue("HTTP_PORT", "19090", func(_ string) {
h := &Http{}
err = c.BindStruct("http", h)
is.NoError(err)
is.NoErr(err)
dump.Println(h)
is.Equal(19090, h.Port)
is.Eq(19090, h.Port)
})
}

Expand All @@ -125,19 +125,48 @@ func TestIssues_59(t *testing.T) {
c.AddDriver(ini.Driver)

err := c.LoadFiles("testdata/ini_base.ini")
is.NoError(err)
is.NoErr(err)
dump.Println(c.Data())

dumpfile := "testdata/issues59.ini"
out := fsutil.MustCreateFile(dumpfile, 0666, 0666)
_, err = c.DumpTo(out, config.Ini)
is.NoError(err)
is.NoErr(err)

str := string(fsutil.MustReadFile(dumpfile))
is.Contains(str, "name = app")
is.Contains(str, "key1 = val1")
}

// https://github.com/gookit/config/issues/70
func TestIssues_70(t *testing.T) {
c := config.New("test")

err := c.LoadStrings(config.JSON, `{
"parent": {
"child": "Test Var"
}
}`)

assert.NoErr(t, err)
assert.Eq(t, "Test Var", c.String("parent.child"))
dump.P(c.Data())

// cannot this.
err = c.Set("parent.child.grandChild", "New Val")
assert.Err(t, err)

err = c.Set("parent.child", map[string]interface{}{
"grandChild": "New Val",
})
assert.NoErr(t, err)
assert.Eq(t, map[string]interface{}{
"grandChild": "New Val",
}, c.Get("parent.child"))

dump.P(c.Data())
}

// https://github.com/gookit/config/issues/76
func TestIssues_76(t *testing.T) {
is := assert.New(t)
Expand All @@ -153,15 +182,15 @@ func TestIssues_76(t *testing.T) {
"key0": 234
}
`)
is.NoError(err)
is.NoErr(err)

ss := c.Strings("key0")
is.Empty(ss)

lastErr := c.Error()
is.Error(lastErr)
is.Equal("value cannot be convert to []string, key is 'key0'", lastErr.Error())
is.NoError(c.Error())
is.Err(lastErr)
is.Eq("value cannot be convert to []string, key is 'key0'", lastErr.Error())
is.NoErr(c.Error())
}

// https://github.com/gookit/config/issues/81
Expand All @@ -179,7 +208,7 @@ func TestIssues_81(t *testing.T) {
"idleTime": "1m"
}
`)
is.NoError(err)
is.NoErr(err)

type Options struct {
ConnTime time.Duration `json:"connTime"`
Expand All @@ -189,14 +218,14 @@ func TestIssues_81(t *testing.T) {
opt := &Options{}
err = c.BindStruct("", opt)

is.NoError(err)
is.Equal("10s", c.String("connTime"))
is.NoErr(err)
is.Eq("10s", c.String("connTime"))
wantTm, err := time.ParseDuration("10s")
is.NoError(err)
is.Equal(wantTm, opt.ConnTime)
is.NoErr(err)
is.Eq(wantTm, opt.ConnTime)

is.Equal("1m", c.String("idleTime"))
is.Eq("1m", c.String("idleTime"))
wantTm, err = time.ParseDuration("1m")
is.NoError(err)
is.Equal(wantTm, opt.IdleTime)
is.NoErr(err)
is.Eq(wantTm, opt.IdleTime)
}
7 changes: 3 additions & 4 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/strutil"
"github.com/imdario/mergo"
)
Expand Down Expand Up @@ -90,10 +91,8 @@ func (c *Config) Set(key string, val interface{}, setByPath ...bool) (err error)

c.data[topK] = dstItem
case map[string]interface{}: // from json,toml,yaml.v3
// create a new item for the topK
newItem := buildValueByPath(paths, val)
// merge new item to old item
if err = mergo.Merge(&typeData, &newItem, mergo.WithOverride); err != nil {
// enhanced: 'top.sub'=string, can set 'top.sub' to other type. eg: 'top.sub'=map
if err = maputil.SetByKeys(&typeData, paths, val); err != nil {
return
}

Expand Down

0 comments on commit 46cb2f1

Please sign in to comment.