Skip to content

Commit

Permalink
implement MarshalBinary (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin authored Dec 30, 2024
2 parents 9edc4c1 + ccefb57 commit 31060b9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ func (c *Carbon) UnmarshalJSON(b []byte) error {
return c.Error
}

// MarshalBinary implements the interface encoding.BinaryMarshaler for Carbon struct.
// 实现 encoding.BinaryMarshaler 接口
func (c Carbon) MarshalBinary() ([]byte, error) {
return []byte(c.String()), nil
}

// UnmarshalBinary implements the interface encoding.BinaryUnmarshaler for Carbon struct
// 实现 encoding.BinaryUnmarshaler 接口
func (c *Carbon) UnmarshalBinary(b []byte) error {
src := bytes.Clone(b)
if len(src) == 0 {
return nil
}
*c = ParseByLayout(string(src), c.layout)
return c.Error
}

// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
func (t *DateTime) Scan(src interface{}) error {
switch v := src.(type) {
Expand Down
14 changes: 14 additions & 0 deletions database_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,20 @@ func BenchmarkNewTimestampNano_Value(b *testing.B) {
}
}

func BenchmarkCarbon_MarshalBinary(b *testing.B) {
now := Now()
for n := 0; n < b.N; n++ {
now.MarshalBinary()
}
}

func BenchmarkCarbon_UnmarshalBinary(b *testing.B) {
now := Now()
for n := 0; n < b.N; n++ {
now.UnmarshalBinary(nil)
}
}

func BenchmarkCarbon_MarshalJSON(b *testing.B) {
now := Now()
for n := 0; n < b.N; n++ {
Expand Down

0 comments on commit 31060b9

Please sign in to comment.