Skip to content

Commit

Permalink
Allow any string for parameter names in router, fixes #871.
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Jul 4, 2022
1 parent 9fc906c commit 271373c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Handlers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ mutable struct Variable
pattern::Union{Nothing, Regex}
end

const VARREGEX = r"^{([[:alnum:]]+):?(.*)}$"
const VARREGEX = r"^{([^:{}]+)(?::(.*))?}$"

function Variable(pattern)
re = Base.match(VARREGEX, pattern)
if re === nothing
error("problem parsing path variable for route: `$pattern`")
end
pat = re.captures[2]
return Variable(re.captures[1], pat == "" ? nothing : Regex(pat))
return Variable(re.captures[1], pat === nothing ? nothing : Regex(pat))
end

struct Leaf
Expand All @@ -178,7 +178,7 @@ end
Base.show(io::IO, x::Node) = print(io, "Node($(x.segment))")

isvariable(x) = startswith(x, "{") && endswith(x, "}")
segment(x) = segment == "*" ? String(segment) : isvariable(x) ? Variable(x) : String(x)
segment(x) = isvariable(x) ? Variable(x) : String(x)

Node(x) = Node(x, Node[], Node[], nothing, nothing, Leaf[])
Node() = Node("*")
Expand Down
3 changes: 3 additions & 0 deletions test/handlers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ using HTTP, Test
@test r(HTTP.Request("GET", "/test/sarv/ghotra/seven")) == 10
@test r(HTTP.Request("GET", "/test/foo")) == 8

HTTP.register!(r, "/api/issue/{issue_id}", req -> req.context[:params]["issue_id"])
@test r(HTTP.Request("GET", "/api/issue/871")) == "871"

HTTP.register!(r, "/api/widgets/{id}", req -> req.context[:params]["id"])
@test r(HTTP.Request("GET", "/api/widgets/11")) == "11"

Expand Down

0 comments on commit 271373c

Please sign in to comment.