From 6a09bc02b95d580ddd2df5ed2be216f962f2eb19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E6=B3=BD=E8=BD=A9?= Date: Tue, 29 Dec 2020 13:11:19 +0800 Subject: [PATCH] feat: allow disabling param match (#83) --- README.md | 7 ++++++- lib/resty/radixtree.lua | 18 +++++++++++++----- t/parameter.t | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9f27469..7f6197b 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 diff --git a/lib/resty/radixtree.lua b/lib/resty/radixtree.lua index d89d199..0b2a4cb 100644 --- a/lib/resty/radixtree.lua +++ b/lib/resty/radixtree.lua @@ -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 @@ -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 = "<=" @@ -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() @@ -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 diff --git a/t/parameter.t b/t/parameter.t index f31c3e2..675a228 100644 --- a/t/parameter.t +++ b/t/parameter.t @@ -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"}