Skip to content

Commit

Permalink
fix cast test failure (#140)
Browse files Browse the repository at this point in the history
Co-authored-by: Donghong Huang <[email protected]>
  • Loading branch information
EastMacro2020 and Donghong Huang authored Feb 22, 2021
1 parent 1c871db commit d114333
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 56 deletions.
62 changes: 8 additions & 54 deletions pkg/cast/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,65 +206,19 @@ func (val *configValue) ToIntSlice() ([]int, error) {
}

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)
if val.err != nil {
return false, val.err
}

return ca.ToBoolE(val.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)
if val.err != nil {
return 0, val.err
}

return ca.ToFloat64E(val.value)
}

func parsingString(dataType string, value interface{}) (float64, error) {
Expand Down
10 changes: 8 additions & 2 deletions pkg/cast/cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,14 @@ func TestCast(t *testing.T) {
assert.Equal(t, nil, err)
assert.Equal(t, false, configvalue)
configvalue, err = NewValue("improperstring", nil).ToBool()
assert.Equal(t, nil, err)
assert.NotNil(t, err)
assert.Equal(t, false, configvalue)
configvalue, err = NewValue(testmap3, nil).ToBool()
assert.NotEqual(t, nil, err)
assert.Equal(t, false, configvalue)
configvalue, err = NewValue(nil, errors.New("error")).ToBool()
assert.Equal(t, errors.New("error"), err)
assert.False(t, configvalue.(bool))

t.Log("converting the data into float type by ToFloat64 method and verifying")
configvalue, err = NewValue(float64(10), nil).ToFloat64()
Expand Down Expand Up @@ -219,7 +222,7 @@ func TestCast(t *testing.T) {
assert.Equal(t, nil, err)
assert.Equal(t, float64(10), configvalue)
configvalue, err = NewValue(nil, nil).ToFloat64()
assert.Equal(t, nil, err)
assert.NotNil(t, err)
assert.Equal(t, float64(0), configvalue)
configvalue, err = NewValue("10", nil).ToFloat64()
assert.Equal(t, nil, err)
Expand All @@ -230,4 +233,7 @@ func TestCast(t *testing.T) {
configvalue, err = NewValue(testmap3, nil).ToFloat64()
assert.NotEqual(t, nil, err)
assert.Equal(t, float64(0), configvalue)
configvalue, err = NewValue(nil, errors.New("error")).ToFloat64()
assert.Equal(t, errors.New("error"), err)
assert.Equal(t, float64(0), configvalue.(float64))
}

0 comments on commit d114333

Please sign in to comment.