diff --git a/archaius.go b/archaius.go index b0cd8374..6564eb99 100755 --- a/archaius.go +++ b/archaius.go @@ -5,11 +5,13 @@ package archaius import ( "errors" "fmt" + filesource "github.com/go-chassis/go-archaius/source/file" + "github.com/spf13/cast" + "os" "strings" - "github.com/go-chassis/go-archaius/cast" "github.com/go-chassis/go-archaius/event" "github.com/go-chassis/go-archaius/source" "github.com/go-chassis/go-archaius/source/cli" @@ -155,16 +157,9 @@ func Get(key string) interface{} { } //GetValue return interface -func GetValue(key string) cast.Value { - var confValue cast.Value - val := manager.GetConfig(key) - if val == nil { - confValue = cast.NewValue(nil, source.ErrKeyNotExist) - } else { - confValue = cast.NewValue(val, nil) - } +func GetValue(key string) interface{} { - return confValue + return manager.GetConfig(key) } // Exist check the configuration key existence @@ -179,47 +174,52 @@ func UnmarshalConfig(obj interface{}) error { // GetBool is gives the key value in the form of bool func GetBool(key string, defaultValue bool) bool { - b, err := GetValue(key).ToBool() - if err != nil { + v := GetValue(key) + if v == nil { return defaultValue } - return b + r := cast.ToBool(v) + return r } // GetFloat64 gives the key value in the form of float64 func GetFloat64(key string, defaultValue float64) float64 { - result, err := GetValue(key).ToFloat64() - if err != nil { + v := GetValue(key) + if v == nil { return defaultValue } - return result + r := cast.ToFloat64(v) + return r } // GetInt gives the key value in the form of GetInt func GetInt(key string, defaultValue int) int { - result, err := GetValue(key).ToInt() - if err != nil { + v := GetValue(key) + if v == nil { return defaultValue } - return result + r := cast.ToInt(v) + return r } // GetInt64 gives the key value in the form of int64 func GetInt64(key string, defaultValue int64) int64 { - result, err := GetValue(key).ToInt64() - if err != nil { + v := GetValue(key) + if v == nil { return defaultValue } - return result + r := cast.ToInt64(v) + return r } // GetString gives the key value in the form of GetString func GetString(key string, defaultValue string) string { - result, err := GetValue(key).ToString() - if err != nil { + v := GetValue(key) + if v == nil { return defaultValue } - return result + r := cast.ToString(v) + return r } // GetConfigs gives the information about all configurations diff --git a/archaius_test.go b/archaius_test.go index d595946d..531cc88e 100755 --- a/archaius_test.go +++ b/archaius_test.go @@ -80,11 +80,6 @@ func TestConfig_Get(t *testing.T) { n3 := archaius.GetString("name", "") assert.Equal(t, "peter", n3) - n2 := archaius.GetValue("name") - name, err := n2.ToString() - assert.NoError(t, err) - assert.Equal(t, "peter", name) - b := archaius.Exist("name") assert.True(t, b) diff --git a/cast/cast.go b/cast/cast.go deleted file mode 100755 index 202b8e64..00000000 --- a/cast/cast.go +++ /dev/null @@ -1,291 +0,0 @@ -// Copyright © 2014 Steve Francia . -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. - -// Package cast provides easy and safe casting in Go. - -// Forked from github.com/spf13/cast -// Some parts of this file has been modified to make it functional in this package - -// Package cast provides the typeCasting of an object -package cast - -import ( - "fmt" - "reflect" - "strconv" - - ca "github.com/spf13/cast" -) - -//const -const ( - fmtToFloat64Failed = "unable to cast %#v of type %T to float64" -) - -type configValue struct { - value interface{} - err error -} - -// NewValue creates an object for an interface X -func NewValue(val interface{}, err error) Value { - confVal := new(configValue) - confVal.value = val - confVal.err = err - return confVal -} - -// Value is an interface to typecast an Object -type Value interface { - ToInt64() (int64, error) - ToInt32() (int32, error) - ToInt16() (int16, error) - ToInt8() (int8, error) - ToInt() (int, error) - ToUint() (uint, error) - ToUint64() (uint64, error) - ToUint32() (uint32, error) - ToUint16() (uint16, error) - ToUint8() (uint8, error) - ToString() (string, error) - ToStringMapStringSlice() (map[string][]string, error) - ToStringMapBool() (map[string]bool, error) - ToStringMap() (map[string]interface{}, error) - ToSlice() ([]interface{}, error) - ToBoolSlice() ([]bool, error) - ToStringSlice() ([]string, error) - ToIntSlice() ([]int, error) - ToBool() (bool, error) - ToFloat64() (float64, error) -} - -func (val *configValue) ToInt64() (int64, error) { - if val.err != nil { - return 0, val.err - } - - return ca.ToInt64E(val.value) -} - -func (val *configValue) ToInt32() (int32, error) { - if val.err != nil { - return 0, val.err - } - - return ca.ToInt32E(val.value) -} - -func (val *configValue) ToInt16() (int16, error) { - if val.err != nil { - return 0, val.err - } - - return ca.ToInt16E(val.value) -} - -func (val *configValue) ToInt8() (int8, error) { - if val.err != nil { - return 0, val.err - } - - return ca.ToInt8E(val.value) -} - -func (val *configValue) ToInt() (int, error) { - if val.err != nil { - return 0, val.err - } - - return ca.ToIntE(val.value) -} - -func (val *configValue) ToUint() (uint, error) { - if val.err != nil { - return 0, val.err - } - - return ca.ToUintE(val.value) -} - -func (val *configValue) ToUint64() (uint64, error) { - if val.err != nil { - return 0, val.err - } - - return ca.ToUint64E(val.value) -} - -func (val *configValue) ToUint32() (uint32, error) { - if val.err != nil { - return 0, val.err - } - - return ca.ToUint32E(val.value) -} - -func (val *configValue) ToUint16() (uint16, error) { - if val.err != nil { - return 0, val.err - } - - return ca.ToUint16E(val.value) -} - -func (val *configValue) ToUint8() (uint8, error) { - if val.err != nil { - return 0, val.err - } - - return ca.ToUint8E(val.value) -} - -func (val *configValue) ToString() (string, error) { - if val.err != nil { - return "", val.err - } - - return ca.ToStringE(val.value) -} - -func (val *configValue) ToStringMapStringSlice() (map[string][]string, error) { - if val.err != nil { - return nil, val.err - } - - return ca.ToStringMapStringSliceE(val.value) -} - -func (val *configValue) ToStringMapBool() (map[string]bool, error) { - if val.err != nil { - return nil, val.err - } - - return ca.ToStringMapBoolE(val.value) -} - -func (val *configValue) ToStringMap() (map[string]interface{}, error) { - if val.err != nil { - return nil, val.err - } - - return ca.ToStringMapE(val.value) -} - -func (val *configValue) ToSlice() ([]interface{}, error) { - if val.err != nil { - return nil, val.err - } - - return ca.ToSliceE(val.value) -} - -func (val *configValue) ToBoolSlice() ([]bool, error) { - if val.err != nil { - return nil, val.err - } - - return ca.ToBoolSliceE(val.value) -} - -func (val *configValue) ToStringSlice() ([]string, error) { - if val.err != nil { - return nil, val.err - } - - return ca.ToStringSliceE(val.value) -} - -func (val *configValue) ToIntSlice() ([]int, error) { - if val.err != nil { - return nil, val.err - } - - return ca.ToIntSliceE(val.value) -} - -func (val *configValue) ToBool() (bool, error) { - value := indirect(val.value) - switch dataType := value.(type) { - case bool: - return dataType, nil - case int: - if value.(int) != 0 { - return true, nil - } - return false, nil - case string: - if len(value.(string)) != 0 { - value, parseError := strconv.ParseBool(dataType) - if parseError != nil { - fmt.Println("error in parsing string to bool", parseError, value) - } - return value, nil - } - return false, nil - default: - return false, fmt.Errorf("unable to cast %#v of type %T to bool", value, value) - } -} - -func (val *configValue) ToFloat64() (float64, error) { - value := indirect(val.value) - - switch dataType := value.(type) { - case float64: - return dataType, nil - case float32: - return float64(dataType), nil - case int: - return float64(dataType), nil - case int64: - return float64(dataType), nil - case int32: - return float64(dataType), nil - case int16: - return float64(dataType), nil - case int8: - return float64(dataType), nil - case uint: - return float64(dataType), nil - case uint64: - return float64(dataType), nil - case uint32: - return float64(dataType), nil - case uint16: - return float64(dataType), nil - case uint8: - return float64(dataType), nil - case nil: - return 0, nil - case string: - floatData, err := parsingString(dataType, value) - return floatData, err - default: - return 0, fmt.Errorf(fmtToFloat64Failed, value, value) - } -} - -func parsingString(dataType string, value interface{}) (float64, error) { - parseValue, parseError := strconv.ParseFloat(dataType, 64) - if parseError == nil { - return parseValue, nil - } - return 0, fmt.Errorf(fmtToFloat64Failed, value, value) -} - -func indirect(val interface{}) interface{} { - if val == nil { - return nil - } - if t := reflect.TypeOf(val); t.Kind() != reflect.Ptr { - // Avoid creating a reflect.value if it's not a pointer. - return val - } - value := reflect.ValueOf(val) - for value.Kind() == reflect.Ptr && !value.IsNil() { - value = value.Elem() - } - return value.Interface() -} diff --git a/cast/cast_test.go b/cast/cast_test.go deleted file mode 100755 index eceddecf..00000000 --- a/cast/cast_test.go +++ /dev/null @@ -1,233 +0,0 @@ -package cast - -import ( - "errors" - "reflect" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestCast(t *testing.T) { - - var configvalue interface{} - var err error - - t.Log("Test core/cast/cast.go") - assert.NotEqual(t, nil, NewValue("testkey", nil)) - - t.Log("converting the data into int type by ToIntX methods and verifying") - configvalue, err = NewValue(10, errors.New("error")).ToInt64() - assert.Equal(t, errors.New("error"), err) - configvalue, err = NewValue(10, nil).ToInt64() - assert.Equal(t, int64(10), configvalue) - assert.Equal(t, nil, err) - - configvalue, err = NewValue(10, errors.New("error")).ToInt32() - assert.Equal(t, errors.New("error"), err) - configvalue, err = NewValue(10, nil).ToInt32() - assert.Equal(t, int32(10), configvalue) - assert.Equal(t, nil, err) - - configvalue, err = NewValue(10, errors.New("error")).ToInt16() - assert.Equal(t, errors.New("error"), err) - configvalue, err = NewValue(10, nil).ToInt16() - assert.Equal(t, int16(10), configvalue) - assert.Equal(t, nil, err) - - configvalue, err = NewValue(10, errors.New("error")).ToInt8() - assert.Equal(t, errors.New("error"), err) - configvalue, err = NewValue(10, nil).ToInt8() - assert.Equal(t, int8(10), configvalue) - assert.Equal(t, nil, err) - - configvalue, err = NewValue(10, errors.New("error")).ToInt() - assert.Equal(t, errors.New("error"), err) - configvalue, err = NewValue(10, nil).ToInt() - assert.Equal(t, int(10), configvalue) - assert.Equal(t, 10, configvalue) - assert.Equal(t, nil, err) - - configvalue, err = NewValue(10, errors.New("error")).ToUint() - assert.Equal(t, errors.New("error"), err) - configvalue, err = NewValue(10, nil).ToUint() - assert.Equal(t, uint(10), configvalue) - assert.Equal(t, nil, err) - - configvalue, err = NewValue(10, errors.New("error")).ToUint64() - assert.Equal(t, errors.New("error"), err) - configvalue, err = NewValue(10, nil).ToUint64() - assert.Equal(t, uint64(10), configvalue) - assert.Equal(t, nil, err) - - configvalue, err = NewValue(10, errors.New("error")).ToUint32() - assert.Equal(t, errors.New("error"), err) - configvalue, err = NewValue(10, nil).ToUint32() - assert.Equal(t, uint32(10), configvalue) - assert.Equal(t, nil, err) - - configvalue, err = NewValue(10, errors.New("error")).ToUint16() - assert.Equal(t, errors.New("error"), err) - configvalue, err = NewValue(10, nil).ToUint16() - assert.Equal(t, uint16(10), configvalue) - assert.Equal(t, nil, err) - - configvalue, err = NewValue(10, errors.New("error")).ToUint8() - assert.Equal(t, errors.New("error"), err) - configvalue, err = NewValue(10, nil).ToUint8() - assert.Equal(t, uint8(10), configvalue) - assert.Equal(t, nil, err) - - t.Log("verifying ToString method") - configvalue, err = NewValue("hello", errors.New("error")).ToString() - assert.Equal(t, errors.New("error"), err) - configvalue, err = NewValue("hello", nil).ToString() - assert.Equal(t, "hello", configvalue) - assert.Equal(t, nil, err) - configvalue, err = NewValue(10, nil).ToString() - assert.Equal(t, "10", configvalue) - assert.Equal(t, nil, err) - configvalue, err = NewValue(true, nil).ToString() - assert.Equal(t, "true", configvalue) - assert.Equal(t, nil, err) - - t.Log("verifying ToStringMapStringSlice method in different scenarios") - testmap := map[string]string{"testkey1": "test1", "testkey2": "test2"} - expectedmap := make(map[string][]string) - configvalue, err = NewValue(testmap, errors.New("error")).ToStringMapStringSlice() - assert.Equal(t, errors.New("error"), err) - configvalue, err = NewValue(testmap, nil).ToStringMapStringSlice() - assert.Equal(t, nil, err) - if reflect.TypeOf(configvalue) != reflect.TypeOf(expectedmap) { - t.Error("Fileed to get the data in expected(map[string][]string) format") - } - - t.Log("verifying ToStringMapBool method") - testmap2 := map[string]interface{}{"testkey1": "test1", "testkey2": "test2"} - expectedmap2 := make(map[string]bool) - configvalue, err = NewValue(testmap2, errors.New("error")).ToStringMapBool() - assert.Equal(t, errors.New("error"), err) - configvalue, err = NewValue(testmap2, nil).ToStringMapBool() - assert.Equal(t, nil, err) - if reflect.TypeOf(configvalue) != reflect.TypeOf(expectedmap2) { - t.Error("Fileed to get the data in expected(map[string]bool) format") - } - - t.Log("verifying ToStringMap method") - testmap3 := map[interface{}]interface{}{"testkey1": "test1", "testkey2": "test2"} - expectedmap3 := make(map[string]interface{}) - configvalue, err = NewValue(testmap3, errors.New("error")).ToStringMap() - assert.Equal(t, errors.New("error"), err) - configvalue, err = NewValue(testmap3, nil).ToStringMap() - assert.Equal(t, nil, err) - if reflect.TypeOf(configvalue) != reflect.TypeOf(expectedmap3) { - t.Error("Fileed to get the data in expected(map[string]interface{}) format") - } - - t.Log("verifying ToSlice method") - configvalue, err = NewValue("hello", errors.New("error")).ToSlice() - assert.Equal(t, errors.New("error"), err) - configvalue, err = NewValue(testmap3, nil).ToSlice() - assert.NotEqual(t, nil, configvalue) - assert.NotEqual(t, nil, err) - - t.Log("verifying ToBoolSlice method") - testarray := []int{1, 2, 3} - configvalue, err = NewValue(testarray, errors.New("error")).ToBoolSlice() - assert.Equal(t, errors.New("error"), err) - configvalue2, err := NewValue(testarray, nil).ToBoolSlice() - assert.Equal(t, nil, err) - if configvalue2[0] != true || configvalue2[1] != true || configvalue2[2] != true { - t.Error("Fileed to get the data in expected([]bool) format") - } - - t.Log("verifying ToStringSlice method") - teststring := "hello" - var expectedstring []string - configvalue, err = NewValue(teststring, errors.New("error")).ToStringSlice() - assert.Equal(t, errors.New("error"), err) - configvalue, err = NewValue(teststring, nil).ToStringSlice() - assert.Equal(t, nil, err) - if reflect.TypeOf(configvalue) != reflect.TypeOf(expectedstring) { - t.Error("Fileed to get the data in expected([]string) format") - } - - t.Log("verifying ToIntSlice method") - teststring = "hello" - configvalue, err = NewValue(teststring, errors.New("error")).ToIntSlice() - assert.Equal(t, errors.New("error"), err) - configvalue, err = NewValue(teststring, nil).ToIntSlice() - assert.NotEqual(t, nil, configvalue) - assert.NotEqual(t, nil, err) - - t.Log("verifying ToBool method in different scenarios") - var boolkey = false - configvalue, err = NewValue(boolkey, nil).ToBool() - assert.Equal(t, nil, err) - assert.Equal(t, false, configvalue) - configvalue, err = NewValue(10, nil).ToBool() - assert.Equal(t, nil, err) - assert.Equal(t, true, configvalue) - configvalue, err = NewValue(0, nil).ToBool() - assert.Equal(t, nil, err) - assert.Equal(t, false, configvalue) - configvalue, err = NewValue("true", nil).ToBool() - assert.Equal(t, nil, err) - assert.Equal(t, true, configvalue) - configvalue, err = NewValue("f", nil).ToBool() - assert.Equal(t, nil, err) - assert.Equal(t, false, configvalue) - configvalue, err = NewValue("improperstring", nil).ToBool() - assert.Equal(t, nil, err) - assert.Equal(t, false, configvalue) - configvalue, err = NewValue(testmap3, nil).ToBool() - assert.NotEqual(t, nil, err) - assert.Equal(t, false, configvalue) - - t.Log("converting the data into float type by ToFloat64 method and verifying") - configvalue, err = NewValue(float64(10), nil).ToFloat64() - assert.Equal(t, nil, err) - assert.Equal(t, float64(10), configvalue) - configvalue, err = NewValue(10, nil).ToFloat64() - assert.Equal(t, nil, err) - assert.Equal(t, float64(10), configvalue) - configvalue, err = NewValue(int64(10), nil).ToFloat64() - assert.Equal(t, nil, err) - assert.Equal(t, float64(10), configvalue) - configvalue, err = NewValue(int32(10), nil).ToFloat64() - assert.Equal(t, nil, err) - assert.Equal(t, float64(10), configvalue) - configvalue, err = NewValue(int16(10), nil).ToFloat64() - assert.Equal(t, nil, err) - assert.Equal(t, float64(10), configvalue) - configvalue, err = NewValue(int8(10), nil).ToFloat64() - assert.Equal(t, nil, err) - assert.Equal(t, float64(10), configvalue) - configvalue, err = NewValue(uint(10), nil).ToFloat64() - assert.Equal(t, nil, err) - assert.Equal(t, float64(10), configvalue) - configvalue, err = NewValue(uint64(10), nil).ToFloat64() - assert.Equal(t, nil, err) - assert.Equal(t, float64(10), configvalue) - configvalue, err = NewValue(uint32(10), nil).ToFloat64() - assert.Equal(t, nil, err) - assert.Equal(t, float64(10), configvalue) - configvalue, err = NewValue(uint16(10), nil).ToFloat64() - assert.Equal(t, nil, err) - assert.Equal(t, float64(10), configvalue) - configvalue, err = NewValue(uint8(10), nil).ToFloat64() - assert.Equal(t, nil, err) - assert.Equal(t, float64(10), configvalue) - configvalue, err = NewValue(nil, nil).ToFloat64() - assert.Equal(t, nil, err) - assert.Equal(t, float64(0), configvalue) - configvalue, err = NewValue("10", nil).ToFloat64() - assert.Equal(t, nil, err) - assert.Equal(t, float64(10), configvalue) - configvalue, err = NewValue("hello", nil).ToFloat64() - assert.NotEqual(t, nil, err) - assert.Equal(t, float64(0), configvalue) - configvalue, err = NewValue(testmap3, nil).ToFloat64() - assert.NotEqual(t, nil, err) - assert.Equal(t, float64(0), configvalue) -} diff --git a/go.mod b/go.mod index e3af3a64..43d15130 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/go-chassis/go-archaius require ( github.com/Shonminh/apollo-client v0.4.0 github.com/fsnotify/fsnotify v1.4.7 - github.com/go-chassis/foundation v0.2.2-0.20201210043510-9f6d3de40234 + github.com/go-chassis/foundation v0.2.2 github.com/go-chassis/kie-client v0.0.0-20201210060018-938c7680a9ab github.com/go-chassis/openlog v1.1.2 github.com/gorilla/websocket v1.4.0 diff --git a/go.sum b/go.sum index 1becbdb3..f8cb94d3 100644 --- a/go.sum +++ b/go.sum @@ -19,6 +19,8 @@ github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/ github.com/go-chassis/cari v0.0.0-20201210041921-7b6fbef2df11/go.mod h1:MgtsEI0AM4Ush6Lyw27z9Gk4nQ/8GWTSXrFzupawWDM= github.com/go-chassis/foundation v0.2.2-0.20201210043510-9f6d3de40234 h1:5qa8IJk6m6zP8k9fvUMYULtrLmMRmNAbRprWlgwfLq4= github.com/go-chassis/foundation v0.2.2-0.20201210043510-9f6d3de40234/go.mod h1:2PjwqpVwYEVaAldl5A58a08viH8p27pNeYaiE3ZxOBA= +github.com/go-chassis/foundation v0.2.2 h1:pu378+kf/NejqgHU9LogU6smWpjs+Mx0S99y27nW8OQ= +github.com/go-chassis/foundation v0.2.2/go.mod h1:2PjwqpVwYEVaAldl5A58a08viH8p27pNeYaiE3ZxOBA= github.com/go-chassis/kie-client v0.0.0-20201210060018-938c7680a9ab h1:3vZbK1AZjYiOOXrRUdrcTNKvM1MP2qk73UyRLxaKWbI= github.com/go-chassis/kie-client v0.0.0-20201210060018-938c7680a9ab/go.mod h1:UTdbtyN5ge/v9DmQzdVRxQP7z51Q4z6hyl+W6ZpUHFM= github.com/go-chassis/openlog v1.1.2 h1:LgGfwwOhpU8c6URV6ADpaRBPVY7Ph1C28jCQ6zzQawQ= diff --git a/source/unmarshal.go b/source/unmarshal.go index 5ed034f0..7c0ffb96 100755 --- a/source/unmarshal.go +++ b/source/unmarshal.go @@ -27,8 +27,8 @@ import ( "strings" "unicode" - "github.com/go-chassis/go-archaius/cast" "github.com/go-chassis/openlog" + "github.com/spf13/cast" ) const ( @@ -538,43 +538,27 @@ func toSnake(in string) string { // ToRvalueType Deserializes the object to a particular type func (m *Manager) toRvalueType(confValue interface{}, rValue reflect.Value) (returnValue reflect.Value, err error) { convertType := rValue.Type() - castValue := cast.NewValue(confValue, nil) returnValue = reflect.New(convertType).Elem() switch convertType.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - returnInt, rErr := castValue.ToInt64() - if err != nil { - err = rErr - } + returnInt := cast.ToInt64(confValue) returnValue.SetInt(returnInt) case reflect.String: - returnString, rErr := castValue.ToString() - if err != nil { - err = rErr - } + returnString := cast.ToString(confValue) returnValue.SetString(returnString) case reflect.Float32, reflect.Float64: - returnFloat, rErr := castValue.ToFloat64() - if err != nil { - err = rErr - } + returnFloat := cast.ToFloat64(confValue) returnValue.SetFloat(returnFloat) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - returnUInt, rErr := castValue.ToUint64() - if err != nil { - err = rErr - } + returnUInt := cast.ToUint64(confValue) returnValue.SetUint(returnUInt) case reflect.Bool: - returnBool, rErr := castValue.ToBool() - if err != nil { - err = rErr - } + returnBool := cast.ToBool(confValue) returnValue.SetBool(returnBool) case reflect.Array, reflect.Slice: