ruler

package module
v0.0.0-...-213a348 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

README

ruler

rule-engine inspired by grule

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TypeMismatchErr   = errors.New("type mismatch error")
	TypeNotSupportErr = errors.New("type not support error")
)
View Source
var (
	TypeMisMatch = errors.New("ruler type mismatch")
)

Functions

func Contains

func Contains(actual, expect interface{}) (bool, error)

Contains actual集合包含expect

func Endwith

func Endwith(actual, expect interface{}) (bool, error)

Endwith actual以expect结尾

func Eq

func Eq(actual, expect interface{}) (bool, error)

Eq actual和expect相等

func Exist

func Exist(actual, expect interface{}) (bool, error)

Exist actual不是nil

func Gt

func Gt(actual, expect interface{}) (bool, error)

Gt actual大于expect

func Gte

func Gte(actual, expect interface{}) (bool, error)

Gte actual大于等于expect

func JsonDecoder

func JsonDecoder(bts []byte, m *map[string]interface{}) error

func JsonPathPluck

func JsonPathPluck(props map[string]interface{}, path string, decoder DataDecoder) (interface{}, error)

JsonPathPluck 根据路径基于jsonpath解析map值

func Lt

func Lt(actual, expect interface{}) (bool, error)

Lt actual小于expect

func Lte

func Lte(actual, expect interface{}) (bool, error)

Lte actual小于等于expect

func Ncontains

func Ncontains(actual, expect interface{}) (bool, error)

Ncontains actual集合不包含expect

func Nendwith

func Nendwith(actual, expect interface{}) (bool, error)

Nendwith actual不以expect结尾

func Neq

func Neq(actual, expect interface{}) (bool, error)

Neq actual和expect不相等

func Nexist

func Nexist(actual, expect interface{}) (bool, error)

Nexist actual是nil

func NoneOf

func NoneOf(actual, expect interface{}) (bool, error)

NoneOf actual不在expect集合中

func Nregex

func Nregex(actual, expect interface{}) (bool, error)

Nregex actual不符合expect的正则匹配

func Nstartwith

func Nstartwith(actual, expect interface{}) (bool, error)

Nstartwith actual不以expect开头

func OneOf

func OneOf(actual, expect interface{}) (bool, error)

OneOf actual在expect集合中

func PathPluck

func PathPluck(props map[string]interface{}, path string, decoder DataDecoder) (interface{}, error)

PathPluck 根据路径解析map值

func Regex

func Regex(actual, expect interface{}) (bool, error)

Regex actual符合expect的正则匹配

func Startwith

func Startwith(actual, expect interface{}) (bool, error)

Startwith actual以expect开头

func XmlDecoder

func XmlDecoder(bts []byte, m *map[string]interface{}) error

func YamlDecoder

func YamlDecoder(bts []byte, m *map[string]interface{}) error

Types

type Comparator

type Comparator func(actual, expected interface{}) (bool, error)

Comparator 是比较器 目前实现了一些基本的比较器 可以根据需求来实现相应的比较器函数传入Rule

type DataDecoder

type DataDecoder func(bts []byte, m *map[string]interface{}) error

DataDecoder 数据解析

type Group

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

Group 规则集合

type Pluck

type Pluck func(props map[string]interface{}, path string, decoder DataDecoder) (interface{}, error)

Pluck 根据路径嵌套解析map值

type Result

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

Result Rule校验结果

func (*Result) JsonString

func (r *Result) JsonString() string

JsonString Rule校验结果生成Json类型字符串

type Rule

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

Rule 规则 comparator 是比较器, 用于比较两个值是否相等 path 基于Map的取值路径 value 期待值 pluck 取值方式

func NewRule

func NewRule(path, t string, value interface{}) *Rule

NewRule 根据条件创建Rule

func NewRuleByOptions

func NewRuleByOptions(path string, value interface{}, comparator Comparator, pluck Pluck) *Rule

NewRuleByOptions 自定义Rule

func (*Rule) Compare

func (r *Rule) Compare(actual interface{}) bool

Compare 规则比较

func (*Rule) CompareReturnResult

func (r *Rule) CompareReturnResult(actual interface{}) (bool, error)

CompareReturnResult 比较结果, 并返回error

type Ruler

type Ruler interface {
	AddRule(rule *Rule) Ruler
	Validate(msg map[string]interface{}) bool
	ValidateWithResult(msg map[string]interface{}) (map[string]*Result, bool)
}

Ruler 这个接口用来规定一组RuleSet以哪种规则匹配

func NewDefaultRuler

func NewDefaultRuler(rules []*Rule, t string, decoder DataDecoder) (Ruler, error)

NewDefaultRuler 创建基础Ruler

Jump to

Keyboard shortcuts

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