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:
Tristan Storch 2014-09-30 14:02:51 +02:00
parent 591d4a4057
commit f1212ecd58
2 changed files with 35 additions and 29 deletions

View File

@ -2,6 +2,7 @@ package gorm
import (
"database/sql"
"errors"
"reflect"
"time"
)
@ -35,9 +36,15 @@ func (field *Field) IsTime() bool {
return isTime
}
func (field *Field) Set(value interface{}) (result bool) {
if field.Field.IsValid() && field.Field.CanAddr() {
result = true
func (field *Field) Set(value interface{}) (err error) {
if !field.Field.IsValid() {
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 {
value = rvalue.Interface()
}
@ -47,11 +54,10 @@ func (field *Field) Set(value interface{}) (result bool) {
} else if reflect.TypeOf(value).ConvertibleTo(field.Field.Type()) {
field.Field.Set(reflect.ValueOf(value).Convert(field.Field.Type()))
} else {
result = false
return errors.New("could not convert argument")
}
}
if result {
field.IsBlank = isBlank(field.Field)
}
return
}

View File

@ -129,19 +129,18 @@ func (scope *Scope) PrimaryKeyValue() interface{} {
}
// HasColumn to check if has column
func (scope *Scope) HasColumn(column string) bool {
func (scope *Scope) HasColumn(column string) (hasColumn bool) {
clone := scope
if scope.IndirectValue().Kind() == reflect.Slice {
value := reflect.New(scope.IndirectValue().Type().Elem()).Interface()
clone = scope.New(value)
}
for _, field := range clone.Fields(false) {
if field.Name == column || field.DBName == column {
return true
}
}
return false
dbName := ToSnake(column)
_, hasColumn = clone.Fields(false)[dbName]
return
}
// 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
func (scope *Scope) SetColumn(column interface{}, value interface{}) bool {
func (scope *Scope) SetColumn(column interface{}, value interface{}) error {
if field, ok := column.(*Field); ok {
return field.Set(value)
} else if str, ok := column.(string); ok {
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 false
return errors.New("could not convert column to field")
}
// CallMethod invoke method with necessary argument