Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement MarshalBinary #274

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading