mirror of
https://gitea.invidious.io/iv-org/shard-radix.git
synced 2024-08-15 00:43:21 +00:00
No description
e0ef8d83da
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 |
||
---|---|---|
spec | ||
src | ||
.gitignore | ||
.travis.yml | ||
CHANGELOG.md | ||
LICENSE | ||
README.md | ||
shard.yml |
Radix Tree
Radix tree implementation for Crystal language
Installation
Add this to your application's shard.yml
:
dependencies:
radix:
github: luislavena/radix
Usage
You can associate a payload with each path added to the tree:
require "radix"
tree = Radix::Tree.new
tree.add "/products", :products
tree.add "/products/featured", :featured
result = tree.find "/products/featured"
if result.found?
puts result.payload # => :featured
end
You can also extract values from placeholders (as named segments or globbing):
tree.add "/products/:id", :product
result = tree.find "/products/1234"
if result.found?
puts result.params["id"]? # => "1234"
end
Please see Radix::Tree#add
documentation for more usage examples.
Implementation
This project has been inspired and adapted from julienschmidt/httprouter and spriet2000/vertx-http-router Go and Java implementations, respectively.
Changes to logic and optimizations have been made to take advantage of Crystal's features.
Contributing
- Fork it ( https://github.com/luislavena/radix/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- Luis Lavena - creator, maintainer