fix doSave
calls to check if the document's ID is a zero value
This commit is contained in:
parent
a44b9679aa
commit
bb37212be7
@ -96,13 +96,15 @@ func (d *Document) Save() error {
|
||||
if val.Kind() == reflect.Slice {
|
||||
for i := 0; i < val.Len(); i++ {
|
||||
cur := val.Index(i)
|
||||
if err := doSave(d.model.getColl(), !d.exists, cur.Interface()); err != nil {
|
||||
asHId := asId(cur.Interface())
|
||||
if err := doSave(d.model.getColl(), !d.exists && reflect.ValueOf(asHId.Id()).IsZero(), cur.Interface()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
} else {
|
||||
return doSave(d.model.getColl(), !d.exists, d.self)
|
||||
asHId := asId(val.Interface())
|
||||
return doSave(d.model.getColl(), !d.exists && reflect.ValueOf(asHId.Id()).IsZero(), d.self)
|
||||
}
|
||||
}
|
||||
|
||||
|
21
util.go
21
util.go
@ -39,6 +39,27 @@ func valueOf(i interface{}) reflect.Value {
|
||||
return v
|
||||
}
|
||||
|
||||
func asId(i interface{}) HasID {
|
||||
v := reflect.ValueOf(i)
|
||||
var asHasId HasID
|
||||
var ok bool
|
||||
switch v.Kind() {
|
||||
case reflect.Struct:
|
||||
v = reflect.New(reflect.PointerTo(v.Type()))
|
||||
fallthrough
|
||||
case reflect.Pointer:
|
||||
asHasId, ok = v.Interface().(HasID)
|
||||
if ok {
|
||||
return asHasId
|
||||
} else {
|
||||
panic("value does not implemenet `HasId`!")
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
return asHasId
|
||||
}
|
||||
|
||||
func coerceInt(input reflect.Value, dst reflect.Value) interface{} {
|
||||
if input.Type().Kind() == reflect.Pointer {
|
||||
input = input.Elem()
|
||||
|
Loading…
x
Reference in New Issue
Block a user