|
|
|
@ -11,27 +11,27 @@ import ( |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// Compose is a function to compose filters into a single query
|
|
|
|
|
func Compose(filters ...interface{}) *Query { |
|
|
|
|
func Compose(filters ...interface{}) (query *Query) { |
|
|
|
|
|
|
|
|
|
q := &Query{} |
|
|
|
|
query = &Query{} |
|
|
|
|
|
|
|
|
|
for _, f := range filters { |
|
|
|
|
if !Push(q, f) { |
|
|
|
|
if !Push(query, f) { |
|
|
|
|
panic(fmt.Errorf("unknown filter %v", f)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return q |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Push applies single filter to a query
|
|
|
|
|
func Push(q *Query, f interface{}) bool { |
|
|
|
|
func Push(q *Query, f interface{}) (ok bool) { |
|
|
|
|
|
|
|
|
|
if utils.IsNil(f) { |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ok := false |
|
|
|
|
ok = false |
|
|
|
|
ok = ok || applyBson(q, f) |
|
|
|
|
ok = ok || applyLimit(q, f) |
|
|
|
|
ok = ok || applySort(q, f) |
|
|
|
@ -43,7 +43,7 @@ func Push(q *Query, f interface{}) bool { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// applyBson is a fallback for a custom bson.M
|
|
|
|
|
func applyBson(q *Query, f interface{}) bool { |
|
|
|
|
func applyBson(q *Query, f interface{}) (ok bool) { |
|
|
|
|
|
|
|
|
|
if f, ok := f.(bson.M); ok { |
|
|
|
|
q.And(f) |
|
|
|
@ -54,7 +54,7 @@ func applyBson(q *Query, f interface{}) bool { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// applyLimits extends query with a limiter
|
|
|
|
|
func applyLimit(q *Query, f interface{}) bool { |
|
|
|
|
func applyLimit(q *Query, f interface{}) (ok bool) { |
|
|
|
|
|
|
|
|
|
if f, ok := f.(Limiter); ok { |
|
|
|
|
q.limiter = f |
|
|
|
@ -65,7 +65,7 @@ func applyLimit(q *Query, f interface{}) bool { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// applySort extends query with a sort rule
|
|
|
|
|
func applySort(q *Query, f interface{}) bool { |
|
|
|
|
func applySort(q *Query, f interface{}) (ok bool) { |
|
|
|
|
|
|
|
|
|
if f, ok := f.(Sorter); ok { |
|
|
|
|
q.sorter = f |
|
|
|
@ -76,7 +76,7 @@ func applySort(q *Query, f interface{}) bool { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// applySkip extends query with a skip number
|
|
|
|
|
func applySkip(q *Query, f interface{}) bool { |
|
|
|
|
func applySkip(q *Query, f interface{}) (ok bool) { |
|
|
|
|
|
|
|
|
|
if f, ok := f.(Skipper); ok { |
|
|
|
|
q.skipper = f |
|
|
|
@ -86,7 +86,7 @@ func applySkip(q *Query, f interface{}) bool { |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func applyProtection(q *Query, f interface{}) bool { |
|
|
|
|
func applyProtection(q *Query, f interface{}) (ok bool) { |
|
|
|
|
|
|
|
|
|
var x *primitive.ObjectID |
|
|
|
|
var v *int64 |
|
|
|
@ -114,7 +114,7 @@ func applyProtection(q *Query, f interface{}) bool { |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func applyPreloader(q *Query, f interface{}) bool { |
|
|
|
|
func applyPreloader(q *Query, f interface{}) (ok bool) { |
|
|
|
|
|
|
|
|
|
if f, ok := f.(Preloader); ok { |
|
|
|
|
q.preloader = f |
|
|
|
|