Skip to content

Commit

Permalink
Fix hyperbunTableForType for pointer types (2) (#4)
Browse files Browse the repository at this point in the history
In addition to pointers, hyperbunTableForType can also be called
for slices, pointer to slices, slice of pointers etc. This fix handles
all those scenarios too
  • Loading branch information
hi-rai authored Jul 10, 2024
1 parent af996f7 commit f6d6d4e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 5 additions & 1 deletion hyperbun.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,12 @@ func annotate(err error, op string, kvs ...interface{}) error {
func hyperbunTableForType[T any]() string {
var t T
typ := reflect.TypeOf(t)
if typ.Kind() == reflect.Pointer {
kind := typ.Kind()

// This covers the case like *U, []U, *[]U, []*U etc
for kind == reflect.Pointer || kind == reflect.Slice {
typ = typ.Elem()
kind = typ.Kind()
}

for i := 0; i < typ.NumField(); i++ {
Expand Down
6 changes: 3 additions & 3 deletions hyperbun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ type testStruct struct {

func TestHyperbunTableForType(t *testing.T) {
assert.Equal(t, "test_struct", hyperbunTableForType[testStruct]())
}

func TestHyperbunTableForPtrType(t *testing.T) {
assert.Equal(t, "test_struct", hyperbunTableForType[*testStruct]())
assert.Equal(t, "test_struct", hyperbunTableForType[[]testStruct]())
assert.Equal(t, "test_struct", hyperbunTableForType[[]*testStruct]())
assert.Equal(t, "test_struct", hyperbunTableForType[*[]testStruct]())
}

func TestAnnotateEven(t *testing.T) {
Expand Down

0 comments on commit f6d6d4e

Please sign in to comment.