From 8dd9a0cafdd442ee853022c45908eeb55374cdf8 Mon Sep 17 00:00:00 2001 From: Cyril Tovena Date: Mon, 23 Mar 2020 10:46:50 -0400 Subject: [PATCH 1/3] Support 0 for model.Duration. This allows to pass the string "0" to parse a model.Duration. This is because `time.Duration` also support this zero value and so it is to interchange model.Duration with time.Duration. time.Duration does parses 0 using this: ``` // Special case: if all that is left is "0", this is zero. if s == "0" { return 0, nil } ``` Signed-off-by: Cyril Tovena --- model/time.go | 4 ++++ model/time_test.go | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/model/time.go b/model/time.go index 7b0064fd..a2fcbe20 100644 --- a/model/time.go +++ b/model/time.go @@ -186,6 +186,10 @@ var durationRE = regexp.MustCompile("^([0-9]+)(y|w|d|h|m|s|ms)$") // ParseDuration parses a string into a time.Duration, assuming that a year // always has 365d, a week always has 7d, and a day always has 24h. func ParseDuration(durationStr string) (Duration, error) { + // shortcut if the value is 0 + if durationStr == "0" { + return 0, nil + } matches := durationRE.FindStringSubmatch(durationStr) if len(matches) != 3 { return 0, fmt.Errorf("not a valid duration string: %q", durationStr) diff --git a/model/time_test.go b/model/time_test.go index 315bc4e5..330cc6e4 100644 --- a/model/time_test.go +++ b/model/time_test.go @@ -90,8 +90,14 @@ func TestParseDuration(t *testing.T) { var cases = []struct { in string out time.Duration + + expectedString string }{ { + in: "0", + out: 0, + expectedString: "0s", + }, { in: "0s", out: 0, }, { @@ -126,7 +132,11 @@ func TestParseDuration(t *testing.T) { if time.Duration(d) != c.out { t.Errorf("Expected %v but got %v", c.out, d) } - if d.String() != c.in { + expectedString := c.expectedString + if c.expectedString == "" { + expectedString = c.in + } + if d.String() != expectedString { t.Errorf("Expected duration string %q but got %q", c.in, d.String()) } } From c1655c136f904ae9a2380fde36ebf9d5a270a460 Mon Sep 17 00:00:00 2001 From: Cyril Tovena Date: Tue, 24 Mar 2020 18:53:05 -0400 Subject: [PATCH 2/3] Improve comment based on review. Signed-off-by: Cyril Tovena --- model/time.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/time.go b/model/time.go index a2fcbe20..adfa42bd 100644 --- a/model/time.go +++ b/model/time.go @@ -186,7 +186,7 @@ var durationRE = regexp.MustCompile("^([0-9]+)(y|w|d|h|m|s|ms)$") // ParseDuration parses a string into a time.Duration, assuming that a year // always has 365d, a week always has 7d, and a day always has 24h. func ParseDuration(durationStr string) (Duration, error) { - // shortcut if the value is 0 + // Special case: if we receive a "0" string we can directly return a zero value without further processing. if durationStr == "0" { return 0, nil } From 83be373190578d89daedb5e2a810abe7682e7bf4 Mon Sep 17 00:00:00 2001 From: Cyril Tovena Date: Wed, 25 Mar 2020 14:19:35 -0400 Subject: [PATCH 3/3] Comments ! Signed-off-by: Cyril Tovena --- model/time.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/time.go b/model/time.go index adfa42bd..490a0240 100644 --- a/model/time.go +++ b/model/time.go @@ -186,7 +186,7 @@ var durationRE = regexp.MustCompile("^([0-9]+)(y|w|d|h|m|s|ms)$") // ParseDuration parses a string into a time.Duration, assuming that a year // always has 365d, a week always has 7d, and a day always has 24h. func ParseDuration(durationStr string) (Duration, error) { - // Special case: if we receive a "0" string we can directly return a zero value without further processing. + // Allow 0 without a unit. if durationStr == "0" { return 0, nil }