From ff817912564cdd74fcccf8d7fa22703de4997eae Mon Sep 17 00:00:00 2001 From: John McFarlane Date: Mon, 4 Jun 2018 09:22:42 -0700 Subject: [PATCH 1/2] Support upstream time.ParseDuration (#117) This change supports model time.ParseDuration and is backwards compatible with upstream. Signed-off-by: John McFarlane --- model/time.go | 4 ++++ model/time_test.go | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/model/time.go b/model/time.go index 74ed5a9f7..66cecc836 100644 --- a/model/time.go +++ b/model/time.go @@ -182,6 +182,10 @@ var durationRE = regexp.MustCompile("^([0-9]+)(y|w|d|h|m|s|ms)$") func ParseDuration(durationStr string) (Duration, error) { matches := durationRE.FindStringSubmatch(durationStr) if len(matches) != 3 { + // If single unit validation fails, still support time.ParseDuration + if d, err := time.ParseDuration(durationStr); err == nil { + return Duration(d), nil + } return 0, fmt.Errorf("not a valid duration string: %q", durationStr) } var ( diff --git a/model/time_test.go b/model/time_test.go index 3efdd65ff..9e75ca6a1 100644 --- a/model/time_test.go +++ b/model/time_test.go @@ -115,6 +115,18 @@ func TestParseDuration(t *testing.T) { in: "10y", out: 10 * 365 * 24 * time.Hour, }, + + // Test compatibility with time.Duration + { + in: "5m0s", + out: 5 * time.Minute, + }, { + in: "5m4s", + out: 5*time.Minute + time.Second*4, + }, { + in: "5m4s3ns", + out: 5*time.Minute + time.Second*4 + time.Nanosecond*3, + }, } for _, c := range cases { @@ -126,7 +138,26 @@ func TestParseDuration(t *testing.T) { t.Errorf("Expected %v but got %v", c.out, d) } if d.String() != c.in { - t.Errorf("Expected duration string %q but got %q", c.in, d.String()) + // If the string representations differ but are logically + // the same consider this a non error condition. + if c.out.Nanoseconds() != time.Duration(d).Nanoseconds() { + t.Errorf("Expected duration string %q but got %q", c.in, d.String()) + } + } + } +} + +func TestParseDurationInvalidFormats(t *testing.T) { + for _, invalid := range []string{ + // Not a duration + "", + // Seemingly valid duration, but is neither single unit nor upstream + "5w0s", + // Assumption about units + "5", + } { + if _, err := ParseDuration(invalid); err == nil { + t.Errorf("Expected error on invalid input %q", invalid) } } } From c0b6942c0cccfd604e64dabd5e5163ec420ab454 Mon Sep 17 00:00:00 2001 From: John McFarlane Date: Mon, 4 Jun 2018 09:23:37 -0700 Subject: [PATCH 2/2] Fix: arg p.currentLabelPair.Value of wrong type *string Signed-off-by: John McFarlane --- expfmt/text_parse.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/expfmt/text_parse.go b/expfmt/text_parse.go index b86290afa..ec3d86ba7 100644 --- a/expfmt/text_parse.go +++ b/expfmt/text_parse.go @@ -359,7 +359,7 @@ func (p *TextParser) startLabelValue() stateFn { } return p.readingValue default: - p.parseError(fmt.Sprintf("unexpected end of label value %q", p.currentLabelPair.Value)) + p.parseError(fmt.Sprintf("unexpected end of label value %q", p.currentLabelPair.GetValue())) return nil } }