-
Notifications
You must be signed in to change notification settings - Fork 12
/
converter.rb
207 lines (176 loc) · 6.03 KB
/
converter.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
# typed: ignore
require 'polyfill'
require 'safe_type'
require 'set'
require 'sorbet-runtime'
require 'sorbet-coerce/configuration'
using Polyfill(Hash: %w[#slice])
module TypeCoerce
class CoercionError < SafeType::CoercionError; end
class ShapeError < SafeType::CoercionError; end
end
class TypeCoerce::Converter
def initialize(type)
@type = type
end
def new
self
end
def to_s
"#{name}#[#{@type.to_s}]"
end
def from(args, raise_coercion_error: nil, coerce_empty_to_nil: false)
if raise_coercion_error.nil?
raise_coercion_error = TypeCoerce::Configuration.raise_coercion_error
end
T.let(_convert(args, @type, raise_coercion_error, coerce_empty_to_nil), @type)
end
private
PRIMITIVE_TYPES = T.let(::Set[
Date,
DateTime,
Float,
Integer,
String,
Symbol,
Time,
], T.untyped)
def _convert(value, type, raise_coercion_error, coerce_empty_to_nil)
if type.is_a?(T::Types::Untyped)
value
elsif type.is_a?(T::Types::ClassOf)
value
elsif type.is_a?(T::Types::TypedArray)
_convert_to_a(value, type.type, raise_coercion_error, coerce_empty_to_nil)
elsif type.is_a?(T::Types::FixedArray)
_convert_to_a(value, type.types, raise_coercion_error, coerce_empty_to_nil)
elsif type.is_a?(T::Types::TypedSet)
Set.new(_convert_to_a(value, type.type, raise_coercion_error, coerce_empty_to_nil))
elsif type.is_a?(T::Types::Simple)
_convert(value, type.raw_type, raise_coercion_error, coerce_empty_to_nil)
elsif type.is_a?(T::Types::Union)
true_idx = T.let(nil, T.nilable(Integer))
false_idx = T.let(nil, T.nilable(Integer))
nil_idx = T.let(nil, T.nilable(Integer))
type.types.each_with_index do |t, i|
nil_idx = i if t.is_a?(T::Types::Simple) && t.raw_type == NilClass
true_idx = i if t.is_a?(T::Types::Simple) && t.raw_type == TrueClass
false_idx = i if t.is_a?(T::Types::Simple) && t.raw_type == FalseClass
end
return value unless (
(!true_idx.nil? && !false_idx.nil? && !nil_idx.nil?) || # T.nilable(T::Boolean)
(type.types.length == 2 && (
!nil_idx.nil? || (!true_idx.nil? && !false_idx.nil?) # T.nilable || T::Boolean
))
)
if !true_idx.nil? && !false_idx.nil?
_convert_simple(value, T::Boolean, raise_coercion_error, coerce_empty_to_nil)
else
_convert(value, type.types[nil_idx == 0 ? 1 : 0], raise_coercion_error, coerce_empty_to_nil)
end
elsif type.is_a?(T::Types::TypedHash)
return {} if _nil_like?(value, type, coerce_empty_to_nil)
unless value.respond_to?(:map)
raise TypeCoerce::ShapeError.new(value, type)
end
value.map do |k, v|
[
_convert(k, type.keys, raise_coercion_error, coerce_empty_to_nil),
_convert(v, type.values, raise_coercion_error, coerce_empty_to_nil),
]
end.to_h
elsif Object.const_defined?('T::Private::Types::TypeAlias') &&
type.is_a?(T::Private::Types::TypeAlias)
_convert(value, type.aliased_type, raise_coercion_error, coerce_empty_to_nil)
elsif type.is_a?(Class) || type.is_a?(Module)
return coerce_nil(value, type, coerce_empty_to_nil) if value.is_a?(type)
if type < T::Struct
args = _build_args(value, type, raise_coercion_error, coerce_empty_to_nil)
type.new(args)
elsif type < T::Enum
_convert_enum(value, type, raise_coercion_error, coerce_empty_to_nil)
else
_convert_simple(value, type, raise_coercion_error, coerce_empty_to_nil)
end
else
if raise_coercion_error
raise TypeCoerce::CoercionError.new(value, type)
else
value
end
end
end
def _convert_enum(value, type, raise_coercion_error, coerce_empty_to_nil)
if raise_coercion_error
type.deserialize(value)
else
type.try_deserialize(value)
end
rescue KeyError
raise TypeCoerce::CoercionError.new(value, type)
end
def _convert_simple(value, type, raise_coercion_error, coerce_empty_to_nil)
return nil if _nil_like?(value, type, coerce_empty_to_nil)
safe_type_rule = T.let(nil, T.untyped)
if type == T::Boolean
safe_type_rule = SafeType::Boolean.strict
elsif value.is_a?(type)
return value
elsif type == BigDecimal
return BigDecimal(value)
elsif PRIMITIVE_TYPES.include?(type)
safe_type_rule = SafeType.const_get(type.name).strict
else
safe_type_rule = type
end
if safe_type_rule.is_a?(SafeType::Rule)
SafeType::coerce(value, safe_type_rule)
else
type.new(value)
end
rescue SafeType::EmptyValueError, SafeType::CoercionError
if raise_coercion_error
raise TypeCoerce::CoercionError.new(value, type)
else
nil
end
end
def _convert_to_a(ary, type, raise_coercion_error, coerce_empty_to_nil)
return [] if _nil_like?(ary, type, coerce_empty_to_nil)
unless ary.respond_to?(:map)
raise TypeCoerce::ShapeError.new(ary, type)
end
ary.map.with_index do |value, i|
if type.is_a?(Array)
_convert(value, type[i], raise_coercion_error, coerce_empty_to_nil)
else
_convert(value, type, raise_coercion_error, coerce_empty_to_nil)
end
end
end
def _build_args(args, type, raise_coercion_error, coerce_empty_to_nil)
return {} if _nil_like?(args, Hash, coerce_empty_to_nil)
unless args.respond_to?(:each_pair)
raise TypeCoerce::ShapeError.new(args, type)
end
props = type.props
args.map { |name, value|
key = name.to_sym
[
key,
(!props.include?(key) || value.nil?) ?
nil : _convert(value, props[key][:type], raise_coercion_error, coerce_empty_to_nil),
]
}.to_h.slice(*props.keys)
end
def _nil_like?(value, type, coerce_empty_to_nil)
return true if value.nil?
return true if value == '' && coerce_empty_to_nil
return true if value == '' && type != String
false
end
def coerce_nil(value, type, coerce_empty_to_nil)
return nil if _nil_like?(value, type, coerce_empty_to_nil)
value
end
end