From 512c9a9da064dba401cb2df94c378ab8275e44a4 Mon Sep 17 00:00:00 2001 From: dudaping Date: Thu, 3 Nov 2022 23:14:29 +0800 Subject: [PATCH] fix unmarshal error --- json.go | 22 +++++++++++----------- json_test.go | 12 +++--------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/json.go b/json.go index 44b59b67..b282dad9 100644 --- a/json.go +++ b/json.go @@ -113,7 +113,7 @@ func (t *DateTimeMilli) UnmarshalJSON(b []byte) error { if c.Error == nil { *t = DateTimeMilli{c} } - return nil + return c.Error } // String implements the interface Stringer for DateTimeMilli struct. @@ -135,7 +135,7 @@ func (t *DateTimeMicro) UnmarshalJSON(b []byte) error { if c.Error == nil { *t = DateTimeMicro{c} } - return nil + return c.Error } // String implements the interface Stringer for DateTimeMicro struct. @@ -157,7 +157,7 @@ func (t *DateTimeNano) UnmarshalJSON(b []byte) error { if c.Error == nil { *t = DateTimeNano{c} } - return nil + return c.Error } // String implements the interface Stringer for DateTimeNano struct. @@ -179,7 +179,7 @@ func (t *Date) UnmarshalJSON(b []byte) error { if c.Error == nil { *t = Date{c} } - return nil + return c.Error } // String implements the interface Stringer for Date struct. @@ -201,7 +201,7 @@ func (t *DateMilli) UnmarshalJSON(b []byte) error { if c.Error == nil { *t = DateMilli{c} } - return nil + return c.Error } // String implements the interface Stringer for DateMilli struct. @@ -223,7 +223,7 @@ func (t *DateMicro) UnmarshalJSON(b []byte) error { if c.Error == nil { *t = DateMicro{c} } - return nil + return c.Error } // String implements the interface Stringer for DateMicro struct. @@ -245,7 +245,7 @@ func (t *DateNano) UnmarshalJSON(b []byte) error { if c.Error == nil { *t = DateNano{c} } - return nil + return c.Error } // String implements the interface Stringer for DateNano struct. @@ -268,7 +268,7 @@ func (t *Timestamp) UnmarshalJSON(b []byte) error { if c.Error == nil { *t = Timestamp{c} } - return nil + return c.Error } // String implements the interface Stringer for Timestamp struct. @@ -291,7 +291,7 @@ func (t *TimestampMilli) UnmarshalJSON(b []byte) error { if c.Error == nil { *t = TimestampMilli{c} } - return nil + return c.Error } // String implements the interface Stringer for TimestampMilli struct. @@ -314,7 +314,7 @@ func (t *TimestampMicro) UnmarshalJSON(b []byte) error { if c.Error == nil { *t = TimestampMicro{c} } - return nil + return c.Error } // String implements the interface Stringer for TimestampMicro struct. @@ -337,7 +337,7 @@ func (t *TimestampNano) UnmarshalJSON(b []byte) error { if c.Error == nil { *t = TimestampNano{c} } - return nil + return c.Error } // String implements the interface Stringer for TimestampNano struct. diff --git a/json_test.go b/json_test.go index e0ec0502..149645b5 100644 --- a/json_test.go +++ b/json_test.go @@ -45,9 +45,7 @@ func TestCarbon_MarshalJSON(t *testing.T) { CreatedAt4: TimestampNano{Parse("2025-08-05 13:14:15.999999999")}, } data, err := json.Marshal(&person) - if err != nil { - assert.NotNil(t, err) - } + assert.Nil(t, err) fmt.Printf("Person output by json:\n%s\n", data) } @@ -70,9 +68,7 @@ func TestCarbon_UnmarshalJSON(t *testing.T) { }` err := json.Unmarshal([]byte(str), &person) - if err != nil { - assert.NotNil(t, err) - } + assert.Nil(t, err) fmt.Printf("Json string parse to person:\n%+v\n", person) } @@ -94,8 +90,6 @@ func TestError_Json(t *testing.T) { "created_at4": 0 }` err := json.Unmarshal([]byte(str), &person) - if err != nil { - assert.NotNil(t, err) - } + assert.NotNil(t, err) fmt.Printf("Json string parse to person:\n%+v\n", person) }