Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow route globbing #40

Merged
merged 7 commits into from
Oct 27, 2020
Merged

Allow route globbing #40

merged 7 commits into from
Oct 27, 2020

Conversation

matthewmcgarvey
Copy link
Member

@matthewmcgarvey matthewmcgarvey commented Oct 27, 2020

Fixes #3

This will allow using a glob pattern for paths.

Unnamed Usage

router = LuckyRouter::Matcher(Symbol).new
router.add("get", "/users/*", :catch_all_users)

match = router.match!("get", "/users/catch/all/path")
match.params # => { "glob" => "catch/all/path" }

Named Usage

router = LuckyRouter::Matcher(Symbol).new
router.add("get", "/users/*:with_name", :catch_all_users)

match = router.match!("get", "/users/catch/all/path")
match.params # => { "with_name" => "catch/all/path" }

It is required that a glob can only go on the end of the path. Adding a path like /users/*/extra will result in a LuckyRouter::InvalidPathError

Benchmark (with --release)

  • Before: 267 ms
  • After: 282 ms
  • Difference: -15 ms
  • Change: -6%

Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be nice to get in! Thanks for taking this on. Overall it looks fine and pretty straight forward. I just had a few spec suggestions.

spec/path_part_spec.cr Show resolved Hide resolved
spec/integration_spec.cr Show resolved Hide resolved

class InvalidGlobError < LuckyRouterError
def initialize(glob)
super "Tried to define a glob as `#{glob}`, but it is invalid. Globs must be defined like `*` or given a name like `*:name`."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! This will be super helpful

Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the only real way to test is getting this in to some apps! 🥳 /cc @russ 😬

@matthewmcgarvey matthewmcgarvey merged commit e21fca1 into luckyframework:master Oct 27, 2020
@matthewmcgarvey matthewmcgarvey deleted the route-globbing branch October 27, 2020 20:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Route Globbing
2 participants