From 6499bf9fc634663929d9fc8b8ca924bc4bd44782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E9=A9=AC=E5=93=A5?= Date: Fri, 27 May 2022 21:46:37 +0800 Subject: [PATCH] add decode nil to time.Time support. --- io/time_decoder.go | 4 ++-- io/time_decoder_test.go | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/io/time_decoder.go b/io/time_decoder.go index ef0af67..d1b796a 100644 --- a/io/time_decoder.go +++ b/io/time_decoder.go @@ -6,7 +6,7 @@ | | | io/time_decoder.go | | | -| LastModified: Feb 20, 2022 | +| LastModified: May 27, 2022 | | Author: Ma Bingyao | | | \*________________________________________________________*/ @@ -159,7 +159,7 @@ func (dec *Decoder) decodeTime(t reflect.Type, tag byte, p *time.Time) { return } switch tag { - case TagEmpty, TagFalse: + case TagNull, TagEmpty, TagFalse: *p = time.Unix(0, 0) case TagTrue: *p = time.Unix(0, 1) diff --git a/io/time_decoder_test.go b/io/time_decoder_test.go index 6d894dd..ce63bb7 100644 --- a/io/time_decoder_test.go +++ b/io/time_decoder_test.go @@ -6,7 +6,7 @@ | | | io/time_decoder_test.go | | | -| LastModified: Jun 05, 2021 | +| LastModified: May 27, 2022 | | Author: Ma Bingyao | | | \*________________________________________________________*/ @@ -34,12 +34,16 @@ func TestDecodeTime(t *testing.T) { assert.NoError(t, enc.Encode(t1)) assert.NoError(t, enc.Encode(&t1)) assert.NoError(t, enc.Encode(t1)) + assert.NoError(t, enc.Encode(nil)) + assert.NoError(t, enc.Encode(nil)) t2 := Time(t1) assert.NoError(t, enc.Encode(&t2)) assert.NoError(t, enc.Encode(t2)) assert.NoError(t, enc.Encode(&t2)) assert.NoError(t, enc.Encode(t2)) + assert.NoError(t, enc.Encode(nil)) + assert.NoError(t, enc.Encode(nil)) dec := NewDecoder(([]byte)(sb.String())).Simple(false) var t3 *time.Time @@ -54,6 +58,11 @@ func TestDecodeTime(t *testing.T) { assert.Equal(t, *t3, t1) dec.Decode(&t4) assert.Equal(t, t4, t1) + dec.Decode(&t3) + assert.Nil(t, t3) + dec.Decode(&t4) + assert.Equal(t, t4, time.Unix(0, 0)) + dec.Decode(&t5) assert.Equal(t, *t5, t2) dec.Decode(&t6) @@ -62,4 +71,8 @@ func TestDecodeTime(t *testing.T) { assert.Equal(t, *t5, t2) dec.Decode(&t6) assert.Equal(t, t6, t2) + dec.Decode(&t5) + assert.Nil(t, t5) + dec.Decode(&t6) + assert.Equal(t, t6, Time(time.Unix(0, 0))) }