Skip to content

Commit

Permalink
Merge pull request #104 from littldr/fix_negative_int_duration
Browse files Browse the repository at this point in the history
Allow negative integers as durations
  • Loading branch information
knadh authored Sep 1, 2021
2 parents ebc62d9 + eacd3fa commit a242b92
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
8 changes: 7 additions & 1 deletion koanf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var testKeys = []string{
"duration",
"empty",
"intbools",
"negative_int",
"orphan",
"parent1.boolmap.notok3",
"parent1.boolmap.ok1",
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand All @@ -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"))
Expand Down Expand Up @@ -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"))
Expand Down
1 change: 1 addition & 0 deletions mock/mock.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@
"strbools" = ["1", "t", "f"]
"time" = "2019-01-01"
"duration" = "3s"
"negative_int" = -1234
"type" = "hcl"
3 changes: 2 additions & 1 deletion mock/mock.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@
"strbools": ["1", "t", "f"],
"strbool": "1",
"time": "2019-01-01",
"duration": "3s"
"duration": "3s",
"negative_int": -1234
}
1 change: 1 addition & 0 deletions mock/mock.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ strbools = [
strbool = "1"
time = "2019-01-01"
duration = "3s"
negative_int = -1234

[parent1]
name = "parent1"
Expand Down
1 change: 1 addition & 0 deletions mock/mock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ strbools:
strbool: "1"
time: "2019-01-01"
duration: "3s"
negative_int: -1234
2 changes: 1 addition & 1 deletion providers/fs/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit a242b92

Please sign in to comment.