diff --git a/sq/structscanner.go b/sq/structscanner.go index d2307e5..b28808c 100644 --- a/sq/structscanner.go +++ b/sq/structscanner.go @@ -64,7 +64,7 @@ func (r *StructScanner) StructScanExt(dest any) error { v = v.Elem() - err := fieldsByTraversal(v, r.fields, r.values) + err := fieldsByTraversalExtended(v, r.fields, r.values) if err != nil { return err } @@ -159,7 +159,7 @@ func (r *StructScanner) StructScanBase(dest any) error { v = v.Elem() - err := fieldsByTraversal(v, r.fields, r.values) + err := fieldsByTraversalBase(v, r.fields, r.values, true) if err != nil { return err } @@ -172,7 +172,7 @@ func (r *StructScanner) StructScanBase(dest any) error { } // fieldsByTraversal forked from github.com/jmoiron/sqlx@v1.3.5/sqlx.go -func fieldsByTraversal(v reflect.Value, traversals [][]int, values []interface{}) error { +func fieldsByTraversalExtended(v reflect.Value, traversals [][]int, values []interface{}) error { v = reflect.Indirect(v) if v.Kind() != reflect.Struct { return errors.New("argument not a struct") @@ -190,6 +190,28 @@ func fieldsByTraversal(v reflect.Value, traversals [][]int, values []interface{} return nil } +// fieldsByTraversal forked from github.com/jmoiron/sqlx@v1.3.5/sqlx.go +func fieldsByTraversalBase(v reflect.Value, traversals [][]int, values []interface{}, ptrs bool) error { + v = reflect.Indirect(v) + if v.Kind() != reflect.Struct { + return errors.New("argument not a struct") + } + + for i, traversal := range traversals { + if len(traversal) == 0 { + values[i] = new(interface{}) + continue + } + f := reflectx.FieldByIndexes(v, traversal) + if ptrs { + values[i] = f.Addr().Interface() + } else { + values[i] = f.Interface() + } + } + return nil +} + // missingFields forked from github.com/jmoiron/sqlx@v1.3.5/sqlx.go func missingFields(transversals [][]int) (field int, err error) { for i, t := range transversals {