Skip to content
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
10 changes: 10 additions & 0 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ func lookupNextToken(in string, offset int, parent *node, res *[]Token, numParam
// -- -- {END}

if offset+child.Token.Len <= len(in) {
if child.nextEnd() && offset+child.Token.Len != len(in) {
continue
}
if in[offset:offset+child.Token.Len] == child.Token.Raw {
*res = append(*res, child.Token)

Expand Down Expand Up @@ -289,6 +292,13 @@ func (n *node) lengthConstOrZero() int {
return len(n.Token.Raw)
}

func (n *node) nextEnd() bool {
if len(n.Childs) != 1 {
return false
}
return n.Childs[0].Token.Mode == END
}

// Less returns true if
// - left token type is CONST
// - more length of value of token (type is CONST) on the left than right
Expand Down
43 changes: 43 additions & 0 deletions store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,36 @@ func Test_StoreMultiple(t *testing.T) {
{"pathParams", "/path/{param}"},
{"path", "/path/"},
}, "/path/foo", true, Tokens{StartToken, ConstToken("/path/"), ParsedParameterToken("param", "foo"), NamedEndToken("pathParams")}},

// https://github.com/gebv/strparam/issues/7
{[][]string{
{"a", "/{foobar}"},
{"b", "/b"},
{"c", "/ba"},
{"d", "/baz"},
{"e", "/ba{foobar}"},
}, "/baz", true, Tokens{StartToken, ConstToken("/baz"), NamedEndToken("d")}},
{[][]string{
{"a", "/{foobar}"},
{"b", "/b"},
{"c", "/ba"},
// {"d", "/baz"},
{"e", "/ba{foobar}"},
}, "/baz", true, Tokens{StartToken, ConstToken("/ba"), ParsedParameterToken("foobar", "z"), NamedEndToken("e")}},
{[][]string{
{"a", "/{foobar}"},
{"b", "/b"},
{"c", "/ba"},
// {"d", "/baz"},
// {"e", "/ba{foobar}"},
}, "/baz", true, Tokens{StartToken, ConstToken("/"), ParsedParameterToken("foobar", "baz"), NamedEndToken("a")}},
{[][]string{
// {"a", "/{foobar}"},
{"b", "/b"},
{"c", "/ba"},
// {"d", "/baz"},
// {"e", "/ba{foobar}"},
}, "/baz", false, nil},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%q->%q", tt.namedPatterns, tt.in), func(t *testing.T) {
Expand Down Expand Up @@ -271,3 +301,16 @@ func Test_PatternWithSeparator(t *testing.T) {
t.Log(params)
})
}

func Test_nextEnd(t *testing.T) {
n := &node{}
assert.False(t, n.nextEnd())
n = &node{Childs: []*node{{Token: ConstToken("!")}}}
assert.False(t, n.nextEnd())
n = &node{Childs: []*node{{Token: SeparatorToken("a")}}}
assert.False(t, n.nextEnd())
n = &node{Childs: []*node{{Token: EndToken}}}
assert.True(t, n.nextEnd())
n = &node{Childs: []*node{{Token: NamedEndToken("abc")}}}
assert.True(t, n.nextEnd())
}