fix doSave calls to check if the document's ID is a zero value

This commit is contained in:
☙◦ The Tablet ❀ GamerGirlandCo ◦❧ 2025-03-26 23:25:44 -04:00
parent a44b9679aa
commit bb37212be7
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C
2 changed files with 25 additions and 2 deletions

View File

@ -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
View File

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