-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathroute_params.cr
More file actions
142 lines (124 loc) · 3.1 KB
/
Copy pathroute_params.cr
File metadata and controls
142 lines (124 loc) · 3.1 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
require "big"
module ActionController::Route::Param
# Handle this to return a 404
class Error < ArgumentError
def initialize(@message, @parameter = nil, @restriction = nil)
end
getter parameter : String?
getter restriction : String?
end
class MissingError < Error
end
class ValueError < Error
end
# The method for building in support of different route params
abstract struct Conversion
# convert should typically return nil if the conversion failed
# this allows support for Union types, however may not be practical
# or desirable for most.
#
# Nilable Unions against a single type that raises an error is supported
abstract def convert(raw : String)
end
struct ConvertString < Conversion
def convert(raw : String)
raw
end
end
struct ConvertChar < Conversion
def convert(raw : String)
raw[0]?
end
end
struct ConvertBool < Conversion
def initialize(@true_string : String = "true")
end
def convert(raw : String)
raw.strip.downcase == @true_string
end
end
struct ConvertTime < Conversion
def initialize(@format : String? = nil)
end
def convert(raw : String)
if format = @format
Time.parse_utc raw, format
else
Time.parse_iso8601 raw
end
end
end
struct ConvertBigInt < Conversion
def initialize(@base : Int32 = 10)
end
def convert(raw : String)
raw.to_big_i(@base)
end
end
{% begin %}
# Big converters
{%
bigs = {
to_big_d: BigDecimal,
to_big_f: BigFloat,
}
%}
{% for convert, klass in bigs %}
struct Convert{{klass}} < Conversion
def convert(raw : String)
raw.{{convert}}?
end
end
{% end %}
# Float converters
{%
floats = {
to_f32: Float32,
to_f64: Float64,
}
%}
{% for convert, klass in floats %}
struct Convert{{klass}} < Conversion
def initialize(@whitespace : Bool = true, @strict : Bool = true)
end
def convert(raw : String)
raw.{{convert}}?(
whitespace: @whitespace,
strict: @strict,
)
end
end
{% end %}
# Integer converters
{%
ints = {
to_i8: Int8,
to_u8: UInt8,
to_i16: Int16,
to_u16: UInt16,
to_i32: Int32,
to_u32: UInt32,
to_i64: Int64,
to_u64: UInt64,
to_i128: Int128,
to_u128: UInt128,
}
%}
{% for convert, klass in ints %}
struct Convert{{klass}} < Conversion
def initialize(@base : Int32 = 10, @whitespace : Bool = true, @underscore : Bool = false, @prefix : Bool = false, @strict : Bool = true, @leading_zero_is_octal : Bool = false)
end
def convert(raw : String)
raw.{{convert}}?(
base: @base,
whitespace: @whitespace,
underscore: @underscore,
prefix: @prefix,
strict: @strict,
leading_zero_is_octal: @leading_zero_is_octal
)
end
end
{% end %}
{% end %}
end