-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
setter_factory.rb
257 lines (245 loc) 路 8.6 KB
/
setter_factory.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
# frozen_string_literal: true
# typed: strict
module T::Props
module Private
module SetterFactory
extend T::Sig
SetterProc = T.type_alias {T.proc.params(val: T.untyped).void}
ValueValidationProc = T.type_alias {T.proc.params(val: T.untyped).void}
ValidateProc = T.type_alias {T.proc.params(prop: Symbol, value: T.untyped).void}
sig do
params(
klass: T.all(Module, T::Props::ClassMethods),
prop: Symbol,
rules: T::Hash[Symbol, T.untyped]
)
.returns([SetterProc, ValueValidationProc])
.checked(:never)
end
def self.build_setter_proc(klass, prop, rules)
# Our nil check works differently than a simple T.nilable for various
# reasons (including the `raise_on_nil_write` setting and the existence
# of defaults & factories), so unwrap any T.nilable and do a check
# manually.
non_nil_type = T::Utils::Nilable.get_underlying_type_object(rules.fetch(:type_object))
accessor_key = rules.fetch(:accessor_key)
validate = rules[:setter_validate]
# It seems like a bug that this affects the behavior of setters, but
# some existing code relies on this behavior
has_explicit_nil_default = rules.key?(:default) && rules.fetch(:default).nil?
# Use separate methods in order to ensure that we only close over necessary
# variables
if !T::Props::Utils.need_nil_write_check?(rules) || has_explicit_nil_default
if validate.nil? && non_nil_type.is_a?(T::Types::Simple)
simple_nilable_proc(prop, accessor_key, non_nil_type.raw_type, klass)
else
nilable_proc(prop, accessor_key, non_nil_type, klass, validate)
end
else
if validate.nil? && non_nil_type.is_a?(T::Types::Simple)
simple_non_nil_proc(prop, accessor_key, non_nil_type.raw_type, klass)
else
non_nil_proc(prop, accessor_key, non_nil_type, klass, validate)
end
end
end
sig do
params(
prop: Symbol,
accessor_key: Symbol,
non_nil_type: Module,
klass: T.all(Module, T::Props::ClassMethods),
)
.returns([SetterProc, ValueValidationProc])
end
private_class_method def self.simple_non_nil_proc(prop, accessor_key, non_nil_type, klass)
[
proc do |val|
unless val.is_a?(non_nil_type)
T::Props::Private::SetterFactory.raise_pretty_error(
klass,
prop,
T::Utils.coerce(non_nil_type),
val,
)
end
instance_variable_set(accessor_key, val)
end,
proc do |val|
unless val.is_a?(non_nil_type)
T::Props::Private::SetterFactory.raise_pretty_error(
klass,
prop,
T::Utils.coerce(non_nil_type),
val,
)
end
end,
]
end
sig do
params(
prop: Symbol,
accessor_key: Symbol,
non_nil_type: T::Types::Base,
klass: T.all(Module, T::Props::ClassMethods),
validate: T.nilable(ValidateProc)
)
.returns([SetterProc, ValueValidationProc])
end
private_class_method def self.non_nil_proc(prop, accessor_key, non_nil_type, klass, validate)
[
proc do |val|
# this use of recursively_valid? is intentional: unlike for
# methods, we want to make sure data at the 'edge'
# (e.g. models that go into databases or structs serialized
# from disk) are correct, so we use more thorough runtime
# checks there
if non_nil_type.recursively_valid?(val)
validate&.call(prop, val)
else
T::Props::Private::SetterFactory.raise_pretty_error(
klass,
prop,
non_nil_type,
val,
)
end
instance_variable_set(accessor_key, val)
end,
proc do |val|
# this use of recursively_valid? is intentional: unlike for
# methods, we want to make sure data at the 'edge'
# (e.g. models that go into databases or structs serialized
# from disk) are correct, so we use more thorough runtime
# checks there
if non_nil_type.recursively_valid?(val)
validate&.call(prop, val)
else
T::Props::Private::SetterFactory.raise_pretty_error(
klass,
prop,
non_nil_type,
val,
)
end
end,
]
end
sig do
params(
prop: Symbol,
accessor_key: Symbol,
non_nil_type: Module,
klass: T.all(Module, T::Props::ClassMethods),
)
.returns([SetterProc, ValueValidationProc])
end
private_class_method def self.simple_nilable_proc(prop, accessor_key, non_nil_type, klass)
[
proc do |val|
unless val.nil? || val.is_a?(non_nil_type)
T::Props::Private::SetterFactory.raise_pretty_error(
klass,
prop,
T::Utils.coerce(non_nil_type),
val,
)
end
instance_variable_set(accessor_key, val)
end,
proc do |val|
unless val.nil? || val.is_a?(non_nil_type)
T::Props::Private::SetterFactory.raise_pretty_error(
klass,
prop,
T::Utils.coerce(non_nil_type),
val,
)
end
end,
]
end
sig do
params(
prop: Symbol,
accessor_key: Symbol,
non_nil_type: T::Types::Base,
klass: T.all(Module, T::Props::ClassMethods),
validate: T.nilable(ValidateProc),
)
.returns([SetterProc, ValueValidationProc])
end
private_class_method def self.nilable_proc(prop, accessor_key, non_nil_type, klass, validate)
[
proc do |val|
if val.nil?
instance_variable_set(accessor_key, nil)
# this use of recursively_valid? is intentional: unlike for
# methods, we want to make sure data at the 'edge'
# (e.g. models that go into databases or structs serialized
# from disk) are correct, so we use more thorough runtime
# checks there
elsif non_nil_type.recursively_valid?(val)
validate&.call(prop, val)
instance_variable_set(accessor_key, val)
else
T::Props::Private::SetterFactory.raise_pretty_error(
klass,
prop,
non_nil_type,
val,
)
instance_variable_set(accessor_key, val)
end
end,
proc do |val|
if val.nil?
# this use of recursively_valid? is intentional: unlike for
# methods, we want to make sure data at the 'edge'
# (e.g. models that go into databases or structs serialized
# from disk) are correct, so we use more thorough runtime
# checks there
elsif non_nil_type.recursively_valid?(val)
validate&.call(prop, val)
else
T::Props::Private::SetterFactory.raise_pretty_error(
klass,
prop,
non_nil_type,
val,
)
end
end,
]
end
sig do
params(
klass: T.all(Module, T::Props::ClassMethods),
prop: Symbol,
type: T.any(T::Types::Base, Module),
val: T.untyped,
)
.void
end
def self.raise_pretty_error(klass, prop, type, val)
base_message = "Can't set #{klass.name}.#{prop} to #{val.inspect} (instance of #{val.class}) - need a #{type}"
pretty_message = "Parameter '#{prop}': #{base_message}\n"
caller_loc = caller_locations.find {|l| !l.to_s.include?('sorbet-runtime/lib/types/props')}
if caller_loc
pretty_message += "Caller: #{caller_loc.path}:#{caller_loc.lineno}\n"
end
T::Configuration.call_validation_error_handler(
nil,
message: base_message,
pretty_message: pretty_message,
kind: 'Parameter',
name: prop,
type: type,
value: val,
location: caller_loc,
)
end
end
end
end