@@ -146,8 +146,22 @@ func prepareNamed(p namedPreparer, query string) (*NamedStmt, error) {
146146 }, nil
147147}
148148
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+
149163func 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 {
151165 return bindMapArgs (names , maparg )
152166 }
153167 return bindArgs (names , arg , m )
@@ -383,12 +397,10 @@ func bindNamedMapper(bindType int, query string, arg interface{}, m *reflectx.Ma
383397 k := t .Kind ()
384398 switch {
385399 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 {
388402 return "" , nil , fmt .Errorf ("sqlx.bindNamedMapper: unsupported map type: %T" , arg )
389403 }
390-
391- m = reflect .ValueOf (arg ).Convert (reflect .TypeOf (m )).Interface ().(map [string ]interface {})
392404 return bindMap (bindType , query , m )
393405 case k == reflect .Array || k == reflect .Slice :
394406 return bindArray (bindType , query , arg , m )
0 commit comments