expr

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

README

expr

expr 是基于 github.com/google/cel-go 再次封装的表达式解析和执行的工具,简化了 cel-go 的用法。

已实现的特性:

  • 表达式解析支持 Common Expression Language (CEL)
  • 表达式执行入参支持 Go 的基础类型或者 ProtoBuf 声明的类型
  • 表达式解析支持自定义函数

待实现的特性:

  • 表达式执行出参支持自定义解析器

用法

简单用法举例:

	expr, err := NewExpr("this.value > 60", ThisVariable())
	if err != nil {
		panic(err)
	}

	result, err := expr.Eval(map[string]any{"this": map[string]any{"value": 50}})

	if err != nil {
		panic(err)
	}

	fmt.Println("result:", result)
	// result: false

自定义函数用法举例:

expr, err := NewExpr("shake_hands(i,you)",
		Variable("i", StringType),
		Variable("you", StringType),
		Function("shake_hands",
			Overload("shake_hands_string_string", []*Type{StringType, StringType}, StringType,
				BinaryBinding(func(arg1, arg2 Val) Val {
					return String(fmt.Sprintf("%v and %v are shaking hands.\n", arg1, arg2))
				}),
			),
		))
	if err != nil {
		panic(err)
	}

	result, err := expr.Eval(map[string]any{
		"i":   "CEL",
		"you": func() Val { return String("world") },
	})
	if err != nil {
		panic(err)
	}

	fmt.Println("result:", result)
	// result: CEL and world are shaking hands.

内置的函数(macro)如下:

has

  • 用途:用于测试某个字段是否存在,避免需要将字段指定为字符串。
    • 用法举例has(m.f)
      • 如果对象 m 具有字段 f,则返回 true,否则返回 false

all

  • 用途:将 range.all(var, predicate) 转换为一个理解式,确保范围内的所有元素都满足谓词条件。
    • 用法举例range.all(x, x > 0)
      • 如果范围内的所有元素都大于 0,则返回 true

exists

  • 用途:将 range.exists(var, predicate) 转换为一个理解式,确保范围内至少有一个元素满足谓词条件。
    • 用法举例range.exists(x, x > 0)
      • 如果范围内至少有一个元素大于 0,则返回 true

existsOne

  • 用途:将 range.existsOne(var, predicate) 转换为一个表达式,确保范围内恰好有一个元素满足谓词条件。
    • 用法举例range.existsOne(x, x == 1)
      • 如果范围内恰好有一个元素等于 1,则返回 true

map

  • 用途一:将 range.map(var, function) 转换为一个理解式,对范围内的每个元素应用函数,生成一个新列表。
    • 用法举例range.map(x, x * 2)
      • 将范围内的每个元素乘以 2,并返回新列表。
  • 用途二:将 range.map(var, predicate, function) 转换为一个理解式,首先通过谓词过滤范围内的元素,然后应用转换函数生成一个新列表。
    • 用法举例range.map(x, x > 0, x * 2)
      • 过滤出大于 0 的元素,然后将这些元素乘以 2,并返回新列表。

filter

  • 用途:将 range.filter(var, predicate) 转换为一个理解式,过滤范围内的元素,生成一个满足谓词条件的新列表。
    • 用法举例range.filter(x, x > 0)
      • 返回一个新列表,其中包含范围内所有大于 0 的元素。

欢迎贡献

项目刚拉起,欢迎向 https://github.com/zhijingtech/expr 提交问题或者PR。

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEnvNil = errors.New("env is nil")

	DefaultEnv, _ = NewEnv(UseThisVariable())
)
View Source
var (
	AnyType       = cel.AnyType
	BoolType      = cel.BoolType
	BytesType     = cel.BytesType
	DoubleType    = cel.DoubleType
	DurationType  = cel.DurationType
	DynType       = cel.DynType
	IntType       = cel.IntType
	NullType      = cel.NullType
	StringType    = cel.StringType
	TimestampType = cel.TimestampType
	TypeType      = cel.TypeType
	UintType      = cel.UintType
)

Functions

func BinaryBinding added in v1.0.1

func BinaryBinding(binding BinaryOp) cel.OverloadOpt

func FunctionBinding added in v1.0.1

func FunctionBinding(binding FunctionOp) cel.OverloadOpt

func MemberOverload added in v1.0.4

func MemberOverload(overloadID string, args []*Type, resultType *Type, opts ...cel.OverloadOpt) cel.FunctionOpt

func Overload added in v1.0.1

func Overload(overloadID string, args []*Type, resultType *Type, opts ...cel.OverloadOpt) cel.FunctionOpt

func UnaryBinding added in v1.0.1

func UnaryBinding(binding UnaryOp) cel.OverloadOpt

func WrapThisVariable added in v1.0.7

func WrapThisVariable(this map[string]any) map[string]any

Types

type BinaryOp added in v1.0.1

type BinaryOp = functions.BinaryOp

type Bool added in v1.0.1

type Bool = types.Bool

type Bytes added in v1.0.1

type Bytes = types.Bytes

type Double added in v1.0.1

type Double = types.Double

type Duration added in v1.0.1

type Duration = types.Duration

type Env added in v1.0.3

type Env cel.Env

func NewEnv added in v1.0.3

func NewEnv(opts ...Option) (*Env, error)

func (*Env) Extend added in v1.0.6

func (e *Env) Extend(opts ...Option) (*Env, error)

type Error added in v1.0.1

type Error = types.Error

type Expr

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

定义一个接口,使用类型集来限制为基础类型

func NewExpr

func NewExpr(expression string, env ...*Env) (*Expr, error)

func (*Expr) Eval

func (e *Expr) Eval(input any) (any, error)

type FunctionOp added in v1.0.1

type FunctionOp = functions.FunctionOp

type Int added in v1.0.1

type Int = types.Int

type Null added in v1.0.1

type Null = types.Null

type Option

type Option = cel.EnvOption

func Function added in v1.0.1

func Function(name string, opts ...cel.FunctionOpt) Option

func Types added in v1.0.2

func Types(addTypes ...any) Option

func UseThisVariable

func UseThisVariable() Option

UseThisVariable 注册map类型的this变量,方便在表达式中操作this数据

func Variable added in v1.0.1

func Variable(name string, t *Type) Option

type String added in v1.0.1

type String = types.String

type Type

type Type = cel.Type

func ListType added in v1.0.2

func ListType(elemType *Type) *Type

func MapType added in v1.0.2

func MapType(keyType, valueType *Type) *Type

func NullableType added in v1.0.2

func NullableType(wrapped *Type) *Type

func ObjectType added in v1.0.2

func ObjectType(typeName string) *Type

type Uint added in v1.0.1

type Uint = types.Uint

type UnaryOp added in v1.0.1

type UnaryOp = functions.UnaryOp

type Unknown added in v1.0.1

type Unknown = types.Unknown

type Val added in v1.0.1

type Val = ref.Val

func NewErr added in v1.0.5

func NewErr(format string, args ...any) Val

func ValOrErr added in v1.0.5

func ValOrErr(val Val, format string, args ...any) Val

func WrapErr added in v1.0.5

func WrapErr(err error) Val

Jump to

Keyboard shortcuts

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