Fix TestUpdateDecodeVirtualAttributes

This commit is contained in:
Jinzhu 2016-08-14 16:10:30 +08:00
parent fb09befb9b
commit 2f27f0f27f
2 changed files with 5 additions and 6 deletions

View File

@ -813,7 +813,7 @@ func (scope *Scope) callCallbacks(funcs []*func(s *Scope)) *Scope {
return scope return scope
} }
func convertInterfaceToMap(values interface{}) map[string]interface{} { func convertInterfaceToMap(values interface{}, withIgnoredField bool) map[string]interface{} {
var attrs = map[string]interface{}{} var attrs = map[string]interface{}{}
switch value := values.(type) { switch value := values.(type) {
@ -821,7 +821,7 @@ func convertInterfaceToMap(values interface{}) map[string]interface{} {
return value return value
case []interface{}: case []interface{}:
for _, v := range value { for _, v := range value {
for key, value := range convertInterfaceToMap(v) { for key, value := range convertInterfaceToMap(v, withIgnoredField) {
attrs[key] = value attrs[key] = value
} }
} }
@ -835,7 +835,7 @@ func convertInterfaceToMap(values interface{}) map[string]interface{} {
} }
default: default:
for _, field := range (&Scope{Value: values}).Fields() { for _, field := range (&Scope{Value: values}).Fields() {
if !field.IsBlank && !field.IsIgnored { if !field.IsBlank && (withIgnoredField || !field.IsIgnored) {
attrs[field.DBName] = field.Field.Interface() attrs[field.DBName] = field.Field.Interface()
} }
} }
@ -846,12 +846,12 @@ func convertInterfaceToMap(values interface{}) map[string]interface{} {
func (scope *Scope) updatedAttrsWithValues(value interface{}) (results map[string]interface{}, hasUpdate bool) { func (scope *Scope) updatedAttrsWithValues(value interface{}) (results map[string]interface{}, hasUpdate bool) {
if scope.IndirectValue().Kind() != reflect.Struct { if scope.IndirectValue().Kind() != reflect.Struct {
return convertInterfaceToMap(value), true return convertInterfaceToMap(value, false), true
} }
results = map[string]interface{}{} results = map[string]interface{}{}
for key, value := range convertInterfaceToMap(value) { for key, value := range convertInterfaceToMap(value, true) {
if field, ok := scope.FieldByName(key); ok && scope.changeableField(field) { if field, ok := scope.FieldByName(key); ok && scope.changeableField(field) {
if _, ok := value.(*expr); ok { if _, ok := value.(*expr); ok {
hasUpdate = true hasUpdate = true

View File

@ -431,7 +431,6 @@ func (e ElementWithIgnoredField) TableName() string {
func TestUpdatesTableWithIgnoredValues(t *testing.T) { func TestUpdatesTableWithIgnoredValues(t *testing.T) {
elem := ElementWithIgnoredField{Value: "foo", IgnoredField: 10} elem := ElementWithIgnoredField{Value: "foo", IgnoredField: 10}
DB.LogMode(true)
DB.Save(&elem) DB.Save(&elem)
DB.Table(elem.TableName()). DB.Table(elem.TableName()).