Skip to content

Commit 421673d

Browse files
authored
feature: supported to match var with regular expression. (#23)
1 parent 3104e1e commit 421673d

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,22 @@ The attributes of each element may contain these:
7777
|uris |option |A list of client request uris, not only supports static uri, but also supports prefix uri.|{"/foo", "/bar/*"}|
7878
|remote_addrs|option |A list of client remote address(IPv4 and IPv6), and we can use CIDR format, eg `192.168.1.0/24`.|{"127.0.0.1", "192.0.0.0/8", "::1", "fe80::/32"}|
7979
|methods |option |A list of method name. Here is full valid method list: "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "CONNECT" and "TRACE".|{"GET", "POST"}|
80-
|vars |option |A list of `{var, operator, val}`. For example: {{var, operator, val}, {var, operator, val}, ...}, `{"arg_name", "==", "json"}` means the value of argument `name` expect to `json`.|{{"arg_name", "==", "json"}, {"arg_age", ">", 18}}|
80+
|vars |option |A list of `{var, operator, val}`. For example: {{var, operator, val}, {var, operator, val}, ...}, `{"arg_name", "==", "json"}` means the value of argument `name` expect to `json`. Here is the full [Operator List](#operator-list).|{{"arg_name", "==", "json"}, {"arg_age", ">", 18}}|
8181
|filter_fun |option |User defined filter function, We can use it to achieve matching logic for special scenes. `radixtree` will only pass one parameter which named `vars` when matching route.|function(vars) return vars["arg_name"] == "json" end|
8282
|metadata |option |Will return this field if using `rx:match` to match route.||
8383
|handler |option |Will call this function using `rx:dispatch` to match route.||
84-
[Back to TOC](#table-of-contents)
8584

85+
#### Operator List
86+
87+
|operator|description|example|
88+
|--------|-----------|-------|
89+
|== |equal |{"arg_name", "==", "json"}|
90+
|~= |not equal |{"arg_name", "~=", "json"}|
91+
|> |greater than|{"arg_age", ">", 24}|
92+
|< |less than |{"arg_age", "<", 24}|
93+
|~~ |Regular match|{"arg_name", "~~", "[a-z]+"}|
94+
95+
[Back to TOC](#table-of-contents)
8696

8797
match
8898
-----

lib/resty/radixtree.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ local tostring = tostring
2424
local cur_level = ngx.config.subsystem == "http" and
2525
require("ngx.errlog").get_sys_filter_level()
2626
local ngx_var = ngx.var
27+
local re_find = ngx.re.find
2728

2829

2930
local function load_shared_lib(so_name)
@@ -127,7 +128,7 @@ local mt = { __index = _M, __gc = gc_free }
127128

128129

129130
local function insert_route(self, opts)
130-
local path = opts.path
131+
local path = opts.path
131132
opts = clone_tab(opts)
132133

133134
if not self.disable_path_cache_opt
@@ -405,8 +406,16 @@ local compare_funcs = {
405406
end
406407
return l_v < r_v
407408
end,
409+
["~~"] = function (l_v, r_v)
410+
local from = re_find(l_v, r_v, "jo")
411+
if from then
412+
return true
413+
end
414+
return false
415+
end,
408416
}
409417

418+
410419
local function compare_val(l_v, op, r_v)
411420
local com_fun = compare_funcs[op or "=="]
412421
if not com_fun then

t/vars.t

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,42 @@ GET /t?k=xxxx
406406
[error]
407407
--- response_body
408408
metadata /aa2
409+
410+
411+
412+
=== TEST 15: uri args
413+
--- config
414+
location /t {
415+
content_by_lua_block {
416+
local radix = require("resty.radixtree")
417+
local routes = {}
418+
for i, reg in ipairs({"[0-9]+", "^1[0-9]+", "[1-3]+", "^[1-3]+",
419+
"^2[0-9]+", "[1-3]+$", "[a-z]+", "[0"}) do
420+
routes[i] = {
421+
paths = "/" .. i,
422+
metadata = "metadata /" .. i,
423+
vars = {
424+
{"arg_k", "~~", reg},
425+
}
426+
}
427+
end
428+
local rx = radix.new(routes)
429+
430+
for i =1, 8 do
431+
ngx.say(rx:match("/" .. i, {vars = ngx.var}))
432+
end
433+
}
434+
}
435+
--- request
436+
GET /t?k=1234
437+
--- no_error_log
438+
[error]
439+
--- response_body
440+
metadata /1
441+
metadata /2
442+
metadata /3
443+
metadata /4
444+
nil
445+
nil
446+
nil
447+
nil

0 commit comments

Comments
 (0)