Skip to content

Commit

Permalink
fixed json date decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
bzimmer committed Oct 19, 2021
1 parent 8dfc7da commit f0528e8
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cyclinganalytics/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (d *Datetime) UnmarshalJSON(b []byte) (err error) {
return
}

func (d *Datetime) MarshalJSON() ([]byte, error) {
func (d Datetime) MarshalJSON() ([]byte, error) {
return []byte(d.Time.Format(datetimeFormat)), nil
}

Expand Down
40 changes: 40 additions & 0 deletions cyclinganalytics/model_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cyclinganalytics_test

import (
"bytes"
"encoding/json"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/bzimmer/activity/cyclinganalytics"
)

func TestModel(t *testing.T) {
t.Parallel()
a := assert.New(t)

tests := []struct {
name string
}{
{
name: "success",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
src := cyclinganalytics.Ride{
UTCDatetime: cyclinganalytics.Datetime{Time: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)},
}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
a.NoError(enc.Encode(src))
var dst cyclinganalytics.Ride
dec := json.NewDecoder(&buf)
a.NoError(dec.Decode(&dst))
a.Equal(src.UTCDatetime, dst.UTCDatetime)
})
}
}
5 changes: 1 addition & 4 deletions zwift/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (d *Datetime) UnmarshalJSON(b []byte) (err error) {
return
}

func (d *Datetime) MarshalJSON() ([]byte, error) {
func (d Datetime) MarshalJSON() ([]byte, error) {
return []byte(d.Time.Format(datetimeFormat)), nil
}

Expand Down Expand Up @@ -134,8 +134,6 @@ type Profile struct {
Affiliate string `json:"affiliate"`
AvantlinkID string `json:"avantlinkId"`
FundraiserID string `json:"fundraiserId"`
// PublicAttributes interface{} `json:"publicAttributes"`
// PrivateAttributes interface{} `json:"privateAttributes"`
}

type Activity struct {
Expand Down Expand Up @@ -165,5 +163,4 @@ type Activity struct {
PrimaryImageURL string `json:"primaryImageUrl"`
MovingTimeInMillis int `json:"movingTimeInMs"`
Privacy string `json:"privacy"`
// SnapshotList interface{} `json:"snapshotList"`
}
40 changes: 40 additions & 0 deletions zwift/model_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package zwift_test

import (
"bytes"
"encoding/json"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/bzimmer/activity/zwift"
)

func TestModel(t *testing.T) {
t.Parallel()
a := assert.New(t)

tests := []struct {
name string
}{
{
name: "success",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
src := zwift.Activity{
StartDate: zwift.Datetime{Time: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)},
}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
a.NoError(enc.Encode(src))
var dst zwift.Activity
dec := json.NewDecoder(&buf)
a.NoError(dec.Decode(&dst))
a.Equal(src.StartDate, dst.StartDate)
})
}
}

0 comments on commit f0528e8

Please sign in to comment.