A tiny DSL to evaluate expressions which is used inside of APISIX.
This project has been working in microservices API gateway Apache APISIX.
The project is open sourced by Shenzhen ZhiLiu Technology Co., Ltd.
In addition to this open source version, our company also provides a more powerful and performing commercial version, and provides technical support. If you are interested in our commercial version, please contact us.
location / {
content_by_lua_block {
local expr = require("resty.expr.v1")
local ex = expr.new({
{
{"arg_name", "==", "json"},
{"arg_weight", ">", 10},
{"arg_weight", "!", ">", 15},
}
})
-- equal to
-- 'ngx.say(ngx.var.arg_name == "json" and ngx.var.arg_weight > 10 and not ngx.var.arg_weight > 15)'
ngx.say(ex:eval(ngx.var))
}
}
syntax: ex, err = expr.new(rule)
Create a expression object which can be evaluated later.
The syntax of rule is an array table of expressions. Each expression is an array table which has three elements:
{
{"var name (aka. left value)", "optional '!' operator", "operator", "const value (aka. right value)"},
...
}
operator | description | example |
---|---|---|
== | equal | {"arg_name", "==", "json"} |
~= | not equal | {"arg_name", "~=", "json"} |
> | greater than | {"arg_age", ">", 24} |
< | less than | {"arg_age", "<", 24} |
~~ | Regular match | {"arg_name", "~~", "[a-z]+"} |
~* | Case insensitive regular match | {"arg_name", "~*", "[a-z]+"} |
in | find in array | {"arg_name", "in", {"1","2"}} |
has | left value array has value in the right | {"graphql_root_fields", "has", "repo"} |
! | reverse the result | {"arg_name", "!", "~~", "[a-z]+"} |
syntax: ok, err = ex:eval(ctx)
Evaluate the expression according to the ctx
. If ctx
is missing, ngx.var
is used by default.
local ok = rx:eval()
if ok == nil then
log_err("failed to eval expression: ", err)
return false
end
return ok
make install
make deps