Skip to content

Commit

Permalink
fix timeuuid makes zeroUUID marshalling into zero bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-li committed Jan 19, 2025
1 parent 9bd5024 commit e9e6c0f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
6 changes: 6 additions & 0 deletions serialization/timeuuid/marshal_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ func encReflectBytes(rv reflect.Value) ([]byte, error) {
// The following code was taken from the `Parse` function of the "github.com/google/uuid" package.
func encReflectString(v reflect.Value) ([]byte, error) {
s := v.String()
if s == zeroUUID {
return make([]byte, 0), nil
}
switch len(s) {
case 45: // urn:timeuuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
if !strings.EqualFold(s[:9], "urn:timeuuid:") {
Expand Down Expand Up @@ -165,6 +168,9 @@ func encReflectString(v reflect.Value) ([]byte, error) {
// encString encodes uuid strings.
// The following code was taken from the `Parse` function of the "github.com/google/uuid" package.
func encString(s string) ([]byte, error) {
if s == zeroUUID {
return make([]byte, 0), nil
}
switch len(s) {
case 45: // urn:timeuuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
if !strings.EqualFold(s[:9], "urn:timeuuid:") {
Expand Down
13 changes: 8 additions & 5 deletions serialization/timeuuid/unmarshal_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"time"
)

const hexString = "0123456789abcdef"
const (
hexString = "0123456789abcdef"
zeroUUID = "00000000-0000-0000-0000-000000000000"
)

var (
offsets = [...]int{0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34}
Expand Down Expand Up @@ -104,7 +107,7 @@ func DecString(p []byte, v *string) error {
if p == nil {
*v = ""
} else {
*v = "00000000-0000-0000-0000-000000000000"
*v = zeroUUID
}
case 16:
*v = decString(p)
Expand All @@ -123,7 +126,7 @@ func DecStringR(p []byte, v **string) error {
if p == nil {
*v = nil
} else {
tmp := "00000000-0000-0000-0000-000000000000"
tmp := zeroUUID
*v = &tmp
}
case 16:
Expand Down Expand Up @@ -255,7 +258,7 @@ func decReflectString(p []byte, v reflect.Value) error {
if p == nil {
v.SetString("")
} else {
v.SetString("00000000-0000-0000-0000-000000000000")
v.SetString(zeroUUID)
}
case 16:
v.SetString(decString(p))
Expand Down Expand Up @@ -313,7 +316,7 @@ func decReflectStringR(p []byte, v reflect.Value) error {
v.Set(reflect.Zero(v.Type()))
} else {
val := reflect.New(v.Type().Elem())
val.Elem().SetString("00000000-0000-0000-0000-000000000000")
val.Elem().SetString(zeroUUID)
v.Set(val)
}
case 16:
Expand Down

0 comments on commit e9e6c0f

Please sign in to comment.