feat(coperator): 添加逻辑运算符支持
- 在 operatorMap 中添加 $and、$or 和 $nor 运算符 - 新增 logic.go 文件实现逻辑运算符的功能 - operatorAnd: 所有子过滤器都必须匹配 - operatorOr: 任意一个子过滤器匹配即可 - operatorNor: 所有子过滤器都不匹配
This commit is contained in:
parent
0772d8e2fe
commit
4257e7a3d6
|
@ -159,4 +159,7 @@ func init() {
|
||||||
operatorMap["$ne"] = operatorNe
|
operatorMap["$ne"] = operatorNe
|
||||||
operatorMap["$in"] = operatorIn
|
operatorMap["$in"] = operatorIn
|
||||||
operatorMap["$nin"] = operatorNotIn
|
operatorMap["$nin"] = operatorNotIn
|
||||||
|
operatorMap["$and"] = operatorAnd
|
||||||
|
operatorMap["$or"] = operatorOr
|
||||||
|
operatorMap["$nor"] = operatorNor
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
package coperator
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
func operatorAnd(doc *Document, filter Filter, key string, value interface{}) (bool, error) {
|
||||||
|
vfilter, ok := filter.([]interface{})
|
||||||
|
if !ok {
|
||||||
|
return false, errors.New("filter must be a slice")
|
||||||
|
}
|
||||||
|
for _, v := range vfilter {
|
||||||
|
ret, err := DocumentOperator(doc, v.(Filter), "", nil)
|
||||||
|
if !ret {
|
||||||
|
return ret, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func operatorOr(doc *Document, filter Filter, key string, value interface{}) (bool, error) {
|
||||||
|
vfilter, ok := filter.([]interface{})
|
||||||
|
if !ok {
|
||||||
|
return false, errors.New("filter must be a slice")
|
||||||
|
}
|
||||||
|
for _, v := range vfilter {
|
||||||
|
ret, err := DocumentOperator(doc, v.(Filter), "", nil)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if ret {
|
||||||
|
return ret, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func operatorNor(doc *Document, filter Filter, key string, value interface{}) (bool, error) {
|
||||||
|
vfilter, ok := filter.([]interface{})
|
||||||
|
if !ok {
|
||||||
|
return false, errors.New("filter must be a slice")
|
||||||
|
}
|
||||||
|
for _, v := range vfilter {
|
||||||
|
ret, err := DocumentOperator(doc, v.(Filter), "", nil)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if ret {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
Loading…
Reference in New Issue