Skip to content

Commit

Permalink
Change int64 parsing behaviour to internally do a ParseFloat first in…
Browse files Browse the repository at this point in the history
…stead
  • Loading branch information
knadh committed Nov 23, 2019
1 parent fa779ad commit f97bb3d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions koanf.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,12 +668,12 @@ func toInt64(v interface{}) (int64, error) {
}

// Force it to a string and try to convert.
i, err := strconv.Atoi(fmt.Sprintf("%v", v))
f, err := strconv.ParseFloat(fmt.Sprintf("%v", v), 64)
if err != nil {
return 0, err
}

return int64(i), nil
return int64(f), nil
}

// toInt64 takes an interface v interface{}value and if it is a float type,
Expand Down
2 changes: 1 addition & 1 deletion koanf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ func TestGetTypes(t *testing.T) {
assert.Equal(t, map[string]int64{"key1": 1, "key2": 1, "key3": 1}, c.koanf.Int64Map("parent1.intmap"), "get value mismatch")
assert.Equal(t, map[string]int64{}, c.koanf.Int64Map("parent1.boolmap"), "get value mismatch")
assert.Equal(t, map[string]int64{}, c.koanf.Int64Map("xxxx"), "get value mismatch")
assert.Equal(t, map[string]int64{}, c.koanf.Int64Map("parent1.floatmap"), "get value mismatch")
assert.Equal(t, map[string]int64{"key1": 1, "key2": 1, "key3": 1}, c.koanf.Int64Map("parent1.floatmap"), "get value mismatch")
assert.Equal(t, []int{1, 2, 3}, c.koanf.Ints("parent1.child1.grandchild1.ids"), "get value mismatch")
assert.Equal(t, []int{}, c.koanf.Ints("xxxx"), "get value mismatch")
assert.Equal(t, map[string]int{"key1": 1, "key2": 1, "key3": 1}, c.koanf.IntMap("parent1.intmap"), "get value mismatch")
Expand Down

0 comments on commit f97bb3d

Please sign in to comment.