mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-05-22 15:53:36 +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"
|
||||
|
||||
"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/query"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
@@ -25,10 +26,7 @@ func (l *StreamLoader) DecodeNext() error {
|
||||
return errors.NotFoundErrorf("%s", mongo.ErrNoDocuments)
|
||||
}
|
||||
|
||||
resettable, canReset := l.target.(mongox.Resetter)
|
||||
if canReset {
|
||||
resettable.Reset()
|
||||
}
|
||||
base.Reset(l.target)
|
||||
|
||||
err := l.Decode(l.target)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user