Skip to content

Commit

Permalink
feat: support in operator when do route matching (#48)
Browse files Browse the repository at this point in the history
fix #47 #30
  • Loading branch information
lilien1010 authored Aug 6, 2020
1 parent 438b9af commit 904799f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ local rx = radix.new({
|> |greater than|{"arg_age", ">", 24}|
|< |less than |{"arg_age", "<", 24}|
|~~ |Regular match|{"arg_name", "~~", "[a-z]+"}|
|in |find in array|{"arg_name", "in", {"1","2"}}|

[Back to TOC](#table-of-contents)

Expand Down
13 changes: 13 additions & 0 deletions lib/resty/radixtree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,17 @@ local function compare_gin(l_v, r_v, opts)
return true
end

local function in_array(l_v, r_v)
if type(r_v) == "table" then
for _,v in ipairs(r_v) do
if v == l_v then
return true
end
end
end
return false
end

local compare_funcs = {
["=="] = function (l_v, r_v)
if type(r_v) == "number" then
Expand Down Expand Up @@ -491,6 +502,8 @@ local compare_funcs = {
end
return false
end,
["IN"] = in_array,
["in"] = in_array,
}


Expand Down
35 changes: 35 additions & 0 deletions t/vars.t
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,38 @@ GET /t?k=v
[error]
--- response_body
metadata /aa



=== TEST 18: IN: hit
--- config
location /t {
content_by_lua_block {
local radix = require("resty.radixtree")
local rx = radix.new({
{
paths = "/aa",
metadata = "metadata /aa",
vars = {
{"arg_k", "in", {'1','2'}},
},
}
})
ngx.say(rx:match("/aa", {vars = ngx.var}))
ngx.say(rx:match("/aa", {vars = {arg_k='2'}}))
ngx.say(rx:match("/aa", {vars = {arg_k='4'}}))
ngx.say(rx:match("/aa", {vars = {}}))
ngx.say(rx:match("/aa", {vars = {arg_k=nil}}))
}
}
--- request
GET /t?k=1
--- no_error_log
[error]
--- response_body
metadata /aa
metadata /aa
nil
nil
nil

0 comments on commit 904799f

Please sign in to comment.