After Crystal 0.15, compiler will require declare the types used
by instance variables on classes.
This require changes to the usage of `Radix::Tree` by introducing
the type of payload elements it will handle:
# Will only support symbols as payload
tree = Radix::Tree(Symbol).new
tree.add "/", :root
# Error: cannot add node with anything other than Symbol
tree.add "/meaning-of-life", 42
The changes ensure future compatibility with Crystal and also
enforces a more declarative usage of `Radix::Tree`.
If necessary, you can combine multiple types to ensure a tree
can contain all the wide range of payloads you need:
tree = Radix::Tree.new(Foo | Bar | Symbol).new
tree.add "/", :root
tree.add "/foo", foo_instance
This change includes:
- Tree, Node and Result has been updated to require types.
- Node is capable of have optional payload (from defined type).
- Documentation has been updated to reflect this change.
Deal with named parameters under same level (shared) and raise
proper `SharedKeyError` exception.
This is a non-backward compatible change aims to solve result
mapping issues and tree lookup.
Now README covers this under *Caveats* section and offers an
alternative organization of the paths used on the tree.
To avoid potential issues when using `master` instead of a locked
release, bump the version.
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