Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 'path' as a greedy version of 'string' in Path() expression #21

Merged
merged 1 commit into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module github.com/vulcand/route

go 1.18

require (
github.com/stretchr/testify v1.2.2
github.com/vulcand/predicate v1.1.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gravitational/trace v0.0.0-20190726142706-a535a178675f // indirect
github.com/jonboulle/clockwork v0.1.0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/kr/text v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.4.2 // indirect
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b // indirect
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
)
33 changes: 33 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gravitational/trace v0.0.0-20190726142706-a535a178675f h1:68WxnfBzJRYktZ30fmIjGQ74RsXYLoeH2/NITPktTMY=
github.com/gravitational/trace v0.0.0-20190726142706-a535a178675f/go.mod h1:RvdOUHE4SHqR3oXlFFKnGzms8a5dugHygGw1bqDstYI=
github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/vulcand/predicate v1.1.0 h1:Gq/uWopa4rx/tnZu2opOSBqHK63Yqlou/SzrbwdJiNg=
github.com/vulcand/predicate v1.1.0/go.mod h1:mlccC5IRBoc2cIFmCB8ZM62I3VDb6p2GXESMHa3CnZg=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b h1:ZmngSVLe/wycRns9MKikG9OWIEjGcGAkacif7oYQaUY=
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 h1:Q5284mrmYTpACcm+eAKjKJH48BBwSyfJqmmGDTtT8Vc=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
76 changes: 43 additions & 33 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ package route

import (
"net/http"
"testing"

. "gopkg.in/check.v1"
"github.com/stretchr/testify/assert"
)

type ParseSuite struct {
}

var _ = Suite(&ParseSuite{})

func (s *ParseSuite) TestParseAndMatchSuccess(c *C) {
func TestParseAndMatchSuccess(t *testing.T) {
testCases := []struct {
Expression string
Url string
Expand Down Expand Up @@ -62,6 +58,20 @@ func (s *ParseSuite) TestParseAndMatchSuccess(c *C) {
"localhost",
nil,
},
{
`Method("POST") && Path("/helloworld/<path:name>")`,
`http://google.com/helloworld/some/name`,
"POST",
"localhost",
nil,
},
{
`Method("POST") && Path("/escaped/<path:name>")`,
`http://google.com/escaped/some%2Fpath`,
"POST",
"localhost",
nil,
},
{
`Path("/helloworld")`,
`http://google.com/helloworld`,
Expand Down Expand Up @@ -149,44 +159,44 @@ func (s *ParseSuite) TestParseAndMatchSuccess(c *C) {
},
}
for _, tc := range testCases {
comment := Commentf("%v", tc.Expression)
result := &match{val: "ok"}
p, err := parse(tc.Expression, result)
c.Assert(err, IsNil)
c.Assert(p, NotNil)

req := makeReq(req{url: tc.Url})
req.Method = tc.Method
req.Host = tc.Host
t.Run(tc.Expression, func(t *testing.T) {
result := &match{val: "ok"}
p, err := parse(tc.Expression, result)
assert.NoError(t, err)
assert.NotNil(t, p)

req.Header = tc.Headers
req := makeReq(req{url: tc.Url})
req.Method = tc.Method
req.Host = tc.Host
req.Header = tc.Headers

out := p.match(req)
c.Assert(out, NotNil, comment)
c.Assert(out, Equals, result, comment)
out := p.match(req)
assert.NotNil(t, p)
assert.Equal(t, result, out)
})
}
}

func (s *ParseSuite) TestParseFailures(c *C) {
func TestParseFailures(t *testing.T) {
testCases := []string{
`bad`, // unsupported identifier
`bad expression`, // not a valid go expression
`Path("/path") || Path("/path2")`, // unsupported operator
`1 && 2`, // unsupported statements
`"standalone literal"`, // standalone literal
`UnknownFunction("hi")`, // unknown functin
`Path(1)`, // bad argument type
`RegexpRoute(1)`, // bad argument type
`Path()`, // no arguments
`PathRegexp()`, // no arguments
`Path(Path("hello"))`, // nested calls
`Path("")`, // bad trie expression
`PathRegexp("[[[[")`, // bad regular expression
`1 && 2`, // unsupported statements
`"standalone literal"`, // standalone literal
`UnknownFunction("hi")`, // unknown function
`Path(1)`, // bad argument type
`RegexpRoute(1)`, // bad argument type
`Path()`, // no arguments
`PathRegexp()`, // no arguments
`Path(Path("hello"))`, // nested calls
`Path("")`, // bad trie expression
`PathRegexp("[[[[")`, // bad regular expression
}

for _, expr := range testCases {
m, err := parse(expr, &match{val: "ok"})
c.Assert(err, NotNil)
c.Assert(m, IsNil)
assert.Error(t, err)
assert.Nil(t, m)
}
}
40 changes: 40 additions & 0 deletions trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,52 @@ func makeMatcher(matcherType string, matcherArgs []string) (patternMatcher, erro
switch matcherType {
case "string":
return newStringMatcher(matcherArgs)
case "path":
return newPathMatcher(matcherArgs)
case "int":
return newIntMatcher(matcherArgs)
}
return nil, fmt.Errorf("unsupported matcher: %s", matcherType)
}

func newPathMatcher(args []string) (patternMatcher, error) {
if len(args) != 1 {
return nil, fmt.Errorf("expected only one parameter - variable name, got: %s", args)
}
return &pathMatcher{name: args[0]}, nil
}

type pathMatcher struct {
name string
}

func (m *pathMatcher) String() string {
return fmt.Sprintf("<path:%s>", m.name)
}

func (m *pathMatcher) getName() string {
return m.name
}

func (m *pathMatcher) match(i *charIter) bool {
m.grabValue(i)
return true
}

func (m *pathMatcher) equals(other patternMatcher) bool {
_, ok := other.(*pathMatcher)
return ok && other.getName() == m.getName()
}

func (m *pathMatcher) grabValue(i *charIter) {
for {
_, _, ok := i.next()
if !ok {
return
}
}
}

func newStringMatcher(args []string) (patternMatcher, error) {
if len(args) != 1 {
return nil, fmt.Errorf("expected only one parameter - variable name, got: %s", args)
Expand Down
11 changes: 10 additions & 1 deletion trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ root(0)
match(0:<string:param1>)
`)

// Path with `path` parameter
s.testPathToTrie(c, "/m/<path:param1>", `
root(0)
node(0:/)
node(0:m)
node(0:/)
match(0:<path:param1>)
`)

// Path with parameter in the middle
s.testPathToTrie(c, "/m/<string:param1>/a", `
root(0)
Expand Down Expand Up @@ -215,7 +224,7 @@ func (s *TrieSuite) TestMergeAndMatchCases(c *C) {
"http://google.com",
"/",
},
// Choosing longest path
// Choosing the longest path
{
[]string{"/v2/domains/", "/v2/domains/domain1"},
"http://google.com/v2/domains/domain1",
Expand Down