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

Add support for glob params #1294

Merged
merged 1 commit into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions spec/lucky/action_route_params_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ private class TestOptionalParamAction < TestAction
end
end

private class TestGlobAction < TestAction
get "/complex_posts/*" do
plain_text "test"
end
end

private class TestNamedGlobAction < TestAction
get "/complex_posts/*:leftover" do
plain_text "test"
end
end

describe "Automatically generated param helpers" do
it "generates helpers for all route params" do
action = TestParamAction.new(build_context, {"param_1" => "param_1_value", "param_2" => "param_2_value"})
Expand All @@ -31,4 +43,24 @@ describe "Automatically generated param helpers" do
typeof(action.optional_1).should eq String?
typeof(action.optional_2).should eq String?
end

it "generates helper for unnamed glob" do
action = TestGlobAction.new(build_context, {"glob" => "globbed/path"})
action.glob.should eq "globbed/path"

action = TestGlobAction.new(build_context, {} of String => String)
action.glob.should be_nil

typeof(action.glob).should eq String?
end

it "generates helper for named glob" do
action = TestNamedGlobAction.new(build_context, {"leftover" => "globbed/path"})
action.leftover.should eq "globbed/path"

action = TestNamedGlobAction.new(build_context, {} of String => String)
action.leftover.should be_nil

typeof(action.leftover).should eq String?
end
end
25 changes: 25 additions & 0 deletions src/lucky/routable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ module Lucky::Routable
{% path_parts = path.split('/').reject(&.empty?) %}
{% path_params = path_parts.select(&.starts_with?(':')) %}
{% optional_path_params = path_parts.select(&.starts_with?("?:")) %}
{% glob_param = path_parts.select(&.starts_with?("*")) %}
{% if glob_param.size > 1 %}
{% glob_param.raise "Only one glob can be in a path, but found more than one." %}
{% end %}
{% glob_param = glob_param.first %}
{% if glob_param && path_parts.last != glob_param %}
{% glob_param.raise "Glob param must be defined at the end of the path." %}
{% end %}

{% for param in path_params %}
{% if param.includes?("-") %}
Expand All @@ -215,6 +223,23 @@ module Lucky::Routable
end
{% end %}

{% if glob_param %}
{% if glob_param.includes?("-") %}
{% glob_param.raise "Named globs must only use underscores. Use #{glob_param.gsub(/-/, "_")} instead of #{glob_param}." %}
{% end %}
{% part = nil %}
{% if glob_param.starts_with?("*:") %}
{% part = glob_param.gsub(/\*:/, "") %}
{% elsif glob_param == "*" %}
{% part = "glob" %}
{% else %}
{% glob_param.raise "Invalid glob format #{glob_param}." %}
{% end %}
def {{ part.id }} : String?
params.get?({{ part.id.symbolize }})
end
{% end %}

def self.path(*args, **named_args) : String
route(*args, **named_args).path
end
Expand Down