From 2224db8e858d2e908dce32eec0cdd5a2d472f6c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Thu, 22 Dec 2022 15:59:12 +0100 Subject: [PATCH] v0.0.46 --- sq/structscanner.go | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) 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 {