bug fixed for empty struct when update
This commit is contained in:
parent
5c116a34d6
commit
56d649f413
20
helpers.go
20
helpers.go
|
@ -147,6 +147,26 @@ func isZero(k interface{}) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func isStructZero(v reflect.Value) bool {
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
field := v.Field(i)
|
||||
switch field.Kind() {
|
||||
case reflect.Ptr:
|
||||
field = field.Elem()
|
||||
fallthrough
|
||||
case reflect.Struct:
|
||||
if !isStructZero(field) {
|
||||
return false
|
||||
}
|
||||
default:
|
||||
if !isZero(field.Interface()) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func int64ToIntValue(id int64, tp reflect.Type) reflect.Value {
|
||||
var v interface{}
|
||||
switch tp.Kind() {
|
||||
|
|
|
@ -352,7 +352,7 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
|
|||
if len(table.PrimaryKeys) == 1 {
|
||||
pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName)
|
||||
// fix non-int pk issues
|
||||
if pkField.IsValid() && !isZero(pkField.Interface()) {
|
||||
if pkField.IsValid() && (!requiredField && !isZero(pkField.Interface())) {
|
||||
val = pkField.Interface()
|
||||
} else {
|
||||
continue
|
||||
|
@ -365,6 +365,8 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
|
|||
val = fieldValue.Interface()
|
||||
}
|
||||
} else {
|
||||
// Blank struct could not be as update data
|
||||
if requiredField || !isStructZero(fieldValue) {
|
||||
bytes, err := json.Marshal(fieldValue.Interface())
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("mashal %v failed", fieldValue.Interface()))
|
||||
|
@ -374,6 +376,9 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
|
|||
} else if col.SQLType.IsBlob() {
|
||||
val = bytes
|
||||
}
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
case reflect.Array, reflect.Slice, reflect.Map:
|
||||
|
|
Loading…
Reference in New Issue