Call oncreate callback if new elem created while loading

v2
Nikita Tokarchuk 4 years ago
parent 0bc4e62d80
commit 18d9f9bed3
No known key found for this signature in database
GPG Key ID: DBFD964471BAE65C
  1. 15
      mongox/database/loadarray.go
  2. 7
      mongox/database/loadone.go
  3. 7
      mongox/database/streamloader.go
  4. 6
      mongox/query/query.go

@ -78,12 +78,25 @@ func (d *Database) LoadArray(target interface{}, filters ...interface{}) (err er
value := reflect.New(targetSliceElemT.Elem()) value := reflect.New(targetSliceElemT.Elem())
err = result.Decode(value.Interface()) err = result.Decode(value.Interface())
elem = value.Interface() elem = value.Interface()
err = composed.OnCreate().Invoke(ctx, elem)
if err != nil {
return
}
if err == nil { if err == nil {
targetSliceV = reflect.Append(targetSliceV, value) targetSliceV = reflect.Append(targetSliceV, value)
} }
} else { } else {
elem = targetSliceV.Index(i).Interface() elem = targetSliceV.Index(i).Interface()
base.Reset(elem)
if created := base.Reset(elem); created {
err = composed.OnCreate().Invoke(ctx, elem)
}
if err != nil {
return
}
err = result.Decode(elem) err = result.Decode(elem)
} }
if err != nil { if err != nil {

@ -56,7 +56,12 @@ func (d *Database) LoadOne(target interface{}, filters ...interface{}) (err erro
return mongox.ErrNoDocuments return mongox.ErrNoDocuments
} }
base.Reset(target) if created := base.Reset(target); created {
err = composed.OnCreate().Invoke(ctx, target)
}
if err != nil {
return
}
err = result.Decode(target) err = result.Decode(target)
if err != nil { if err != nil {

@ -35,7 +35,12 @@ func (l *StreamLoader) DecodeNext() (err error) {
// Decode function decodes the current cursor document into the target // Decode function decodes the current cursor document into the target
func (l *StreamLoader) Decode() (err error) { func (l *StreamLoader) Decode() (err error) {
base.Reset(l.target) if created := base.Reset(l.target); created {
err = l.query.OnDecode().Invoke(l.ctx, l.target)
}
if err != nil {
return
}
err = l.cur.Decode(l.target) err = l.cur.Decode(l.target)
if err != nil { if err != nil {

@ -17,6 +17,7 @@ type Query struct {
updater Updater updater Updater
ondecode Callbacks ondecode Callbacks
onclose Callbacks onclose Callbacks
oncreate Callbacks
} }
// And function pushes the elem query to the $and array of the query // And function pushes the elem query to the $and array of the query
@ -122,6 +123,11 @@ func (q *Query) OnClose() (callbacks Callbacks) {
return q.onclose return q.onclose
} }
// OnCreate callback is called if the mongox creates a new document instance during loading
func (q *Query) OnCreate() (callbacks Callbacks) {
return q.onclose
}
// Empty checks the query for any content // Empty checks the query for any content
func (q *Query) Empty() (isEmpty bool) { func (q *Query) Empty() (isEmpty bool) {
return len(q.m) == 0 return len(q.m) == 0

Loading…
Cancel
Save