Repository navigation
Expand file tree
/
Copy pathbuilder.cr
More file actions
392 lines (335 loc) · 16.7 KB
/
Copy pathbuilder.cr
File metadata and controls
392 lines (335 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
require "../router"
require "./route_params"
# Create the method annotations
{% begin %}
{% for http_method in ::ActionController::Router::HTTP_METHODS.reject(&.==("head")) %}
annotation ActionController::Route::{{http_method.upcase.id}}
end
{% end %}
{% end %}
annotation ActionController::Route::WebSocket
end
annotation ActionController::Route::Filter
end
annotation ActionController::Route::Exception
end
module ActionController::Route
class Error < ::Exception
def initialize(message : String? = nil, @accepts : Array(String)? = nil)
super message
end
getter accepts : Array(String)?
end
# we don't support any of the accepted response content types
class NotAcceptable < Error
end
# we don't support the posted media type
class UnsupportedMediaType < Error
end
end
module ActionController::Route::Builder
ROUTE_FUNCTIONS = {} of Nil => Nil
DEFAULT_RESPONDER = ["application/json"]
RESPONDERS = {} of Nil => Nil
macro default_responder(content_type)
{% DEFAULT_RESPONDER[0] = content_type %}
{% raise "no responder available for default content type: #{content_type}" unless RESPONDERS[content_type] %}
end
macro add_responder(content_type, &block)
{% RESPONDERS[content_type] = block %}
end
DEFAULT_PARSER = ["application/json"]
PARSERS = {} of Nil => Nil
macro default_parser(content_type)
{% DEFAULT_PARSER[0] = content_type %}
{% raise "no responder available for default content type: #{content_type}" unless PARSERS[content_type] %}
end
macro add_parser(content_type, &block)
{% PARSERS[content_type] = block %}
end
macro __build_transformer_functions__
{% for type, block in RESPONDERS %}
# :nodoc:
def self.transform_{{type.gsub(/\/|\-|\~|\*|\:|\./, "_").id}}(%instance, {{*block.args}}, *_args)
__yield__(%instance) do
{{block.body}}
end
end
{% end %}
RESPONDER_LIST = [
{% for type, _block in RESPONDERS %}
{{type}},
{% end %}
]
# :nodoc:
def self.can_respond_with?(content_type)
RESPONDER_LIST.includes? content_type
end
# :nodoc:
def self.accepts
RESPONDER_LIST
end
{% for type, block in PARSERS %}
# :nodoc:
def self.parse_{{type.gsub(/\/|\-|\~|\*|\:|\./, "_").id}}({{*block.args}})
{{block.body}}
end
{% end %}
PARSER_LIST = [
{% for type, _block in PARSERS %}
{{type}},
{% end %}
]
# :nodoc:
def self.can_parse?(content_type)
PARSER_LIST.includes? content_type
end
# :nodoc:
def self.parsable
PARSER_LIST
end
end
macro __parse_inferred_routes__
# Check if they have been applied to any of the methods
{% for method in @type.methods %}
{% method_name = method.name %}
{% annotation_found = false %}
# Run through the various route annotations
{% for route_method in {AC::Route::WebSocket, AC::Route::GET, AC::Route::POST, AC::Route::PUT, AC::Route::PATCH, AC::Route::DELETE, AC::Route::OPTIONS, AC::Route::Filter, AC::Route::Exception} %}
{% lower_route_method = route_method.stringify.split("::")[-1].downcase.id %}
# Multiple routes can be applied to a single method
{% for ann, idx in method.annotations(route_method) %}
{% annotation_found = true %}
{% if route_method == AC::Route::Filter && ann[0] == :around_action %}
{% raise "#{@type.name}##{method_name} method must yield" unless method.accepts_block? %}
{% else %}
{% raise "#{@type.name}##{method_name} accepts a block, this is incompatible with the router" if method.accepts_block? %}
{% end %}
# Grab the response details from the annotations
{% content_type = ann[:content_type] %}
{% param_mapping = ann[:map] || {} of SymbolLiteral => SymbolLiteral %} # function argument name => param name
{% status_code = ann[:status_code] || HTTP::Status::OK %}
{% status_code_map = ann[:status] || {} of TypeNode => Path %}
{% body_argument = (ann[:body] || "%").id.stringify %} # % is an invalid argument name
# support annotation based filters
{% if route_method == AC::Route::Filter %}
{% required_params = [] of StringLiteral %}
{% filter_type = ann[0].id %}
{% function_wrapper_name = "_#{filter_type.stringify.underscore.gsub(/\:\:/, "_").id}_#{method_name}_wrapper_".id %}
{{filter_type}}({{function_wrapper_name.symbolize}}, only: {{ann[:only]}}, except: {{ann[:except]}})
# :nodoc:
def {{function_wrapper_name}}
{% elsif route_method == AC::Route::Exception %}
# annotation based exception handlers
{% required_params = [] of StringLiteral %}
{% exception_class = ann[0] %}
{% function_wrapper_name = "_#{exception_class.stringify.underscore.gsub(/\:\:/, "_").id}_#{method_name}_wrapper_".id %}
rescue_from {{exception_class}}, {{function_wrapper_name.symbolize}}
# :nodoc:
def {{function_wrapper_name}}(error)
# Check we can satisfy the accepts header, if provided
{% if content_type %}
responds_with = {{content_type}}
{% else %}
responds_with = accepts_formats.first? || {{ DEFAULT_RESPONDER[0] }}
responds_with = {{ DEFAULT_RESPONDER[0] }} unless {{@type.name.id}}.can_respond_with?(responds_with)
{% end %}
{% else %}
# annotation based route
# Grab the param parts
{% full_route = (NAMESPACE[0] + ann[0]).split("/").reject(&.empty?) %}
{% required_params = full_route.select(&.starts_with?(":")).map { |part| part.split(":")[1] } %}
# {% optional_params = full_route.select(&.starts_with?("?:")).map { |part| part.split(":")[1] } %}
# {% splat_params = full_route.select(&.starts_with?("*:")).map { |part| part.split(":")[1] } %}
# add a redirect helper (yes, it will only match the last route applied)
{% if lower_route_method == "get" %}
def self.{{method_name.id}}(**tuple_parts)
route = {{"/" + full_route.join("/")}}
ActionController::Support.build_route(route, nil, **tuple_parts)
end
{% end %}
# build the standard route definition helper (get "/route")
{% if route_method == AC::Route::WebSocket %}
# :nodoc:
ws {{ann[0]}}, reference: {{method_name}} do |socket|
{% else %}
# :nodoc:
{{lower_route_method}} {{ann[0]}}, reference: {{method_name}} do
# Check we can satisfy the accepts header, if provided
{% if content_type %}
responds_with = {{content_type}}
{% else %}
responds_with = accepts_formats.first? || {{ DEFAULT_RESPONDER[0] }}
raise AC::Route::NotAcceptable.new("no renderer available for #{responds_with}", {{@type.name.id}}.accepts) unless {{@type.name.id}}.can_respond_with?(responds_with)
{% end %}
{% end %}
{% end %}
# grab any custom converters or customisations
{% converters = ann[:converters] || {} of SymbolLiteral => NilLiteral %}
{% config = ann[:config] || {} of SymbolLiteral => NilLiteral %}
# Write the method body
{% if method.args.empty? || ({AC::Route::Exception, AC::Route::WebSocket}.includes?(route_method) && method.args.size == 1) %}
{% if route_method == AC::Route::WebSocket %}
{{method_name.id}}(socket)
{% elsif route_method == AC::Route::Exception %}
result = {{method_name.id}}(error)
{% else %}
result = {{method_name.id}}
{% end %}
{% else %}
# check we can parse the body if a content type is provided
{% if body_argument != "%" %}
body_type = @context.request.headers["Content-Type"]? || {{ DEFAULT_PARSER[0] }}
unless {{@type.name.id}}.can_parse?(body_type)
raise AC::Route::UnsupportedMediaType.new("no parser available for #{body_type}", {{@type.name.id}}.parsable)
end
{% end %}
args = {
{% for arg, arg_index in method.args %}
{% unless arg_index == 0 && {AC::Route::Exception, AC::Route::WebSocket}.includes?(route_method) %}
{% string_name = arg.name.id.stringify %}
{% query_param_name = (param_mapping[string_name.id.symbolize] || string_name).id.stringify %}
# Calculate the conversions required to meet the desired restrictions
{% if arg.restriction %}
# Check if restriction is optional
{% nilable = arg.restriction.resolve.nilable? %}
# Check if there are any custom converters
{% if custom_converter = converters[string_name.id.symbolize] %}
{% if converter_args = config[string_name.id.symbolize] %}
{% restrictions = [custom_converter.stringify + ".new(**" + converter_args.stringify + ").convert(param_value)"] %}
{% else %}
{% restrictions = [custom_converter.stringify + ".new.convert(param_value)"] %}
{% end %}
# Check for custom converter arguments (assumes a single type)
{% elsif converter_args = config[string_name.id.symbolize] %}
{% union_types = arg.restriction.resolve.union_types.reject(&.nilable?) %}
{% if union_types[0] < Enum %}
{% if converter_args[:from_value] %}
{% restrictions = [union_types[0].stringify + ".from_value?(param_value.to_i64)"] %}
{% else %}
{% restrictions = [union_types[0].stringify + ".parse?(param_value)"] %}
{% end %}
{% else %}
{% restrictions = ["::AC::Route::Param::Convert" + union_types[0].stringify + ".new(**" + converter_args.stringify + ").convert(param_value)"] %}
{% end %}
# do we want to parse the request body
{% elsif body_argument == string_name %}
# ignore this arg here
# There are a bunch of types this might be
{% else %}
{% union_types = arg.restriction.resolve.union_types.reject(&.nilable?) %}
{% if union_types[0] < Enum %}
{% restrictions = [union_types[0].stringify + ".parse?(param_value)"] %}
{% else %}
{% restrictions = union_types.map { |type| "::AC::Route::Param::Convert" + type.stringify + ".new.convert(param_value)" } %}
{% end %}
{% end %}
{% else %}
{% nilable = true %}
{% restrictions = ["::AC::Route::Param::ConvertString.new.convert(param_value)"] %}
{% end %}
# Build the argument named tuple with the correct types
{{arg.name.id}}: (
{% if body_argument == string_name %}
if body_io = @context.request.body
case body_type
{% for type, _block in PARSERS %}
when {{type}}
{{@type.name.id}}.parse_{{type.gsub(/\/|\-|\~|\*|\:|\./, "_").id}}({{ arg.restriction }}, body_io)
{% end %}
end
end
# Required route param, so we ensure it
{% elsif required_params.includes? string_name %}
if param_value = route_params[{{query_param_name}}]?
{{restrictions.join(" || ").id}}
else
raise ::AC::Route::Param::MissingError.new("missing required parameter", {{string_name}}, {{arg.restriction.resolve.stringify}})
end
# An optional route param, might be passed as an query param
{% else %}
if param_value = params[{{query_param_name}}]?
{{restrictions.join(" || ").id}}
{% if arg.default_value %}
else
{{arg.default_value}}
{% end %}
end
{% end %}
# Use tap to ensure a good error message if the function param isn't nilable
){% if !nilable %}.tap { |result| raise AC::Route::Param::ValueError.new("invalid parameter value", {{string_name}}, {{arg.restriction.resolve.stringify}}) if result.nil? }.not_nil!{% end %},
{% end %}
{% end %}
}
{% if route_method == AC::Route::WebSocket %}
{{method_name.id}}(socket, **args)
{% elsif route_method == AC::Route::Exception %}
result = {{method_name.id}}(error, **args)
{% else %}
result = {{method_name.id}}(**args){% if route_method == AC::Route::Filter && ann[0] == :around_action %} { yield } {% end %}
{% end %}
{% end %}
{% if !{AC::Route::Filter, AC::Route::WebSocket}.includes?(route_method) %}
unless @render_called
responose = @context.response
{% if status_code_map.empty? %}
response.status_code = ({{status_code}}).to_i
{% else %}
case result
{% for result_klass, status_mapped in status_code_map %}
when {{result_klass}}
response.status_code = ({{status_mapped}}).to_i
{% end %}
else
response.status_code = ({{status_code}}).to_i
end
{% end %}
content_type = response.headers["Content-Type"]?
response.headers["Content-Type"] = responds_with unless content_type
session = @__session__
session.encode(response.cookies) if session && session.modified
unless @__head_request__
case responds_with
{% for type, _block in RESPONDERS %}
when {{type}}
{{@type.name.id}}.transform_{{type.gsub(/\/|\-|\~|\*|\:|\./, "_").id}}(self, response, result, {{@type.name.underscore.symbolize}}, {{method_name.id.symbolize}})
{% end %}
else
# return the default, which is allowed in HTTP 1.1
# we've checked the accepts header at the top of the function and this might be an error response
{{@type.name.id}}.transform_{{DEFAULT_RESPONDER[0].gsub(/\/|\-|\~|\*|\:|\./, "_").id}}(self, response, result, {{@type.name.underscore.symbolize}}, {{method_name.id.symbolize}})
end
end
@render_called = true
end
{% end %}
end
{% end %}
{% end %}
{% if annotation_found %}
# ensure this method hasn't already been used (overloading is undesirable)
{% klasses = [@type.name.id] %}
{% @type.ancestors.each { |name| klasses.unshift(name) } %}
{% for klass in klasses %}
{% if ROUTE_FUNCTIONS["#{klass}##{method_name}"] %}
{% raise "#{@type.name.id}##{method_name} already exists in #{klass}, duplicate annotated function names are not allowed" %}
{% end %}
{% end %}
{% ROUTE_FUNCTIONS["#{@type.name.id}##{method_name}"] = true %}
{% end %}
{% end %}
end
macro included
# JSON APIs by default
add_responder("application/json") { |io, result| result.to_json(io) }
default_responder "application/json"
add_parser("application/json") { |klass, body_io| klass.from_json(body_io.gets_to_end) }
default_parser "application/json"
macro inherited
macro finished
__build_transformer_functions__
__parse_inferred_routes__
end
end
end
end