build(deps): bump github.com/gookit/config/v2 from 2.2.6 to 2.2.7
Bumps [github.com/gookit/config/v2](https://github.com/gookit/config) from 2.2.6 to 2.2.7. - [Release notes](https://github.com/gookit/config/releases) - [Commits](https://github.com/gookit/config/compare/v2.2.6...v2.2.7) --- updated-dependencies: - dependency-name: github.com/gookit/config/v2 dependency-version: 2.2.7 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com>
This commit is contained in:
+4
-1
@@ -421,7 +421,10 @@ config.WithOptions(func(opt *Options) {
|
||||
|
||||
### Options: Parse default
|
||||
|
||||
Support parse default value by struct tag `default`
|
||||
Support parse default value by struct tag `default`, and support parse fields in sub struct.
|
||||
|
||||
> **NOTE**⚠️ If you want to parse a sub-struct, you need to set the `default:""` flag on the parent struct,
|
||||
> otherwise the fields of the sub-struct will not be resolved.
|
||||
|
||||
```go
|
||||
// add option: config.ParseDefault
|
||||
|
||||
+4
-2
@@ -26,7 +26,7 @@
|
||||
- `Readonly` 支持设置配置数据只读
|
||||
- `EnableCache` 支持设置配置数据缓存
|
||||
- `ParseEnv` 支持获取时自动解析string值里的ENV变量(`shell: ${SHELL}` -> `shell: /bin/zsh`)
|
||||
- `ParseDefault` 支持在绑定数据到结构体时解析默认值 (tag: `default:"def_value"`, 配合ParseEnv也支持ENV变量)
|
||||
- `ParseDefault` 支持在绑定数据到结构体时解析默认值 (tag: `default:"def_value"`, 配合`ParseEnv`也支持ENV变量)
|
||||
- `ParseTime` 支持绑定数据到struct时自动转换 `10s`,`2m` 为 `time.Duration`
|
||||
- 完整选项设置请查看 `config.Options`
|
||||
- 支持将全部或部分配置数据绑定到结构体 `config.BindStruct("key", &s)`
|
||||
@@ -414,7 +414,9 @@ config.WithOptions(func(opt *Options) {
|
||||
|
||||
### 选项: 解析默认值
|
||||
|
||||
NEW: 支持通过结构标签 `default` 解析并设置默认值
|
||||
NEW: 支持通过结构标签 `default` 解析并设置默认值,支持嵌套解析处理。
|
||||
|
||||
> 注意 ⚠️ 如果想要解析子结构体字段,需要对父结构体设置 `default:""` 标记,否则不会解析子结构体的字段。
|
||||
|
||||
```go
|
||||
// add option: config.ParseDefault
|
||||
|
||||
+5
-8
@@ -7,8 +7,8 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/go-viper/mapstructure/v2"
|
||||
"github.com/gookit/goutil/structs"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
// Decode all config data to the dst ptr
|
||||
@@ -37,17 +37,13 @@ func (c *Config) Decode(dst any) error {
|
||||
func MapStruct(key string, dst any) error { return dc.MapStruct(key, dst) }
|
||||
|
||||
// MapStruct alias method of the 'Structure'
|
||||
func (c *Config) MapStruct(key string, dst any) error {
|
||||
return c.Structure(key, dst)
|
||||
}
|
||||
func (c *Config) MapStruct(key string, dst any) error { return c.Structure(key, dst) }
|
||||
|
||||
// BindStruct alias method of the 'Structure'
|
||||
func BindStruct(key string, dst any) error { return dc.BindStruct(key, dst) }
|
||||
|
||||
// BindStruct alias method of the 'Structure'
|
||||
func (c *Config) BindStruct(key string, dst any) error {
|
||||
return c.Structure(key, dst)
|
||||
}
|
||||
func (c *Config) BindStruct(key string, dst any) error { return c.Structure(key, dst) }
|
||||
|
||||
// MapOnExists mapping data to the dst structure only on key exists.
|
||||
func MapOnExists(key string, dst any) error {
|
||||
@@ -63,7 +59,6 @@ func (c *Config) MapOnExists(key string, dst any) error {
|
||||
if err != nil && err == ErrNotFound {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -86,6 +81,7 @@ func (c *Config) Structure(key string, dst any) (err error) {
|
||||
if c.opts.ParseDefault {
|
||||
err = structs.InitDefaults(dst, func(opt *structs.InitOptions) {
|
||||
opt.ParseEnv = c.opts.ParseEnv
|
||||
opt.ParseTime = c.opts.ParseTime // add ParseTime support on parse default value
|
||||
})
|
||||
}
|
||||
return
|
||||
@@ -116,6 +112,7 @@ func (c *Config) Structure(key string, dst any) (err error) {
|
||||
if c.opts.ParseDefault {
|
||||
err = structs.InitDefaults(dst, func(opt *structs.InitOptions) {
|
||||
opt.ParseEnv = c.opts.ParseEnv
|
||||
opt.ParseTime = c.opts.ParseTime
|
||||
})
|
||||
}
|
||||
return err
|
||||
|
||||
+5
-2
@@ -4,8 +4,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"dario.cat/mergo"
|
||||
"github.com/go-viper/mapstructure/v2"
|
||||
"github.com/gookit/goutil"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
// there are some event names for config data changed.
|
||||
@@ -33,12 +33,15 @@ type Options struct {
|
||||
// ParseDefault tag on binding data to struct. default: false
|
||||
//
|
||||
// - tag: default
|
||||
//
|
||||
// NOTE: If you want to parse a substruct, you need to set the `default:""` flag on the struct,
|
||||
// otherwise the fields that will not resolve to it will not be resolved.
|
||||
ParseDefault bool
|
||||
// Readonly config is readonly. default: false
|
||||
Readonly bool
|
||||
// EnableCache enable config data cache. default: false
|
||||
EnableCache bool
|
||||
// ParseKey support key path, allow find value by key path. default: true
|
||||
// ParseKey support key path, allow finding value by key path. default: true
|
||||
//
|
||||
// - eg: 'key.sub' will find `map[key]sub`
|
||||
ParseKey bool
|
||||
|
||||
+5
-11
@@ -4,10 +4,10 @@ import (
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-viper/mapstructure/v2"
|
||||
"github.com/gookit/goutil/envutil"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/gookit/goutil/reflects"
|
||||
)
|
||||
|
||||
// ValDecodeHookFunc returns a mapstructure.DecodeHookFunc
|
||||
@@ -31,15 +31,9 @@ func ValDecodeHookFunc(parseEnv, parseTime bool) mapstructure.DecodeHookFunc {
|
||||
return str, nil
|
||||
}
|
||||
|
||||
// start char is number(1-9)
|
||||
if str[0] > '0' && str[0] <= '9' {
|
||||
// parse time string. eg: 10s
|
||||
if parseTime && t.Kind() == reflect.Int64 {
|
||||
dur, err := time.ParseDuration(str)
|
||||
if err == nil {
|
||||
return dur, nil
|
||||
}
|
||||
}
|
||||
// feat: support parse time or duration string. eg: 10s
|
||||
if parseTime && str[0] > '0' && str[0] <= '9' {
|
||||
return reflects.ToTimeOrDuration(str, t)
|
||||
}
|
||||
return str, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user