Skip to content

Commit

Permalink
chore: remove un-used func, update some code styles
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 25, 2022
1 parent 88d7e3d commit a2a0b8b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 71 deletions.
30 changes: 16 additions & 14 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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{}{}

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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), ".")
}

Expand Down Expand Up @@ -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
Expand Down
25 changes: 14 additions & 11 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
47 changes: 1 addition & 46 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}

0 comments on commit a2a0b8b

Please sign in to comment.