sorbet-coerce 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/private/converter.rb +11 -0
- data/rbi/sorbet-coerce.rbi +7 -0
- data/spec/coerce_spec.rb +32 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5c3d4dbcef7e149630285f69ffdb189af9c939a6fc23569550f98c8f6dee5fa
|
4
|
+
data.tar.gz: d6692abfe404b6cc9c0a7e3e79a7c3ff43416f1a68ab9c2140f552d17395141e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71d5ff39ed7674752b3437e812d4518c7f3e51d28d517e47fcdaea32d0d548c8e56eb9178158a8db990fa15d1dce71f91697a4250c99578dda1b05fa91503e95
|
7
|
+
data.tar.gz: bf4534d41c56e2e82a5a5219099dcefa8d96fd4887670b50c86dfd60dede71710dbc934addafb83e833a7a7ce221789244089ddfb5caec637e84af57e1bf325e
|
data/lib/private/converter.rb
CHANGED
@@ -51,6 +51,17 @@ module T::Private
|
|
51
51
|
else
|
52
52
|
_convert(value, type.types[nil_idx == 0 ? 1 : 0], raise_coercion_error)
|
53
53
|
end
|
54
|
+
elsif type.is_a?(T::Types::TypedHash)
|
55
|
+
unless value.respond_to?(:map)
|
56
|
+
raise T::Coerce::ShapeError.new(value, type)
|
57
|
+
end
|
58
|
+
|
59
|
+
value.map do |k, v|
|
60
|
+
[
|
61
|
+
_convert(k, type.keys, raise_coercion_error),
|
62
|
+
_convert(v, type.values, raise_coercion_error),
|
63
|
+
]
|
64
|
+
end.to_h
|
54
65
|
elsif Object.const_defined?('T::Private::Types::TypeAlias') &&
|
55
66
|
type.is_a?(T::Private::Types::TypeAlias)
|
56
67
|
_convert(value, type.aliased_type, raise_coercion_error)
|
data/rbi/sorbet-coerce.rbi
CHANGED
@@ -1,4 +1,8 @@
|
|
1
1
|
# typed: true
|
2
|
+
module SafeType
|
3
|
+
class CoercionError < StandardError; end
|
4
|
+
end
|
5
|
+
|
2
6
|
module T
|
3
7
|
module Coerce
|
4
8
|
extend T::Sig
|
@@ -8,6 +12,9 @@ module T
|
|
8
12
|
|
9
13
|
sig { params(args: T.untyped, raise_coercion_error: T.nilable(T::Boolean)).returns(Elem) }
|
10
14
|
def from(args, raise_coercion_error: nil); end
|
15
|
+
|
16
|
+
class CoercionError < SafeType::CoercionError; end
|
17
|
+
class ShapeError < SafeType::CoercionError; end
|
11
18
|
end
|
12
19
|
|
13
20
|
module Private
|
data/spec/coerce_spec.rb
CHANGED
@@ -27,6 +27,10 @@ describe T::Coerce do
|
|
27
27
|
const :a, Integer, default: 1
|
28
28
|
end
|
29
29
|
|
30
|
+
class HashParams < T::Struct
|
31
|
+
const :myhash, T::Hash[String, Integer]
|
32
|
+
end
|
33
|
+
|
30
34
|
class CustomType
|
31
35
|
attr_reader :a
|
32
36
|
|
@@ -188,6 +192,34 @@ describe T::Coerce do
|
|
188
192
|
end
|
189
193
|
end
|
190
194
|
|
195
|
+
context 'when dealing with hashes' do
|
196
|
+
it 'coreces correctly' do
|
197
|
+
expect(T::Coerce[T::Hash[String, T::Boolean]].new.from({
|
198
|
+
a: 'true',
|
199
|
+
b: 'false',
|
200
|
+
})).to eql({
|
201
|
+
'a' => true,
|
202
|
+
'b' => false,
|
203
|
+
})
|
204
|
+
|
205
|
+
expect(T::Coerce[HashParams].new.from({
|
206
|
+
myhash: {'a' => '1', 'b' => '2'},
|
207
|
+
}).myhash).to eql({'a' => 1, 'b' => 2})
|
208
|
+
|
209
|
+
|
210
|
+
expect {
|
211
|
+
T::Coerce[T::Hash[String, T::Boolean]].new.from({
|
212
|
+
a: 'invalid',
|
213
|
+
b: 'false',
|
214
|
+
})
|
215
|
+
}.to raise_error(T::Coerce::CoercionError)
|
216
|
+
|
217
|
+
expect {
|
218
|
+
T::Coerce[T::Hash[String, Integer]].new.from(1)
|
219
|
+
}.to raise_error(T::Coerce::ShapeError)
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
191
223
|
context 'when given a type alias' do
|
192
224
|
MyType = T.type_alias(T::Boolean)
|
193
225
|
|