Skip to content

Commit

Permalink
feat: add Decode func for mapping all data to struct
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 25, 2022
1 parent df9cb3b commit 88d7e3d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
39 changes: 18 additions & 21 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ import (
"github.com/mitchellh/mapstructure"
)

// Decode all config data to the dst ptr
//
// Usage:
//
// myConf := &MyConf{}
// config.Decode(myConf)
func Decode(dst interface{}) error { return dc.Decode(dst) }

// Decode all config data to the dst ptr.
//
// It's equals:
//
// c.Structure("", dst)
func (c *Config) Decode(dst interface{}) error {
return c.Structure("", dst)
}

// MapStruct alias method of the 'Structure'
//
// Usage:
Expand Down Expand Up @@ -145,33 +162,13 @@ func (c *Config) DumpTo(out io.Writer, format string) (n int64, err error) {

// DumpToFile use the format(json,yaml,toml) dump config data to a writer
func (c *Config) DumpToFile(fileName string, format string) (err error) {
var ok bool
var encoder Encoder

format = fixFormat(format)
if encoder, ok = c.encoders[format]; !ok {
err = errors.New("not exists/register encoder for the format: " + format)
return
}

if len(c.data) == 0 {
return
}

// encode data to string
encoded, err := encoder(c.data)
if err != nil {
return
}

// write content to out
fsFlags := os.O_CREATE | os.O_WRONLY | os.O_TRUNC
f, err := os.OpenFile(fileName, fsFlags, os.ModePerm)
if err != nil {
return err
}

_, err = f.Write(encoded)
_, err = c.DumpTo(f, format)
if err1 := f.Close(); err1 != nil && err == nil {
err = err1
}
Expand Down
22 changes: 14 additions & 8 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ func TestConfig_Structure(t *testing.T) {
}`)

is.Nil(err)

user := &struct {
type User struct {
Age int // always float64 from JSON
Name string
Sports []string
}{}
}

user := &User{}
// map all data
err = MapStruct("", user)
is.Nil(err)
Expand All @@ -65,6 +66,15 @@ func TestConfig_Structure(t *testing.T) {
is.Equal("inhere", user.Name)
is.Equal("pingPong", user.Sports[0])

// map all data
u1 := &User{}
err = Decode(u1)
is.Nil(err)

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

// - auto convert string to int
// age use string in JSON
cfg1 := New("test")
Expand All @@ -76,11 +86,7 @@ func TestConfig_Structure(t *testing.T) {

is.Nil(err)

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

Expand Down
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Options struct {
ParseKey bool
// TagName tag name for binding data to struct
//
// Deprecated: please set tag name by DecoderConfig
// Tips: please set tag name by DecoderConfig
TagName string
// Delimiter the delimiter char for split key path, if `FindByPath=true`. default is '.'
Delimiter byte
Expand Down

0 comments on commit 88d7e3d

Please sign in to comment.