6
6
"reflect"
7
7
"strings"
8
8
9
+ "github.com/lib/pq"
9
10
"golang.org/x/exp/slices"
10
11
11
12
"github.com/domonda/go-sqldb"
@@ -73,12 +74,10 @@ func ReflectStructColumnPointers(structVal reflect.Value, namer sqldb.StructFiel
73
74
}
74
75
75
76
func reflectStructColumnPointers (structVal reflect.Value , namer sqldb.StructFieldMapper , columns []string , pointers []any ) error {
76
- var (
77
- structType = structVal .Type ()
78
- )
77
+ structType := structVal .Type ()
79
78
for i := 0 ; i < structType .NumField (); i ++ {
80
- fieldType := structType .Field (i )
81
- _ , column , _ , use := namer .MapStructField (fieldType )
79
+ field := structType .Field (i )
80
+ _ , column , _ , use := namer .MapStructField (field )
82
81
if ! use {
83
82
continue
84
83
}
@@ -99,10 +98,16 @@ func reflectStructColumnPointers(structVal reflect.Value, namer sqldb.StructFiel
99
98
}
100
99
101
100
if pointers [colIndex ] != nil {
102
- return fmt .Errorf ("duplicate mapped column %s onto field %s of struct %s" , column , fieldType .Name , structType )
101
+ return fmt .Errorf ("duplicate mapped column %s onto field %s of struct %s" , column , field .Name , structType )
103
102
}
104
103
105
- pointers [colIndex ] = fieldValue .Addr ().Interface ()
104
+ pointer := fieldValue .Addr ().Interface ()
105
+ // If field is a slice or array that does not implement sql.Scanner
106
+ // then wrap it with pq.Array to make it scannable
107
+ if k := field .Type .Kind (); (k == reflect .Slice || k == reflect .Array ) && field .Type != typeOfByteSlice && ! fieldValue .Addr ().Type ().Implements (typeOfSQLScanner ) {
108
+ pointer = pq .Array (pointer )
109
+ }
110
+ pointers [colIndex ] = pointer
106
111
}
107
112
return nil
108
113
}
0 commit comments