From 75db02dde15f8ae29c260da6ede00d74f2b56819 Mon Sep 17 00:00:00 2001 From: Fs02 Date: Mon, 8 Nov 2021 14:52:22 +0900 Subject: [PATCH 1/4] Fix structset to handle uuid ([16]byte) correctly --- structset_test.go | 19 +++++++++++++++++++ util.go | 5 +++++ 2 files changed, 24 insertions(+) diff --git a/structset_test.go b/structset_test.go index 35723397..1e381b4c 100644 --- a/structset_test.go +++ b/structset_test.go @@ -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))) +} diff --git a/util.go b/util.go index c91eb583..3e9f5d50 100644 --- a/util.go +++ b/util.go @@ -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 (ex: [16]byte) + if depth == 0 && rv.Type().Elem().Kind() == reflect.Uint8 { + depth = 1 + } + for i := 0; i < rv.Len(); i++ { if !isDeepZero(rv.Index(i), depth-1) { return false From 66ed77a54be4c8e21d5ef35825fe355655925bed Mon Sep 17 00:00:00 2001 From: Fs02 Date: Mon, 8 Nov 2021 14:56:40 +0900 Subject: [PATCH 2/4] only check for [16]byte --- util.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/util.go b/util.go index 3e9f5d50..283f3d7a 100644 --- a/util.go +++ b/util.go @@ -104,8 +104,8 @@ 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 (ex: [16]byte) - if depth == 0 && rv.Type().Elem().Kind() == reflect.Uint8 { + // check one level deeper if it's an uuid ([16]byte) + if depth == 0 && rv.Type().Elem().Kind() == reflect.Uint8 && rv.Len() == 16 { depth = 1 } From 48f0ec2eba6f7d6bfe72d0de5af257d6d20d375c Mon Sep 17 00:00:00 2001 From: Fs02 Date: Mon, 8 Nov 2021 14:59:07 +0900 Subject: [PATCH 3/4] don't rewrite depth --- util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util.go b/util.go index 283f3d7a..959675fe 100644 --- a/util.go +++ b/util.go @@ -106,7 +106,7 @@ func isDeepZero(rv reflect.Value, depth int) bool { case reflect.Array: // check one level deeper if it's an uuid ([16]byte) if depth == 0 && rv.Type().Elem().Kind() == reflect.Uint8 && rv.Len() == 16 { - depth = 1 + depth += 1 } for i := 0; i < rv.Len(); i++ { From 8a81031053a0a44d9a550dc896d7207f7e73d200 Mon Sep 17 00:00:00 2001 From: Fs02 Date: Mon, 8 Nov 2021 14:59:42 +0900 Subject: [PATCH 4/4] fix --- util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util.go b/util.go index 959675fe..2f1f9711 100644 --- a/util.go +++ b/util.go @@ -105,7 +105,7 @@ func isDeepZero(rv reflect.Value, depth int) bool { 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 depth == 0 && rv.Type().Elem().Kind() == reflect.Uint8 && rv.Len() == 16 { + if rv.Type().Elem().Kind() == reflect.Uint8 && rv.Len() == 16 { depth += 1 }