From f97bb3dbaeed9288ac6ef7fb1c0a46e6d4c51485 Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Sat, 23 Nov 2019 18:16:51 +0530 Subject: [PATCH] Change int64 parsing behaviour to internally do a ParseFloat first instead --- koanf.go | 4 ++-- koanf_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/koanf.go b/koanf.go index 2e9af9de..c4cbf73c 100644 --- a/koanf.go +++ b/koanf.go @@ -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, diff --git a/koanf_test.go b/koanf_test.go index 7a2be99e..b707e80b 100644 --- a/koanf_test.go +++ b/koanf_test.go @@ -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")