Skip to content

Commit

Permalink
wip: parameters binding
Browse files Browse the repository at this point in the history
  • Loading branch information
vm-001 committed Dec 19, 2023
1 parent fa64a66 commit 52700f9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 19 deletions.
54 changes: 51 additions & 3 deletions spec/router_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe("Router", function()
assert.equal("1", router:match("/aa"))
end)

describe("route.paths", function()
describe("paths", function()
describe("static matching", function()
local router = Router.new({
{
Expand All @@ -120,14 +120,62 @@ describe("Router", function()
describe("prefix matching", function()

end)

describe("mix matching", function()
it("longer path has higher priority", function()
local router = Router.new({
{
paths = { "/a/:name/doge" },
handler = "1",
},
{
paths = { "/a/:name/dog*" },
handler = "2",
},
})
local handler = router:match("/a/john/doge")
assert.equal("1", handler)
end)
end)
end)

describe("route.methods", function()
describe("methods", function()

end)

describe("route.priority", function()
describe("priority", function()

end)

--describe("multiple", function()
-- local router = Router.new({
-- {
-- paths = { "/a/:name1/api" },
-- handler = "1",
-- },
-- {
-- paths = { "/a/:name2/api" },
-- methods = { "POST" },
-- handler = "2",
-- },
-- })
-- assert.equal("1", router:match("/aa/bb", { method = "GET" }))
--end)

describe("parameters binding", function()
local router = Router.new({
{
paths = { "/a/:name1/api" },
handler = "1",
},
{
paths = { "/a/:name2/api" },
handler = "2",
priority = 1,
},
})
--local ctx = { matched = {} }
--assert.equal("2", router:match("/a/foo/api", ctx))
end)
end)
end)
4 changes: 3 additions & 1 deletion src/resty/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ function Router:match(path, ctx)
if route then
if test then
local _path = ctx.matched._path
local params = route.path_params[_path]

-- 我们不知道这个 route 到底是由哪条 path 匹配的,所以根本不知道 path 里有多少个变量
local params = route.path_params[_path] -- todo 移除掉了 _path, 那么如何做参数绑定呢?
for i, param in ipairs(params) do
ctx.matched[param] = ctx.matched[i]
end
Expand Down
19 changes: 4 additions & 15 deletions src/resty/trie.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ function TrieNode.new(o)
type = o.type,
value = o.value,
indexs = o.indexs or {},
full_path = o.full_path, -- 如果叶子节点可以有多个 values,那么这里的 full_path 不能代表完成的路径
}

self.path_n = self.path and #self.path or 0
Expand All @@ -62,7 +61,7 @@ function TrieNode:set(value, conflict_callback)
end


local function insert_child(node, path, full_path, value, fn)
local function insert_child(node, path, value, fn)
local parser = Parser.new(path, "default") -- 外部 :add 应该传一个 parser 进来?
local token = parser:next()
while token do
Expand Down Expand Up @@ -94,13 +93,11 @@ local function insert_child(node, path, full_path, value, fn)
end

node:set(value, fn)
node.full_path = full_path
end

local function split(node, path, prefix_n)
local child = TrieNode.new({
path = str_sub(node.path, prefix_n + 1),
full_path= node.full_path,
children = node.children,
children_n = node.children_n,
type = TYPES.STATIC,
Expand All @@ -110,7 +107,6 @@ local function split(node, path, prefix_n)

-- update current node
node.path = str_sub(path, 1, prefix_n)
node.full_path = nil
node.path_n = #node.path
node.children = { child }
node.children_n = 1
Expand All @@ -120,11 +116,9 @@ local function split(node, path, prefix_n)
end

function TrieNode:add(path, value, conflict_cb)
local full_path = path

if self.path == "" and not self.type then
-- current node is empty
insert_child(self, path, full_path, value, conflict_cb)
insert_child(self, path, value, conflict_cb)
return
end

Expand Down Expand Up @@ -163,7 +157,7 @@ function TrieNode:add(path, value, conflict_cb)
node.children = node.children or {}
table.insert(node.children, child)
node.children_n = node.children_n + 1
insert_child(child, path, full_path, value, conflict_cb)
insert_child(child, path, value, conflict_cb)
node.indexs[first_char] = #node.children -- use self.children_n?
return
end
Expand Down Expand Up @@ -233,7 +227,6 @@ function TrieNode:traverse(path, ctx)
if binding_params then
param_n = param_n + 1
ctx.matched[param_n] = path
ctx.matched["_path"] = node.children[idx].full_path
end
end

Expand All @@ -260,7 +253,7 @@ function TrieNode:traverse(path, ctx)
end
if node.value then
if binding_params then
ctx.matched["_path"] = node.full_path

end
matched_n = matched_n + 1
matched_values[matched_n] = node.value
Expand All @@ -270,9 +263,6 @@ function TrieNode:traverse(path, ctx)
end
elseif path == prefix then
if node.value then
if binding_params then
ctx.matched["_path"] = node.full_path
end
matched_n = matched_n + 1
matched_values[matched_n] = node.value
break
Expand All @@ -286,7 +276,6 @@ function TrieNode:traverse(path, ctx)
if binding_params then
param_n = param_n + 1
ctx.matched[param_n] = ""
ctx.matched["_path"] = node.full_path
end
end
end
Expand Down

0 comments on commit 52700f9

Please sign in to comment.