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

Fix Structset to handle uuid ([16]byte) correctly #253

Merged
merged 4 commits into from Nov 8, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions structset_test.go
Expand Up @@ -217,3 +217,22 @@ func TestStructset_differentStructMissingField(t *testing.T) {
Apply(doc, NewStructset(&user, true))
})
}

func TestStructset_uuid(t *testing.T) {
// package like https://github.com/google/uuid use [16]byte to represent uuid
var (
uuid = [16]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
record = struct {
UUID [16]byte `db:",primary"`
}{UUID: uuid}
doc = NewDocument(&record)
mutation = Mutation{
Cascade: true,
Mutates: map[string]Mutate{
"uuid": Set("uuid", uuid),
},
}
)

assert.Equal(t, mutation, Apply(doc, NewStructset(&record, false)))
}
5 changes: 5 additions & 0 deletions util.go
Expand Up @@ -104,6 +104,11 @@ func isDeepZero(rv reflect.Value, depth int) bool {
c := rv.Complex()
return math.Float64bits(real(c)) == 0 && math.Float64bits(imag(c)) == 0
case reflect.Array:
// check one level deeper if it's an uuid ([16]byte)
if rv.Type().Elem().Kind() == reflect.Uint8 && rv.Len() == 16 {
depth += 1
}

for i := 0; i < rv.Len(); i++ {
if !isDeepZero(rv.Index(i), depth-1) {
return false
Expand Down