From 024ea196f67e53860d701f189ef6d5a7de7e6af4 Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Wed, 18 Nov 2020 20:03:33 +0100 Subject: [PATCH] Allow to combine query filter purposes --- mongox/query/compose.go | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/mongox/query/compose.go b/mongox/query/compose.go index 621ecbc..40bb12a 100644 --- a/mongox/query/compose.go +++ b/mongox/query/compose.go @@ -9,6 +9,8 @@ import ( "github.com/mainnika/mongox-go-driver/v2/mongox/base/protection" ) +type applyFilterFunc = func(query *Query, filter interface{}) (ok bool) + // Compose is a function to compose filters into a single query func Compose(filters ...interface{}) (query *Query) { @@ -30,17 +32,20 @@ func Push(q *Query, f interface{}) (ok bool) { return true } - ok = false - ok = ok || applyBson(q, f) - ok = ok || applyLimit(q, f) - ok = ok || applySort(q, f) - ok = ok || applySkip(q, f) - ok = ok || applyProtection(q, f) - ok = ok || applyPreloader(q, f) - ok = ok || applyUpdater(q, f) - ok = ok || applyCallbacks(q, f) - - return ok + for _, applier := range []applyFilterFunc{ + applyBson, + applyLimit, + applySort, + applySkip, + applyProtection, + applyPreloader, + applyUpdater, + applyCallbacks, + } { + ok = applier(q, f) || ok + } + + return } // applyBson is a fallback for a custom primitive.M