Skip to content

Commit

Permalink
up: replace some input interface{} params to any
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 20, 2022
1 parent 831b20e commit fd71638
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 48 deletions.
7 changes: 7 additions & 0 deletions any.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !go1.18
// +build !go1.18

package config

// alias of interface{}, use for go < 1.18
type any = interface{}
8 changes: 4 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ type Config struct {
// config options
opts *Options
// all config data
data map[string]interface{}
data map[string]any

// loaded config files records
loadedUrls []string
Expand Down Expand Up @@ -114,7 +114,7 @@ func New(name string) *Config {
return &Config{
name: name,
opts: newDefaultOption(),
data: make(map[string]interface{}),
data: make(map[string]any),

// default add JSON driver
encoders: map[string]Encoder{JSON: JSONEncoder},
Expand All @@ -127,7 +127,7 @@ func NewEmpty(name string) *Config {
return &Config{
name: name,
opts: newDefaultOption(),
data: make(map[string]interface{}),
data: make(map[string]any),

// don't add any drivers
encoders: map[string]Encoder{},
Expand Down Expand Up @@ -278,6 +278,6 @@ func (c *Config) addError(err error) {
}

// format and record error
func (c *Config) addErrorf(format string, a ...interface{}) {
func (c *Config) addErrorf(format string, a ...any) {
c.err = fmt.Errorf(format, a...)
}
16 changes: 8 additions & 8 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ type Driver interface {
}

// Decoder for decode yml,json,toml format content
type Decoder func(blob []byte, v interface{}) (err error)
type Decoder func(blob []byte, v any) (err error)

// Encoder for decode yml,json,toml format content
type Encoder func(v interface{}) (out []byte, err error)
type Encoder func(v any) (out []byte, err error)

// StdDriver struct
type StdDriver struct {
Expand All @@ -39,12 +39,12 @@ func (d *StdDriver) Name() string {
}

// Decode of driver
func (d *StdDriver) Decode(blob []byte, v interface{}) (err error) {
func (d *StdDriver) Decode(blob []byte, v any) (err error) {
return d.decoder(blob, v)
}

// Encode of driver
func (d *StdDriver) Encode(v interface{}) ([]byte, error) {
func (d *StdDriver) Encode(v any) ([]byte, error) {
return d.encoder(v)
}

Expand Down Expand Up @@ -75,13 +75,13 @@ var (
)

// JSONDecoder for json decode
var JSONDecoder Decoder = func(data []byte, v interface{}) (err error) {
var JSONDecoder Decoder = func(data []byte, v any) (err error) {
JSONDriver.ClearComments = JSONAllowComments
return JSONDriver.Decode(data, v)
}

// JSONEncoder for json encode
var JSONEncoder Encoder = func(v interface{}) (out []byte, err error) {
var JSONEncoder Encoder = func(v any) (out []byte, err error) {
JSONDriver.MarshalIndent = JSONMarshalIndent
return JSONDriver.Encode(v)
}
Expand All @@ -108,7 +108,7 @@ func (d *jsonDriver) Name() string {
}

// Decode for the driver
func (d *jsonDriver) Decode(data []byte, v interface{}) error {
func (d *jsonDriver) Decode(data []byte, v any) error {
if d.ClearComments {
str := jsonutil.StripComments(string(data))
return json.Unmarshal([]byte(str), v)
Expand All @@ -122,7 +122,7 @@ func (d *jsonDriver) GetDecoder() Decoder {
}

// Encode for the driver
func (d *jsonDriver) Encode(v interface{}) (out []byte, err error) {
func (d *jsonDriver) Encode(v any) (out []byte, err error) {
if len(d.MarshalIndent) > 0 {
return json.MarshalIndent(v, "", d.MarshalIndent)
}
Expand Down
18 changes: 9 additions & 9 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import (
//
// myConf := &MyConf{}
// config.Decode(myConf)
func Decode(dst interface{}) error { return dc.Decode(dst) }
func Decode(dst any) error { return dc.Decode(dst) }

// Decode all config data to the dst ptr.
//
// It's equals:
//
// c.Structure("", dst)
func (c *Config) Decode(dst interface{}) error {
func (c *Config) Decode(dst any) error {
return c.Structure("", dst)
}

Expand All @@ -34,28 +34,28 @@ func (c *Config) Decode(dst interface{}) error {
//
// dbInfo := &Db{}
// config.MapStruct("db", dbInfo)
func MapStruct(key string, dst interface{}) error { return dc.MapStruct(key, dst) }
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 interface{}) error {
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 interface{}) error { return dc.BindStruct(key, dst) }
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 interface{}) error {
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 interface{}) error {
func MapOnExists(key string, dst any) error {
return dc.MapOnExists(key, dst)
}

// MapOnExists mapping data to the dst structure only on key exists.
func (c *Config) MapOnExists(key string, dst interface{}) error {
func (c *Config) MapOnExists(key string, dst any) error {
err := c.Structure(key, dst)
if err != nil && err == ErrNotFound {
return nil
Expand All @@ -70,7 +70,7 @@ func (c *Config) MapOnExists(key string, dst interface{}) error {
//
// dbInfo := Db{}
// config.Structure("db", &dbInfo)
func (c *Config) Structure(key string, dst interface{}) error {
func (c *Config) Structure(key string, dst any) error {
var data interface{}
// binding all data
if key == "" {
Expand Down
45 changes: 22 additions & 23 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (c *Config) Exists(key string, findByPath ...bool) (ok bool) {
topK := keys[0]

// find top item data based on top key
var item interface{}
var item any
if item, ok = c.data[topK]; !ok {
return
}
Expand All @@ -51,11 +51,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/yaml.v3)
case map[string]any: // is map(decode from toml/json/yaml.v3)
if item, ok = typeData[k]; !ok {
return
}
case map[interface{}]interface{}: // is map(decode from yaml.v2)
case map[any]any: // is map(decode from yaml.v2)
if item, ok = typeData[k]; !ok {
return
}
Expand All @@ -71,7 +71,7 @@ func (c *Config) Exists(key string, findByPath ...bool) (ok bool) {
if err != nil || len(typeData) < i {
return false
}
case []interface{}: // is array(load from file)
case []any: // is array(load from file)
i, err := strconv.Atoi(k)
if err != nil || len(typeData) < i {
return false
Expand Down Expand Up @@ -147,7 +147,7 @@ func (c *Config) GetValue(key string, findByPath ...bool) (value interface{}, ok
topK := keys[0]

// find top item data based on top key
var item interface{}
var item any
if item, ok = c.data[topK]; !ok {
// c.addError(ErrNotFound)
return
Expand All @@ -169,11 +169,11 @@ func (c *Config) GetValue(key string, findByPath ...bool) (value interface{}, ok
if item, ok = typeData[k]; !ok {
return
}
case map[string]interface{}: // is map(decode from toml/json)
case map[string]any: // is map(decode from toml/json)
if item, ok = typeData[k]; !ok {
return
}
case map[interface{}]interface{}: // is map(decode from yaml)
case map[any]any: // is map(decode from yaml)
if item, ok = typeData[k]; !ok {
return
}
Expand All @@ -197,7 +197,7 @@ func (c *Config) GetValue(key string, findByPath ...bool) (value interface{}, ok
}

item = typeData[i]
case []interface{}: // is array(load from file)
case []any: // is array(load from file)
i, err := strconv.Atoi(k)
if err != nil || len(typeData) < i {
ok = false
Expand Down Expand Up @@ -269,7 +269,7 @@ func (c *Config) getString(key string) (value string, ok bool) {
return
}

// Int get a int by key
// Int get an int by key
func Int(key string, defVal ...int) int { return dc.Int(key, defVal...) }

// Int get a int value, if not found return default value
Expand Down Expand Up @@ -386,10 +386,10 @@ func (c *Config) Bool(key string, defVal ...bool) (value bool) {
* read config (complex data type)
*************************************************************/

// Ints get config data as a int slice/array
// Ints get config data as an int slice/array
func Ints(key string) []int { return dc.Ints(key) }

// Ints get config data as a int slice/array
// Ints get config data as an int slice/array
func (c *Config) Ints(key string) (arr []int) {
rawVal, ok := c.GetValue(key)
if !ok {
Expand All @@ -399,7 +399,7 @@ func (c *Config) Ints(key string) (arr []int) {
switch typeData := rawVal.(type) {
case []int:
arr = typeData
case []interface{}:
case []any:
for _, v := range typeData {
iv, err := mathutil.ToInt(v)
// iv, err := strconv.Atoi(fmt.Sprintf("%v", v))
Expand Down Expand Up @@ -430,7 +430,7 @@ func (c *Config) IntMap(key string) (mp map[string]int) {
switch typeData := rawVal.(type) {
case map[string]int: // from Set
mp = typeData
case map[string]interface{}: // decode from json,toml
case map[string]any: // decode from json,toml
mp = make(map[string]int)
for k, v := range typeData {
// iv, err := strconv.Atoi(fmt.Sprintf("%v", v))
Expand All @@ -442,7 +442,7 @@ func (c *Config) IntMap(key string) (mp map[string]int) {
}
mp[k] = iv
}
case map[interface{}]interface{}: // if decode from yaml
case map[any]any: // if decode from yaml
mp = make(map[string]int)
for k, v := range typeData {
// iv, err := strconv.Atoi(fmt.Sprintf( "%v", v))
Expand Down Expand Up @@ -485,7 +485,7 @@ func (c *Config) Strings(key string) (arr []string) {
switch typeData := rawVal.(type) {
case []string:
arr = typeData
case []interface{}:
case []any:
for _, v := range typeData {
// arr = append(arr, fmt.Sprintf("%v", v))
arr = append(arr, strutil.MustString(v))
Expand Down Expand Up @@ -528,8 +528,8 @@ func (c *Config) StringMap(key string) (mp map[string]string) {
switch typeData := rawVal.(type) {
case map[string]string: // from Set
mp = typeData
case map[string]interface{}: // decode from json,toml
mp = make(map[string]string)
case map[string]any: // decode from json,toml,yaml.v3
mp = make(map[string]string, len(typeData))

for k, v := range typeData {
switch tv := v.(type) {
Expand All @@ -540,14 +540,14 @@ func (c *Config) StringMap(key string) (mp map[string]string) {
mp[k] = tv
}
default:
mp[k], _ = strutil.AnyToString(v, false)
mp[k] = strutil.QuietString(v)
}
}
case map[interface{}]interface{}: // decode from yaml
mp = make(map[string]string)
case map[any]any: // decode from yaml v2
mp = make(map[string]string, len(typeData))

for k, v := range typeData {
sk, _ := strutil.AnyToString(k, false)
sk := strutil.QuietString(k)

switch typVal := v.(type) {
case string:
Expand All @@ -557,8 +557,7 @@ func (c *Config) StringMap(key string) (mp map[string]string) {
mp[sk] = typVal
}
default:
// mp[sk] = fmt.Sprintf("%v", v)
mp[sk], _ = strutil.AnyToString(v, false)
mp[sk] = strutil.QuietString(v)
}
}
default:
Expand Down
8 changes: 4 additions & 4 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ var (
)

// SetData for override the Config.Data
func SetData(data map[string]interface{}) {
func SetData(data map[string]any) {
dc.SetData(data)
}

// SetData for override the Config.Data
func (c *Config) SetData(data map[string]interface{}) {
func (c *Config) SetData(data map[string]any) {
c.lock.Lock()
c.data = data
c.lock.Unlock()
Expand All @@ -29,12 +29,12 @@ func (c *Config) SetData(data map[string]interface{}) {
}

// Set val by key
func Set(key string, val interface{}, setByPath ...bool) error {
func Set(key string, val any, setByPath ...bool) error {
return dc.Set(key, val, setByPath...)
}

// Set a value by key string.
func (c *Config) Set(key string, val interface{}, setByPath ...bool) (err error) {
func (c *Config) Set(key string, val any, setByPath ...bool) (err error) {
if c.opts.Readonly {
return ErrReadonly
}
Expand Down

0 comments on commit fd71638

Please sign in to comment.