From 77c5ac039127164e32a490ebd756717462652a8b Mon Sep 17 00:00:00 2001 From: Daniel Gront Date: Mon, 22 Feb 2021 09:54:31 +0200 Subject: [PATCH] fix failing unit tests Ints should avoid calling Ints64s to avoid unneeded conversion between []in64 and []int. MergeAt unit test cannot be tested against JSON file since JSON treats numbers as float64 and confmap.Provider/YAML treats them as integers. --- getters.go | 41 +++++++++++++++++++++++++++++++++++------ koanf.go | 3 +++ koanf_test.go | 4 ++-- mock/mock.yml | 1 - 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/getters.go b/getters.go index ff4ffed8..2f94e7ac 100644 --- a/getters.go +++ b/getters.go @@ -36,6 +36,19 @@ func (ko *Koanf) Int64s(path string) []int64 { var out []int64 switch v := o.(type) { + case []int: + out = make([]int64, 0, len(v)) + for _, vi := range v { + i, err := toInt64(vi) + + // On error, return as it's not a valid + // int slice. + if err != nil { + return []int64{} + } + out = append(out, i) + } + return out case []interface{}: out = make([]int64, 0, len(v)) for _, vi := range v { @@ -128,16 +141,31 @@ func (ko *Koanf) MustInt(path string) int { // empty []int slice if the path does not exist or if the value // is not a valid int slice. func (ko *Koanf) Ints(path string) []int { - ints := ko.Int64s(path) - if len(ints) == 0 { + o := ko.Get(path) + if o == nil { return []int{} } - out := make([]int, len(ints)) - for i, v := range ints { - out[i] = int(v) + var out []int + switch v := o.(type) { + case []int: + return v + case []interface{}: + out = make([]int, 0, len(v)) + for _, vi := range v { + i, err := toInt64(vi) + + // On error, return as it's not a valid + // int slice. + if err != nil { + return []int{} + } + out = append(out, int(i)) + } + return out } - return out + + return []int{} } // MustInts returns the []int slice value of a given key path or panics @@ -379,6 +407,7 @@ func (ko *Koanf) Strings(path string) []string { copy(out[:], v[:]) return out } + return []string{} } diff --git a/koanf.go b/koanf.go index b7e99d31..6a53f46b 100644 --- a/koanf.go +++ b/koanf.go @@ -301,6 +301,9 @@ func (ko *Koanf) Get(path string) interface{} { } out, _ := copystructure.Copy(&res) + if ptrOut, ok := out.(*interface{}); ok { + return *ptrOut + } return out } diff --git a/koanf_test.go b/koanf_test.go index 4b05e2fc..1e8925a6 100644 --- a/koanf_test.go +++ b/koanf_test.go @@ -624,7 +624,7 @@ func TestRaw_YamlTypes(t *testing.T) { "error loading file") raw := k.Raw() - i, ok := raw["ints"] + i, ok := raw["intbools"] assert.True(ok, "ints key does not exist in the map") arr, ok := i.([]interface{}) @@ -665,7 +665,7 @@ func TestMergeAt(t *testing.T) { assert = assert.New(t) k = koanf.New(delim) ) - assert.Nil(k.Load(file.Provider(mockJSON), json.Parser()), + assert.Nil(k.Load(file.Provider(mockYAML), yaml.Parser()), "error loading file") // Get expected koanf, and root data diff --git a/mock/mock.yml b/mock/mock.yml index 6143d097..63d0c32d 100644 --- a/mock/mock.yml +++ b/mock/mock.yml @@ -69,4 +69,3 @@ strbools: strbool: "1" time: "2019-01-01" duration: "3s" -ints: [1,2,3]