mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-06-13 01:03:35 +00:00
Split compose applier
This commit is contained in:
+32
-16
@@ -14,7 +14,9 @@ func Compose(filters ...interface{}) *Query {
|
|||||||
|
|
||||||
ok := false
|
ok := false
|
||||||
ok = ok || applyBson(q, f)
|
ok = ok || applyBson(q, f)
|
||||||
ok = ok || applyLimits(q, f)
|
ok = ok || applyLimit(q, f)
|
||||||
|
ok = ok || applySort(q, f)
|
||||||
|
ok = ok || applySkip(q, f)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
panic(errors.InternalErrorf("unknown filter %v", f))
|
panic(errors.InternalErrorf("unknown filter %v", f))
|
||||||
@@ -27,29 +29,43 @@ func Compose(filters ...interface{}) *Query {
|
|||||||
// applyBson is a fallback for a custom bson.M
|
// applyBson is a fallback for a custom bson.M
|
||||||
func applyBson(q *Query, f interface{}) bool {
|
func applyBson(q *Query, f interface{}) bool {
|
||||||
|
|
||||||
switch f := f.(type) {
|
if f, ok := f.(bson.M); ok {
|
||||||
case bson.M:
|
|
||||||
q.And(f)
|
q.And(f)
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// applyLimits extends query with contol functions
|
return false
|
||||||
func applyLimits(q *Query, f interface{}) bool {
|
}
|
||||||
|
|
||||||
switch f := f.(type) {
|
// applyLimits extends query with a limiter
|
||||||
case Limiter:
|
func applyLimit(q *Query, f interface{}) bool {
|
||||||
|
|
||||||
|
if f, ok := f.(Limiter); ok {
|
||||||
q.limiter = f
|
q.limiter = f
|
||||||
case Sorter:
|
return true
|
||||||
q.sorter = f
|
}
|
||||||
case Skipper:
|
|
||||||
q.skipper = f
|
|
||||||
default:
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// applySort extends query with a sort rule
|
||||||
|
func applySort(q *Query, f interface{}) bool {
|
||||||
|
|
||||||
|
if f, ok := f.(Sorter); ok {
|
||||||
|
q.sorter = f
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// applySkip extends query with a skip number
|
||||||
|
func applySkip(q *Query, f interface{}) bool {
|
||||||
|
|
||||||
|
if f, ok := f.(Skipper); ok {
|
||||||
|
q.skipper = f
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user