Skip to content

Commit

Permalink
Merge pull request #1165 from eliaperantoni/master
Browse files Browse the repository at this point in the history
Allocate struct pointers fields with "bind" tag
  • Loading branch information
stephenafamo committed Dec 14, 2022
2 parents 22497a8 + 4a0f774 commit 332721a
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion queries/reflect.go
Expand Up @@ -251,7 +251,7 @@ Rows:
case kindSliceStruct:
pointers = PtrsFromMapping(oneStruct, mapping)
case kindPtrSliceStruct:
newStruct = reflect.New(structType)
newStruct = makeStructPtr(structType)
pointers = PtrsFromMapping(reflect.Indirect(newStruct), mapping)
}
if err != nil {
Expand Down Expand Up @@ -279,6 +279,27 @@ Rows:
return nil
}

// makeStructPtr takes a struct type and returns a pointer to a new instance of it. This is used by bind to allocate new
// slice elements when the bound-to variable is []*Struct
func makeStructPtr(typ reflect.Type) reflect.Value {
// Allocate struct
val := reflect.New(typ)

// For all the fields
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
_, recurse := getBoilTag(field)

// If ",bind" was in the tag and the field is a pointer
if recurse && field.Type.Kind() == reflect.Ptr {
// Then allocate the field
val.Elem().Field(i).Set(reflect.New(field.Type.Elem()))
}
}

return val
}

// BindMapping creates a mapping that helps look up the pointer for the
// column given.
func BindMapping(typ reflect.Type, mapping map[string]uint64, cols []string) ([]uint64, error) {
Expand Down

0 comments on commit 332721a

Please sign in to comment.