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
|
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 {
|
func int64ToIntValue(id int64, tp reflect.Type) reflect.Value {
|
||||||
var v interface{}
|
var v interface{}
|
||||||
switch tp.Kind() {
|
switch tp.Kind() {
|
||||||
|
|
|
@ -352,7 +352,7 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
|
||||||
if len(table.PrimaryKeys) == 1 {
|
if len(table.PrimaryKeys) == 1 {
|
||||||
pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName)
|
pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName)
|
||||||
// fix non-int pk issues
|
// fix non-int pk issues
|
||||||
if pkField.IsValid() && !isZero(pkField.Interface()) {
|
if pkField.IsValid() && (!requiredField && !isZero(pkField.Interface())) {
|
||||||
val = pkField.Interface()
|
val = pkField.Interface()
|
||||||
} else {
|
} else {
|
||||||
continue
|
continue
|
||||||
|
@ -365,6 +365,8 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
|
||||||
val = fieldValue.Interface()
|
val = fieldValue.Interface()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// Blank struct could not be as update data
|
||||||
|
if requiredField || !isStructZero(fieldValue) {
|
||||||
bytes, err := json.Marshal(fieldValue.Interface())
|
bytes, err := json.Marshal(fieldValue.Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("mashal %v failed", fieldValue.Interface()))
|
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() {
|
} else if col.SQLType.IsBlob() {
|
||||||
val = bytes
|
val = bytes
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case reflect.Array, reflect.Slice, reflect.Map:
|
case reflect.Array, reflect.Slice, reflect.Map:
|
||||||
|
|
Loading…
Reference in New Issue