Skip to content

Commit

Permalink
Fix failing test on call to sqlx.In with (driver.Valuer)(nil)
Browse files Browse the repository at this point in the history
  • Loading branch information
moxar committed Oct 18, 2024
1 parent 0a64be9 commit 402ed0c
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func In(query string, args ...interface{}) (string, []interface{}, error) {
for i, arg := range args {
if a, ok := arg.(driver.Valuer); ok {
var err error
arg, err = a.Value()
arg, err = callValuerValue(a)
if err != nil {
return "", nil, err
}
Expand Down Expand Up @@ -263,3 +263,24 @@ func appendReflectSlice(args []interface{}, v reflect.Value, vlen int) []interfa

return args
}

// callValuerValue returns vr.Value(), with one exception:
// If vr.Value is an auto-generated method on a pointer type and the
// pointer is nil, it would panic at runtime in the panicwrap
// method. Treat it like nil instead.
// Issue 8415.
//
// This is so people can implement driver.Value on value types and
// still use nil pointers to those types to mean nil/NULL, just like
// string/*string.
//
// This function is copied from database/sql/driver package
// and mirrored in the database/sql package.
func callValuerValue(vr driver.Valuer) (v driver.Value, err error) {
if rv := reflect.ValueOf(vr); rv.Kind() == reflect.Pointer &&
rv.IsNil() &&
rv.Type().Elem().Implements(reflect.TypeOf((*driver.Valuer)(nil)).Elem()) {
return nil, nil
}
return vr.Value()
}

0 comments on commit 402ed0c

Please sign in to comment.