From aa959c8b1e77e752e75204d82a4d03215f23f3b7 Mon Sep 17 00:00:00 2001 From: Douglas-Lee Date: Mon, 18 Dec 2023 00:48:36 +0800 Subject: [PATCH] wip --- spec/parser_spec.lua | 14 ++++++++++++-- src/resty/sample.lua | 5 +++++ src/resty/trie.lua | 6 ++++-- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/spec/parser_spec.lua b/spec/parser_spec.lua index e37b83f..1541d79 100644 --- a/spec/parser_spec.lua +++ b/spec/parser_spec.lua @@ -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) diff --git a/src/resty/sample.lua b/src/resty/sample.lua index 86363c2..4b5d5ed 100644 --- a/src/resty/sample.lua +++ b/src/resty/sample.lua @@ -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({ { diff --git a/src/resty/trie.lua b/src/resty/trie.lua index 883620b..b3d6ef4 100644 --- a/src/resty/trie.lua +++ b/src/resty/trie.lua @@ -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) @@ -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