From 40b7a6b06996440e06fcc3096a60b998fda12d26 Mon Sep 17 00:00:00 2001 From: songzhibin97 <49082129+songzhibin97@users.noreply.github.com> Date: Fri, 20 May 2022 16:55:32 +0800 Subject: [PATCH] fix:#2006 config.atomicValue Other basic types are supported (#2007) * Update value.go * fix:supplement --- config/value.go | 40 ++++++++++++++++++++++++++++++++++++---- config/value_test.go | 6 +++--- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/config/value.go b/config/value.go index 04c1c0ebe..805f155e5 100644 --- a/config/value.go +++ b/config/value.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 diff --git a/config/value_test.go b/config/value_test.go index 1b1daadc3..9f6745380 100644 --- a/config/value_test.go +++ b/config/value_test.go @@ -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)