diff --git a/getters.go b/getters.go index 81a2cebb..c337e352 100644 --- a/getters.go +++ b/getters.go @@ -319,7 +319,7 @@ func (ko *Koanf) MustFloat64Map(path string) map[string]float64 { // that the key contains a valid numeric value. func (ko *Koanf) Duration(path string) time.Duration { // Look for a parsable string representation first. - if v := ko.Int64(path); v > 0 { + if v := ko.Int64(path); v != 0 { return time.Duration(v) } diff --git a/koanf_test.go b/koanf_test.go index 4b860889..f1a8300c 100644 --- a/koanf_test.go +++ b/koanf_test.go @@ -78,6 +78,7 @@ var testKeys = []string{ "duration", "empty", "intbools", + "negative_int", "orphan", "parent1.boolmap.notok3", "parent1.boolmap.ok1", @@ -117,6 +118,7 @@ var testKeyMap = map[string][]string{ "duration": {"duration"}, "empty": {"empty"}, "intbools": {"intbools"}, + "negative_int": {"negative_int"}, "orphan": {"orphan"}, "parent1": {"parent1"}, "parent1.boolmap": {"parent1", "boolmap"}, @@ -168,6 +170,7 @@ var testAll = `bools -> [true false true] duration -> 3s empty -> map[] intbools -> [1 0 1] +negative_int -> -1234 orphan -> [red blue orange] parent1.boolmap.notok3 -> false parent1.boolmap.ok1 -> true @@ -1060,7 +1063,7 @@ func TestGetTypes(t *testing.T) { assert.Equal(time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC), c.koanf.Time("time", "2006-01-02")) assert.Equal([]string{}, c.koanf.MapKeys("xxxx"), "map keys mismatch") - assert.Equal([]string{"bools", "duration", "empty", "intbools", "orphan", "parent1", "parent2", "strbool", "strbools", "time", "type"}, + assert.Equal([]string{"bools", "duration", "empty", "intbools", "negative_int", "orphan", "parent1", "parent2", "strbool", "strbools", "time", "type"}, c.koanf.MapKeys(""), "map keys mismatch") assert.Equal([]string{"key1", "key2", "key3"}, c.koanf.MapKeys("parent1.strmap"), "map keys mismatch") @@ -1075,9 +1078,11 @@ func TestMustGetTypes(t *testing.T) { // Int. assert.Panics(func() { c.koanf.MustInt64("xxxx") }) assert.Equal(int64(1234), c.koanf.MustInt64("parent1.id")) + assert.Equal(int64(-1234), c.koanf.MustInt64("negative_int")) assert.Panics(func() { c.koanf.MustInt("xxxx") }) assert.Equal(int(1234), c.koanf.MustInt("parent1.id")) + assert.Equal(int(-1234), c.koanf.MustInt("negative_int")) assert.Panics(func() { c.koanf.MustInt64s("xxxx") }) assert.Equal([]int64{1, 2, 3}, c.koanf.MustInt64s("parent1.child1.grandchild1.ids")) @@ -1136,6 +1141,7 @@ func TestMustGetTypes(t *testing.T) { assert.Panics(func() { c.koanf.MustDuration("xxxx") }) assert.Equal(time.Duration(1234), c.koanf.MustDuration("parent1.id")) assert.Equal(time.Second*3, c.koanf.MustDuration("duration")) + assert.Equal(time.Duration(-1234), c.koanf.MustDuration("negative_int")) assert.Panics(func() { c.koanf.MustTime("xxxx", "2006-01-02") }) assert.Equal(time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC), c.koanf.MustTime("time", "2006-01-02")) diff --git a/mock/mock.hcl b/mock/mock.hcl index 7bf44a7e..037dfd94 100644 --- a/mock/mock.hcl +++ b/mock/mock.hcl @@ -55,4 +55,5 @@ "strbools" = ["1", "t", "f"] "time" = "2019-01-01" "duration" = "3s" +"negative_int" = -1234 "type" = "hcl" diff --git a/mock/mock.json b/mock/mock.json index cde06c5f..d1211f30 100644 --- a/mock/mock.json +++ b/mock/mock.json @@ -56,5 +56,6 @@ "strbools": ["1", "t", "f"], "strbool": "1", "time": "2019-01-01", - "duration": "3s" + "duration": "3s", + "negative_int": -1234 } diff --git a/mock/mock.toml b/mock/mock.toml index d59483e2..31ca6652 100644 --- a/mock/mock.toml +++ b/mock/mock.toml @@ -22,6 +22,7 @@ strbools = [ strbool = "1" time = "2019-01-01" duration = "3s" +negative_int = -1234 [parent1] name = "parent1" diff --git a/mock/mock.yml b/mock/mock.yml index 63d0c32d..da0cbd88 100644 --- a/mock/mock.yml +++ b/mock/mock.yml @@ -69,3 +69,4 @@ strbools: strbool: "1" time: "2019-01-01" duration: "3s" +negative_int: -1234 diff --git a/providers/fs/fs_test.go b/providers/fs/fs_test.go index bb190152..de520fc4 100644 --- a/providers/fs/fs_test.go +++ b/providers/fs/fs_test.go @@ -130,7 +130,7 @@ func TestFSProvider(t *testing.T) { assert.Equal(time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC), c.koanf.Time("time", "2006-01-02")) assert.Equal([]string{}, c.koanf.MapKeys("xxxx"), "map keys mismatch") - assert.Equal([]string{"bools", "duration", "empty", "intbools", "orphan", "parent1", "parent2", "strbool", "strbools", "time", "type"}, + assert.Equal([]string{"bools", "duration", "empty", "intbools", "negative_int", "orphan", "parent1", "parent2", "strbool", "strbools", "time", "type"}, c.koanf.MapKeys(""), "map keys mismatch") assert.Equal([]string{"key1", "key2", "key3"}, c.koanf.MapKeys("parent1.strmap"), "map keys mismatch")