Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IN operator when do route matching #48

Merged
merged 4 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -456,6 +456,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 @@ -492,6 +503,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