gofilter

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2024 License: MIT Imports: 7 Imported by: 0

README

GoFilter

Description

A data filter based on the go struct data type

  • Supports logical operation data filtering based on struct data types.

  • Such as IN / NIN

      //in  (contains)                包含
      //nin (not contains)            不包含
      //lt  (less than)               小于
      //le  (less than or equal to)   小于等于
      //eq  (equal to)                等于
      //ne  (not equal to)            不等于
      //ge  (greater than or equal to)大于等于
      //gt  (greater than)            大于
      // in 和 nin、eq、ne 用于string
      // 整形和浮点型可以使用全部
    

Exp

func TestSelfDataRule(t *testing.T) {
	dat := []struct {
		ID     int
		Msg    string
		Tag    string
		Origin string
	}{
		{
			ID:     1,
			Msg:    "hello-1",
			Tag:    "AAA",
			Origin: "a",
		},
		{
			ID:     2,
			Msg:    "hello-2",
			Tag:    "AA",
			Origin: "b",
		},
		{
			ID:     3,
			Msg:    "hello-3",
			Tag:    "AAA",
			Origin: "b",
		},
		{
			ID:     4,
			Msg:    "hello-4",
			Tag:    "AAA",
			Origin: "c",
		},
	}

	ruleEmpty := Rule{
		RType: OR,
		RRules: []RulePool{
			{
				CName:   "F1", // 注意,同一个规则内部不能重名,否则会被覆盖
				CParams: []any{1},
				CType:   "ID",
				CSymbol: gofilter.GT,
			},
			{
				CName:   "F2",
				CParams: []any{"AAA", "AA"},
				CType:   "Tag",
				CSymbol: gofilter.IN,
			},
		},
	}

	rl, err := NewFilter("test_rule_self", ruleEmpty)
	if err != nil {
		t.Error(err)
		return
	}

	for _, item := range dat {
		flag, err := rl.Exec(item)
		if err != nil {
			t.Error(err)
			continue
		}
		if flag {
			t.Logf("-->msg: {%v, %v, %v,%v}", item.ID, item.Tag, item.Origin, item.Msg)
		}
	}
}

Documentation

Index

Constants

View Source
const (
	AND = 0
	OR  = 1
)
View Source
const (
	IN  = iota
	NIN // 和in相反
	GT
	LT
	EQ
	NE
	LE
	GE
)

Variables

This section is empty.

Functions

func AnyEqual

func AnyEqual[T comparable](arr []T, item T) (bool, error)

func AnyFind

func AnyFind[T comparable](arr []T, item T) bool

func AnyGreaterThan

func AnyGreaterThan[T comparable](arr []T, item T) (bool, error)

func AnyLessThan

func AnyLessThan[T comparable](arr []T, item T) (bool, error)

func GetDataField

func GetDataField(data any, ctype string) (string, any)

GetDataField 通过反射获取结构体字段名称, data类型必须是struct

func GetFnMaps

func GetFnMaps(item any, fs map[string]RulePool) map[string]interface{}

GetFnMaps 重要,处理规则和数据的关联部分

Types

type Filter

type Filter struct {
	Rule Rule `json:"rules,omitempty"`
	// contains filtered or unexported fields
}

func NewFilter added in v1.0.2

func NewFilter(tag string, rule Rule) (*Filter, error)

func (*Filter) Exec added in v1.0.1

func (f *Filter) Exec(data any) (bool, error)

func (*Filter) ExecWithSlice added in v1.0.4

func (f *Filter) ExecWithSlice(data any) (any, error)

func (*Filter) Tag added in v1.0.1

func (f *Filter) Tag() string

type FilterFunc added in v1.0.1

type FilterFunc func(option any, data any) bool // 根据过滤

type GValuate

type GValuate struct {
	Name string

	Valuate *govaluate.EvaluableExpression
	// contains filtered or unexported fields
}

func NewGValuate

func NewGValuate(name string) *GValuate

func (*GValuate) Eval

func (gv *GValuate) Eval(exp string) error

func (*GValuate) Exec

func (gv *GValuate) Exec(parameters map[string]interface{}) (bool, error)

type Rule

type Rule struct {
	RSign  int        `json:"sign,omitempty"`   // 用于RRules和RChild的逻辑运算符
	RType  int        `json:"rtype,omitempty"`  // 用于RChild之间的逻辑运算符
	RChild *Rule      `json:"rchild,omitempty"` // children级别rules []Rule不好在rule之间逻辑运算
	RRules []RulePool `json:"rrules,omitempty"` // 本级rules
}

type RulePool

type RulePool struct {
	CName   string // 函数标识
	CParams []any  // 参数
	CType   string // 字段名称
	CSymbol int    // 逻辑运算符号(包含、大于、小于、不等于、等于等)
}

type Stack

type Stack struct {
	// contains filtered or unexported fields
}

func NewStack

func NewStack() *Stack

func (*Stack) IsEmpty

func (s *Stack) IsEmpty() bool

func (*Stack) Pop

func (s *Stack) Pop() (interface{}, error)

func (*Stack) Push

func (s *Stack) Push(v interface{})

func (*Stack) ToExpress

func (s *Stack) ToExpress() string

Jump to

Keyboard shortcuts

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