From 3750e9ddd9ed9836c8c48e5eea997d1438370748 Mon Sep 17 00:00:00 2001 From: kingecg Date: Thu, 19 Jun 2025 20:52:17 +0800 Subject: [PATCH] =?UTF-8?q?refactor(coperator):=20=E5=B0=86=20Filter=20?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E4=BB=8E=20map=20=E4=BF=AE=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 Filter 类型从 map[string]interface{} 修改为 interface{} - 在 DocumentOperator 和 FieldOperator 函数中添加 filter 类型检查 - 修改函数参数名称以适应新的 Filter 类型 - 优化错误处理,当 filter 类型不正确时返回错误 --- coperator.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/coperator.go b/coperator.go index d37ea73..f2136f7 100644 --- a/coperator.go +++ b/coperator.go @@ -72,14 +72,17 @@ func (d *Document) GetPathArray(path []string) interface{} { } type Collection []Document -type Filter map[string]interface{} +type Filter interface{} type Operator func(doc *Document, filter Filter, key string, value interface{}) (bool, error) var operatorMap = map[string]Operator{} -func DocumentOperator(doc *Document, filter Filter, key string, value interface{}) (ret bool, err error) { - +func DocumentOperator(doc *Document, vfilter Filter, key string, value interface{}) (ret bool, err error) { + filter, ok := vfilter.(map[string]interface{}) + if !ok { + return false, errors.New("filter must be a map") + } for k, v := range filter { if strings.HasPrefix(k, "$") { @@ -117,7 +120,11 @@ func ValueOperator(doc *Document, filter Filter, key string, value interface{}) return true, nil } -func FieldOperator(doc *Document, filter Filter, key string, value interface{}) (ret bool, err error) { +func FieldOperator(doc *Document, vfilter Filter, key string, value interface{}) (ret bool, err error) { + filter, ok := vfilter.(map[string]interface{}) + if !ok { + return false, errors.New("filter must be a map") + } for k, v := range filter { if !strings.HasPrefix(k, "$") { return false, errors.New("uncorrect grammar")