Add reflect based reset

fix
v2
Nikita Tokarchuk 6 years ago
parent 57fb40693a
commit dad0a0598b
  1. 27
      mongox/base/reset.go
  2. 6
      mongox/common/loadstream.go

@ -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 {

Loading…
Cancel
Save