Browse Source

lib/duration: Implement encoding.TextUnmarshaler

respondd-crashed
Julian Kornberger 2 years ago
parent
commit
5ca13ff9e5
2 changed files with 5 additions and 18 deletions
  1. + 2
    - 8
      lib/duration/duration.go
  2. + 3
    - 10
      lib/duration/duration_test.go

+ 2
- 8
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)

+ 3
- 10
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")
}

Loading…
Cancel
Save