From 6abb271f65d974dd583b4386c9891f9dc2ffd8b4 Mon Sep 17 00:00:00 2001 From: Sergio Checa Blanco Date: Fri, 24 Apr 2020 16:09:07 +0200 Subject: [PATCH] fix: Instantiate duration encoder for EncDurationInTraffic field (#230) --- encoding.go | 1 + encoding_test.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/encoding.go b/encoding.go index e44aa0d..fb14cab 100644 --- a/encoding.go +++ b/encoding.go @@ -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) } diff --git a/encoding_test.go b/encoding_test.go index ef9845f..e7af237 100644 --- a/encoding_test.go +++ b/encoding_test.go @@ -18,6 +18,7 @@ import ( "encoding/json" "reflect" "testing" + "time" ) const ( @@ -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) + } +}