fix:#2006 config.atomicValue Other basic types are supported (#2007)

* Update value.go

* fix:supplement
pull/2009/head
songzhibin97 3 years ago committed by GitHub
parent 8a8626331d
commit ef6fb480c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 40
      config/value.go
  2. 6
      config/value_test.go

@ -40,7 +40,7 @@ func (v *atomicValue) Bool() (bool, error) {
switch val := v.Load().(type) {
case bool:
return val, nil
case int, int32, int64, float64, string:
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, string:
return strconv.ParseBool(fmt.Sprint(val))
}
return false, fmt.Errorf("type assert to %v failed", reflect.TypeOf(v.Load()))
@ -50,10 +50,26 @@ func (v *atomicValue) Int() (int64, error) {
switch val := v.Load().(type) {
case int:
return int64(val), nil
case int8:
return int64(val), nil
case int16:
return int64(val), nil
case int32:
return int64(val), nil
case int64:
return val, nil
case uint:
return int64(val), nil
case uint8:
return int64(val), nil
case uint16:
return int64(val), nil
case uint32:
return int64(val), nil
case uint64:
return int64(val), nil
case float32:
return int64(val), nil
case float64:
return int64(val), nil
case string:
@ -90,14 +106,30 @@ func (v *atomicValue) Map() (map[string]Value, error) {
func (v *atomicValue) Float() (float64, error) {
switch val := v.Load().(type) {
case float64:
return val, nil
case int:
return float64(val), nil
case int8:
return float64(val), nil
case int16:
return float64(val), nil
case int32:
return float64(val), nil
case int64:
return float64(val), nil
case uint:
return float64(val), nil
case uint8:
return float64(val), nil
case uint16:
return float64(val), nil
case uint32:
return float64(val), nil
case uint64:
return float64(val), nil
case float32:
return float64(val), nil
case float64:
return val, nil
case string:
return strconv.ParseFloat(val, 64) //nolint:gomnd
}
@ -108,7 +140,7 @@ func (v *atomicValue) String() (string, error) {
switch val := v.Load().(type) {
case string:
return val, nil
case bool, int, int32, int64, float64:
case bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
return fmt.Sprint(val), nil
case []byte:
return string(val), nil

@ -33,7 +33,7 @@ func Test_atomicValue_Bool(t *testing.T) {
}
}
vlist = []interface{}{uint16(1), "bbb", "-1"}
vlist = []interface{}{"bbb", "-1"}
for _, x := range vlist {
v := atomicValue{}
v.Store(x)
@ -58,7 +58,7 @@ func Test_atomicValue_Int(t *testing.T) {
}
}
vlist = []interface{}{uint16(1), "bbb", "-x1", true}
vlist = []interface{}{"bbb", "-x1", true}
for _, x := range vlist {
v := atomicValue{}
v.Store(x)
@ -83,7 +83,7 @@ func Test_atomicValue_Float(t *testing.T) {
}
}
vlist = []interface{}{float32(1123123), uint16(1), "bbb", "-x1"}
vlist = []interface{}{"bbb", "-x1"}
for _, x := range vlist {
v := atomicValue{}
v.Store(x)

Loading…
Cancel
Save