Support decode to virtual attributes, close #798

This commit is contained in:
Jinzhu 2016-02-18 23:29:45 +08:00
parent 6bd0862811
commit 2786ab34b4
3 changed files with 10 additions and 12 deletions

View File

@ -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 {

View File

@ -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,

View File

@ -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()
}
}