From 2786ab34b4e19fc12ebaa148519c999fb4093f0a Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 18 Feb 2016 23:29:45 +0800 Subject: [PATCH] Support decode to virtual attributes, close #798 --- scope_private.go | 18 +++++++++--------- update_test.go | 2 -- utils.go | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/scope_private.go b/scope_private.go index 9b01dcb9..ceaaf9c6 100644 --- a/scope_private.go +++ b/scope_private.go @@ -328,18 +328,18 @@ func (scope *Scope) updatedAttrsWithValues(values map[string]interface{}) (resul for key, value := range values { if field, ok := scope.FieldByName(key); ok && scope.changeableField(field) { if !reflect.DeepEqual(field.Field, reflect.ValueOf(value)) { - if field.IsNormal { - if _, ok := value.(*expr); ok { + if _, ok := value.(*expr); ok { + hasUpdate = true + results[field.DBName] = value + } else if !equalAsString(field.Field.Interface(), value) { + field.Set(value) + if field.IsNormal { hasUpdate = true - results[field.DBName] = value - } else if !equalAsString(field.Field.Interface(), value) { - hasUpdate = true - field.Set(value) results[field.DBName] = field.Field.Interface() } - } else { - field.Set(value) } + } else { + field.Set(value) } } } @@ -428,7 +428,7 @@ func (scope *Scope) changeableField(field *Field) bool { } } - return !field.IsIgnored + return true } func (scope *Scope) shouldSaveAssociations() bool { diff --git a/update_test.go b/update_test.go index fd193ada..218c5834 100644 --- a/update_test.go +++ b/update_test.go @@ -438,8 +438,6 @@ func TestUpdatesWithBlankValues(t *testing.T) { } func TestUpdateDecodeVirtualAttributes(t *testing.T) { - t.Skip("not implemented") - var user = User{ Name: "jinzhu", IgnoreMe: 88, diff --git a/utils.go b/utils.go index 55d75619..f6bce479 100644 --- a/utils.go +++ b/utils.go @@ -209,7 +209,7 @@ func convertInterfaceToMap(values interface{}) map[string]interface{} { } default: for _, field := range (&Scope{Value: values}).Fields() { - if !field.IsBlank && !field.IsIgnored { + if !field.IsBlank { attrs[field.DBName] = field.Field.Interface() } }