fix(yaml): parse float64 from uint64

resolves ##3591
This commit is contained in:
Jan De Dobbeleer 2023-03-16 07:06:39 +01:00 committed by Jan De Dobbeleer
parent 01c5f6bf71
commit 3067ccd3a5
2 changed files with 27 additions and 27 deletions

View file

@ -98,17 +98,18 @@ func (m Map) GetFloat64(property Property, defaultValue float64) float64 {
return defaultValue
}
if floatValue, ok := val.(float64); ok {
return floatValue
}
// config parser parses an int
intValue, ok := val.(int)
if !ok {
switch v := val.(type) {
case int:
return float64(v)
case int64:
return float64(v)
case uint64:
return float64(v)
case float64:
return v
default:
return defaultValue
}
return float64(intValue)
}
func (m Map) GetInt(property Property, defaultValue int) int {

View file

@ -86,10 +86,23 @@ func TestGetBoolInvalidProperty(t *testing.T) {
}
func TestGetFloat64(t *testing.T) {
expected := float64(1337)
var properties = Map{Foo: expected}
value := properties.GetFloat64(Foo, 9001)
assert.Equal(t, expected, value)
cases := []struct {
Case string
Expected float64
Input interface{}
}{
{Case: "int", Expected: 1337, Input: 1337},
{Case: "float64", Expected: 1337, Input: float64(1337)},
{Case: "uint64", Expected: 1337, Input: uint64(1337)},
{Case: "int64", Expected: 1337, Input: int64(1337)},
{Case: "string", Expected: 9001, Input: "invalid"},
{Case: "bool", Expected: 9001, Input: true},
}
for _, tc := range cases {
properties := Map{Foo: tc.Input}
value := properties.GetFloat64(Foo, 9001)
assert.Equal(t, tc.Expected, value, tc.Case)
}
}
func TestGetFloat64PropertyNotInMap(t *testing.T) {
@ -98,17 +111,3 @@ func TestGetFloat64PropertyNotInMap(t *testing.T) {
value := properties.GetFloat64(Foo, expected)
assert.Equal(t, expected, value)
}
func TestGetFloat64InvalidStringProperty(t *testing.T) {
expected := float64(1337)
var properties = Map{Foo: "invalid"}
value := properties.GetFloat64(Foo, expected)
assert.Equal(t, expected, value)
}
func TestGetFloat64InvalidBoolProperty(t *testing.T) {
expected := float64(1337)
var properties = Map{Foo: true}
value := properties.GetFloat64(Foo, expected)
assert.Equal(t, expected, value)
}