Skip to content

Commit

Permalink
add decode nil to time.Time support.
Browse files Browse the repository at this point in the history
  • Loading branch information
andot committed May 27, 2022
1 parent 2cf37f8 commit 6499bf9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 2 additions & 2 deletions io/time_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
| |
| io/time_decoder.go |
| |
| LastModified: Feb 20, 2022 |
| LastModified: May 27, 2022 |
| Author: Ma Bingyao <[email protected]> |
| |
\*________________________________________________________*/
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 14 additions & 1 deletion io/time_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
| |
| io/time_decoder_test.go |
| |
| LastModified: Jun 05, 2021 |
| LastModified: May 27, 2022 |
| Author: Ma Bingyao <[email protected]> |
| |
\*________________________________________________________*/
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)))
}

0 comments on commit 6499bf9

Please sign in to comment.