Skip to content

Commit

Permalink
fix failing unit tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
grount authored and Daniel Gront committed Feb 22, 2021
1 parent 46deeac commit a6b8093
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
42 changes: 36 additions & 6 deletions getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -378,6 +406,8 @@ func (ko *Koanf) Strings(path string) []string {
out := make([]string, len(v))
copy(out[:], v[:])
return out
default:
fmt.Printf("type: %T", v)
}
return []string{}
}
Expand Down
3 changes: 3 additions & 0 deletions koanf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions koanf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion mock/mock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,3 @@ strbools:
strbool: "1"
time: "2019-01-01"
duration: "3s"
ints: [1,2,3]

0 comments on commit a6b8093

Please sign in to comment.