/
type_casting.rb
302 lines (261 loc) · 7.02 KB
/
type_casting.rb
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
# frozen_string_literal: true
module Dynamoid
# @private
module TypeCasting
def self.cast_attributes(attributes, attributes_options)
{}.tap do |h|
attributes.symbolize_keys.each do |attribute, value|
h[attribute] = cast_field(value, attributes_options[attribute])
end
end
end
def self.cast_field(value, options)
return value if options.nil?
return nil if value.nil?
type_caster = find_type_caster(options)
if type_caster.nil?
raise ArgumentError, "Unknown type #{options[:type]}"
end
type_caster.process(value)
end
def self.find_type_caster(options)
type_caster_class = case options[:type]
when :string then StringTypeCaster
when :integer then IntegerTypeCaster
when :number then NumberTypeCaster
when :set then SetTypeCaster
when :array then ArrayTypeCaster
when :map then MapTypeCaster
when :datetime then DateTimeTypeCaster
when :date then DateTypeCaster
when :raw then RawTypeCaster
when :serialized then SerializedTypeCaster
when :boolean then BooleanTypeCaster
when :binary then BinaryTypeCaster
when Class then CustomTypeCaster
end
if type_caster_class.present?
type_caster_class.new(options)
end
end
class Base
def initialize(options)
@options = options
end
def process(value)
value
end
end
class StringTypeCaster < Base
def process(value)
case value
when true
't'
when false
'f'
when String
value.dup
else
value.to_s
end
end
end
class IntegerTypeCaster < Base
def process(value)
# rubocop:disable Lint/DuplicateBranch
if value == true
1
elsif value == false
0
elsif value.is_a?(String) && value.blank?
nil
elsif value.is_a?(Float) && !value.finite?
nil
elsif !value.respond_to?(:to_i)
nil
else
value.to_i
end
# rubocop:enable Lint/DuplicateBranch
end
end
class NumberTypeCaster < Base
def process(value)
# rubocop:disable Lint/DuplicateBranch
if value == true
1
elsif value == false
0
elsif value.is_a?(Symbol)
value.to_s.to_d
elsif value.is_a?(String) && value.blank?
nil
elsif value.is_a?(Float) && !value.finite?
nil
elsif !value.respond_to?(:to_d)
nil
else
value.to_d
end
# rubocop:enable Lint/DuplicateBranch
end
end
class SetTypeCaster < Base
def process(value)
set = type_cast_to_set(value)
if set.present? && @options[:of].present?
process_typed_set(set)
else
set
end
end
private
def type_cast_to_set(value)
if value.is_a?(Set)
value.dup
elsif value.respond_to?(:to_set)
value.to_set
end
end
def process_typed_set(set)
type_caster = TypeCasting.find_type_caster(element_options)
if type_caster.nil?
raise ArgumentError, "Set element type #{element_type} isn't supported"
end
set.to_set { |el| type_caster.process(el) }
end
def element_type
if @options[:of].is_a?(Hash)
@options[:of].keys.first
else
@options[:of]
end
end
def element_options
if @options[:of].is_a?(Hash)
@options[:of][element_type].dup.tap do |options|
options[:type] = element_type
end
else
{ type: element_type }
end
end
end
class ArrayTypeCaster < Base
def process(value)
array = type_cast_to_array(value)
if array.present? && @options[:of].present?
process_typed_array(array)
else
array
end
end
private
def type_cast_to_array(value)
if value.is_a?(Array)
value.dup
elsif value.respond_to?(:to_a)
value.to_a
end
end
def process_typed_array(array)
type_caster = TypeCasting.find_type_caster(element_options)
if type_caster.nil?
raise ArgumentError, "Set element type #{element_type} isn't supported"
end
array.map { |el| type_caster.process(el) }
end
def element_type
if @options[:of].is_a?(Hash)
@options[:of].keys.first
else
@options[:of]
end
end
def element_options
if @options[:of].is_a?(Hash)
@options[:of][element_type].dup.tap do |options|
options[:type] = element_type
end
else
{ type: element_type }
end
end
end
class MapTypeCaster < Base
def process(value)
return nil if value.nil?
if value.is_a? Hash
value
elsif value.respond_to? :to_hash
value.to_hash
elsif value.respond_to? :to_h
value.to_h
end
end
end
class DateTimeTypeCaster < Base
def process(value)
if !value.respond_to?(:to_datetime)
nil
elsif value.is_a?(String)
dt = begin
DateTime.parse(value)
rescue StandardError
nil
end
if dt
seconds = string_utc_offset(value) || ApplicationTimeZone.utc_offset
offset = seconds_to_offset(seconds)
DateTime.new(dt.year, dt.mon, dt.mday, dt.hour, dt.min, dt.sec, offset)
end
else
value.to_datetime
end
end
private
def string_utc_offset(string)
Date._parse(string)[:offset]
end
# 3600 -> "+01:00"
def seconds_to_offset(seconds)
ActiveSupport::TimeZone.seconds_to_utc_offset(seconds)
end
end
class DateTypeCaster < Base
def process(value)
if value.respond_to?(:to_date)
begin
value.to_date
rescue StandardError
nil
end
end
end
end
class RawTypeCaster < Base
end
class SerializedTypeCaster < Base
end
class BooleanTypeCaster < Base
def process(value)
if value == ''
nil
else
![false, 'false', 'FALSE', 0, '0', 'f', 'F', 'off', 'OFF'].include? value
end
end
end
class BinaryTypeCaster < Base
def process(value)
if value.is_a? String
value.dup
else
value.to_s
end
end
end
class CustomTypeCaster < Base
end
end
end