Skip to content

Commit

Permalink
Accept int(0) as zero duration
Browse files Browse the repository at this point in the history
While in go-yaml#200 it was correctly
stated that durations without units are misleading, there's an exception
to that: zero duration.

This is a special case in time.Duration.ParseDuration():
https://github.com/golang/go/blob/176b63e7113b82c140a4ecb2620024526c2c42e3/src/time/format.go#L1536-L1539

And supporting this improves backwards compatibility with YAMLs written
for v2, as its very common to write just a zero for zero duration
(especially because this works for other config-parsing sources like
flags).

Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com>
  • Loading branch information
colega committed Jul 20, 2022
1 parent f6f7691 commit 9f1369f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,9 @@ func (d *decoder) scalar(n *Node, out reflect.Value) bool {
if !isDuration && !out.OverflowInt(int64(resolved)) {
out.SetInt(int64(resolved))
return true
} else if isDuration && resolved == 0 {
out.SetInt(0)
return true
}
case int64:
if !isDuration && !out.OverflowInt(resolved) {
Expand Down
12 changes: 12 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,18 @@ var unmarshalTests = []struct {
map[string]time.Duration{"a": 3 * time.Second},
},

// Zero duration as a string.
{
"a: '0'",
map[string]time.Duration{"a": 0},
},

// Zero duration as an int.
{
"a: 0",
map[string]time.Duration{"a": 0},
},

// Issue #24.
{
"a: <foo>",
Expand Down

0 comments on commit 9f1369f

Please sign in to comment.