Skip to content

Commit 6a09bc0

Browse files
authored
feat: allow disabling param match (#83)
1 parent 53f8615 commit 6a09bc0

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ In addition to this open source version, our company also provides a more powerf
8686

8787
### new
8888

89-
`syntax: rx, err = radix.new(routes)`
89+
`syntax: rx, err = radix.new(routes, opts)`
9090

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

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

107+
The `opts` is an optional configuration controls the behavior of match. Fields below are supported:
108+
|name |description|default|
109+
|:-------- |:-----------|:-----|
110+
|no_param_match|disable [Parameters in path](#parameters-in-path)|false|
111+
107112
### Path
108113

109114
#### Full path match

lib/resty/radixtree.lua

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ local pre_insert_route
256256
do
257257
local route_opts = {}
258258

259-
function pre_insert_route(self, path, route)
259+
function pre_insert_route(self, path, route, global_opts)
260260
if type(path) ~= "string" then
261261
error("invalid argument path", 2)
262262
end
@@ -319,7 +319,7 @@ function pre_insert_route(self, path, route)
319319
route_opts.path_org = path
320320
route_opts.param = false
321321

322-
local pos = str_find(path, ':', 1, true)
322+
local pos = not global_opts.no_param_match and str_find(path, ':', 1, true)
323323
if pos then
324324
path = path:sub(1, pos - 1)
325325
route_opts.path_op = "<="
@@ -361,11 +361,19 @@ end
361361
end -- do
362362

363363

364-
function _M.new(routes)
364+
local default_global_opts = {
365+
no_param_match = false,
366+
}
367+
368+
function _M.new(routes, opts)
365369
if not routes then
366370
return nil, "missing argument route"
367371
end
368372

373+
if not opts then
374+
opts = default_global_opts
375+
end
376+
369377
local route_n = #routes
370378

371379
local tree = radix.radix_tree_new()
@@ -387,11 +395,11 @@ function _M.new(routes)
387395
local route = routes[i]
388396
local paths = route.paths
389397
if type(paths) == "string" then
390-
pre_insert_route(self, paths, route)
398+
pre_insert_route(self, paths, route, opts)
391399

392400
else
393401
for _, path in ipairs(paths) do
394-
pre_insert_route(self, path, route)
402+
pre_insert_route(self, path, route, opts)
395403
end
396404
end
397405
end

t/parameter.t

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,39 @@ match meta: metadata /:name/foo
276276
match meta: nil
277277
--- error_log
278278
pcre pat:
279+
280+
281+
282+
=== TEST 9: disable param match
283+
--- config
284+
location /t {
285+
content_by_lua_block {
286+
local json = require("toolkit.json")
287+
local radix = require("resty.radixtree")
288+
local rx = radix.new({
289+
{
290+
paths = {"/name/:name/id/:id"},
291+
metadata = "metadata /name",
292+
},
293+
}, {
294+
no_param_match = true,
295+
})
296+
297+
local opts = {matched = {}}
298+
local meta = rx:match("/name/json/id/1", opts)
299+
ngx.say("match meta: ", meta)
300+
ngx.say("matched: ", json.encode(opts.matched))
301+
local meta = rx:match("/name/:name/id/:id", opts)
302+
ngx.say("match meta: ", meta)
303+
ngx.say("matched: ", json.encode(opts.matched))
304+
}
305+
}
306+
--- request
307+
GET /t
308+
--- no_error_log
309+
[error]
310+
--- response_body
311+
match meta: nil
312+
matched: []
313+
match meta: metadata /name
314+
matched: {"_path":"/name/:name/id/:id"}

0 commit comments

Comments
 (0)