Check valuer interface before scan value (#2155)

Scan interface only accept int64, float64, bool, []byte, string,
time.Time or nil. When do scan, it's better to check whether the type
support valuer interface and do convert.
This commit is contained in:
Jun Jie Nan 2018-11-03 22:14:39 +08:00 committed by Jinzhu
parent 68f5d25d64
commit 472c70caa4
2 changed files with 27 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package gorm
import ( import (
"database/sql" "database/sql"
"database/sql/driver"
"errors" "errors"
"fmt" "fmt"
"reflect" "reflect"
@ -44,7 +45,14 @@ func (field *Field) Set(value interface{}) (err error) {
if reflectValue.Type().ConvertibleTo(fieldValue.Type()) { if reflectValue.Type().ConvertibleTo(fieldValue.Type()) {
fieldValue.Set(reflectValue.Convert(fieldValue.Type())) fieldValue.Set(reflectValue.Convert(fieldValue.Type()))
} else if scanner, ok := fieldValue.Addr().Interface().(sql.Scanner); ok { } else if scanner, ok := fieldValue.Addr().Interface().(sql.Scanner); ok {
err = scanner.Scan(reflectValue.Interface()) v := reflectValue.Interface()
if valuer, ok := v.(driver.Valuer); ok {
if v, err = valuer.Value(); err == nil {
err = scanner.Scan(v)
}
} else {
err = scanner.Scan(v)
}
} else { } else {
err = fmt.Errorf("could not convert argument of field %s from %s to %s", field.Name, reflectValue.Type(), fieldValue.Type()) err = fmt.Errorf("could not convert argument of field %s from %s to %s", field.Name, reflectValue.Type(), fieldValue.Type())
} }

View File

@ -3,6 +3,7 @@ package gorm_test
import ( import (
"testing" "testing"
"github.com/gofrs/uuid"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
) )
@ -47,3 +48,20 @@ func TestCalculateField(t *testing.T) {
t.Errorf("should find embedded field's tag settings") t.Errorf("should find embedded field's tag settings")
} }
} }
func TestFieldSet(t *testing.T) {
type TestFieldSetNullUUID struct {
NullUUID uuid.NullUUID
}
scope := DB.NewScope(&TestFieldSetNullUUID{})
field := scope.Fields()[0]
err := field.Set(uuid.FromStringOrNil("3034d44a-da03-11e8-b366-4a00070b9f00"))
if err != nil {
t.Fatal(err)
}
if id, ok := field.Field.Addr().Interface().(*uuid.NullUUID); !ok {
t.Fatal()
} else if !id.Valid || id.UUID.String() != "3034d44a-da03-11e8-b366-4a00070b9f00" {
t.Fatal(id)
}
}