mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-06-12 16:53:35 +00:00
Add reflect based reset
fix
This commit is contained in:
@@ -0,0 +1,27 @@
|
|||||||
|
package base
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/mainnika/mongox-go-driver/mongox"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Reset function creates new zero object for the target pointer
|
||||||
|
func Reset(target interface{}) {
|
||||||
|
|
||||||
|
resettable, canReset := target.(mongox.Resetter)
|
||||||
|
if canReset {
|
||||||
|
resettable.Reset()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
v := reflect.ValueOf(target)
|
||||||
|
if v.Kind() != reflect.Ptr {
|
||||||
|
panic("reset target should be a pointer")
|
||||||
|
}
|
||||||
|
|
||||||
|
t := v.Elem().Type()
|
||||||
|
zero := reflect.Zero(t)
|
||||||
|
|
||||||
|
v.Elem().Set(zero)
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/mongox"
|
"github.com/mainnika/mongox-go-driver/mongox"
|
||||||
|
"github.com/mainnika/mongox-go-driver/mongox/base"
|
||||||
"github.com/mainnika/mongox-go-driver/mongox/errors"
|
"github.com/mainnika/mongox-go-driver/mongox/errors"
|
||||||
"github.com/mainnika/mongox-go-driver/mongox/query"
|
"github.com/mainnika/mongox-go-driver/mongox/query"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
@@ -25,10 +26,7 @@ func (l *StreamLoader) DecodeNext() error {
|
|||||||
return errors.NotFoundErrorf("%s", mongo.ErrNoDocuments)
|
return errors.NotFoundErrorf("%s", mongo.ErrNoDocuments)
|
||||||
}
|
}
|
||||||
|
|
||||||
resettable, canReset := l.target.(mongox.Resetter)
|
base.Reset(l.target)
|
||||||
if canReset {
|
|
||||||
resettable.Reset()
|
|
||||||
}
|
|
||||||
|
|
||||||
err := l.Decode(l.target)
|
err := l.Decode(l.target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user