|
|
@ -16,9 +16,9 @@ func Compose(filters ...interface{}) (query *Query) { |
|
|
|
|
|
|
|
|
|
|
|
query = &Query{} |
|
|
|
query = &Query{} |
|
|
|
|
|
|
|
|
|
|
|
for _, f := range filters { |
|
|
|
for _, filter := range filters { |
|
|
|
if !Push(query, f) { |
|
|
|
if !Push(query, filter) { |
|
|
|
panic(fmt.Errorf("unknown filter %v", f)) |
|
|
|
panic(fmt.Errorf("unknown filter %v", filter)) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -26,10 +26,12 @@ func Compose(filters ...interface{}) (query *Query) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Push applies single filter to a query
|
|
|
|
// Push applies single filter to a query
|
|
|
|
func Push(q *Query, f interface{}) (ok bool) { |
|
|
|
func Push(query *Query, filter interface{}) (ok bool) { |
|
|
|
|
|
|
|
|
|
|
|
if reflect2.IsNil(f) { |
|
|
|
ok = reflect2.IsNil(filter) |
|
|
|
return true |
|
|
|
if ok { |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for _, applier := range []applyFilterFunc{ |
|
|
|
for _, applier := range []applyFilterFunc{ |
|
|
@ -42,17 +44,17 @@ func Push(q *Query, f interface{}) (ok bool) { |
|
|
|
applyUpdater, |
|
|
|
applyUpdater, |
|
|
|
applyCallbacks, |
|
|
|
applyCallbacks, |
|
|
|
} { |
|
|
|
} { |
|
|
|
ok = applier(q, f) || ok |
|
|
|
ok = applier(query, filter) || ok |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// applyBson is a fallback for a custom primitive.M
|
|
|
|
// applyBson is a fallback for a custom primitive.M
|
|
|
|
func applyBson(q *Query, f interface{}) (ok bool) { |
|
|
|
func applyBson(query *Query, filter interface{}) (ok bool) { |
|
|
|
|
|
|
|
|
|
|
|
if f, ok := f.(primitive.M); ok { |
|
|
|
if filter, ok := filter.(primitive.M); ok { |
|
|
|
q.And(f) |
|
|
|
query.And(filter) |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -60,10 +62,10 @@ func applyBson(q *Query, f interface{}) (ok bool) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// applyLimits extends query with a limiter
|
|
|
|
// applyLimits extends query with a limiter
|
|
|
|
func applyLimit(q *Query, f interface{}) (ok bool) { |
|
|
|
func applyLimit(query *Query, filter interface{}) (ok bool) { |
|
|
|
|
|
|
|
|
|
|
|
if f, ok := f.(Limiter); ok { |
|
|
|
if filter, ok := filter.(Limiter); ok { |
|
|
|
q.limiter = f |
|
|
|
query.limiter = filter |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -71,10 +73,10 @@ func applyLimit(q *Query, f interface{}) (ok bool) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// applySort extends query with a sort rule
|
|
|
|
// applySort extends query with a sort rule
|
|
|
|
func applySort(q *Query, f interface{}) (ok bool) { |
|
|
|
func applySort(query *Query, filter interface{}) (ok bool) { |
|
|
|
|
|
|
|
|
|
|
|
if f, ok := f.(Sorter); ok { |
|
|
|
if filter, ok := filter.(Sorter); ok { |
|
|
|
q.sorter = f |
|
|
|
query.sorter = filter |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -82,76 +84,74 @@ func applySort(q *Query, f interface{}) (ok bool) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// applySkip extends query with a skip number
|
|
|
|
// applySkip extends query with a skip number
|
|
|
|
func applySkip(q *Query, f interface{}) (ok bool) { |
|
|
|
func applySkip(query *Query, filter interface{}) (ok bool) { |
|
|
|
|
|
|
|
|
|
|
|
if f, ok := f.(Skipper); ok { |
|
|
|
if filter, ok := filter.(Skipper); ok { |
|
|
|
q.skipper = f |
|
|
|
query.skipper = filter |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func applyProtection(q *Query, f interface{}) (ok bool) { |
|
|
|
func applyProtection(query *Query, filter interface{}) (ok bool) { |
|
|
|
|
|
|
|
|
|
|
|
var x *primitive.ObjectID |
|
|
|
var x *primitive.ObjectID |
|
|
|
var v *int64 |
|
|
|
var v *int64 |
|
|
|
|
|
|
|
|
|
|
|
switch f := f.(type) { |
|
|
|
switch filter := filter.(type) { |
|
|
|
case protection.Key: |
|
|
|
case protection.Key: |
|
|
|
x = &f.X |
|
|
|
x = &filter.X |
|
|
|
v = &f.V |
|
|
|
v = &filter.V |
|
|
|
case *protection.Key: |
|
|
|
case *protection.Key: |
|
|
|
x = &f.X |
|
|
|
x = &filter.X |
|
|
|
v = &f.V |
|
|
|
v = &filter.V |
|
|
|
|
|
|
|
|
|
|
|
default: |
|
|
|
default: |
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if x.IsZero() { |
|
|
|
if x.IsZero() { |
|
|
|
q.And(primitive.M{"_x": primitive.M{"$exists": false}}) |
|
|
|
query.And(primitive.M{"_x": primitive.M{"$exists": false}}) |
|
|
|
q.And(primitive.M{"_v": primitive.M{"$exists": false}}) |
|
|
|
query.And(primitive.M{"_v": primitive.M{"$exists": false}}) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
q.And(primitive.M{"_x": *x}) |
|
|
|
query.And(primitive.M{"_x": *x}) |
|
|
|
q.And(primitive.M{"_v": *v}) |
|
|
|
query.And(primitive.M{"_v": *v}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func applyPreloader(q *Query, f interface{}) (ok bool) { |
|
|
|
func applyPreloader(query *Query, filter interface{}) (ok bool) { |
|
|
|
|
|
|
|
|
|
|
|
if f, ok := f.(Preloader); ok { |
|
|
|
if filter, ok := filter.(Preloader); ok { |
|
|
|
q.preloader = f |
|
|
|
query.preloader = filter |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func applyUpdater(q *Query, f interface{}) (ok bool) { |
|
|
|
func applyUpdater(query *Query, filter interface{}) (ok bool) { |
|
|
|
|
|
|
|
|
|
|
|
if f, ok := f.(Updater); ok { |
|
|
|
if filter, ok := filter.(Updater); ok { |
|
|
|
q.updater = f |
|
|
|
query.updater = filter |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func applyCallbacks(q *Query, f interface{}) (ok bool) { |
|
|
|
func applyCallbacks(query *Query, filter interface{}) (ok bool) { |
|
|
|
|
|
|
|
|
|
|
|
switch cb := f.(type) { |
|
|
|
switch callback := filter.(type) { |
|
|
|
case OnDecode: |
|
|
|
case OnDecode: |
|
|
|
q.ondecode = append(q.ondecode, Callback(cb)) |
|
|
|
query.ondecode = append(query.ondecode, Callback(callback)) |
|
|
|
case OnClose: |
|
|
|
case OnClose: |
|
|
|
q.onclose = append(q.onclose, Callback(cb)) |
|
|
|
query.onclose = append(query.onclose, Callback(callback)) |
|
|
|
default: |
|
|
|
default: |
|
|
|
return |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ok = true |
|
|
|
return true |
|
|
|
|
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|