refactor(coperator): 将 Filter 类型断言改为 map[string]interface{} 类型

- 修改了 DocumentOperator 和 FieldOperator 函数中的 Filter 类型断言
- 将其改为 map[string]interface{} 类型,以提高灵活性和兼容性
- 更新了相关测试文件中的 Filter 类型为 map[string]interface{}
This commit is contained in:
kingecg 2025-06-19 21:01:26 +08:00
parent 3750e9ddd9
commit 0772d8e2fe
2 changed files with 12 additions and 12 deletions

View File

@ -97,7 +97,7 @@ func DocumentOperator(doc *Document, vfilter Filter, key string, value interface
} }
continue continue
} }
if f, ok := v.(Filter); ok { if f, ok := v.(map[string]interface{}); ok {
ret, err = FieldOperator(doc, f, k, nil) ret, err = FieldOperator(doc, f, k, nil)
if !ret { if !ret {
return ret, err return ret, err
@ -133,7 +133,7 @@ func FieldOperator(doc *Document, vfilter Filter, key string, value interface{})
if !ok { if !ok {
return false, errors.New("unsupport operator") 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) ret, err = opFn(doc, fv, key, nil)
if !ret { if !ret {
return ret, err return ret, err

View File

@ -105,7 +105,7 @@ func TestValueOperator(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { 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 { if got != tt.want {
t.Errorf("Document.GetPathArray() = %v, want %v", got, tt.want) t.Errorf("Document.GetPathArray() = %v, want %v", got, tt.want)
} }
@ -127,7 +127,7 @@ func TestDocumentOperator(t *testing.T) {
"id": 1, "id": 1,
}, },
}, },
filter: Filter{ filter: map[string]interface{}{
"user.id": 1, "user.id": 1,
}, },
want: true, want: true,
@ -137,8 +137,8 @@ func TestDocumentOperator(t *testing.T) {
doc: Document{ doc: Document{
"age": 18, "age": 18,
}, },
filter: Filter{ filter: map[string]interface{}{
"age": Filter{ "age": map[string]interface{}{
"$gt": 17, "$gt": 17,
}, },
}, },
@ -149,8 +149,8 @@ func TestDocumentOperator(t *testing.T) {
doc: Document{ doc: Document{
"age": 18, "age": 18,
}, },
filter: Filter{ filter: map[string]interface{}{
"age": Filter{ "age": map[string]interface{}{
"$gt": 17, "$gt": 17,
"$lt": 16, "$lt": 16,
}, },
@ -162,8 +162,8 @@ func TestDocumentOperator(t *testing.T) {
doc: Document{ doc: Document{
"age": 18, "age": 18,
}, },
filter: Filter{ filter: map[string]interface{}{
"age": Filter{ "age": map[string]interface{}{
"$in": []interface{}{16, 17, 18}, "$in": []interface{}{16, 17, 18},
}, },
}, },
@ -174,8 +174,8 @@ func TestDocumentOperator(t *testing.T) {
doc: Document{ doc: Document{
"age": 22, "age": 22,
}, },
filter: Filter{ filter: map[string]interface{}{
"age": Filter{ "age": map[string]interface{}{
"$nin": []interface{}{16, 17, 18}, "$nin": []interface{}{16, 17, 18},
}, },
}, },