mirror of https://github.com/go-gorm/gorm.git
Readability update
Small readability update. Includes also a switch from bool return values to error return values in Scope.SetColumn and Field.Set.
This commit is contained in:
parent
591d4a4057
commit
f1212ecd58
36
field.go
36
field.go
|
@ -2,6 +2,7 @@ package gorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"errors"
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -35,23 +36,28 @@ func (field *Field) IsTime() bool {
|
||||||
return isTime
|
return isTime
|
||||||
}
|
}
|
||||||
|
|
||||||
func (field *Field) Set(value interface{}) (result bool) {
|
func (field *Field) Set(value interface{}) (err error) {
|
||||||
if field.Field.IsValid() && field.Field.CanAddr() {
|
if !field.Field.IsValid() {
|
||||||
result = true
|
return errors.New("field value not valid")
|
||||||
if rvalue, ok := value.(reflect.Value); ok {
|
}
|
||||||
value = rvalue.Interface()
|
|
||||||
}
|
|
||||||
|
|
||||||
if scanner, ok := field.Field.Addr().Interface().(sql.Scanner); ok {
|
if !field.Field.CanAddr() {
|
||||||
scanner.Scan(value)
|
return errors.New("field value not addressable")
|
||||||
} else if reflect.TypeOf(value).ConvertibleTo(field.Field.Type()) {
|
|
||||||
field.Field.Set(reflect.ValueOf(value).Convert(field.Field.Type()))
|
|
||||||
} else {
|
|
||||||
result = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if result {
|
|
||||||
field.IsBlank = isBlank(field.Field)
|
if rvalue, ok := value.(reflect.Value); ok {
|
||||||
|
value = rvalue.Interface()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if scanner, ok := field.Field.Addr().Interface().(sql.Scanner); ok {
|
||||||
|
scanner.Scan(value)
|
||||||
|
} else if reflect.TypeOf(value).ConvertibleTo(field.Field.Type()) {
|
||||||
|
field.Field.Set(reflect.ValueOf(value).Convert(field.Field.Type()))
|
||||||
|
} else {
|
||||||
|
return errors.New("could not convert argument")
|
||||||
|
}
|
||||||
|
|
||||||
|
field.IsBlank = isBlank(field.Field)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
28
scope.go
28
scope.go
|
@ -129,19 +129,18 @@ func (scope *Scope) PrimaryKeyValue() interface{} {
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasColumn to check if has column
|
// HasColumn to check if has column
|
||||||
func (scope *Scope) HasColumn(column string) bool {
|
func (scope *Scope) HasColumn(column string) (hasColumn bool) {
|
||||||
clone := scope
|
clone := scope
|
||||||
if scope.IndirectValue().Kind() == reflect.Slice {
|
if scope.IndirectValue().Kind() == reflect.Slice {
|
||||||
value := reflect.New(scope.IndirectValue().Type().Elem()).Interface()
|
value := reflect.New(scope.IndirectValue().Type().Elem()).Interface()
|
||||||
clone = scope.New(value)
|
clone = scope.New(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, field := range clone.Fields(false) {
|
dbName := ToSnake(column)
|
||||||
if field.Name == column || field.DBName == column {
|
|
||||||
return true
|
_, hasColumn = clone.Fields(false)[dbName]
|
||||||
}
|
|
||||||
}
|
return
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FieldValueByName to get column's value and existence
|
// FieldValueByName to get column's value and existence
|
||||||
|
@ -150,20 +149,21 @@ func (scope *Scope) FieldValueByName(name string) (interface{}, bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetColumn to set the column's value
|
// SetColumn to set the column's value
|
||||||
func (scope *Scope) SetColumn(column interface{}, value interface{}) bool {
|
func (scope *Scope) SetColumn(column interface{}, value interface{}) error {
|
||||||
if field, ok := column.(*Field); ok {
|
if field, ok := column.(*Field); ok {
|
||||||
return field.Set(value)
|
return field.Set(value)
|
||||||
} else if str, ok := column.(string); ok {
|
} else if str, ok := column.(string); ok {
|
||||||
if scope.Value == nil {
|
if scope.Value == nil {
|
||||||
return false
|
return errors.New("scope value must not be nil for string columns")
|
||||||
}
|
}
|
||||||
for _, field := range scope.Fields() {
|
|
||||||
if field.Name == str || field.DBName == str {
|
dbName := ToSnake(str)
|
||||||
return field.Set(value)
|
|
||||||
}
|
if field, ok := scope.Fields()[dbName]; ok {
|
||||||
|
return field.Set(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return errors.New("could not convert column to field")
|
||||||
}
|
}
|
||||||
|
|
||||||
// CallMethod invoke method with necessary argument
|
// CallMethod invoke method with necessary argument
|
||||||
|
|
Loading…
Reference in New Issue