Skip to content

Commit

Permalink
refactor (Params): improve containers naming
Browse files Browse the repository at this point in the history
  • Loading branch information
vladfaust committed Feb 26, 2019
1 parent cc10d71 commit 066bea8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
18 changes: 9 additions & 9 deletions src/onyx-rest/endpoint/params/form.cr
Expand Up @@ -75,7 +75,7 @@ module Onyx::REST::Endpoint
# > curl -X POST -d "user[email]=foo@example.com" http://localhost:5000/users/42
# ```
macro form(required = false, &block)
class FormBodyError < Onyx::REST::Error(PARAMS_ERROR_CODE)
class FormError < Onyx::REST::Error(PARAMS_ERROR_CODE)
def initialize(message : String, @path : Array(String))
super(message)
end
Expand All @@ -85,7 +85,7 @@ module Onyx::REST::Endpoint
end
end

struct FormParams
struct Form
include ::HTTP::Params::Serializable

{% verbatim do %}
Expand Down Expand Up @@ -114,10 +114,10 @@ module Onyx::REST::Endpoint

{% if block.body.is_a?(Expressions) %}
{% for expression in block.body.expressions %}
FormParams.{{expression}}
Form.{{expression}}
{% end %}
{% elsif block.body.is_a?(Call) %}
FormParams.{{yield.id}}
Form.{{yield.id}}
{% else %}
{% raise "BUG: Unhandled block body type #{block.body.class_name}" %}
{% end %}
Expand All @@ -138,9 +138,9 @@ module Onyx::REST::Endpoint
end

{% if required %}
getter! form : FormParams
getter! form : Form
{% else %}
getter form : FormParams?
getter form : Form?
{% end %}

def initialize(request : ::HTTP::Request)
Expand All @@ -152,15 +152,15 @@ module Onyx::REST::Endpoint
if request.headers["Content-Type"]?.try &.=~ /^application\/x-www-form-urlencoded/
{% end %}
if body = request.body
@form = FormParams.from_query(body.gets_to_end)
@form = Form.from_query(body.gets_to_end)
else
raise FormBodyError.new("Missing request body", [] of String)
raise FormError.new("Missing request body", [] of String)
end
{% unless required %}
end
{% end %}
rescue ex : ::HTTP::Params::Serializable::Error
raise FormBodyError.new("Form p" + ex.message.not_nil![1..-1], ex.path)
raise FormError.new("Form p" + ex.message.not_nil![1..-1], ex.path)
end
{% end %}
end
Expand Down
28 changes: 14 additions & 14 deletions src/onyx-rest/endpoint/params/json.cr
Expand Up @@ -76,17 +76,17 @@ module Onyx::REST::Endpoint
# > curl -X POST -d '{"user":{"email":"foo@example.com"}}' http://localhost:5000/users/1
# ```
macro json(required = false, &block)
class JSONBodyError < Onyx::REST::Error(PARAMS_ERROR_CODE)
class JSONError < Onyx::REST::Error(PARAMS_ERROR_CODE)
end

struct JSONBody
include JSON::Serializable
struct JSON
include ::JSON::Serializable

{% verbatim do %}
macro type(argument, nilable = false, **options, &block)
{% if block %}
{% unless options.empty? %}
@[JSON::Field({{**options}})]
@[::JSON::Field({{**options}})]
{% end %}

{% if argument.is_a?(Path) %}
Expand All @@ -104,21 +104,21 @@ module Onyx::REST::Endpoint
{% elsif argument.is_a?(Call) %}
struct {{argument.name.camelcase.id}}
{% end %}
include JSON::Serializable
include ::JSON::Serializable

{% if block.body.is_a?(Expressions) %}
{% for expression in block.body.expressions %}
JSONBody.{{expression}}
JSON.{{expression}}
{% end %}
{% elsif block.body.is_a?(Call) %}
JSONBody.{{yield.id}}
JSON.{{yield.id}}
{% else %}
{% raise "BUG: Unhandled block body type #{block.body.class_name}" %}
{% end %}
end
{% elsif argument.is_a?(TypeDeclaration) %}
{% unless options.empty? %}
@[JSON::Field({{**options}})]
@[::JSON::Field({{**options}})]
{% end %}

getter {{argument}}
Expand All @@ -132,9 +132,9 @@ module Onyx::REST::Endpoint
end

{% if required %}
getter! json : JSONBody
getter! json : JSON
{% else %}
getter json : JSONBody?
getter json : JSON?
{% end %}

def initialize(request : HTTP::Request)
Expand All @@ -146,15 +146,15 @@ module Onyx::REST::Endpoint
if request.headers["Content-Type"]?.try &.=~ /^application\/json/
{% end %}
if body = request.body
@json = JSONBody.from_json(body.gets_to_end)
@json = JSON.from_json(body.gets_to_end)
else
raise JSONBodyError.new("Missing request body")
raise JSONError.new("Missing request body")
end
{% unless required %}
end
{% end %}
rescue ex : JSON::MappingError
raise JSONBodyError.new(ex.message.not_nil!.lines.first)
rescue ex : ::JSON::MappingError
raise JSONError.new(ex.message.not_nil!.lines.first)
end
{% end %}
end
Expand Down
8 changes: 4 additions & 4 deletions src/onyx-rest/endpoint/params/path.cr
Expand Up @@ -42,7 +42,7 @@ module Onyx::REST::Endpoint
end
end

struct Path
struct PathParams
include ::HTTP::Params::Serializable

{% verbatim do %}
Expand All @@ -64,16 +64,16 @@ module Onyx::REST::Endpoint
{{yield.id}}
end

@path = uninitialized Path
@path = uninitialized PathParams
getter path

def initialize(request : ::HTTP::Request)
previous_def

@path = uninitialized Path
@path = uninitialized PathParams

begin
@path = Path.from_query(request.path_params.join('&'){ |(k, v)| "#{k}=#{v}" })
@path = PathParams.from_query(request.path_params.join('&'){ |(k, v)| "#{k}=#{v}" })
rescue ex : ::HTTP::Params::Serializable::Error
raise PathParamsError.new("Path p" + ex.message.not_nil![1..-1], ex.path)
end
Expand Down
12 changes: 6 additions & 6 deletions src/onyx-rest/endpoint/params/query.cr
Expand Up @@ -38,7 +38,7 @@ module Onyx::REST::Endpoint
end
end

struct Query
struct QueryParams
include ::HTTP::Params::Serializable

{% verbatim do %}
Expand Down Expand Up @@ -67,10 +67,10 @@ module Onyx::REST::Endpoint

{% if block.body.is_a?(Expressions) %}
{% for expression in block.body.expressions %}
Query.{{expression}}
QueryParams.{{expression}}
{% end %}
{% elsif block.body.is_a?(Call) %}
Query.{{yield.id}}
QueryParams.{{yield.id}}
{% else %}
{% raise "BUG: Unhandled block body type #{block.body.class_name}" %}
{% end %}
Expand All @@ -90,16 +90,16 @@ module Onyx::REST::Endpoint
{{yield.id}}
end

@query = uninitialized Query
@query = uninitialized QueryParams
getter query

def initialize(request : ::HTTP::Request)
previous_def

@query = uninitialized Query
@query = uninitialized QueryParams

begin
@query = Query.from_query(request.query.to_s)
@query = QueryParams.from_query(request.query.to_s)
rescue ex : ::HTTP::Params::Serializable::Error
raise QueryParamsError.new("Query p" + ex.message.not_nil![1..-1], ex.path)
end
Expand Down

0 comments on commit 066bea8

Please sign in to comment.