From d57b5aff2f93f2fb7994617b5229ec40a6aa9921 Mon Sep 17 00:00:00 2001 From: inhere Date: Mon, 13 Jul 2020 18:13:32 +0800 Subject: [PATCH] up: support custom binding tag name by change option --- README.md | 6 +++++- README.zh-CN.md | 4 ++++ config.go | 5 +++++ export.go | 5 +++-- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index dadb77d..35826b3 100644 --- a/README.md +++ b/README.md @@ -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 @@ -109,7 +111,7 @@ err = config.BindStruct("user", &user) fmt.Println(user.UserName) // inhere ``` -### Quick Read data +### Direct Read data - Get integer @@ -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 diff --git a/README.zh-CN.md b/README.zh-CN.md index 15f9583..7bba5db 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -98,6 +98,8 @@ func main() { ### 绑定数据到结构体 +> 注意:结构体默认的绑定映射tag是 `mapstructure`,可以通过设置 `Options.TagName` 来更改它 + ```go user := struct { Age int @@ -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 diff --git a/config.go b/config.go index c6623bb..b14c26c 100644 --- a/config.go +++ b/config.go @@ -57,6 +57,8 @@ const ( // default delimiter defaultDelimiter byte = '.' + // default struct tag name for binding data to struct + defaultStructTag = "mapstructure" ) // internal vars @@ -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 @@ -174,6 +178,7 @@ func Default() *Config { func newDefaultOption() *Options { return &Options{ ParseKey: true, + TagName: defaultStructTag, Delimiter: defaultDelimiter, DumpFormat: JSON, diff --git a/export.go b/export.go index df459bb..8a4bc30 100644 --- a/export.go +++ b/export.go @@ -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 }