FieldValueByName does now only what it should

FieldValueByName in utils.go does now only what it should and has proper errors.
This commit is contained in:
Tristan Storch 2014-10-01 15:58:13 +02:00
parent 591d4a4057
commit 9bee4239d4
4 changed files with 12 additions and 16 deletions

View File

@ -177,7 +177,7 @@ func (association *Association) Count() int {
whereSql := fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), relationship.ForeignKey)
scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, association.PrimaryKey).Count(&count)
} else if relationship.Kind == "belongs_to" {
if v, ok := scope.FieldValueByName(association.Column); ok {
if v, err := scope.FieldValueByName(association.Column); err == nil {
whereSql := fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), relationship.ForeignKey)
scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, v).Count(&count)
}

View File

@ -145,7 +145,7 @@ func (scope *Scope) HasColumn(column string) bool {
}
// FieldValueByName to get column's value and existence
func (scope *Scope) FieldValueByName(name string) (interface{}, bool) {
func (scope *Scope) FieldValueByName(name string) (interface{}, error) {
return FieldValueByName(name, scope.Value)
}

View File

@ -474,7 +474,7 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
}
// has one
if foreignValue, ok := scope.FieldValueByName(foreignKey); ok {
if foreignValue, err := scope.FieldValueByName(foreignKey); err == nil {
toScope.inlineCondition(foreignValue).callCallbacks(scope.db.parent.callback.queries)
return scope
}

View File

@ -2,6 +2,8 @@ package gorm
import (
"bytes"
"errors"
"fmt"
"reflect"
"strings"
"sync"
@ -24,27 +26,21 @@ func (s *safeMap) Get(key string) string {
return s.m[key]
}
func FieldValueByName(name string, value interface{}, withAddr ...bool) (interface{}, bool) {
func FieldValueByName(name string, value interface{}) (i interface{}, err error) {
data := reflect.Indirect(reflect.ValueOf(value))
name = SnakeToUpperCamel(name)
if data.Kind() == reflect.Struct {
if field := data.FieldByName(name); field.IsValid() {
if len(withAddr) > 0 && field.CanAddr() {
return field.Addr().Interface(), true
i = field.Interface()
} else {
return field.Interface(), true
return nil, errors.New(fmt.Sprintf("struct has no field with name %s", name))
}
}
} else if data.Kind() == reflect.Slice {
elem := data.Type().Elem()
if elem.Kind() == reflect.Ptr {
return nil, reflect.New(data.Type().Elem().Elem()).Elem().FieldByName(name).IsValid()
} else {
return nil, reflect.New(data.Type().Elem()).Elem().FieldByName(name).IsValid()
return nil, errors.New("value must be of kind struct")
}
}
return nil, false
return
}
func newSafeMap() *safeMap {