From a1f52c8a080984b6e47ee13ae9e14340f8d2cb14 Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Wed, 26 Dec 2018 23:17:46 +0100 Subject: [PATCH] Limiter and Sorter should return ready-to-use values --- mongox/common/loadarray.go | 11 +++-------- mongox/common/loadmany.go | 11 +++-------- mongox/common/loadone.go | 6 ++---- mongox/common/saveone.go | 4 ++-- mongox/query/limit.go | 13 +++++++++---- mongox/query/query.go | 19 +++++++++++++------ 6 files changed, 32 insertions(+), 32 deletions(-) diff --git a/mongox/common/loadarray.go b/mongox/common/loadarray.go index 0e1440c..ae424f1 100644 --- a/mongox/common/loadarray.go +++ b/mongox/common/loadarray.go @@ -33,15 +33,10 @@ func LoadArray(db *mongox.Database, target interface{}, composed *query.Query) e dummy := reflect.Zero(targetSliceElemT) collection := db.GetCollectionOf(dummy.Interface()) - opts := &options.FindOptions{} + opts := options.Find() - if composed.Sorter() != nil { - opts.Sort = composed.Sorter().Sort() - } - if composed.Limiter() != nil { - limit := int64(composed.Limiter().Limit()) - opts.Limit = &limit - } + opts.Sort = composed.Sorter() + opts.Limit = composed.Limiter() result, err := collection.Find(db.Context(), composed.M(), opts) if err != nil { diff --git a/mongox/common/loadmany.go b/mongox/common/loadmany.go index ef888f5..d9c9f1d 100644 --- a/mongox/common/loadmany.go +++ b/mongox/common/loadmany.go @@ -44,15 +44,10 @@ func (l *ManyLoader) Close() error { func LoadMany(db *mongox.Database, target interface{}, composed *query.Query) (*ManyLoader, error) { collection := db.GetCollectionOf(target) - opts := &options.FindOptions{} + opts := options.Find() - if composed.Sorter() != nil { - opts.Sort = composed.Sorter().Sort() - } - if composed.Limiter() != nil { - limit := int64(composed.Limiter().Limit()) - opts.Limit = &limit - } + opts.Sort = composed.Sorter() + opts.Limit = composed.Limiter() cursor, err := collection.Find(db.Context(), composed.M(), opts) if err != nil { diff --git a/mongox/common/loadone.go b/mongox/common/loadone.go index c589ca5..8a3d0f7 100644 --- a/mongox/common/loadone.go +++ b/mongox/common/loadone.go @@ -12,11 +12,9 @@ import ( func LoadOne(db *mongox.Database, target interface{}, composed *query.Query) error { collection := db.GetCollectionOf(target) - opts := &options.FindOneOptions{} + opts := options.FindOne() - if composed.Sorter() != nil { - opts.Sort = composed.Sorter().Sort() - } + opts.Sort = composed.Sorter() result := collection.FindOne(db.Context(), composed.M(), opts) if result.Err() != nil { diff --git a/mongox/common/saveone.go b/mongox/common/saveone.go index 5af92be..43f2263 100644 --- a/mongox/common/saveone.go +++ b/mongox/common/saveone.go @@ -2,9 +2,9 @@ package common import ( "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/mongodb/mongo-go-driver/bson" - "github.com/mongodb/mongo-go-driver/bson/primitive" "github.com/mongodb/mongo-go-driver/mongo/options" ) @@ -12,7 +12,7 @@ import ( func SaveOne(db *mongox.Database, source interface{}) error { collection := db.GetCollectionOf(source) - opts := &options.FindOneAndReplaceOptions{} + opts := options.FindOneAndReplace() id := base.GetID(source) opts.SetUpsert(true) diff --git a/mongox/query/limit.go b/mongox/query/limit.go index 076ac37..4e376ed 100644 --- a/mongox/query/limit.go +++ b/mongox/query/limit.go @@ -2,16 +2,21 @@ package query // Limiter is a filter to limit the result type Limiter interface { - Limit() int + Limit() *int64 } // Limit is a simple implementation of the Limiter filter -type Limit int +type Limit int64 var _ Limiter = Limit(0) // Limit returns a limit -func (l Limit) Limit() int { +func (l Limit) Limit() *int64 { - return int(l) + lim := int64(l) + if lim <= 0 { + return nil + } + + return &lim } diff --git a/mongox/query/query.go b/mongox/query/query.go index ee6ff60..f27d336 100644 --- a/mongox/query/query.go +++ b/mongox/query/query.go @@ -32,16 +32,24 @@ func (q *Query) And(elem bson.M) *Query { return q } -// Limiter is a limit function for a query -func (q *Query) Limiter() Limiter { +// Limiter returns limiter value or nil +func (q *Query) Limiter() *int64 { - return q.limiter + if q.limiter == nil { + return nil + } + + return q.limiter.Limit() } // Sorter is a sort rule for a query -func (q *Query) Sorter() Sorter { +func (q *Query) Sorter() interface{} { - return q.sorter + if q.sorter == nil { + return nil + } + + return q.sorter.Sort() } // Empty checks the query for any content @@ -55,6 +63,5 @@ func (q *Query) Empty() bool { // M returns underlying query map func (q *Query) M() bson.M { - return q.m }