Skip to content

Commit

Permalink
add new method for Config
Browse files Browse the repository at this point in the history
  • Loading branch information
yukselcodingwithyou committed Nov 29, 2021
1 parent 6cf9188 commit 770e900
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,36 +102,31 @@ func (v Value) error() error {
return v.err
}

func getType(key string, config Config) (interface{}, error) {
for k, v := range config {
if key == k {
return v, nil
}
switch typedValue := v.(type) {
case map[string]interface{}:
return getType(key, typedValue)
}
}
return nil, errKeyNotFound(key)
}

func getValueFromConfig(m Config, ks []string) (val interface{}, err error) {
var ok bool

if len(ks) == 0 {
return nil, fmt.Errorf("config needs at least one key")
}
if val, ok = m[ks[0]]; !ok {
return nil, fmt.Errorf("key not found; remaining keys: %v", ks)
} else if len(ks) == 1 { // we've reached the final key
return val, nil
} else if m, ok = val.(Config); !ok {
} else if ok = isValueConvertible(val); !ok {
return nil, fmt.Errorf("malformed structure at %#v", val)
} else {
return getValueFromConfig(m, ks[1:])
}
}

func isValueConvertible(val interface{}) bool {
_, ok := val.(Config)
if !ok {
_ = val.(map[string]interface{})
return true
}
return ok
}

func errKeyNotFound(key string) error {
err := errors.New(fmt.Sprintf("given key '%s' not exists in configuration", key))
logError(err)
Expand Down

0 comments on commit 770e900

Please sign in to comment.