Skip to content

Commit

Permalink
FieldValueByName does now only what it should
Browse files Browse the repository at this point in the history
FieldValueByName in utils.go does now only what it should and has proper errors.
  • Loading branch information
tstorch committed Oct 1, 2014
1 parent 591d4a4 commit 9bee423
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
2 changes: 1 addition & 1 deletion association.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion scope_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
22 changes: 9 additions & 13 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package gorm

import (
"bytes"
"errors"
"fmt"
"reflect"
"strings"
"sync"
Expand All @@ -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
} else {
return field.Interface(), true
}
}
} 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()
i = field.Interface()
} else {
return nil, reflect.New(data.Type().Elem()).Elem().FieldByName(name).IsValid()
return nil, errors.New(fmt.Sprintf("struct has no field with name %s", name))
}
} else {
return nil, errors.New("value must be of kind struct")
}
return nil, false

return
}

func newSafeMap() *safeMap {
Expand Down

0 comments on commit 9bee423

Please sign in to comment.