diff --git a/internal/internal.go b/internal/internal.go index a278d2da0..28b37c09f 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -36,6 +36,12 @@ type Duration struct { func (d *Duration) UnmarshalTOML(b []byte) error { var err error + // see if we can straight convert it + d.Duration, err = time.ParseDuration(string(b)) + if err == nil { + return nil + } + // Parse string duration, ie, "1s" if uq, err := strconv.Unquote(string(b)); err == nil && len(uq) > 0 { d.Duration, err = time.ParseDuration(uq) diff --git a/internal/internal_test.go b/internal/internal_test.go index c18991c2d..0d9821857 100644 --- a/internal/internal_test.go +++ b/internal/internal_test.go @@ -131,3 +131,22 @@ func TestRandomSleep(t *testing.T) { elapsed = time.Since(s) assert.True(t, elapsed < time.Millisecond*150) } + +func TestDuration(t *testing.T) { + var d Duration + + d.UnmarshalTOML([]byte(`"1s"`)) + assert.Equal(t, time.Second, d.Duration) + + d = Duration{} + d.UnmarshalTOML([]byte(`1s`)) + assert.Equal(t, time.Second, d.Duration) + + d = Duration{} + d.UnmarshalTOML([]byte(`10`)) + assert.Equal(t, 10*time.Second, d.Duration) + + d = Duration{} + d.UnmarshalTOML([]byte(`1.5`)) + assert.Equal(t, time.Second, d.Duration) +}