luislavena/radix

Unexpected suffix match

RX14 opened this issue · 7 comments

RX14 commented

Not sure what's going on here at all...

require "radix"

t = Radix::Tree(Symbol).new
t.add("/prefix/", :root)
t.add("/prefix/foo", :foo)

t.find("/foo")

returns a match...

Hello @RX14, thanks for your patience.

I just ran your code and doesn't find a result:

require "radix"

tree = Radix::Tree(Symbol).new
tree.add("/prefix", :one)
tree.add("/prefix/foo", :two)

result = tree.find("/foo")
pp result.found? # => false

Please note that #find returns a result object, not a boolean response. (see docs)

If that is not the case, can you provide another example of the issue?

Cheers.

RX14 commented

Using Radix 0.3.9, crystal 0.33.0

require "radix"

t = Radix::Tree(Symbol).new
t.add("/prefix/", :root)
t.add("/prefix/foo", :foo)

res = t.find("/foo")
p res, res.found?
$ crystal radix.cr   
#<Radix::Result(Symbol):0x7f8cdb121740 @key=nil, @payload=:foo, @nodes=[#<Radix::Node(Symbol):0x7f8cdb128f00 @payload=:root, @priority=8, @children=[#<Radix::Node(Symbol):0x7f8cdb128eb0 @payload=:foo, @priority=3, @children=[], @kind=Normal, @key="foo", @placeholder=false>], @kind=Normal, @key="/prefix/", @placeholder=false>, #<Radix::Node(Symbol):0x7f8cdb128eb0 @payload=:foo, @priority=3, @children=[], @kind=Normal, @key="foo", @placeholder=false>], @params={}>
true
RX14 commented

Ah, i see:

the difference between your code and mine is that your code has /prefix and mine has /prefix/. I've checked and the trailing slash breaks this.

Thanks @RX14, definitely didn't see the slash at the end 🤦‍♂

I remember had a branch that sanitizes entries by removing trialing and repeated slashes.

Will try to take a look soon and get back to you.

Cheers.

RX14 commented

So I should strip all training slashes as a workaround for now?

@RX14 yeah, that will be a good workaround for the time being, thank you for your patience.

Hello @RX14, thanks for your patience.

Finally was able to figure this out and was not the trailing slash as originally thought, but the lack of common root node in tree.

I've pushed a fix for this in #31 and will be part of the next release.

Cheers.