Skip to content

Commit

Permalink
feat: allow disabling param match (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander committed Dec 29, 2020
1 parent 53f8615 commit 6a09bc0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ In addition to this open source version, our company also provides a more powerf

### new

`syntax: rx, err = radix.new(routes)`
`syntax: rx, err = radix.new(routes, opts)`

The routes is a array table, like `{ {...}, {...}, {...} }`, Each element in the array is a route, which is a hash table.

Expand All @@ -104,6 +104,11 @@ The attributes of each element may contain these:
|metadata |option |Will return this field if using `rx:match` to match route.||
|handler |option |Will call this function using `rx:dispatch` to match route.||

The `opts` is an optional configuration controls the behavior of match. Fields below are supported:
|name |description|default|
|:-------- |:-----------|:-----|
|no_param_match|disable [Parameters in path](#parameters-in-path)|false|

### Path

#### Full path match
Expand Down
18 changes: 13 additions & 5 deletions lib/resty/radixtree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ local pre_insert_route
do
local route_opts = {}

function pre_insert_route(self, path, route)
function pre_insert_route(self, path, route, global_opts)
if type(path) ~= "string" then
error("invalid argument path", 2)
end
Expand Down Expand Up @@ -319,7 +319,7 @@ function pre_insert_route(self, path, route)
route_opts.path_org = path
route_opts.param = false

local pos = str_find(path, ':', 1, true)
local pos = not global_opts.no_param_match and str_find(path, ':', 1, true)
if pos then
path = path:sub(1, pos - 1)
route_opts.path_op = "<="
Expand Down Expand Up @@ -361,11 +361,19 @@ end
end -- do


function _M.new(routes)
local default_global_opts = {
no_param_match = false,
}

function _M.new(routes, opts)
if not routes then
return nil, "missing argument route"
end

if not opts then
opts = default_global_opts
end

local route_n = #routes

local tree = radix.radix_tree_new()
Expand All @@ -387,11 +395,11 @@ function _M.new(routes)
local route = routes[i]
local paths = route.paths
if type(paths) == "string" then
pre_insert_route(self, paths, route)
pre_insert_route(self, paths, route, opts)

else
for _, path in ipairs(paths) do
pre_insert_route(self, path, route)
pre_insert_route(self, path, route, opts)
end
end
end
Expand Down
36 changes: 36 additions & 0 deletions t/parameter.t
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,39 @@ match meta: metadata /:name/foo
match meta: nil
--- error_log
pcre pat:
=== TEST 9: disable param match
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local radix = require("resty.radixtree")
local rx = radix.new({
{
paths = {"/name/:name/id/:id"},
metadata = "metadata /name",
},
}, {
no_param_match = true,
})
local opts = {matched = {}}
local meta = rx:match("/name/json/id/1", opts)
ngx.say("match meta: ", meta)
ngx.say("matched: ", json.encode(opts.matched))
local meta = rx:match("/name/:name/id/:id", opts)
ngx.say("match meta: ", meta)
ngx.say("matched: ", json.encode(opts.matched))
}
}
--- request
GET /t
--- no_error_log
[error]
--- response_body
match meta: nil
matched: []
match meta: metadata /name
matched: {"_path":"/name/:name/id/:id"}

0 comments on commit 6a09bc0

Please sign in to comment.