From a2a0b8b357f64faadf17c2eff5bd2545abfa7263 Mon Sep 17 00:00:00 2001 From: Inhere Date: Fri, 26 Aug 2022 00:30:08 +0800 Subject: [PATCH] chore: remove un-used func, update some code styles --- load.go | 30 ++++++++++++++++-------------- read.go | 25 ++++++++++++++----------- write.go | 47 +---------------------------------------------- 3 files changed, 31 insertions(+), 71 deletions(-) diff --git a/load.go b/load.go index 4c1a850..efb24bf 100644 --- a/load.go +++ b/load.go @@ -46,7 +46,8 @@ func LoadRemote(format, url string) error { return dc.LoadRemote(format, url) } // LoadRemote load config data from remote URL. // // Usage: -// c.LoadRemote(config.JSON, "http://abc.com/api-config.json") +// +// c.LoadRemote(config.JSON, "http://abc.com/api-config.json") func (c *Config) LoadRemote(format, url string) (err error) { // create http client client := http.Client{Timeout: 300 * time.Second} @@ -59,13 +60,12 @@ func (c *Config) LoadRemote(format, url string) (err error) { defer resp.Body.Close() if resp.StatusCode != 200 { - return fmt.Errorf("fetch remote resource error, reply status code is not equals to 200") + return fmt.Errorf("fetch remote config error, reply status code is %d", resp.StatusCode) } // read response content bts, err := ioutil.ReadAll(resp.Body) if err == nil { - // parse file content if err = c.parseSourceCode(format, bts); err != nil { return } @@ -81,7 +81,7 @@ func LoadOSEnv(keys []string, keyToLower bool) { dc.LoadOSEnv(keys, keyToLower) func (c *Config) LoadOSEnv(keys []string, keyToLower bool) { for _, key := range keys { // NOTICE: - // if is windows os, os.Getenv() Key is not case sensitive + // if is windows os, os.Getenv() Key is not case-sensitive val := os.Getenv(key) if keyToLower { key = strings.ToLower(key) @@ -108,8 +108,9 @@ func LoadFlags(keys []string) error { return dc.LoadFlags(keys) } // LoadFlags parse command line arguments, based on provide keys. // // Usage: -// // debug flag is bool type -// c.LoadFlags([]string{"env", "debug:bool"}) +// +// // debug flag is bool type +// c.LoadFlags([]string{"env", "debug:bool"}) func (c *Config) LoadFlags(keys []string) (err error) { hash := map[string]interface{}{} @@ -161,7 +162,7 @@ func LoadData(dataSource ...interface{}) error { return dc.LoadData(dataSource.. // LoadData load data from map OR struct // // The dataSources can be: -// - map[string]interface{} +// - map[string]interface{} func (c *Config) LoadData(dataSources ...interface{}) (err error) { if c.opts.Delimiter == 0 { c.opts.Delimiter = defaultDelimiter @@ -186,10 +187,12 @@ func LoadSources(format string, src []byte, more ...[]byte) error { // LoadSources load data from byte content. // // Usage: -// config.LoadSources(config.Yml, []byte(` -// name: blog -// arr: -// key: val +// +// config.LoadSources(config.Yml, []byte(` +// name: blog +// arr: +// key: val +// // `)) func (c *Config) LoadSources(format string, src []byte, more ...[]byte) (err error) { err = c.parseSourceCode(format, src) @@ -259,7 +262,6 @@ func (c *Config) LoadExistsByFormat(format string, sourceFiles ...string) (err e // load config file func (c *Config) loadFile(file string, loadExist bool, format string) (err error) { - // open file fd, err := os.Open(file) if err != nil { // skip not exist file @@ -274,8 +276,8 @@ func (c *Config) loadFile(file string, loadExist bool, format string) (err error // read file content bts, err := ioutil.ReadAll(fd) if err == nil { + // get format for file ext if format == "" { - // get format for file ext format = strings.Trim(filepath.Ext(file), ".") } @@ -317,7 +319,7 @@ func (c *Config) parseSourceCode(format string, blob []byte) (err error) { err = mergo.Merge(&c.data, data, mergo.WithOverride, mergo.WithTypeCheck) } - if err != nil { + if err == nil { c.fireHook(OnLoadData) } data = nil diff --git a/read.go b/read.go index b0edd88..bcfaa2c 100644 --- a/read.go +++ b/read.go @@ -57,11 +57,11 @@ func (c *Config) Exists(key string, findByPath ...bool) (ok bool) { if item, ok = typeData[k]; !ok { return } - case map[string]interface{}: // is map(decode from toml/json) + case map[string]interface{}: // is map(decode from toml/json/yaml.v3) if item, ok = typeData[k]; !ok { return } - case map[interface{}]interface{}: // is map(decode from yaml) + case map[interface{}]interface{}: // is map(decode from yaml.v2) if item, ok = typeData[k]; !ok { return } @@ -102,8 +102,9 @@ func (c *Config) Data() map[string]interface{} { } // Get config value by key string, support get sub-value by key path(eg. 'map.key'), -// ok is true, find value from config -// ok is false, not found or error +// +// - ok is true, find value from config +// - ok is false, not found or error func Get(key string, findByPath ...bool) interface{} { return dc.Get(key, findByPath...) } // Get config value by key @@ -317,7 +318,7 @@ func (c *Config) Int64(key string, defVal ...int64) (value int64) { return } -// try get a int64 value by given key +// try to get an int64 value by given key func (c *Config) tryInt64(key string) (value int64, ok bool) { strVal, ok := c.getString(key) if !ok { @@ -356,13 +357,15 @@ func Bool(key string, defVal ...bool) bool { return dc.Bool(key, defVal...) } // Bool looks up a value for a key in this section and attempts to parse that value as a boolean, // along with a boolean result similar to a map lookup. +// // of following(case insensitive): -// - true -// - yes -// - false -// - no -// - 1 -// - 0 +// - true +// - yes +// - false +// - no +// - 1 +// - 0 +// // The `ok` boolean will be false in the event that the value could not be parsed as a bool func (c *Config) Bool(key string, defVal ...bool) (value bool) { rawVal, ok := c.getString(key) diff --git a/write.go b/write.go index 1150df8..e6500df 100644 --- a/write.go +++ b/write.go @@ -4,11 +4,10 @@ import ( "errors" "strings" - "github.com/gookit/goutil/arrutil" "github.com/gookit/goutil/maputil" ) -// some common errors +// some common errors on set value var ( ErrReadonly = errors.New("the config instance in 'readonly' mode") ErrKeyIsEmpty = errors.New("the config key is cannot be empty") @@ -63,47 +62,3 @@ func (c *Config) Set(key string, val interface{}, setByPath ...bool) (err error) keys := strings.Split(key, string(sep)) return maputil.SetByKeys(&c.data, keys, val) } - -/** -more setter: SetIntArr, SetIntMap, SetString, SetStringArr, SetStringMap -*/ - -// build new value by key paths -// "site.info" -> map[string]map[string]val -func buildValueByPath(paths []string, val interface{}) (newItem map[string]interface{}) { - if len(paths) == 1 { - return map[string]interface{}{paths[0]: val} - } - - arrutil.Reverse(paths) - - // multi nodes - for _, p := range paths { - if newItem == nil { - newItem = map[string]interface{}{p: val} - } else { - newItem = map[string]interface{}{p: newItem} - } - } - return -} - -// build new value by key paths, only for yaml.v2 -// "site.info" -> map[interface{}]map[string]val -func buildValueByPath1(paths []string, val interface{}) (newItem map[interface{}]interface{}) { - if len(paths) == 1 { - return map[interface{}]interface{}{paths[0]: val} - } - - arrutil.Reverse(paths) - - // multi nodes - for _, p := range paths { - if newItem == nil { - newItem = map[interface{}]interface{}{p: val} - } else { - newItem = map[interface{}]interface{}{p: newItem} - } - } - return -}