Browse Source

codec: fix canonical encoding with time.Time keys

Ugorji Nwoke 6 years ago
parent
commit
c1211ebcc5
2 changed files with 6 additions and 1 deletions
  1. 5 0
      codec/codec_test.go
  2. 1 1
      codec/encode.go

+ 5 - 0
codec/codec_test.go

@@ -1302,6 +1302,11 @@ func doTestMapEncodeForCanonical(t *testing.T, name string, h Handle) {
 				false: 0,
 				true:  1,
 			},
+			"c/t": map[time.Time]int64{
+				time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC): time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC).UnixNano(),
+				time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC): time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC).UnixNano(),
+				time.Date(2017, 1, 1, 0, 0, 0, 0, time.UTC): time.Date(2017, 1, 1, 0, 0, 0, 0, time.UTC).UnixNano(),
+			},
 		},
 	}
 	var v2 map[stringUint64T]interface{}

+ 1 - 1
codec/encode.go

@@ -702,7 +702,7 @@ func (e *Encoder) kMapCanonical(rtkey, rtval reflect.Type, rv reflect.Value, val
 			e.encodeValue(mapGet(rv, mksv[i].r, rvv), valFn) // e.encodeValue(rv.MapIndex(mksv[i].r), valFn)
 		}
 	case reflect.Struct:
-		if rv.Type() == timeTyp {
+		if rtkey == timeTyp {
 			mksv := make([]timeRv, len(mks))
 			for i, k := range mks {
 				v := &mksv[i]