Skip to content

Commit

Permalink
fix: add WeaklyTypedInput option on binding data to Struct
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 13, 2020
1 parent e6d9375 commit 59422ed
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
26 changes: 13 additions & 13 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (c *Config) BindStruct(key string, dst interface{}) error {
// Usage:
// dbInfo := Db{}
// config.Structure("db", &dbInfo)
func (c *Config) Structure(key string, dst interface{}) (err error) {
func (c *Config) Structure(key string, dst interface{}) error {
var data interface{}

// binding all data
Expand All @@ -44,20 +44,20 @@ func (c *Config) Structure(key string, dst interface{}) (err error) {
}
}

// format := JSON
// if len(driverName) > 0 {
// format = driverName[0]
// }
// err = mapstructure.Decode(data, dst)
config := &mapstructure.DecoderConfig{
Metadata: nil,
Result: dst,
// will auto convert string to int/uint
WeaklyTypedInput: true,
}

// decoder := c.getDecoderByFormat(format)
// if decoder != nil {
//
// } else { // try use mergo
// return mergo.Map(dst, data)
// }
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return err
}

err = mapstructure.Decode(data, dst)
return
return decoder.Decode(data)
}

// ToJSON string
Expand Down
54 changes: 38 additions & 16 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"testing"

"github.com/gookit/goutil/dump"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -38,7 +39,7 @@ func TestExport(t *testing.T) {
}

func TestConfig_Structure(t *testing.T) {
st := assert.New(t)
is := assert.New(t)

cfg := Default()
cfg.ClearAll()
Expand All @@ -49,7 +50,7 @@ func TestConfig_Structure(t *testing.T) {
"sports": ["pingPong", "跑步"]
}`)

st.Nil(err)
is.Nil(err)

user := &struct {
Age int // always float64 from JSON
Expand All @@ -58,11 +59,32 @@ func TestConfig_Structure(t *testing.T) {
}{}
// map all data
err = MapStruct("", user)
st.Nil(err)
is.Nil(err)

st.Equal(28, user.Age)
st.Equal("inhere", user.Name)
st.Equal("pingPong", user.Sports[0])
is.Equal(28, user.Age)
is.Equal("inhere", user.Name)
is.Equal("pingPong", user.Sports[0])

// - auto convert string to int
// age use string in JSON
cfg1 := New("test")
err = cfg1.LoadStrings(JSON, `{
"age": "26",
"name": "inhere",
"sports": ["pingPong", "跑步"]
}`)

is.Nil(err)

user1 := &struct {
Age int // always float64 from JSON
Name string
Sports []string
}{}
err = cfg1.MapStruct("", user1)
is.Nil(err)

dump.P(*user1)

// map some data
err = cfg.LoadStrings(JSON, `{
Expand All @@ -72,17 +94,17 @@ func TestConfig_Structure(t *testing.T) {
"tags": [12, 34]
}
}`)
st.Nil(err)
is.Nil(err)

some := struct {
Age int
Kye string
Tags []int
}{}
err = BindStruct("sec", &some)
st.Nil(err)
st.Equal(120, some.Age)
st.Equal(12, some.Tags[0])
is.Nil(err)
is.Equal(120, some.Age)
is.Equal(12, some.Tags[0])
cfg.ClearAll()

// custom data
Expand All @@ -92,22 +114,22 @@ func TestConfig_Structure(t *testing.T) {
"age": 120,
"tags": []int{12, 34},
})
st.NoError(err)
is.NoError(err)

s1 := struct {
Age int
Kye string
Tags []int
}{}
err = cfg.BindStruct("", &s1)
st.Nil(err)
st.Equal(120, s1.Age)
st.Equal(12, s1.Tags[0])
is.Nil(err)
is.Equal(120, s1.Age)
is.Equal(12, s1.Tags[0])

// key not exist
err = cfg.BindStruct("not-exist", &s1)
st.Error(err)
st.Equal("this key does not exist in the config", err.Error())
is.Error(err)
is.Equal("this key does not exist in the config", err.Error())

cfg.ClearAll()
}

0 comments on commit 59422ed

Please sign in to comment.