Skip to content

Commit

Permalink
Add ToXXXStruct series methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin committed Sep 11, 2023
1 parent bfbf3b6 commit 3f07ac4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (t DateTimeMilli) MarshalJSON() ([]byte, error) {
func (t *DateTimeMilli) UnmarshalJSON(b []byte) error {
c := ParseByLayout(string(bytes.Trim(b, `"`)), DateTimeMilliLayout, t.Location())
if c.Error == nil {
*t = DateTimeMilli{c}
*t = DateTimeMilli{Carbon: c}
}
return c.Error
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func (t DateTimeMicro) MarshalJSON() ([]byte, error) {
func (t *DateTimeMicro) UnmarshalJSON(b []byte) error {
c := ParseByLayout(string(bytes.Trim(b, `"`)), DateTimeMicroLayout, t.Location())
if c.Error == nil {
*t = DateTimeMicro{c}
*t = DateTimeMicro{Carbon: c}
}
return c.Error
}
Expand Down
10 changes: 6 additions & 4 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestCarbon_MarshalJSON(t *testing.T) {
}
data, err := json.Marshal(&person)
assert.Nil(t, err)

assert.Equal(t, "2002-08-05 13:14:15", person.Birthday1.String(), "birthday1 should be \"2005-09-11 09:57:38\"")
assert.Equal(t, "2002-08-05 13:14:15.999", person.Birthday2.String(), "birthday2 should be \"2002-08-05 13:14:15.999\"")
assert.Equal(t, "2002-08-05 13:14:15.999999", person.Birthday3.String(), "birthday3 should be \"2002-08-05 13:14:15.999999\"")
Expand All @@ -58,12 +59,12 @@ func TestCarbon_MarshalJSON(t *testing.T) {

assert.Equal(t, "1691212455", person.CreatedAt1.String(), "created_at1 should be \"1691212455\"")
assert.Equal(t, "1722834855999", person.CreatedAt2.String(), "created_at2 should be \"1722834855999\"")
assert.Equal(t, "1754370855999999", person.CreatedAt3.String(), "created_at2 should be `\"1754370855999999\"")
assert.Equal(t, "1754370855999999999", person.CreatedAt4.String(), "created_at2 should be \"1754370855999999999\"")
assert.Equal(t, "1754370855999999", person.CreatedAt3.String(), "created_at3 should be `\"1754370855999999\"")
assert.Equal(t, "1754370855999999999", person.CreatedAt4.String(), "created_at4 should be \"1754370855999999999\"")
assert.Equal(t, int64(1691212455), person.CreatedAt1.Int64(), "created_at1 should be 1691212455")
assert.Equal(t, int64(1722834855999), person.CreatedAt2.Int64(), "created_at2 should be 1722834855999")
assert.Equal(t, int64(1754370855999999), person.CreatedAt3.Int64(), "created_at2 should be 1754370855999999")
assert.Equal(t, int64(1754370855999999999), person.CreatedAt4.Int64(), "created_at2 should be 1754370855999999999")
assert.Equal(t, int64(1754370855999999), person.CreatedAt3.Int64(), "created_at3 should be 1754370855999999")
assert.Equal(t, int64(1754370855999999999), person.CreatedAt4.Int64(), "created_at4 should be 1754370855999999999")

fmt.Printf("Person output by json:\n%s\n", data)
}
Expand All @@ -88,6 +89,7 @@ func TestCarbon_UnmarshalJSON(t *testing.T) {

err := json.Unmarshal([]byte(str), &person)
assert.Nil(t, err)

assert.Equal(t, "2002-08-05 13:14:15", person.Birthday1.String(), "birthday1 should be \"2002-08-05 13:14:15\"")
assert.Equal(t, "2002-08-05 13:14:15.999", person.Birthday2.String(), "birthday2 should be \"2002-08-05 13:14:15.999\"")
assert.Equal(t, "2002-08-05 13:14:15.999999", person.Birthday3.String(), "birthday3 should be \"2002-08-05 13:14:15.999999\"")
Expand Down

0 comments on commit 3f07ac4

Please sign in to comment.