项目作者: luislavena

项目描述 :
Radix Tree implementation for Crystal
高级语言: Crystal
项目地址: git://github.com/luislavena/radix.git
创建时间: 2016-01-24T22:22:32Z
项目社区:https://github.com/luislavena/radix

开源协议:MIT License

下载


Radix Tree

Radix tree implementation for
Crystal language

CI
Latest Release

Installation

Add this to your application’s shard.yml:

  1. dependencies:
  2. radix:
  3. github: luislavena/radix

Usage

Building Trees

You can associate a payload with each path added to the tree:

  1. require "radix"
  2. tree = Radix::Tree(Symbol).new
  3. tree.add "/products", :products
  4. tree.add "/products/featured", :featured
  5. result = tree.find "/products/featured"
  6. if result.found?
  7. puts result.payload # => :featured
  8. end

The types allowed for payload are defined on Tree definition:

  1. tree = Radix::Tree(Symbol).new
  2. # Good, since Symbol is allowed as payload
  3. tree.add "/", :root
  4. # Compilation error, Int32 is not allowed
  5. tree.add "/meaning-of-life", 42

Can combine multiple types if needed:

  1. tree = Radix::Tree(Int32 | String | Symbol).new
  2. tree.add "/", :root
  3. tree.add "/meaning-of-life", 42
  4. tree.add "/hello", "world"

Lookup and placeholders

You can also extract values from placeholders (as named segments or globbing):

  1. tree.add "/products/:id", :product
  2. result = tree.find "/products/1234"
  3. if result.found?
  4. puts result.params["id"]? # => "1234"
  5. end

Please see Radix::Tree#add documentation for more usage examples.

Caveats

Pretty much all Radix implementations have their limitations and this project
is no exception.

When designing and adding paths to a Tree, please consider that two different
named parameters cannot share the same level:

  1. tree.add "/", :root
  2. tree.add "/:post", :post
  3. tree.add "/:category/:post", :category_post # => Radix::Tree::SharedKeyError

This is because different named parameters at the same level will result in
incorrect params when lookup is performed, and sometimes the value for
post or category parameters will not be stored as expected.

To avoid this issue, usage of explicit keys that differentiate each path is
recommended.

For example, following a good SEO practice will be consider /:post as
absolute permalink for the post and have a list of categories which links to
the permalinks of the posts under that category:

  1. tree.add "/", :root
  2. tree.add "/:post", :post # this is post permalink
  3. tree.add "/categories", :categories # list of categories
  4. tree.add "/categories/:category", :category # listing of posts under each category

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

  1. Fork it ( https://github.com/luislavena/radix/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors