Add whitespaces trimming at duration unmarshalling (#19)
This commit is contained in:
Родитель
749ae1f3dc
Коммит
36aa666702
|
@ -25,6 +25,7 @@ func (dur Duration) MarshalText() ([]byte, error) {
|
|||
// UnmarshalText implements the encoding.TextUnmarshaler interface.
|
||||
func (dur *Duration) UnmarshalText(data []byte) (err error) {
|
||||
s := string(data)
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" || strings.ToLower(s) == "undefined" {
|
||||
*dur = 0
|
||||
return nil
|
||||
|
|
|
@ -40,6 +40,10 @@ func TestDurationUnmarshal(t *testing.T) {
|
|||
assert.Equal(t, Duration(2*time.Second), d)
|
||||
}
|
||||
d = 0
|
||||
if assert.NoError(t, d.UnmarshalText([]byte(" 00:00:02 "))) {
|
||||
assert.Equal(t, Duration(2*time.Second), d)
|
||||
}
|
||||
d = 0
|
||||
if assert.NoError(t, d.UnmarshalText([]byte("00:02:00"))) {
|
||||
assert.Equal(t, Duration(2*time.Minute), d)
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче