Skip to content

Commit

Permalink
wip: all green
Browse files Browse the repository at this point in the history
  • Loading branch information
vm-001 committed Dec 18, 2023
1 parent aa959c8 commit 7be46c7
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 162 deletions.
6 changes: 4 additions & 2 deletions lua-resty-router-tree-dev-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ build = {
type = "builtin",
modules = {
["router-tree"] = "src/resty/router.lua",
["router-tree.parser"] = "src/resty/parser.lua",
["router-tree.parser"] = "src/resty/parser/parser.lua",
["router-tree.parser.style.default"] = "src/resty/parser/style/default.lua",
["router-tree.parser.style.ant"] = "src/resty/parser/style/ant.lua",
["router-tree.trie"] = "src/resty/trie.lua",
["router-tree.utils"] = "src/resty/utils.lua",
},
}
}
88 changes: 52 additions & 36 deletions spec/parser_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,59 @@ local function parse(parser)
end

describe("parser", function()
it("sanity", function()
local tests = {
{
path = "/info/:user/project/:sub",
tokens = { "/info/", ":user", "/project/", ":sub" },
},
{
path = "/api/v1/pay/*",
tokens = { "/api/v1/pay/", "*" },
},
{
path = "/api/v1/pay/*path",
tokens = { "/api/v1/pay/", "*path" },
},
{
path = "/api/v1/users/:id/*",
tokens = { "/api/v1/users/", ":id", "/", "*" },
},
{
path = "/api/v1/users/:id/*path",
tokens = { "/api/v1/users/", ":id", "/", "*path" },
},
{
path = "*",
tokens = { "*" }
},
{
path = ":name",
tokens = { ":name" }
},
}
describe("style", function()
describe("default", function()
it("sanity", function()
local tests = {
{
path = "/info/:user/project/:sub",
tokens = { "/info/", ":user", "/project/", ":sub" },
},
{
path = "/api/v1/pay/*",
tokens = { "/api/v1/pay/", "*" },
},
{
path = "/api/v1/pay/*path",
tokens = { "/api/v1/pay/", "*path" },
},
{
path = "/api/v1/users/:id/*",
tokens = { "/api/v1/users/", ":id", "/", "*" },
},
{
path = "/api/v1/users/:id/*path",
tokens = { "/api/v1/users/", ":id", "/", "*path" },
},
{
path = "*",
tokens = { "*" }
},
{
path = "*doge",
tokens = { "*doge" }
},
{
path = ":",
tokens = { ":" }
},
{
path = ":name",
tokens = { ":name" }
},
{
path = "",
tokens = { "" }
},
}

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

end)
end)
end)
end)
86 changes: 0 additions & 86 deletions src/resty/parser.lua

This file was deleted.

1 change: 0 additions & 1 deletion src/resty/parser/ant.lua

This file was deleted.

25 changes: 25 additions & 0 deletions src/resty/parser/parser.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
local Parser = {}

local parsers = {}
do
local styles = { "default" }
for _, style in ipairs(styles) do
local module = require("router-tree.parser.style." .. style)
parsers[style] = module
end
end


-- return a parser instance
function Parser.parse(path, style)
local parser = parsers[style]
if not parser then
return nil, "invalid style: " .. style
end

local instance = parser.new(path)
return instance
end


return Parser
3 changes: 3 additions & 0 deletions src/resty/parser/style/ant.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- TODO

return {}
78 changes: 78 additions & 0 deletions src/resty/parser/style/default.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
local byte = string.byte
local sub = string.sub

local BYTE_SLASH = byte("/")
local BYTE_COLON = byte(":")
local BYTE_ASTERISK = byte("*")

local Parser = {}
local mt = { __index = Parser }

local STATES = {
none = 0,
finish = 1,
colon = 2,
asterisk = 3,
static = 4,
}

function Parser.new(path)
local self = {
path = path,
anchor = 1,
pos = 1,
path_n = #path,
state = STATES.none,
}
return setmetatable(self, mt)
end

function Parser:next()
if self.state == STATES.finish then
return nil
end

local char, token
while self.pos <= self.path_n do
char = byte(self.path, self.pos)
--print("pos: " .. self.pos .. "(" .. string.char(char) .. ")")
if self.state == STATES.none or self.state == STATES.static then
if char == BYTE_COLON then
if self.state == STATES.static then
local anchor = self.anchor
self.anchor = self.pos
token = sub(self.path, anchor, self.pos - 1)
end
self.state = STATES.colon
elseif char == BYTE_ASTERISK then
if self.state == STATES.static then
local anchor = self.anchor
self.anchor = self.pos
token = sub(self.path, anchor, self.pos - 1)
end
self.state = STATES.asterisk
else
self.state = STATES.static
end
elseif self.state == STATES.colon then
if char == BYTE_SLASH then
self.state = STATES.none
local anchor = self.anchor
self.anchor = self.pos
return sub(self.path, anchor, self.pos - 1)
end
elseif self.state == STATES.asterisk then
-- do nothing
end
self.pos = self.pos + 1

if token then
return token
end
end

self.state = STATES.finish
return sub(self.path, self.anchor, self.pos)
end

return Parser
8 changes: 7 additions & 1 deletion src/resty/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ for i, name in ipairs({"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS
-- ngx.log(ngx.WARN, "name: ", name, " val: ", METHODS[name])
end

local function sort_route(route_a, route_b)
return (route_a.priority or 0) > (route_b.priority or 0)
end

local function add_route(self, path, route)
local wildcard = has_wildcard(path)
if wildcard then
Expand All @@ -31,13 +35,14 @@ local function add_route(self, path, route)
self.static[path] = { route }
else
table.insert(self.static[path], route)
table.sort(self.static[path], sort_route)
end
end
end

local function extract_params(path)
local params = {}
local parser = Parser.new(path)
local parser = Parser.parse(path, "default")
local token = parser:next()
while (token) do
local c = string.sub(token, 1, 1)
Expand Down Expand Up @@ -76,6 +81,7 @@ function Router.new(routes, options)
end

local route_t = {
priority = route.priority,
handler = route.handler or route.metadata,
method = bit_methods,
params = {}
Expand Down
19 changes: 17 additions & 2 deletions src/resty/sample.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,20 @@ local function test_example()
ngx.say("match meta: ", meta)
end


local function test5()
local mobdebug = require "mobdebug"
local a, b, c = mobdebug.start("localhost", 28172)
print(a, b, c)
local radix = require("router-tree")
local rx = radix.new({
{
paths = {"/*"},
metadata = "OK",
},
})
local opts = {method = "GET", matched = {}}
ngx.say(rx:match("/ip\ni\ni", opts))
end

--test1()
--test2()
Expand All @@ -143,4 +156,6 @@ end

--test4()

test_example()
test5()

--test_example()
2 changes: 1 addition & 1 deletion src/resty/trie.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ end


local function insert_child(node, path, full_path, value)
local parser = Parser.new(path)
local parser = Parser.parse(path, "default")
local token = parser:next()
while token do
if byte(token) == BYTE_COLON then
Expand Down
2 changes: 1 addition & 1 deletion t/matched.t
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ OK
local radix = require("router-tree")
local rx = radix.new({
{
paths = {"*"},
paths = {"/*"},
metadata = "OK",
},
})
Expand Down
Loading

0 comments on commit 7be46c7

Please sign in to comment.