coperator

package module
v0.0.0-...-4b4b4ad Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2025 License: MIT Imports: 4 Imported by: 0

README

COperator

实现MongoDB风格查询操作符的文档过滤库,提供灵活的条件匹配功能

安装

# 确保已安装Go环境(1.23.1+)
go get git.pyer.club/kingecg/coperator

核心功能

  • Document过滤:通过DocumentOperator检查文档是否满足过滤条件
  • 支持操作符$gt$ge$lt$le$eq$ne$in$nin$and$or$nor
  • 嵌套查询:支持嵌套文档和数组的路径访问(.分隔路径)
  • 链式条件:支持$and$or等组合条件查询

使用示例

package main

import (
	"fmt"
	"git.pyer.club/kingecg/coperator"
)

func main() {
	// 创建测试文档
	doc := &coperator.Document{
		"name": "Alice",
		"age": 30,
		"address": map[string]interface{}{
			"city": "Beijing",
			"zip": 100000,
		},
	}

	// 定义过滤条件
	filter := coperator.Filter{
		"age": map[string]interface{}{
			"$gt": 25,
		},
		"address.city": "Beijing",
	}

	// 执行查询
	result, err := coperator.DocumentOperator(doc, filter, "", nil)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	fmt.Printf("Document matches filter: %v\n", result) // 输出: Document matches filter: true
}

测试

# 运行单元测试
go test -v ./...

贡献

欢迎提交PR或Issue

  1. Fork仓库
  2. 创建新分支
  3. 提交代码
  4. 创建Pull Request

许可证

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DocumentOperator

func DocumentOperator(doc *Document, vfilter Filter, key string, value interface{}) (ret bool, err error)

func FieldOperator

func FieldOperator(doc *Document, vfilter Filter, key string, value interface{}) (ret bool, err error)

func ValueOperator

func ValueOperator(doc *Document, filter Filter, key string, value interface{}) (bool, error)

Types

type Array

type Array []interface{}

func (*Array) Get

func (a *Array) Get(path string) interface{}

func (*Array) GetPathArray

func (a *Array) GetPathArray(path []string) interface{}

type Collection

type Collection []Document

type Document

type Document map[string]interface{}

func (*Document) Get

func (d *Document) Get(path string) interface{}

func (*Document) GetPathArray

func (d *Document) GetPathArray(path []string) interface{}

type Filter

type Filter interface{}

type Object

type Object interface {
	Get(path string) interface{}
	GetPathArray(path []string) interface{}
}

type Operator

type Operator func(doc *Document, filter Filter, key string, value interface{}) (bool, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL