diff --git a/lib/duration/duration.go b/lib/duration/duration.go index 7ab819c..6ea5932 100644 --- a/lib/duration/duration.go +++ b/lib/duration/duration.go @@ -18,14 +18,8 @@ type Duration struct { } // UnmarshalTOML parses a duration string. -func (d *Duration) UnmarshalTOML(dataInterface interface{}) error { - var data string - switch dataInterface.(type) { - case string: - data = dataInterface.(string) - default: - return fmt.Errorf("invalid duration: \"%s\"", dataInterface) - } +func (d *Duration) UnmarshalText(data []byte) error { + // " + int + unit + " if len(data) < 2 { return fmt.Errorf("invalid duration: \"%s\"", data) diff --git a/lib/duration/duration_test.go b/lib/duration/duration_test.go index 79af648..e318b3c 100644 --- a/lib/duration/duration_test.go +++ b/lib/duration/duration_test.go @@ -16,6 +16,8 @@ func TestDuration(t *testing.T) { duration time.Duration }{ {"", "invalid duration: \"\"", 0}, + {"3", "invalid duration: \"3\"", 0}, + {"am", "unable to parse duration \"am\": strconv.Atoi: parsing \"a\": invalid syntax", 0}, {"1x", "invalid duration unit \"x\"", 0}, {"1s", "", time.Second}, {"73s", "", time.Second * 73}, @@ -34,7 +36,7 @@ func TestDuration(t *testing.T) { for _, test := range tests { d := Duration{} - err := d.UnmarshalTOML(test.input) + err := d.UnmarshalText([]byte(test.input)) duration := d.Duration if test.err == "" { @@ -44,13 +46,4 @@ func TestDuration(t *testing.T) { assert.EqualError(err, test.err) } } - - d := Duration{} - err := d.UnmarshalTOML(3) - assert.Error(err) - assert.Contains(err.Error(), "invalid duration") - - err = d.UnmarshalTOML("am") - assert.Error(err) - assert.EqualError(err, "unable to parse duration \"am\": strconv.Atoi: parsing \"a\": invalid syntax") }