sorbet-coerce 0.2.3 → 0.2.4

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: f267197b77c7b2c9ed7558ad7f94818fc54f9af5fc56c99c9d824af47e40bf78
4
- data.tar.gz: 04da4e2a3ebdde38cf4d2e6d60200c365d371250f9d33f588f67adc5b766c6b2
3
+ metadata.gz: 8c5679d22c95a68cf624bef8a9d3f0c62b9b33f158b439b4115b2cb2f18dc508
4
+ data.tar.gz: f74881e16d72bb4b07b5de8072ef1f10d7794e8e6af249f9edcf132628a3a0e2
5
5
  SHA512:
6
- metadata.gz: 6ee1d39f9e9b12ebdb2d3f1f9c4066ef2f7707329dfe10ab3330fc33b9e943ae9560028ec467203efad12bad567eb5b226ae4669be0b378bf1897a3ca84fbe37
7
- data.tar.gz: a989b4a83745234e88e5c1ba5f26bda2d57be9ec0f61d26d4ece75f5db68bc88130fad68ec9fd43c6a4a44ae263231b65c8a0235bd34188b9c875f377ad9ddee
6
+ metadata.gz: 5f0a4df69764c02a35aba2101740beb6445bc77dec5f5edea12c31c7db18837696153e4f41ab5604ba374a610a203377ec38f621c2d93f5e58a57d72e6e05b4b
7
+ data.tar.gz: 334a49158e79661994d826dbd5845b071260d66f5ffeae49d45d22c6c62a2deeeccb11fe3efe8e616856c2eb3bf60c12031795c4cace5817b87e6ca7ebfa633f
@@ -15,13 +15,3 @@ module TypeCoerce
15
15
  class CoercionError < SafeType::CoercionError; end
16
16
  class ShapeError < SafeType::CoercionError; end
17
17
  end
18
-
19
- module T
20
- module Private
21
- module Types
22
- class TypeAlias
23
- def aliased_type; end
24
- end
25
- end
26
- end
27
- end
@@ -59,6 +59,8 @@ module TypeCoerce::Private
59
59
  _convert(value, type.types[nil_idx == 0 ? 1 : 0], raise_coercion_error)
60
60
  end
61
61
  elsif type.is_a?(T::Types::TypedHash)
62
+ return {} if _nil_like?(value, type)
63
+
62
64
  unless value.respond_to?(:map)
63
65
  raise TypeCoerce::ShapeError.new(value, type)
64
66
  end
@@ -72,7 +74,7 @@ module TypeCoerce::Private
72
74
  elsif Object.const_defined?('T::Private::Types::TypeAlias') &&
73
75
  type.is_a?(T::Private::Types::TypeAlias)
74
76
  _convert(value, type.aliased_type, raise_coercion_error)
75
- elsif type < T::Struct
77
+ elsif type.respond_to?(:<) && type < T::Struct
76
78
  args = _build_args(value, type, raise_coercion_error)
77
79
  type.new(args)
78
80
  else
@@ -95,15 +97,18 @@ module TypeCoerce::Private
95
97
  else
96
98
  safe_type_rule = type
97
99
  end
98
- SafeType::coerce(value, safe_type_rule)
100
+
101
+ if safe_type_rule.is_a?(SafeType::Rule)
102
+ SafeType::coerce(value, safe_type_rule)
103
+ else
104
+ type.new(value)
105
+ end
99
106
  rescue SafeType::EmptyValueError, SafeType::CoercionError
100
107
  if raise_coercion_error
101
108
  raise TypeCoerce::CoercionError.new(value, type)
102
109
  else
103
110
  nil
104
111
  end
105
- rescue SafeType::InvalidRuleError
106
- type.new(value)
107
112
  end
108
113
 
109
114
  sig { params(ary: T.untyped, type: T.untyped, raise_coercion_error: T::Boolean).returns(T.untyped) }
@@ -0,0 +1,10 @@
1
+ # typed: true
2
+ module T
3
+ module Private
4
+ module Types
5
+ class TypeAlias
6
+ def aliased_type; end
7
+ end
8
+ end
9
+ end
10
+ end
data/spec/coerce_spec.rb CHANGED
@@ -31,6 +31,10 @@ describe TypeCoerce do
31
31
  const :myhash, T::Hash[String, Integer]
32
32
  end
33
33
 
34
+ class HashParamsWithDefault < T::Struct
35
+ const :myhash, T::Hash[String, Integer], default: Hash['a' => 1]
36
+ end
37
+
34
38
  class CustomType
35
39
  attr_reader :a
36
40
 
@@ -194,6 +198,8 @@ describe TypeCoerce do
194
198
 
195
199
  context 'when dealing with hashes' do
196
200
  it 'coreces correctly' do
201
+ expect(TypeCoerce[T::Hash[T.untyped, T.untyped]].new.from(nil)).to eql({})
202
+
197
203
  expect(TypeCoerce[T::Hash[String, T::Boolean]].new.from({
198
204
  a: 'true',
199
205
  b: 'false',
@@ -206,6 +212,7 @@ describe TypeCoerce do
206
212
  myhash: {'a' => '1', 'b' => '2'},
207
213
  }).myhash).to eql({'a' => 1, 'b' => 2})
208
214
 
215
+ expect(TypeCoerce[HashParamsWithDefault].new.from({}).myhash).to eql({'a' => 1})
209
216
 
210
217
  expect {
211
218
  TypeCoerce[T::Hash[String, T::Boolean]].new.from({
data/spec/nested_spec.rb CHANGED
@@ -75,5 +75,14 @@ describe TypeCoerce do
75
75
  T.nilable(T::Array[T.nilable(T::Array[T.nilable(User)])])
76
76
  ].new.from([[{id: '1'}]]).flatten.map(&:id)).to eql([1])
77
77
  end
78
+
79
+ it 'works with nested T::Hash' do
80
+ expect(
81
+ TypeCoerce[T::Hash[Symbol, T::Hash[Symbol, Integer]]].new.from({
82
+ a: nil,
83
+ b: {c: '1'}
84
+ })
85
+ ).to eql({a: {}, b: {c: 1}})
86
+ end
78
87
  end
79
88
  end
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.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chan Zuckerberg Initiative
@@ -118,10 +118,11 @@ executables: []
118
118
  extensions: []
119
119
  extra_rdoc_files: []
120
120
  files:
121
+ - lib/bundled_rbi/sorbet-coerce.rbi
121
122
  - lib/configuration.rb
122
123
  - lib/private/converter.rb
124
+ - lib/private/coverter.rbi
123
125
  - lib/sorbet-coerce.rb
124
- - rbi/sorbet-coerce.rbi
125
126
  - spec/coerce_spec.rb
126
127
  - spec/nested_spec.rb
127
128
  - spec/soft_error_spec.rb