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
20
field.go
20
field.go
|
@ -2,6 +2,7 @@ package gorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"errors"
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -35,9 +36,15 @@ 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 !field.Field.CanAddr() {
|
||||||
|
return errors.New("field value not addressable")
|
||||||
|
}
|
||||||
|
|
||||||
if rvalue, ok := value.(reflect.Value); ok {
|
if rvalue, ok := value.(reflect.Value); ok {
|
||||||
value = rvalue.Interface()
|
value = rvalue.Interface()
|
||||||
}
|
}
|
||||||
|
@ -47,11 +54,10 @@ func (field *Field) Set(value interface{}) (result bool) {
|
||||||
} else if reflect.TypeOf(value).ConvertibleTo(field.Field.Type()) {
|
} else if reflect.TypeOf(value).ConvertibleTo(field.Field.Type()) {
|
||||||
field.Field.Set(reflect.ValueOf(value).Convert(field.Field.Type()))
|
field.Field.Set(reflect.ValueOf(value).Convert(field.Field.Type()))
|
||||||
} else {
|
} else {
|
||||||
result = false
|
return errors.New("could not convert argument")
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if result {
|
|
||||||
field.IsBlank = isBlank(field.Field)
|
field.IsBlank = isBlank(field.Field)
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
26
scope.go
26
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)
|
||||||
|
|
||||||
|
if field, ok := scope.Fields()[dbName]; ok {
|
||||||
return field.Set(value)
|
return field.Set(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return errors.New("could not convert column to field")
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CallMethod invoke method with necessary argument
|
// CallMethod invoke method with necessary argument
|
||||||
|
|
Loading…
Reference in New Issue