mirror of https://github.com/go-gorm/gorm.git
Support decode to virtual attributes, close #798
This commit is contained in:
parent
6bd0862811
commit
2786ab34b4
|
@ -328,18 +328,18 @@ func (scope *Scope) updatedAttrsWithValues(values map[string]interface{}) (resul
|
||||||
for key, value := range values {
|
for key, value := range values {
|
||||||
if field, ok := scope.FieldByName(key); ok && scope.changeableField(field) {
|
if field, ok := scope.FieldByName(key); ok && scope.changeableField(field) {
|
||||||
if !reflect.DeepEqual(field.Field, reflect.ValueOf(value)) {
|
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
|
hasUpdate = true
|
||||||
results[field.DBName] = value
|
|
||||||
} else if !equalAsString(field.Field.Interface(), value) {
|
|
||||||
hasUpdate = true
|
|
||||||
field.Set(value)
|
|
||||||
results[field.DBName] = field.Field.Interface()
|
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 {
|
func (scope *Scope) shouldSaveAssociations() bool {
|
||||||
|
|
|
@ -438,8 +438,6 @@ func TestUpdatesWithBlankValues(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateDecodeVirtualAttributes(t *testing.T) {
|
func TestUpdateDecodeVirtualAttributes(t *testing.T) {
|
||||||
t.Skip("not implemented")
|
|
||||||
|
|
||||||
var user = User{
|
var user = User{
|
||||||
Name: "jinzhu",
|
Name: "jinzhu",
|
||||||
IgnoreMe: 88,
|
IgnoreMe: 88,
|
||||||
|
|
2
utils.go
2
utils.go
|
@ -209,7 +209,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 {
|
||||||
attrs[field.DBName] = field.Field.Interface()
|
attrs[field.DBName] = field.Field.Interface()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue