Skip to content

Commit ee4e3e2

Browse files
committed
Update reflectx to allow for optional nested structs
1 parent 28212d4 commit ee4e3e2

3 files changed

Lines changed: 303 additions & 5 deletions

File tree

reflectx/reflect.go

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
package reflectx
88

99
import (
10+
"database/sql"
11+
"fmt"
1012
"reflect"
1113
"runtime"
14+
"strconv"
1215
"strings"
1316
"sync"
1417
)
@@ -201,6 +204,191 @@ func (m *Mapper) TraversalsByNameFunc(t reflect.Type, names []string, fn func(in
201204
return nil
202205
}
203206

207+
// ObjectContext provides a single layer to abstract away
208+
// nested struct scanning functionality
209+
type ObjectContext struct {
210+
value reflect.Value
211+
}
212+
213+
func NewObjectContext() *ObjectContext {
214+
return &ObjectContext{}
215+
}
216+
217+
// NewRow updates the object reference.
218+
// This ensures all columns point to the same object
219+
func (o *ObjectContext) NewRow(value reflect.Value) {
220+
o.value = value
221+
}
222+
223+
// FieldForIndexes returns the value for address. If the address is a nested struct,
224+
// a nestedFieldScanner is returned instead of the standard value reference
225+
func (o *ObjectContext) FieldForIndexes(indexes []int) reflect.Value {
226+
if len(indexes) == 1 {
227+
val := FieldByIndexes(o.value, indexes)
228+
return val
229+
}
230+
231+
obj := &nestedFieldScanner{
232+
parent: o,
233+
indexes: indexes,
234+
}
235+
236+
v := reflect.ValueOf(obj).Elem()
237+
return v
238+
}
239+
240+
// nestedFieldScanner will only forward the Scan to the nested value if
241+
// the database value is not nil.
242+
type nestedFieldScanner struct {
243+
parent *ObjectContext
244+
indexes []int
245+
}
246+
247+
// Scan implements sql.Scanner.
248+
// This method largely mirrors the sql.convertAssign() method with some minor changes
249+
func (o *nestedFieldScanner) Scan(src interface{}) error {
250+
if src == nil {
251+
return nil
252+
}
253+
254+
dv := FieldByIndexes(o.parent.value, o.indexes)
255+
// Dereference pointer fields to avoid double pointers **T
256+
if dv.Kind() == reflect.Pointer {
257+
dv.Set(reflect.New(dv.Type().Elem()))
258+
dv = dv.Elem()
259+
}
260+
iface := dv.Addr().Interface()
261+
262+
if scan, ok := iface.(sql.Scanner); ok {
263+
return scan.Scan(src)
264+
}
265+
266+
sv := reflect.ValueOf(src)
267+
268+
// below is taken from https://cs.opensource.google/go/go/+/refs/tags/go1.19.5:src/database/sql/convert.go
269+
// with a few minor edits
270+
271+
if sv.IsValid() && sv.Type().AssignableTo(dv.Type()) {
272+
switch b := src.(type) {
273+
case []byte:
274+
dv.Set(reflect.ValueOf(bytesClone(b)))
275+
default:
276+
dv.Set(sv)
277+
}
278+
279+
return nil
280+
}
281+
282+
if dv.Kind() == sv.Kind() && sv.Type().ConvertibleTo(dv.Type()) {
283+
dv.Set(sv.Convert(dv.Type()))
284+
return nil
285+
}
286+
287+
// The following conversions use a string value as an intermediate representation
288+
// to convert between various numeric types.
289+
//
290+
// This also allows scanning into user defined types such as "type Int int64".
291+
// For symmetry, also check for string destination types.
292+
switch dv.Kind() {
293+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
294+
if src == nil {
295+
return fmt.Errorf("converting NULL to %s is unsupported", dv.Kind())
296+
}
297+
s := asString(src)
298+
i64, err := strconv.ParseInt(s, 10, dv.Type().Bits())
299+
if err != nil {
300+
err = strconvErr(err)
301+
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
302+
}
303+
dv.SetInt(i64)
304+
return nil
305+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
306+
if src == nil {
307+
return fmt.Errorf("converting NULL to %s is unsupported", dv.Kind())
308+
}
309+
s := asString(src)
310+
u64, err := strconv.ParseUint(s, 10, dv.Type().Bits())
311+
if err != nil {
312+
err = strconvErr(err)
313+
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
314+
}
315+
dv.SetUint(u64)
316+
return nil
317+
case reflect.Float32, reflect.Float64:
318+
if src == nil {
319+
return fmt.Errorf("converting NULL to %s is unsupported", dv.Kind())
320+
}
321+
s := asString(src)
322+
f64, err := strconv.ParseFloat(s, dv.Type().Bits())
323+
if err != nil {
324+
err = strconvErr(err)
325+
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
326+
}
327+
dv.SetFloat(f64)
328+
return nil
329+
case reflect.String:
330+
if src == nil {
331+
return fmt.Errorf("converting NULL to %s is unsupported", dv.Kind())
332+
}
333+
switch v := src.(type) {
334+
case string:
335+
dv.SetString(v)
336+
return nil
337+
case []byte:
338+
dv.SetString(string(v))
339+
return nil
340+
}
341+
}
342+
343+
return fmt.Errorf("don't know how to parse type %T -> %T", src, iface)
344+
}
345+
346+
// returns internal conversion error if available
347+
// taken from https://cs.opensource.google/go/go/+/refs/tags/go1.19.5:src/database/sql/convert.go
348+
func strconvErr(err error) error {
349+
if ne, ok := err.(*strconv.NumError); ok {
350+
return ne.Err
351+
}
352+
return err
353+
}
354+
355+
// converts value to it's string value
356+
// taken from https://cs.opensource.google/go/go/+/refs/tags/go1.19.5:src/database/sql/convert.go
357+
func asString(src interface{}) string {
358+
switch v := src.(type) {
359+
case string:
360+
return v
361+
case []byte:
362+
return string(v)
363+
}
364+
rv := reflect.ValueOf(src)
365+
switch rv.Kind() {
366+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
367+
return strconv.FormatInt(rv.Int(), 10)
368+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
369+
return strconv.FormatUint(rv.Uint(), 10)
370+
case reflect.Float64:
371+
return strconv.FormatFloat(rv.Float(), 'g', -1, 64)
372+
case reflect.Float32:
373+
return strconv.FormatFloat(rv.Float(), 'g', -1, 32)
374+
case reflect.Bool:
375+
return strconv.FormatBool(rv.Bool())
376+
}
377+
return fmt.Sprintf("%v", src)
378+
}
379+
380+
// bytesClone returns a copy of b[:len(b)].
381+
// The result may have additional unused capacity.
382+
// Clone(nil) returns nil.
383+
//
384+
// bytesClone is a mirror of bytes.Clone while our go.mod is on an older version
385+
func bytesClone(b []byte) []byte {
386+
if b == nil {
387+
return nil
388+
}
389+
return append([]byte{}, b...)
390+
}
391+
204392
// FieldByIndexes returns a value for the field given by the struct traversal
205393
// for the given value.
206394
func FieldByIndexes(v reflect.Value, indexes []int) reflect.Value {

sqlx.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,8 @@ func (r *Rows) StructScan(dest interface{}) error {
621621
r.started = true
622622
}
623623

624-
err := fieldsByTraversal(v, r.fields, r.values, true)
624+
octx := reflectx.NewObjectContext()
625+
err := fieldsByTraversal(octx, v, r.fields, r.values, true)
625626
if err != nil {
626627
return err
627628
}
@@ -781,7 +782,9 @@ func (r *Row) scanAny(dest interface{}, structOnly bool) error {
781782
}
782783
values := make([]interface{}, len(columns))
783784

784-
err = fieldsByTraversal(v, fields, values, true)
785+
octx := reflectx.NewObjectContext()
786+
787+
err = fieldsByTraversal(octx, v, fields, values, true)
785788
if err != nil {
786789
return err
787790
}
@@ -948,13 +951,14 @@ func scanAll(rows rowsi, dest interface{}, structOnly bool) error {
948951
return fmt.Errorf("missing destination name %s in %T", columns[f], dest)
949952
}
950953
values = make([]interface{}, len(columns))
954+
octx := reflectx.NewObjectContext()
951955

952956
for rows.Next() {
953957
// create a new struct type (which returns PtrTo) and indirect it
954958
vp = reflect.New(base)
955959
v = reflect.Indirect(vp)
956960

957-
err = fieldsByTraversal(v, fields, values, true)
961+
err = fieldsByTraversal(octx, v, fields, values, true)
958962
if err != nil {
959963
return err
960964
}
@@ -1020,18 +1024,20 @@ func baseType(t reflect.Type, expected reflect.Kind) (reflect.Type, error) {
10201024
// when iterating over many rows. Empty traversals will get an interface pointer.
10211025
// Because of the necessity of requesting ptrs or values, it's considered a bit too
10221026
// specialized for inclusion in reflectx itself.
1023-
func fieldsByTraversal(v reflect.Value, traversals [][]int, values []interface{}, ptrs bool) error {
1027+
func fieldsByTraversal(octx *reflectx.ObjectContext, v reflect.Value, traversals [][]int, values []interface{}, ptrs bool) error {
10241028
v = reflect.Indirect(v)
10251029
if v.Kind() != reflect.Struct {
10261030
return errors.New("argument not a struct")
10271031
}
10281032

1033+
octx.NewRow(v)
1034+
10291035
for i, traversal := range traversals {
10301036
if len(traversal) == 0 {
10311037
values[i] = new(interface{})
10321038
continue
10331039
}
1034-
f := reflectx.FieldByIndexes(v, traversal)
1040+
f := octx.FieldForIndexes(traversal)
10351041
if ptrs {
10361042
values[i] = f.Addr().Interface()
10371043
} else {

sqlx_context_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,110 @@ func TestNamedQueryContext(t *testing.T) {
642642
t.Errorf("Expected place name of %v, got %v", pp.Place.ID, pp2.Place.ID)
643643
}
644644
}
645+
646+
rows.Close()
647+
648+
type Owner struct {
649+
Email *string `db:"email"`
650+
FirstName string `db:"first_name"`
651+
LastName string `db:"last_name"`
652+
}
653+
654+
// Test optional nested structs with left join
655+
type PlaceOwner struct {
656+
Place Place `db:"place"`
657+
Owner *Owner `db:"owner"`
658+
}
659+
660+
pl = Place{
661+
Name: sql.NullString{String: "the-house", Valid: true},
662+
}
663+
664+
q4 := `INSERT INTO place (id, name) VALUES (2, :name)`
665+
_, err = db.NamedExecContext(ctx, q4, pl)
666+
if err != nil {
667+
log.Fatal(err)
668+
}
669+
670+
id = 2
671+
pp.Place.ID = id
672+
673+
q5 := `INSERT INTO placeperson (first_name, last_name, email, place_id) VALUES (:first_name, :last_name, :email, :place.id)`
674+
_, err = db.NamedExecContext(ctx, q5, pp)
675+
if err != nil {
676+
log.Fatal(err)
677+
}
678+
679+
pp3 := &PlaceOwner{}
680+
rows, err = db.NamedQueryContext(ctx, `
681+
SELECT
682+
placeperson.first_name "owner.first_name",
683+
placeperson.last_name "owner.last_name",
684+
placeperson.email "owner.email",
685+
place.id AS "place.id",
686+
place.name AS "place.name"
687+
FROM place
688+
LEFT JOIN placeperson ON false -- null left join
689+
WHERE
690+
place.id=:place.id`, pp)
691+
if err != nil {
692+
log.Fatal(err)
693+
}
694+
for rows.Next() {
695+
err = rows.StructScan(pp3)
696+
if err != nil {
697+
t.Error(err)
698+
}
699+
if pp3.Owner != nil {
700+
t.Error("Expected `Owner`, to be nil")
701+
}
702+
if pp3.Place.Name.String != "the-house" {
703+
t.Error("Expected place name of `the-house`, got " + pp3.Place.Name.String)
704+
}
705+
if pp3.Place.ID != pp.Place.ID {
706+
t.Errorf("Expected place name of %v, got %v", pp.Place.ID, pp3.Place.ID)
707+
}
708+
}
709+
710+
rows.Close()
711+
712+
pp3 = &PlaceOwner{}
713+
rows, err = db.NamedQueryContext(ctx, `
714+
SELECT
715+
placeperson.first_name "owner.first_name",
716+
placeperson.last_name "owner.last_name",
717+
placeperson.email "owner.email",
718+
place.id AS "place.id",
719+
place.name AS "place.name"
720+
FROM place
721+
left JOIN placeperson ON placeperson.place_id = place.id
722+
WHERE
723+
place.id=:place.id`, pp)
724+
if err != nil {
725+
log.Fatal(err)
726+
}
727+
for rows.Next() {
728+
err = rows.StructScan(pp3)
729+
if err != nil {
730+
t.Error(err)
731+
}
732+
if pp3.Owner == nil {
733+
t.Error("Expected `Owner`, to not be nil")
734+
}
735+
736+
if pp3.Owner.FirstName != "ben" {
737+
t.Error("Expected first name of `ben`, got " + pp3.Owner.FirstName)
738+
}
739+
if pp3.Owner.LastName != "doe" {
740+
t.Error("Expected first name of `doe`, got " + pp3.Owner.LastName)
741+
}
742+
if pp3.Place.Name.String != "the-house" {
743+
t.Error("Expected place name of `the-house`, got " + pp3.Place.Name.String)
744+
}
745+
if pp3.Place.ID != pp.Place.ID {
746+
t.Errorf("Expected place name of %v, got %v", pp.Place.ID, pp3.Place.ID)
747+
}
748+
}
645749
})
646750
}
647751

0 commit comments

Comments
 (0)