Commit graph

4 commits

Author SHA1 Message Date
Luis Lavena
e0ef8d83da Correctly split named parameters on tree insert
Our Radix implementation was literally considering every single
character as candidate for splitting, which caused keys that
contained named parameters markers (`:foo`) to be broken across
nodes:

    tree = Radix::Tree.new
    tree.add "/", :root
    tree.add "/:post", :post
    tree.add "/:category/:post", :category_post
    # /
    #  :
    #   post
    #   category/:post

This caused incorrect behavior when performing lookup (`Tree#find`)
and failing to detect and map the key name, even when the value
was properly captured:

    result = tree.find "/example"
    pp result.found? # => false

This change corrects the issue by identifying named parameter marker
and once detected, consider everything until a separator or the end
of the supplied string is reached to be a unique key:

    tree = Radix::Tree.new
    tree.add "/", :root
    tree.add "/:post", :post
    tree.add "/:category/:post", :category_post
    # /
    #  :category/:post
    #  :post

However, due how Radix tree is structured, two named parameters at the
same level might result in problems during lookup phase:

    tree = Radix::Tree.new
    tree.add "/", :root
    tree.add "/:post", :post
    tree.add "/:category/:post", :category_post
    # /
    #  :category/:post
    #  :post

    tree.root.sort!
    # /
    #  :post
    #  :category/:post

    result = tree.find "/example"
    pp result.found? # => false
    pp result.params # => {"post" => "example"}

    result = tree.find "/news/first-post"
    pp result.found? # => false
    pp result.params # => {"post" => "news"}

Causing lookup to fail and values be stored under incorrect keys
for the parameters.

Because of this, a deprecation warning will be shown to allow
users adjust and correct their code prior fully removing it and
raise error (you know, semantic versioning and all that jazz).

This fixes #5 and closes #4
2016-03-10 17:50:08 -03:00
Luis Lavena
fe1ebb7d76 Correctly build key from detected parameters
Given a key `/:foo/:bar`, the find mechanism was incorrectly picking
the separator character as part of the key name (`foo/`).

This caused incorrect match between expected name (`foo`) and the
one obtained.

When the key name was extracted from the named parameter, the size
of the returned key was not compensated, given that we move +1 positions
to avoid having ':' as part of the key.

This is now corrected by reducing the key size one shorter to
compensate.

Fixes Issue #2
2016-02-29 11:40:18 -03:00
Luis Lavena
c79f4e5c21 spec: ensure Node usage is tracked
Add missing spec for disable of node payload when collecting the
usage of them in `Result`.
2016-01-24 19:21:19 -03:00
Luis Lavena
7f348cae8c Extraction: initial import
Extract Radix Tree implementation from `Beryl` project into an
standalone library to facilitate usage by other developers.

- Move `Tree`, `Node` and `Result` into `Radix` namespace
- Clenaup standalone README and describe usage
2016-01-24 19:19:42 -03:00