Skip to content

Commit

Permalink
up: support custom binding tag name by change option
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 13, 2020
1 parent 59422ed commit d57b5af
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ func main() {

## Map Data To Structure

> Note: The default binding mapping tag of a structure is `mapstructure`, which can be changed by setting `Options.TagName`
```go
user := struct {
Age int
Expand All @@ -109,7 +111,7 @@ err = config.BindStruct("user", &user)
fmt.Println(user.UserName) // inhere
```

### Quick Read data
### Direct Read data

- Get integer

Expand Down Expand Up @@ -232,6 +234,8 @@ type Options struct {
EnableCache bool
// parse key, allow find value by key path. default is True eg: 'key.sub' will find `map[key]sub`
ParseKey bool
// tag name for binding data to struct
TagName string
// the delimiter char for split key, when `FindByPath=true`. default is '.'
Delimiter byte
// default write format. default is JSON
Expand Down
4 changes: 4 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ func main() {

### 绑定数据到结构体

> 注意:结构体默认的绑定映射tag是 `mapstructure`,可以通过设置 `Options.TagName` 来更改它
```go
user := struct {
Age int
Expand Down Expand Up @@ -215,6 +217,8 @@ type Options struct {
EnableCache bool
// parse key, allow find value by key path. default is True eg: 'key.sub' will find `map[key]sub`
ParseKey bool
// tag name for binding data to struct
TagName string
// the delimiter char for split key, when `FindByPath=true`. default is '.'
Delimiter byte
// default write format. default is JSON
Expand Down
5 changes: 5 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const (

// default delimiter
defaultDelimiter byte = '.'
// default struct tag name for binding data to struct
defaultStructTag = "mapstructure"
)

// internal vars
Expand Down Expand Up @@ -93,6 +95,8 @@ type Options struct {
EnableCache bool
// parse key, allow find value by key path. eg: 'key.sub' will find `map[key]sub`
ParseKey bool
// tag name for binding data to struct
TagName string
// the delimiter char for split key path, if `FindByPath=true`. default is '.'
Delimiter byte
// default write format
Expand Down Expand Up @@ -174,6 +178,7 @@ func Default() *Config {
func newDefaultOption() *Options {
return &Options{
ParseKey: true,
TagName: defaultStructTag,
Delimiter: defaultDelimiter,

DumpFormat: JSON,
Expand Down
5 changes: 3 additions & 2 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ func (c *Config) Structure(key string, dst interface{}) error {
}

// err = mapstructure.Decode(data, dst)
config := &mapstructure.DecoderConfig{
mapConf := &mapstructure.DecoderConfig{
Metadata: nil,
Result: dst,
TagName: c.opts.TagName,
// will auto convert string to int/uint
WeaklyTypedInput: true,
}

decoder, err := mapstructure.NewDecoder(config)
decoder, err := mapstructure.NewDecoder(mapConf)
if err != nil {
return err
}
Expand Down

0 comments on commit d57b5af

Please sign in to comment.