Add NewDuration constructor.

This commit is contained in:
Mariano Cano 2019-06-05 17:53:28 -07:00
parent 37f2096dff
commit 4fa9e9333d
2 changed files with 42 additions and 3 deletions

View file

@ -12,6 +12,16 @@ type Duration struct {
time.Duration time.Duration
} }
// NewDuration parses a duration string and returns a Duration type or and error
// if the given string is not a duration.
func NewDuration(s string) (*Duration, error) {
d, err := time.ParseDuration(s)
if err != nil {
return nil, errors.Wrapf(err, "error parsing %s as duration", s)
}
return &Duration{Duration: d}, nil
}
// MarshalJSON parses a duration string and sets it to the duration. // MarshalJSON parses a duration string and sets it to the duration.
// //
// A duration string is a possibly signed sequence of decimal numbers, each with // A duration string is a possibly signed sequence of decimal numbers, each with
@ -29,7 +39,7 @@ func (d *Duration) MarshalJSON() ([]byte, error) {
func (d *Duration) UnmarshalJSON(data []byte) (err error) { func (d *Duration) UnmarshalJSON(data []byte) (err error) {
var ( var (
s string s string
_d time.Duration dd time.Duration
) )
if d == nil { if d == nil {
return errors.New("duration cannot be nil") return errors.New("duration cannot be nil")
@ -37,10 +47,10 @@ func (d *Duration) UnmarshalJSON(data []byte) (err error) {
if err = json.Unmarshal(data, &s); err != nil { if err = json.Unmarshal(data, &s); err != nil {
return errors.Wrapf(err, "error unmarshaling %s", data) return errors.Wrapf(err, "error unmarshaling %s", data)
} }
if _d, err = time.ParseDuration(s); err != nil { if dd, err = time.ParseDuration(s); err != nil {
return errors.Wrapf(err, "error parsing %s as duration", s) return errors.Wrapf(err, "error parsing %s as duration", s)
} }
d.Duration = _d d.Duration = dd
return return
} }

View file

@ -6,6 +6,35 @@ import (
"time" "time"
) )
func TestNewDuration(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want *Duration
wantErr bool
}{
{"ok", args{"1h2m3s"}, &Duration{Duration: 3723 * time.Second}, false},
{"fail empty", args{""}, nil, true},
{"fail number", args{"123"}, nil, true},
{"fail string", args{"1hour"}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewDuration(tt.args.s)
if (err != nil) != tt.wantErr {
t.Errorf("NewDuration() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewDuration() = %v, want %v", got, tt.want)
}
})
}
}
func TestDuration_UnmarshalJSON(t *testing.T) { func TestDuration_UnmarshalJSON(t *testing.T) {
type args struct { type args struct {
data []byte data []byte