From 50c61291de2f96a25627c55adcfda719ff5adae8 Mon Sep 17 00:00:00 2001 From: RikiyaFujii Date: Sat, 3 Nov 2018 22:55:52 +0900 Subject: [PATCH 1/3] add comment (#2163) * add comment * typo --- association.go | 1 + 1 file changed, 1 insertion(+) diff --git a/association.go b/association.go index 1b7744b5..a73344fe 100644 --- a/association.go +++ b/association.go @@ -368,6 +368,7 @@ func (association *Association) saveAssociations(values ...interface{}) *Associa return association } +// setErr set error when the error is not nil. And return Association. func (association *Association) setErr(err error) *Association { if err != nil { association.Error = err From 68f5d25d640b04d1b302993b609b2b1c693432ad Mon Sep 17 00:00:00 2001 From: teresy <43420401+teresy@users.noreply.github.com> Date: Sat, 3 Nov 2018 09:56:27 -0400 Subject: [PATCH 2/3] simplify cases of strings.Index with strings.Contains (#2162) --- scope.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scope.go b/scope.go index 378025bd..806ccb7d 100644 --- a/scope.go +++ b/scope.go @@ -68,7 +68,7 @@ func (scope *Scope) Dialect() Dialect { // Quote used to quote string to escape them for database func (scope *Scope) Quote(str string) string { - if strings.Index(str, ".") != -1 { + if strings.Contains(str, ".") { newStrs := []string{} for _, str := range strings.Split(str, ".") { newStrs = append(newStrs, scope.Dialect().Quote(str)) @@ -330,7 +330,7 @@ func (scope *Scope) TableName() string { // QuotedTableName return quoted table name func (scope *Scope) QuotedTableName() (name string) { if scope.Search != nil && len(scope.Search.tableName) > 0 { - if strings.Index(scope.Search.tableName, " ") != -1 { + if strings.Contains(scope.Search.tableName, " ") { return scope.Search.tableName } return scope.Quote(scope.Search.tableName) From 472c70caa40267cb89fd8facb07fe6454b578626 Mon Sep 17 00:00:00 2001 From: Jun Jie Nan Date: Sat, 3 Nov 2018 22:14:39 +0800 Subject: [PATCH 3/3] 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. --- field.go | 10 +++++++++- field_test.go | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/field.go b/field.go index 11c410b0..acd06e20 100644 --- a/field.go +++ b/field.go @@ -2,6 +2,7 @@ package gorm import ( "database/sql" + "database/sql/driver" "errors" "fmt" "reflect" @@ -44,7 +45,14 @@ func (field *Field) Set(value interface{}) (err error) { if reflectValue.Type().ConvertibleTo(fieldValue.Type()) { fieldValue.Set(reflectValue.Convert(fieldValue.Type())) } 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 { err = fmt.Errorf("could not convert argument of field %s from %s to %s", field.Name, reflectValue.Type(), fieldValue.Type()) } diff --git a/field_test.go b/field_test.go index c3afdff5..03a3b3b7 100644 --- a/field_test.go +++ b/field_test.go @@ -3,6 +3,7 @@ package gorm_test import ( "testing" + "github.com/gofrs/uuid" "github.com/jinzhu/gorm" ) @@ -47,3 +48,20 @@ func TestCalculateField(t *testing.T) { 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) + } +}