mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-06-12 16:53:35 +00:00
Call oncreate callback if new elem created while loading
This commit is contained in:
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user