refactor(coperator): 将 Filter 类型从 map 修改为接口

- 将 Filter 类型从 map[string]interface{} 修改为 interface{}
- 在 DocumentOperator 和 FieldOperator 函数中添加 filter 类型检查
- 修改函数参数名称以适应新的 Filter 类型
- 优化错误处理,当 filter 类型不正确时返回错误
This commit is contained in:
kingecg 2025-06-19 20:52:17 +08:00
parent 32250ddaaa
commit 3750e9ddd9
1 changed files with 11 additions and 4 deletions

View File

@ -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")