Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
vm-001 committed Dec 17, 2023
1 parent 97e1c9c commit aa959c8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
14 changes: 12 additions & 2 deletions spec/parser_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,22 @@ describe("parser", function()
{
path = "/api/v1/users/:id/*path",
tokens = { "/api/v1/users/", ":id", "/", "*path" },
}
},
{
path = "*",
tokens = { "*" }
},
{
path = ":name",
tokens = { ":name" }
},
}

for _, test in ipairs(tests) do
local parser = Parser.new(test.path)
assert(test.tokens, parse(parser))
print(test.path, require("inspect")(parse(parser)))
--assert.same(test.tokens, parse(parser))
end

end)
end)
5 changes: 5 additions & 0 deletions src/resty/sample.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ end
local function test_example()
local mobdebug = require "mobdebug"
mobdebug.start("localhost", 28172)
local Parser = require ("router-tree.parser")
local parser = Parser.new("*") -- 这里也是错误的
parser:next() -- 应该得到 *


local radix = require("router-tree")
local rx = radix.new({
{
Expand Down
6 changes: 4 additions & 2 deletions src/resty/trie.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,17 @@ function TrieNode:add(path, value)
end

if prefix_n < #path then
path = str_sub(path, prefix_n + 1)


if self.type == TYPES.PARAM then
-- 当前的节点是 param 类型,所以应该是匹配的
path = str_sub(path, prefix_n + 1)
self = self.children[1]
goto continue
elseif self.type == TYPES.CATCH_ALL then

else
path = str_sub(path, prefix_n + 1)
end

local first_char = str_sub(path, 1, 1)
Expand All @@ -155,7 +157,7 @@ function TrieNode:add(path, value)
if first_char == ":" then
local idx_slash = find(path, "/", nil, true)
if idx_slash then
path = str_sub(path, idx_slash)
path = ":" .. str_sub(path, idx_slash)
else
path = ""
break
Expand Down

0 comments on commit aa959c8

Please sign in to comment.