Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion expfmt/text_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
4 changes: 4 additions & 0 deletions model/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
33 changes: 32 additions & 1 deletion model/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
}