From 2f27f0f27f12f5c68ae4b12213b34fdff312dcf3 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 14 Aug 2016 16:10:30 +0800 Subject: [PATCH] Fix TestUpdateDecodeVirtualAttributes --- scope.go | 10 +++++----- update_test.go | 1 - 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/scope.go b/scope.go index c37fc078..e7a53dd4 100644 --- a/scope.go +++ b/scope.go @@ -813,7 +813,7 @@ func (scope *Scope) callCallbacks(funcs []*func(s *Scope)) *Scope { return scope } -func convertInterfaceToMap(values interface{}) map[string]interface{} { +func convertInterfaceToMap(values interface{}, withIgnoredField bool) map[string]interface{} { var attrs = map[string]interface{}{} switch value := values.(type) { @@ -821,7 +821,7 @@ func convertInterfaceToMap(values interface{}) map[string]interface{} { return value case []interface{}: for _, v := range value { - for key, value := range convertInterfaceToMap(v) { + for key, value := range convertInterfaceToMap(v, withIgnoredField) { attrs[key] = value } } @@ -835,7 +835,7 @@ func convertInterfaceToMap(values interface{}) map[string]interface{} { } default: for _, field := range (&Scope{Value: values}).Fields() { - if !field.IsBlank && !field.IsIgnored { + if !field.IsBlank && (withIgnoredField || !field.IsIgnored) { 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) { if scope.IndirectValue().Kind() != reflect.Struct { - return convertInterfaceToMap(value), true + return convertInterfaceToMap(value, false), true } 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 _, ok := value.(*expr); ok { hasUpdate = true diff --git a/update_test.go b/update_test.go index 82dd6e64..3ce64ce3 100644 --- a/update_test.go +++ b/update_test.go @@ -431,7 +431,6 @@ func (e ElementWithIgnoredField) TableName() string { func TestUpdatesTableWithIgnoredValues(t *testing.T) { elem := ElementWithIgnoredField{Value: "foo", IgnoredField: 10} - DB.LogMode(true) DB.Save(&elem) DB.Table(elem.TableName()).