https://github.com/seeadoog/json_script
json script, json-rule-engine
https://github.com/seeadoog/json_script
Last synced: 3 months ago
JSON representation
json script, json-rule-engine
- Host: GitHub
- URL: https://github.com/seeadoog/json_script
- Owner: seeadoog
- Created: 2019-08-15T12:39:10.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-31T11:31:19.000Z (about 5 years ago)
- Last Synced: 2025-01-28T22:26:46.073Z (4 months ago)
- Language: Go
- Homepage:
- Size: 174 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
#### a json script for go
#### use
install
````
go get https://github.com/seeadoog/json_script
````````
rule:=[]byte(`
[
{
"if": "lt(user.age,15)",
"then": "user.generation='yong'"
},
{
"if": "lt(user.age,30)",
"then": [
"user.generation='old'",
"user.hasChild=true"
]
},
{
"for":"k,v in user",
"do":"print('k==',k,'v==',v)"
},
{
"func":"show(u)",
"do":"printf('name=%s,age=%v,generation=%s',u.name,u.age,u.generation)"
},
"show(user)"
]
`)
scp,err:=jsonscpt.CompileExpFromJson(rule)
if err !=nil{
panic(err)
}
vm:=jsonscpt.NewVm()
user :=map[string]interface{}{
"name":"bob",
"age":16,
}
vm.Set("user", user)
err =vm.SafeExecute(scp,nil)
if err !=nil{
panic(err)
}
fmt.Println("userMap:",user)
````#### inline functions
funs|use
---|---
append(str...)string|拼接字符串
split(str,sep,n)[]string|按照给定的切割符号 sep切分字符串
len(str)int|求字符串的长度
sprintf(str,obj...)string|obj...|格式化字符串
add(nubmer ...)number|number array|数字求和
isnil(object)bool|判断对象是否为空
and(bool...)bool|对一系列的bool变量做 and操作 相当于 && ,任何变量都可以视为bool值
or(bool ...)bool|对一系列的bool变量做 or 操作 相当于 '\\' ,任何变量都可以视为bool值
eq(objectA,objectB)bool|判断两者转换为字符串后是否相等
gt(numberA,numberB)bool| 大于 > ,numberA>numberB 返回true,否则返回false
ge(numberA,numberB)bool| 大于等于 >= ,numberA>=numberB 返回true,否则返回false
lt(numberA,numberB)bool| 小于 < ,numberA0 为true <=0 为false
bool 值:bool值5. 赋值
//值得类型有三种,bool,string,number
````
[
"va=123", //给va 赋值为 number 类型
"vstr='hello world'" // 给vstr赋值为 string类型
"vb=true" // 给vb赋值为bool类型,值为true
"param.a='hello'" // 给param map中的a赋值string = 'hello'
]````