This commit is contained in:
Mike Schwörer 2022-12-22 15:59:12 +01:00
parent c60afc89bb
commit 2224db8e85
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF

View File

@ -64,7 +64,7 @@ func (r *StructScanner) StructScanExt(dest any) error {
v = v.Elem() v = v.Elem()
err := fieldsByTraversal(v, r.fields, r.values) err := fieldsByTraversalExtended(v, r.fields, r.values)
if err != nil { if err != nil {
return err return err
} }
@ -159,7 +159,7 @@ func (r *StructScanner) StructScanBase(dest any) error {
v = v.Elem() v = v.Elem()
err := fieldsByTraversal(v, r.fields, r.values) err := fieldsByTraversalBase(v, r.fields, r.values, true)
if err != nil { if err != nil {
return err 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 // 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) v = reflect.Indirect(v)
if v.Kind() != reflect.Struct { if v.Kind() != reflect.Struct {
return errors.New("argument not a struct") return errors.New("argument not a struct")
@ -190,6 +190,28 @@ func fieldsByTraversal(v reflect.Value, traversals [][]int, values []interface{}
return nil 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 // missingFields forked from github.com/jmoiron/sqlx@v1.3.5/sqlx.go
func missingFields(transversals [][]int) (field int, err error) { func missingFields(transversals [][]int) (field int, err error) {
for i, t := range transversals { for i, t := range transversals {