https://github.com/gitcfly/goexpr
golang 表达式引擎,规则引擎,支持自定义函数,自定义中缀操作符,自定义前缀操作符,支持传参以及参数层级嵌套,支持数组,字符串,数字,变量等操作,Golang expression engine, rule engine, support custom functions, custom infix operators, custom prefix operators, support parameter passing and parameter hierarchical nesting, support arrays, strings, numbers, variables and other operations
https://github.com/gitcfly/goexpr
engine expression rule
Last synced: 5 months ago
JSON representation
golang 表达式引擎,规则引擎,支持自定义函数,自定义中缀操作符,自定义前缀操作符,支持传参以及参数层级嵌套,支持数组,字符串,数字,变量等操作,Golang expression engine, rule engine, support custom functions, custom infix operators, custom prefix operators, support parameter passing and parameter hierarchical nesting, support arrays, strings, numbers, variables and other operations
- Host: GitHub
- URL: https://github.com/gitcfly/goexpr
- Owner: gitcfly
- Created: 2021-07-04T17:05:48.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-07-07T14:09:02.000Z (almost 5 years ago)
- Last Synced: 2024-06-20T11:57:09.108Z (almost 2 years ago)
- Topics: engine, expression, rule
- Language: Go
- Homepage:
- Size: 40 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# goexpr
golang 表达式引擎,规则引擎,支持自定义函数,自定义中缀操作符,自定义前缀操作符,支持传参以及参数层级嵌套,支持数组,字符串,数字,变量等操作
Golang expression engine, rule engine, support custom functions, custom infix operators, custom prefix operators, support parameter passing and parameter hierarchical nesting, support arrays, strings, numbers, variables and other operations
```
import (
"fmt"
"reflect"
"testing"
)
func Contain(a, b interface{}) interface{} {
bStr := fmt.Sprint(b)
array := reflect.ValueOf(a)
length := array.Len()
for i := 0; i < length; i++ {
aStr := fmt.Sprint(array.Index(i).Interface())
if bStr == aStr {
return true
}
}
return false
}
func TestEngine(t *testing.T) {
exprs := `-4+3>(-9)&&5<4+5&&3NotIN[1,2,4]&&ADD(1,2)<4&&-(#-3-4)<=30&&4>1&&[1,2,4] Contain 4 && ADD(1,2)!=1 && user.name=='kiteee' && user_count>20`
//exprs = `-------1`
//exprs=`user.name=='kiteee' && user_count>20`
//exprs=`user_count>20 && user_count>20`
//exprs=`#--3*-4-#2`
//exprs=`-4-#2`
//exprs = `3NotIN([1,2,3])&&ADD(1,2)<4`
//exprs = `[1,2,4] Contain 4 IN [true] NotIN [false]`
eg := NewEngine()
eg.AddFunc("ADD", func(v ...interface{}) interface{} {
return floatVal(v[0]) + floatVal(v[1])
})
eg.AddPrefix("#", func(v interface{}) interface{} {
return floatVal(v) * floatVal(v)
})
eg.AddInfix("Contain", 30, func(v1, v2 interface{}) interface{} {
return Contain(v1, v2)
})
var params = map[string]interface{}{
"user": map[string]interface{}{
"name": "kiteee",
"age": 50,
},
"user_count": 30,
}
//eg.SetPriority("NotIN", 30)
result := eg.Execute(exprs, params)
fmt.Println(result)
}
func TestSpitExpr(t *testing.T) {
exprs := `('ssss','aaa',['aaa',bb],[aaa,game notIn values],funa bsna ( sa,ssdf), atype contains (90),type notIn [1,2,4],value >= images ,-add(),[name,add()],-otEs(),[[-aaam,bb],bbb])`
exprs = `()`
result := SpitExpr(exprs)
for _, v := range result {
fmt.Println(v)
}
}
func TestGetArgs(t *testing.T) {
var mp = map[string]interface{}{
"user": map[string]interface{}{
"name": "kiteee",
"age": 50,
},
}
va := GetArg("user.name", mp)
fmt.Println(va)
}
```