Skip to content

Commit

Permalink
up: update some error define, update the binding struct logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 25, 2022
1 parent a2a0b8b commit b66f432
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 36 deletions.
33 changes: 9 additions & 24 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func MapOnExists(key string, dst interface{}) error {
// MapOnExists mapping data to the dst structure only on key exists.
func (c *Config) MapOnExists(key string, dst interface{}) error {
err := c.Structure(key, dst)
if err != nil && err == errNotFound {
if err != nil && err == ErrNotFound {
return nil
}

Expand All @@ -71,36 +71,21 @@ func (c *Config) MapOnExists(key string, dst interface{}) error {
// config.Structure("db", &dbInfo)
func (c *Config) Structure(key string, dst interface{}) error {
var data interface{}
if key == "" { // binding all data
// binding all data
if key == "" {
data = c.data
} else { // some data of the config
} else {
// binding sub-data of the config
var ok bool
data, ok = c.GetValue(key)
if !ok {
return errNotFound
}
}

var bindConf *mapstructure.DecoderConfig
if c.opts.DecoderConfig == nil {
bindConf = newDefaultDecoderConfig(c.opts.TagName)
} else {
// copy new config for each binding.
copyConf := *c.opts.DecoderConfig
bindConf = &copyConf

// compatible with previous settings opts.TagName
if bindConf.TagName == "" {
bindConf.TagName = c.opts.TagName
return ErrNotFound
}
}

// add hook on decode value to struct
if bindConf.DecodeHook == nil && c.opts.shouldAddHookFunc() {
bindConf.DecodeHook = ValDecodeHookFunc(c.opts.ParseEnv, c.opts.ParseTime)
}

bindConf.Result = dst // set result struct ptr
bindConf := c.opts.makeDecoderConfig()
// set result struct ptr
bindConf.Result = dst
decoder, err := mapstructure.NewDecoder(bindConf)

if err == nil {
Expand Down
30 changes: 29 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ type Options struct {
ReadFormat string
// DecoderConfig setting for binding data to struct. such as: TagName
DecoderConfig *mapstructure.DecoderConfig
// HookFunc on data changed.
// HookFunc on data changed. you can do something...
HookFunc HookFunc
// ParseDefault tag on binding data to struct. tag: default
ParseDefault bool
// WatchChange bool
}

Expand Down Expand Up @@ -77,6 +79,29 @@ func (o *Options) shouldAddHookFunc() bool {
return o.ParseTime || o.ParseEnv
}

func (o *Options) makeDecoderConfig() *mapstructure.DecoderConfig {
var bindConf *mapstructure.DecoderConfig
if o.DecoderConfig == nil {
bindConf = newDefaultDecoderConfig(o.TagName)
} else {
// copy new config for each binding.
copyConf := *o.DecoderConfig
bindConf = &copyConf

// compatible with previous settings opts.TagName
if bindConf.TagName == "" {
bindConf.TagName = o.TagName
}
}

// add hook on decode value to struct
if bindConf.DecodeHook == nil && o.shouldAddHookFunc() {
bindConf.DecodeHook = ValDecodeHookFunc(o.ParseEnv, o.ParseTime)
}

return bindConf
}

/*************************************************************
* config setting
*************************************************************/
Expand All @@ -87,6 +112,9 @@ func ParseEnv(opts *Options) { opts.ParseEnv = true }
// ParseTime set parse time string.
func ParseTime(opts *Options) { opts.ParseTime = true }

// ParseDefault tag value on binding data to struct.
func ParseDefault(opts *Options) { opts.ParseDefault = true }

// Readonly set readonly
func Readonly(opts *Options) { opts.Readonly = true }

Expand Down
14 changes: 4 additions & 10 deletions read.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"errors"
"strconv"
"strings"

Expand All @@ -10,11 +9,6 @@ import (
"github.com/gookit/goutil/strutil"
)

var (
errInvalidKey = errors.New("invalid config key string")
errNotFound = errors.New("this key does not exist in the config")
)

// Exists key exists check
func Exists(key string, findByPath ...bool) bool { return dc.Exists(key, findByPath...) }

Expand Down Expand Up @@ -122,7 +116,7 @@ func GetValue(key string, findByPath ...bool) (interface{}, bool) {
func (c *Config) GetValue(key string, findByPath ...bool) (value interface{}, ok bool) {
sep := c.opts.Delimiter
if key = formatKey(key, string(sep)); key == "" {
c.addError(errInvalidKey)
c.addError(ErrKeyIsEmpty)
return
}

Expand All @@ -139,13 +133,13 @@ func (c *Config) GetValue(key string, findByPath ...bool) (value interface{}, ok

// disable find by path.
if len(findByPath) > 0 && !findByPath[0] {
// c.addError(errNotFound)
// c.addError(ErrNotFound)
return
}

// has sub key? eg. "lang.dir"
if strings.IndexByte(key, sep) == -1 {
// c.addError(errNotFound)
// c.addError(ErrNotFound)
return
}

Expand All @@ -155,7 +149,7 @@ func (c *Config) GetValue(key string, findByPath ...bool) (value interface{}, ok
// find top item data based on top key
var item interface{}
if item, ok = c.data[topK]; !ok {
// c.addError(errNotFound)
// c.addError(ErrNotFound)
return
}

Expand Down
3 changes: 2 additions & 1 deletion write.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"github.com/gookit/goutil/maputil"
)

// some common errors on set value
// some common errors definitions
var (
ErrReadonly = errors.New("the config instance in 'readonly' mode")
ErrKeyIsEmpty = errors.New("the config key is cannot be empty")
ErrNotFound = errors.New("this key does not exist in the config")
)

// SetData for override the Config.Data
Expand Down

0 comments on commit b66f432

Please sign in to comment.