Consistent onclose callback order and proper err handling

v2
Nikita Tokarchuk 4 years ago
parent cd5b2b85d1
commit 24004ff910
No known key found for this signature in database
GPG Key ID: DBFD964471BAE65C
  1. 11
      mongox/database/deletearray.go
  2. 9
      mongox/database/deleteone.go
  3. 23
      mongox/database/loadarray.go
  4. 19
      mongox/database/loadone.go
  5. 9
      mongox/database/saveone.go
  6. 15
      mongox/database/streamloader.go

@ -46,14 +46,21 @@ func (d *Database) DeleteArray(target interface{}, filters ...interface{}) (err
ids = append(ids, base.GetID(elem.Interface())) ids = append(ids, base.GetID(elem.Interface()))
} }
defer func() {
invokerr := composed.OnClose().Invoke(ctx, target)
if err == nil {
err = invokerr
}
return
}()
if len(ids) == 0 { if len(ids) == 0 {
return fmt.Errorf("can't delete zero elements") return fmt.Errorf("can't delete zero elements")
} }
composed.And(primitive.M{"_id": primitive.M{"$in": ids}}) composed.And(primitive.M{"_id": primitive.M{"$in": ids}})
defer composed.OnClose().Invoke(ctx, target)
result, err := collection.DeleteMany(ctx, composed.M(), opts) result, err := collection.DeleteMany(ctx, composed.M(), opts)
if err != nil { if err != nil {
return return

@ -33,7 +33,14 @@ func (d *Database) DeleteOne(target interface{}, filters ...interface{}) (err er
protected.V = time.Now().Unix() protected.V = time.Now().Unix()
} }
defer composed.OnClose().Invoke(ctx, target) defer func() {
invokerr := composed.OnClose().Invoke(ctx, target)
if err == nil {
err = invokerr
}
return
}()
result := collection.FindOneAndDelete(ctx, composed.M(), opts) result := collection.FindOneAndDelete(ctx, composed.M(), opts)
if result.Err() != nil { if result.Err() != nil {

@ -39,6 +39,23 @@ func (d *Database) LoadArray(target interface{}, filters ...interface{}) (err er
var result *mongox.Cursor var result *mongox.Cursor
var i int var i int
defer func() {
if result != nil {
closerr := result.Close(ctx)
if err == nil {
err = closerr
}
}
invokerr := composed.OnClose().Invoke(ctx, target)
if err == nil {
err = invokerr
}
return
}()
if hasPreloader { if hasPreloader {
result, err = d.createAggregateLoad(zeroElem.Interface(), composed) result, err = d.createAggregateLoad(zeroElem.Interface(), composed)
} else { } else {
@ -49,8 +66,6 @@ func (d *Database) LoadArray(target interface{}, filters ...interface{}) (err er
return return
} }
defer composed.OnClose().Invoke(ctx, target)
for i = 0; result.Next(ctx); { for i = 0; result.Next(ctx); {
var elem interface{} var elem interface{}
@ -68,13 +83,11 @@ func (d *Database) LoadArray(target interface{}, filters ...interface{}) (err er
err = result.Decode(elem) err = result.Decode(elem)
} }
if err != nil { if err != nil {
_ = result.Close(ctx)
return return
} }
err = composed.OnDecode().Invoke(ctx, elem) err = composed.OnDecode().Invoke(ctx, elem)
if err != nil { if err != nil {
_ = result.Close(ctx)
return return
} }
@ -84,5 +97,5 @@ func (d *Database) LoadArray(target interface{}, filters ...interface{}) (err er
targetSliceV = targetSliceV.Slice(0, i) targetSliceV = targetSliceV.Slice(0, i)
targetV.Elem().Set(targetSliceV) targetV.Elem().Set(targetSliceV)
return result.Close(ctx) return
} }

@ -17,6 +17,23 @@ func (d *Database) LoadOne(target interface{}, filters ...interface{}) (err erro
var result *mongox.Cursor var result *mongox.Cursor
defer func() {
if result != nil {
closerr := result.Close(ctx)
if err == nil {
err = closerr
}
}
invokerr := composed.OnClose().Invoke(ctx, target)
if err == nil {
err = invokerr
}
return
}()
if hasPreloader { if hasPreloader {
result, err = d.createAggregateLoad(target, composed) result, err = d.createAggregateLoad(target, composed)
} else { } else {
@ -26,8 +43,6 @@ func (d *Database) LoadOne(target interface{}, filters ...interface{}) (err erro
return fmt.Errorf("can't create find result: %w", err) return fmt.Errorf("can't create find result: %w", err)
} }
defer composed.OnClose().Invoke(ctx, target)
hasNext := result.Next(ctx) hasNext := result.Next(ctx)
if result.Err() != nil { if result.Err() != nil {
return err return err

@ -32,7 +32,14 @@ func (d *Database) SaveOne(source interface{}, filters ...interface{}) (err erro
protected.V = time.Now().Unix() protected.V = time.Now().Unix()
} }
defer composed.OnClose().Invoke(ctx, source) defer func() {
invokerr := composed.OnClose().Invoke(ctx, source)
if err == nil {
err = invokerr
}
return
}()
result := collection.FindOneAndReplace(ctx, composed.M(), source, opts) result := collection.FindOneAndReplace(ctx, composed.M(), source, opts)
if result.Err() != nil { if result.Err() != nil {

@ -73,9 +73,20 @@ func (l *StreamLoader) Cursor() (cursor *mongox.Cursor) {
// Close cursor // Close cursor
func (l *StreamLoader) Close() (err error) { func (l *StreamLoader) Close() (err error) {
_ = l.query.OnClose().Invoke(l.ctx, l.target) closerr := l.cur.Close(l.ctx)
invokerr := l.query.OnClose().Invoke(l.ctx, l.target)
return l.cur.Close(l.ctx) if closerr != nil {
err = closerr
return
}
if invokerr != nil {
err = invokerr
return
}
return
} }
func (l *StreamLoader) Err() (err error) { func (l *StreamLoader) Err() (err error) {

Loading…
Cancel
Save