Don't use pointer in TimeDuration.MarshalJSON

This commit is contained in:
Mariano Cano 2019-03-25 12:34:01 -07:00
parent 698058baa9
commit 6d92ba75b9
2 changed files with 35 additions and 10 deletions

View file

@ -19,6 +19,11 @@ type TimeDuration struct {
d time.Duration d time.Duration
} }
// NewTimeDuration returns a TimeDuration with the defined time.
func NewTimeDuration(t time.Time) TimeDuration {
return TimeDuration{t: t}
}
// ParseTimeDuration returns a new TimeDuration parsing the RFC 3339 time or // ParseTimeDuration returns a new TimeDuration parsing the RFC 3339 time or
// time.Duration string. // time.Duration string.
func ParseTimeDuration(s string) (TimeDuration, error) { func ParseTimeDuration(s string) (TimeDuration, error) {
@ -55,10 +60,8 @@ func (t *TimeDuration) SetTime(tt time.Time) {
// MarshalJSON implements the json.Marshaler interface. If the time is set it // MarshalJSON implements the json.Marshaler interface. If the time is set it
// will return the time in RFC 3339 format if not it will return the duration // will return the time in RFC 3339 format if not it will return the duration
// string. // string.
func (t *TimeDuration) MarshalJSON() ([]byte, error) { func (t TimeDuration) MarshalJSON() ([]byte, error) {
switch { switch {
case t == nil:
return []byte("null"), nil
case t.t.IsZero(): case t.t.IsZero():
if t.d == 0 { if t.d == 0 {
return []byte("null"), nil return []byte("null"), nil
@ -111,7 +114,7 @@ func (t *TimeDuration) Time() time.Time {
t.t = now().Add(t.d) t.t = now().Add(t.d)
return t.t return t.t
default: default:
return t.t return t.t.UTC()
} }
} }

View file

@ -6,6 +6,28 @@ import (
"time" "time"
) )
func TestNewTimeDuration(t *testing.T) {
tm := time.Unix(1584198566, 535897000).UTC()
type args struct {
t time.Time
}
tests := []struct {
name string
args args
want TimeDuration
}{
{"ok", args{tm}, TimeDuration{t: tm}},
{"zero", args{time.Time{}}, TimeDuration{}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewTimeDuration(tt.args.t); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewTimeDuration() = %v, want %v", got, tt.want)
}
})
}
}
func TestParseTimeDuration(t *testing.T) { func TestParseTimeDuration(t *testing.T) {
type args struct { type args struct {
s string s string
@ -111,15 +133,14 @@ func TestTimeDuration_MarshalJSON(t *testing.T) {
tm := time.Unix(1584198566, 535897000).UTC() tm := time.Unix(1584198566, 535897000).UTC()
tests := []struct { tests := []struct {
name string name string
timeDuration *TimeDuration timeDuration TimeDuration
want []byte want []byte
wantErr bool wantErr bool
}{ }{
{"null", nil, []byte("null"), false}, {"null", TimeDuration{}, []byte("null"), false},
{"null", &TimeDuration{}, []byte("null"), false}, {"timestamp", TimeDuration{t: tm}, []byte(`"2020-03-14T15:09:26.535897Z"`), false},
{"timestamp", &TimeDuration{t: tm}, []byte(`"2020-03-14T15:09:26.535897Z"`), false}, {"duration", TimeDuration{d: 1 * time.Hour}, []byte(`"1h0m0s"`), false},
{"duration", &TimeDuration{d: 1 * time.Hour}, []byte(`"1h0m0s"`), false}, {"fail", TimeDuration{t: time.Date(-1, 0, 0, 0, 0, 0, 0, time.UTC)}, nil, true},
{"fail", &TimeDuration{t: time.Date(-1, 0, 0, 0, 0, 0, 0, time.UTC)}, nil, true},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -182,6 +203,7 @@ func TestTimeDuration_Time(t *testing.T) {
{"zero", nil, time.Time{}}, {"zero", nil, time.Time{}},
{"zero", &TimeDuration{}, time.Time{}}, {"zero", &TimeDuration{}, time.Time{}},
{"timestamp", &TimeDuration{t: tm}, tm}, {"timestamp", &TimeDuration{t: tm}, tm},
{"local", &TimeDuration{t: tm.Local()}, tm},
{"duration", &TimeDuration{d: 1 * time.Hour}, tm.Add(1 * time.Hour)}, {"duration", &TimeDuration{d: 1 * time.Hour}, tm.Add(1 * time.Hour)},
} }
for _, tt := range tests { for _, tt := range tests {