expression4go

package module
v1.0.1 Latest Latest
Warning

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

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

README

expression4go

a expression launage toolkit for golang like as expression4j

license

Introduce

This project is come from an unmaintained fork, the branch can see https://github.com/heartlhj/go-expression

  • left only so it doesn't break imports.
  • some enhancement if to need

Feature

This project's idea is come from expression4j, and support a expression lanugage toolkit for golang.

  • Support system output like stdout, stderr and so on

Usage

  • Add expression4go with the following import
import "github.com/gohutool/expression4go"
  • Support standard expression
var (
	context = StandardEvaluationContext{}
	m       = make(map[string]interface{})
	parser  = SpelExpressionParser{}
)

	context.AddPropertyAccessor(MapAccessor{})
	m["name"] = "expression4go"
	m["age"] = 1

	//
	//map1 := make(map[string]interface{})
	//m["map"] = map1
	//map1["name"] = "davidliu"
	//map1["age"] = 10

	context.SetVariables(m)
	parser := SpelExpressionParser{}
	expressionString := "#{name}"
	//expressionString := "#name=='lisi'"
	//expressionString := "#name" //返回lisi
	valueContext := parser.ParseExpression(expressionString).GetValueContext(&context)

	fmt.Println(valueContext)
  • Support Map Object expression
var (
	context = StandardEvaluationContext{}
	m       = make(map[string]interface{})
	parser  = SpelExpressionParser{}
)

	context.AddPropertyAccessor(MapAccessor{})
	m["name"] = "expression4go"
	m["age"] = 1
	m1 := make(map[string]interface{})
	m2 := make(map[string]interface{})
	m2["num"] = 12
	m1["code"] = m2
	m["order"] = m1
	context.SetVariables(m)
	expressionString := "${order.code.num}"
	valueContext := parser.ParseExpression(expressionString).GetValueContext(&context)
	fmt.Println("结果为", valueContext)
  • Support Struct Object expression
var (
	context = StandardEvaluationContext{}
	m       = make(map[string]interface{})
	parser  = SpelExpressionParser{}
)

    context.AddPropertyAccessor(MapAccessor{})
	m1 := make(map[string]interface{})
	m["name"] = "expression4go"
	m["age"] = 1
	//切片
	//orders := make([]Order, 2)
	//数组
	orders := [2]Order{}
	orders[0] = Order{name: "expression4go-1", num: 12}
	orders[1] = Order{name: "expression4go-2", num: 24}
	m1["code"] = orders
	m["order"] = m1
	context.SetVariables(m)
	expressionString := "${order.code[0].name}"
	valueContext := parser.ParseExpression(expressionString).GetValueContext(&context)
	fmt.Println("结果为", valueContext)
  • Support Slice Object expression
var (
	context = StandardEvaluationContext{}
	m       = make(map[string]interface{})
	parser  = SpelExpressionParser{}
)

    context.AddPropertyAccessor(MapAccessor{})
	m1 := make(map[string]interface{})
	m["name"] = "expression4go"
	m["age"] = 1
	//切片
	//orders := make([]Order, 2)
	//数组
	orders := [2]Order{}
	orders[0] = Order{name: "expression4go-1", num: 12}
	orders[1] = Order{name: "expression4go-2", num: 24}
	m1["code"] = orders
	m["order"] = m1
	context.SetVariables(m)
	expressionString := "${order.code[0].name}"
	valueContext := parser.ParseExpression(expressionString).GetValueContext(&context)
	fmt.Println("结果为", valueContext)

Documentation

Index

Constants

View Source
const (
	L4G_VERSION = "expression4go-v1.0.0"
	L4G_MAJOR   = 1
	L4G_MINOR   = 0
	L4G_BUILD   = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Bracket

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

type CompositeStringExpression

type CompositeStringExpression struct {
	*spel.ExpressionImpl
	ExpressionString string
	Expressions      []Expression
}

func (*CompositeStringExpression) GetExpressionString

func (c *CompositeStringExpression) GetExpressionString() string

func (*CompositeStringExpression) GetValue

func (c *CompositeStringExpression) GetValue() interface{}

type Expression

type Expression interface {
	GetExpressionString() string

	GetValue() interface{}

	GetValueContext(context spel.EvaluationContext) interface{}
}

获取值

type ExpressionErr

type ExpressionErr struct {
	Code string
	Msg  string
}

func (ExpressionErr) Error

func (e ExpressionErr) Error() string

type ExpressionParser

type ExpressionParser interface {
	ParseExpression(var1 string) Expression

	DoParseExpression(var1 string) Expression
}

type InternalSpelExpressionParser

type InternalSpelExpressionParser struct {
	*TemplateAwareExpressionParser

	Configuration SpelParserConfiguration
	// contains filtered or unexported fields
}

func (*InternalSpelExpressionParser) DoParseExpression

func (i *InternalSpelExpressionParser) DoParseExpression(expressionString string) Expression

type LiteralExpression

type LiteralExpression struct {
	*spel.ExpressionImpl
	// contains filtered or unexported fields
}

func (*LiteralExpression) GetExpressionString

func (l *LiteralExpression) GetExpressionString() string

func (*LiteralExpression) GetValue

func (l *LiteralExpression) GetValue() interface{}

type ParserContext

type ParserContext interface {
	// contains filtered or unexported methods
}

type SpelExpressionParser

type SpelExpressionParser struct {
	*TemplateAwareExpressionParser
}

type TemplateAwareExpressionParser

type TemplateAwareExpressionParser struct {
	Bracket
}

func (*TemplateAwareExpressionParser) DoParseExpression

func (s *TemplateAwareExpressionParser) DoParseExpression(expressionString string) Expression

func (*TemplateAwareExpressionParser) ParseExpression

func (t *TemplateAwareExpressionParser) ParseExpression(expressionString string) Expression

type TemplateParserContext

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

type Value

type Value interface {
	// contains filtered or unexported methods
}

Directories

Path Synopsis
ast

Jump to

Keyboard shortcuts

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