Skip to content

Commit

Permalink
Avoid a conflict between possible named params & map as param
Browse files Browse the repository at this point in the history
maybeExpandNamedQuery() now checks time.Time & driver.Valuer types
first before trying to check if the passed param is a map of named
params.

It allows to pass a map[string]struct{}{} type implementing
driver.Valuer without suffering any conflict with a map of named
params.

Closes go-gorp#423
  • Loading branch information
maxatome committed Sep 21, 2020
1 parent 03c0a1b commit 3c5617f
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions gorp.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,21 @@ func extractExecutorAndContext(e SqlExecutor) (executor, context.Context) {
// as input to a named query. If so, it rewrites the query to use
// dialect-dependent bindvars and instantiates the corresponding slice of
// parameters by extracting data from the map / struct.
// If not, returns the input values unchanged.
// If not or if the first arg is a time.Time or implements
// driver.Valuer, returns the input values unchanged.
func maybeExpandNamedQuery(m *DbMap, query string, args []interface{}) (string, []interface{}) {
var (
arg = args[0]
argval = reflect.ValueOf(arg)
)
arg := args[0]

switch arg.(type) {
case time.Time:
// time.Time is driver.Value
return query, args
case driver.Valuer:
// driver.Valuer will be converted to driver.Value.
return query, args
}

argval := reflect.ValueOf(arg)
if argval.Kind() == reflect.Ptr {
argval = argval.Elem()
}
Expand All @@ -221,14 +230,6 @@ func maybeExpandNamedQuery(m *DbMap, query string, args []interface{}) (string,
if argval.Kind() != reflect.Struct {
return query, args
}
if _, ok := arg.(time.Time); ok {
// time.Time is driver.Value
return query, args
}
if _, ok := arg.(driver.Valuer); ok {
// driver.Valuer will be converted to driver.Value.
return query, args
}

return expandNamedQuery(m, query, argval.FieldByName)
}
Expand Down

0 comments on commit 3c5617f

Please sign in to comment.