Skip to content

Commit

Permalink
fix: Instantiate duration encoder for EncDurationInTraffic field (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
scheca authored Apr 24, 2020
1 parent 800a710 commit 6abb271
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ func (dme *DistanceMatrixElement) MarshalJSON() ([]byte, error) {
x.safeDistanceMatrixElement = safeDistanceMatrixElement(*dme)

x.EncDuration = internal.NewDuration(dme.Duration)
x.EncDurationInTraffic = internal.NewDuration(dme.DurationInTraffic)

return json.Marshal(x)
}
Expand Down
21 changes: 21 additions & 0 deletions encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/json"
"reflect"
"testing"
"time"
)

const (
Expand Down Expand Up @@ -50,3 +51,23 @@ func TestSnappedPoint(t *testing.T) {
t.Errorf("expected equal snappedPoint, was %+v expected %+v", out, sp)
}
}

func TestDistanceMatrixElement_MarshalJSON(t *testing.T) {
dme := &DistanceMatrixElement{
Duration: 1*time.Second,
DurationInTraffic: 2*time.Second,
}
b, err := dme.MarshalJSON()
if err != nil {
t.Errorf("expected ok encode of DistanceMatrixElement, got: %v", err)
}

out := &DistanceMatrixElement{}
err = out.UnmarshalJSON(b)
if err != nil {
t.Errorf("expected ok decode of DistanceMatrixElement, got: %v", err)
}
if !reflect.DeepEqual(dme, out) {
t.Errorf("expected equal DistanceMatrixElement, was %+v expected %+v", out, dme)
}
}

0 comments on commit 6abb271

Please sign in to comment.