@@ -146,8 +146,22 @@ func prepareNamed(p namedPreparer, query string) (*NamedStmt, error) {
146
146
}, nil
147
147
}
148
148
149
+ // convertMapStringInterface attempts to convert v to map[string]interface{}.
150
+ // Unlike v.(map[string]interface{}), this function works on named types that
151
+ // are convertible to map[string]interface{} as well.
152
+ func convertMapStringInterface (v interface {}) (map [string ]interface {}, bool ) {
153
+ var m map [string ]interface {}
154
+ mtype := reflect .TypeOf (m )
155
+ t := reflect .TypeOf (v )
156
+ if ! t .ConvertibleTo (mtype ) {
157
+ return nil , false
158
+ }
159
+ return reflect .ValueOf (v ).Convert (mtype ).Interface ().(map [string ]interface {}), true
160
+
161
+ }
162
+
149
163
func bindAnyArgs (names []string , arg interface {}, m * reflectx.Mapper ) ([]interface {}, error ) {
150
- if maparg , ok := arg .( map [ string ] interface {} ); ok {
164
+ if maparg , ok := convertMapStringInterface ( arg ); ok {
151
165
return bindMapArgs (names , maparg )
152
166
}
153
167
return bindArgs (names , arg , m )
@@ -202,7 +216,7 @@ func bindStruct(bindType int, query string, arg interface{}, m *reflectx.Mapper)
202
216
return "" , []interface {}{}, err
203
217
}
204
218
205
- arglist , err := bindArgs (names , arg , m )
219
+ arglist , err := bindAnyArgs (names , arg , m )
206
220
if err != nil {
207
221
return "" , []interface {}{}, err
208
222
}
@@ -383,12 +397,10 @@ func bindNamedMapper(bindType int, query string, arg interface{}, m *reflectx.Ma
383
397
k := t .Kind ()
384
398
switch {
385
399
case k == reflect .Map && t .Key ().Kind () == reflect .String :
386
- var m map [ string ] interface {}
387
- if ! t . ConvertibleTo ( reflect . TypeOf ( m )) {
400
+ m , ok := convertMapStringInterface ( arg )
401
+ if ! ok {
388
402
return "" , nil , fmt .Errorf ("sqlx.bindNamedMapper: unsupported map type: %T" , arg )
389
403
}
390
-
391
- m = reflect .ValueOf (arg ).Convert (reflect .TypeOf (m )).Interface ().(map [string ]interface {})
392
404
return bindMap (bindType , query , m )
393
405
case k == reflect .Array || k == reflect .Slice :
394
406
return bindArray (bindType , query , arg , m )
0 commit comments