From 4257e7a3d6263cd36756e64b3c6ce3fffe060e47 Mon Sep 17 00:00:00 2001 From: kingecg Date: Thu, 19 Jun 2025 21:08:13 +0800 Subject: [PATCH] =?UTF-8?q?feat(coperator):=20=E6=B7=BB=E5=8A=A0=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E8=BF=90=E7=AE=97=E7=AC=A6=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 operatorMap 中添加 $and、$or 和 $nor 运算符 - 新增 logic.go 文件实现逻辑运算符的功能 - operatorAnd: 所有子过滤器都必须匹配 - operatorOr: 任意一个子过滤器匹配即可 - operatorNor: 所有子过滤器都不匹配 --- coperator.go | 3 +++ logic.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 logic.go diff --git a/coperator.go b/coperator.go index ee037e0..3b6cc5d 100644 --- a/coperator.go +++ b/coperator.go @@ -159,4 +159,7 @@ func init() { operatorMap["$ne"] = operatorNe operatorMap["$in"] = operatorIn operatorMap["$nin"] = operatorNotIn + operatorMap["$and"] = operatorAnd + operatorMap["$or"] = operatorOr + operatorMap["$nor"] = operatorNor } diff --git a/logic.go b/logic.go new file mode 100644 index 0000000..20751c4 --- /dev/null +++ b/logic.go @@ -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 +}