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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f794cd6c92bb1d7e6481a4fb26b9073ac278f8e3bb379f835333128053f251d6
4
- data.tar.gz: 5013d33d3021f8da6784c7dfa8ff1014d40791d201a6a2b5c41ca31c64968454
3
+ metadata.gz: e5c3d4dbcef7e149630285f69ffdb189af9c939a6fc23569550f98c8f6dee5fa
4
+ data.tar.gz: d6692abfe404b6cc9c0a7e3e79a7c3ff43416f1a68ab9c2140f552d17395141e
5
5
  SHA512:
6
- metadata.gz: f605e6bc1e70dd0bb913cc12fec39d6b171768ff031ad3e67ad0917404fc0c27058c6dc7d15e75e8e2fac38727f3e68a9d48372e40c4ef4cc4d430e4a617dc91
7
- data.tar.gz: a8f8e5d9d3ebdb9b9766e6dbf338e8bb7abf8a5dbf551721904f8e8083a4adad7471164e0a52c0a1cc7c846d19e9b5f1709a6f01916637867374cecd1a190f7a
6
+ metadata.gz: 71d5ff39ed7674752b3437e812d4518c7f3e51d28d517e47fcdaea32d0d548c8e56eb9178158a8db990fa15d1dce71f91697a4250c99578dda1b05fa91503e95
7
+ data.tar.gz: bf4534d41c56e2e82a5a5219099dcefa8d96fd4887670b50c86dfd60dede71710dbc934addafb83e833a7a7ce221789244089ddfb5caec637e84af57e1bf325e
@@ -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)
@@ -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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sorbet-coerce
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chan Zuckerberg Initiative