From 3c5617faed9d63956132ce883aa5e207412abe41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20Soul=C3=A9?= Date: Mon, 21 Sep 2020 17:22:36 +0200 Subject: [PATCH] Avoid a conflict between possible named params & map as param 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 #423 --- gorp.go | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/gorp.go b/gorp.go index fc654567..0ca714ff 100644 --- a/gorp.go +++ b/gorp.go @@ -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() } @@ -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) }