From 0772d8e2fe37607922ecdfba661f5dc9dac16466 Mon Sep 17 00:00:00 2001 From: kingecg Date: Thu, 19 Jun 2025 21:01:26 +0800 Subject: [PATCH] =?UTF-8?q?refactor(coperator):=20=E5=B0=86=20Filter=20?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=96=AD=E8=A8=80=E6=94=B9=E4=B8=BA=20map[st?= =?UTF-8?q?ring]interface{}=20=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改了 DocumentOperator 和 FieldOperator 函数中的 Filter 类型断言 - 将其改为 map[string]interface{} 类型,以提高灵活性和兼容性 - 更新了相关测试文件中的 Filter 类型为 map[string]interface{} --- coperator.go | 4 ++-- coperator_test.go | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/coperator.go b/coperator.go index f2136f7..ee037e0 100644 --- a/coperator.go +++ b/coperator.go @@ -97,7 +97,7 @@ func DocumentOperator(doc *Document, vfilter Filter, key string, value interface } continue } - if f, ok := v.(Filter); ok { + if f, ok := v.(map[string]interface{}); ok { ret, err = FieldOperator(doc, f, k, nil) if !ret { return ret, err @@ -133,7 +133,7 @@ func FieldOperator(doc *Document, vfilter Filter, key string, value interface{}) if !ok { return false, errors.New("unsupport operator") } - if fv, ok := v.(Filter); ok { + if fv, ok := v.(map[string]interface{}); ok { ret, err = opFn(doc, fv, key, nil) if !ret { return ret, err diff --git a/coperator_test.go b/coperator_test.go index 8c409aa..4c8ad7a 100644 --- a/coperator_test.go +++ b/coperator_test.go @@ -105,7 +105,7 @@ func TestValueOperator(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, _ := ValueOperator(&tt.doc, Filter{}, strings.Join(tt.path, "."), tt.value) + got, _ := ValueOperator(&tt.doc, map[string]interface{}{}, strings.Join(tt.path, "."), tt.value) if got != tt.want { t.Errorf("Document.GetPathArray() = %v, want %v", got, tt.want) } @@ -127,7 +127,7 @@ func TestDocumentOperator(t *testing.T) { "id": 1, }, }, - filter: Filter{ + filter: map[string]interface{}{ "user.id": 1, }, want: true, @@ -137,8 +137,8 @@ func TestDocumentOperator(t *testing.T) { doc: Document{ "age": 18, }, - filter: Filter{ - "age": Filter{ + filter: map[string]interface{}{ + "age": map[string]interface{}{ "$gt": 17, }, }, @@ -149,8 +149,8 @@ func TestDocumentOperator(t *testing.T) { doc: Document{ "age": 18, }, - filter: Filter{ - "age": Filter{ + filter: map[string]interface{}{ + "age": map[string]interface{}{ "$gt": 17, "$lt": 16, }, @@ -162,8 +162,8 @@ func TestDocumentOperator(t *testing.T) { doc: Document{ "age": 18, }, - filter: Filter{ - "age": Filter{ + filter: map[string]interface{}{ + "age": map[string]interface{}{ "$in": []interface{}{16, 17, 18}, }, }, @@ -174,8 +174,8 @@ func TestDocumentOperator(t *testing.T) { doc: Document{ "age": 22, }, - filter: Filter{ - "age": Filter{ + filter: map[string]interface{}{ + "age": map[string]interface{}{ "$nin": []interface{}{16, 17, 18}, }, },