sorbet-coerce 0.2.1 → 0.2.6

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: 61b6f639daa4889060a9540e4fd31f7fbfe19529dac7246a91dc4ea9355b9d70
4
+ data.tar.gz: 7d0ec3164d26cbf16bc45e94820f02142197191e3cc7ae4db7e8c21db351a0f3
5
5
  SHA512:
6
- metadata.gz: f605e6bc1e70dd0bb913cc12fec39d6b171768ff031ad3e67ad0917404fc0c27058c6dc7d15e75e8e2fac38727f3e68a9d48372e40c4ef4cc4d430e4a617dc91
7
- data.tar.gz: a8f8e5d9d3ebdb9b9766e6dbf338e8bb7abf8a5dbf551721904f8e8083a4adad7471164e0a52c0a1cc7c846d19e9b5f1709a6f01916637867374cecd1a190f7a
6
+ metadata.gz: d2ce360c7fd4474d57e3fc5b1115d3711ae41b177a60dcc2f07239f602aab45ab2a6a0ba429df9d7d2edc7df4689cac4f76d3b363ebd055b5b9a53be551e6e83
7
+ data.tar.gz: 3a3af86be35e961882e11a326e4609376eb3a2fd6a30658be044559fad9b56feabeeae226b91fb85f6b515304a9f982d40e1aaebc9b3a462c076031c1418dfd4
@@ -0,0 +1,17 @@
1
+ # typed: true
2
+ module SafeType
3
+ class CoercionError < StandardError; end
4
+ end
5
+
6
+ module TypeCoerce
7
+ extend T::Sig
8
+ extend T::Generic
9
+
10
+ Elem = type_member
11
+
12
+ sig { params(args: T.untyped, raise_coercion_error: T.nilable(T::Boolean)).returns(Elem) }
13
+ def from(args, raise_coercion_error: nil); end
14
+
15
+ class CoercionError < SafeType::CoercionError; end
16
+ class ShapeError < SafeType::CoercionError; end
17
+ end
@@ -1,19 +1,19 @@
1
1
  # typed: strict
2
- require_relative 'configuration'
3
- require 'private/converter'
2
+ require 'sorbet-coerce/configuration'
3
+ require 'sorbet-coerce/converter'
4
4
  require 'safe_type'
5
5
 
6
- module T::Coerce
6
+ module TypeCoerce
7
7
  class CoercionError < SafeType::CoercionError; end
8
8
  class ShapeError < SafeType::CoercionError; end
9
9
 
10
10
  define_singleton_method(:[]) do |type|
11
- Class.new(T::Private::Converter) do
11
+ Class.new(TypeCoerce::Private::Converter) do
12
12
  define_method(:to_s) { "#{name}#[#{type.to_s}]" }
13
13
 
14
14
  define_method(:from) do |args, raise_coercion_error: nil|
15
15
  if raise_coercion_error.nil?
16
- raise_coercion_error = T::Coerce::Configuration.raise_coercion_error
16
+ raise_coercion_error = TypeCoerce::Configuration.raise_coercion_error
17
17
  end
18
18
 
19
19
  T.send('let', send('_convert', args, type, raise_coercion_error), type)
@@ -1,7 +1,7 @@
1
1
  # typed: true
2
2
  require 'sorbet-runtime'
3
3
 
4
- module T::Coerce
4
+ module TypeCoerce
5
5
  module Configuration
6
6
  class << self
7
7
  extend T::Sig
@@ -12,4 +12,4 @@ module T::Coerce
12
12
  end
13
13
  end
14
14
 
15
- T::Coerce::Configuration.raise_coercion_error = true
15
+ TypeCoerce::Configuration.raise_coercion_error = true
@@ -1,11 +1,14 @@
1
1
  # typed: strict
2
2
  require 'safe_type'
3
+ require 'set'
3
4
  require 'sorbet-runtime'
4
5
  require 'polyfill'
5
6
 
6
7
  using Polyfill(Hash: %w[#slice])
7
8
 
8
- module T::Private
9
+ module TypeCoerce; end
10
+
11
+ module TypeCoerce::Private
9
12
  class Converter
10
13
  extend T::Sig
11
14
 
@@ -22,8 +25,12 @@ module T::Private
22
25
  protected
23
26
  sig { params(value: T.untyped, type: T.untyped, raise_coercion_error: T::Boolean).returns(T.untyped) }
24
27
  def _convert(value, type, raise_coercion_error)
25
- if type.is_a?(T::Types::TypedArray)
28
+ if type.is_a?(T::Types::Untyped)
29
+ value
30
+ elsif type.is_a?(T::Types::TypedArray)
26
31
  _convert_to_a(value, type.type, raise_coercion_error)
32
+ elsif type.is_a?(T::Types::TypedSet)
33
+ Set.new(_convert_to_a(value, type.type, raise_coercion_error))
27
34
  elsif type.is_a?(T::Types::Simple)
28
35
  _convert(value, type.raw_type, raise_coercion_error)
29
36
  elsif type.is_a?(T::Types::Union)
@@ -51,13 +58,30 @@ module T::Private
51
58
  else
52
59
  _convert(value, type.types[nil_idx == 0 ? 1 : 0], raise_coercion_error)
53
60
  end
61
+ elsif type.is_a?(T::Types::TypedHash)
62
+ return {} if _nil_like?(value, type)
63
+
64
+ unless value.respond_to?(:map)
65
+ raise TypeCoerce::ShapeError.new(value, type)
66
+ end
67
+
68
+ value.map do |k, v|
69
+ [
70
+ _convert(k, type.keys, raise_coercion_error),
71
+ _convert(v, type.values, raise_coercion_error),
72
+ ]
73
+ end.to_h
54
74
  elsif Object.const_defined?('T::Private::Types::TypeAlias') &&
55
75
  type.is_a?(T::Private::Types::TypeAlias)
56
76
  _convert(value, type.aliased_type, raise_coercion_error)
57
- elsif type < T::Struct
77
+ elsif type.respond_to?(:<) && type < T::Struct
78
+ return value if value.is_a?(type)
79
+
58
80
  args = _build_args(value, type, raise_coercion_error)
59
81
  type.new(args)
60
82
  else
83
+ return value if value.is_a?(type)
84
+
61
85
  _convert_simple(value, type, raise_coercion_error)
62
86
  end
63
87
  end
@@ -77,15 +101,18 @@ module T::Private
77
101
  else
78
102
  safe_type_rule = type
79
103
  end
80
- SafeType::coerce(value, safe_type_rule)
104
+
105
+ if safe_type_rule.is_a?(SafeType::Rule)
106
+ SafeType::coerce(value, safe_type_rule)
107
+ else
108
+ type.new(value)
109
+ end
81
110
  rescue SafeType::EmptyValueError, SafeType::CoercionError
82
111
  if raise_coercion_error
83
- raise T::Coerce::CoercionError.new(value, type)
112
+ raise TypeCoerce::CoercionError.new(value, type)
84
113
  else
85
114
  nil
86
115
  end
87
- rescue SafeType::InvalidRuleError
88
- type.new(value)
89
116
  end
90
117
 
91
118
  sig { params(ary: T.untyped, type: T.untyped, raise_coercion_error: T::Boolean).returns(T.untyped) }
@@ -93,7 +120,7 @@ module T::Private
93
120
  return [] if _nil_like?(ary, type)
94
121
 
95
122
  unless ary.respond_to?(:map)
96
- raise T::Coerce::ShapeError.new(ary, type)
123
+ raise TypeCoerce::ShapeError.new(ary, type)
97
124
  end
98
125
 
99
126
  ary.map { |value| _convert(value, type, raise_coercion_error) }
@@ -104,7 +131,7 @@ module T::Private
104
131
  return {} if _nil_like?(args, Hash)
105
132
 
106
133
  unless args.respond_to?(:each_pair)
107
- raise T::Coerce::ShapeError.new(args, type)
134
+ raise TypeCoerce::ShapeError.new(args, type)
108
135
  end
109
136
 
110
137
  props = type.props
@@ -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
@@ -2,7 +2,7 @@
2
2
  require 'sorbet-coerce'
3
3
  require 'sorbet-runtime'
4
4
 
5
- describe T::Coerce do
5
+ describe TypeCoerce do
6
6
  context 'when given T::Struct' do
7
7
  class ParamInfo < T::Struct
8
8
  const :name, String
@@ -27,6 +27,14 @@ 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
+
34
+ class HashParamsWithDefault < T::Struct
35
+ const :myhash, T::Hash[String, Integer], default: Hash['a' => 1]
36
+ end
37
+
30
38
  class CustomType
31
39
  attr_reader :a
32
40
 
@@ -44,7 +52,7 @@ describe T::Coerce do
44
52
  end
45
53
 
46
54
  let!(:param) {
47
- T::Coerce[Param].new.from({
55
+ TypeCoerce[Param].new.from({
48
56
  id: 1,
49
57
  info: {
50
58
  name: 'mango',
@@ -60,7 +68,7 @@ describe T::Coerce do
60
68
  }
61
69
 
62
70
  let!(:param2) {
63
- T::Coerce[Param].new.from({
71
+ TypeCoerce[Param].new.from({
64
72
  id: '2',
65
73
  info: {
66
74
  name: 'honeydew',
@@ -91,6 +99,7 @@ describe T::Coerce do
91
99
  expect(param.info.name).to eql 'mango'
92
100
  expect(param.info.skill_ids).to eql [123, 456]
93
101
  expect(param.opt.notes).to eql []
102
+ expect(TypeCoerce[Param].new.from(param)).to eq(param)
94
103
 
95
104
  expect(param2.id).to eql 2
96
105
  expect(param2.info.name).to eql 'honeydew'
@@ -101,7 +110,7 @@ describe T::Coerce do
101
110
  expect(param2.opt.notes).to eql []
102
111
 
103
112
  expect {
104
- T::Coerce[Param].new.from({
113
+ TypeCoerce[Param].new.from({
105
114
  id: 3,
106
115
  info: {
107
116
  # missing required name
@@ -110,8 +119,8 @@ describe T::Coerce do
110
119
  })
111
120
  }.to raise_error(ArgumentError)
112
121
 
113
- expect(T::Coerce[DefaultParams].new.from(nil).a).to be 1
114
- expect(T::Coerce[DefaultParams].new.from('').a).to be 1
122
+ expect(TypeCoerce[DefaultParams].new.from(nil).a).to be 1
123
+ expect(TypeCoerce[DefaultParams].new.from('').a).to be 1
115
124
  end
116
125
  end
117
126
 
@@ -123,76 +132,132 @@ describe T::Coerce do
123
132
 
124
133
  it 'raises an error' do
125
134
  expect {
126
- T::Coerce[Param2].new.from({id: 1, info: 1})
135
+ TypeCoerce[Param2].new.from({id: 1, info: 1})
127
136
  }.to raise_error(ArgumentError)
128
137
  end
129
138
  end
130
139
 
131
140
  context 'when given primitive types' do
132
141
  it 'reveals the right type' do
133
- T.assert_type!(T::Coerce[Integer].new.from(1), Integer)
134
- T.assert_type!(T::Coerce[Integer].new.from('1.0'), Integer)
135
- T.assert_type!(T::Coerce[T.nilable(Integer)].new.from(nil), T.nilable(Integer))
142
+ T.assert_type!(TypeCoerce[Integer].new.from(1), Integer)
143
+ T.assert_type!(TypeCoerce[Integer].new.from('1.0'), Integer)
144
+ T.assert_type!(TypeCoerce[T.nilable(Integer)].new.from(nil), T.nilable(Integer))
136
145
  end
137
146
 
138
147
  it 'coreces correctly' do
139
- expect{T::Coerce[Integer].new.from(nil)}.to raise_error(TypeError)
140
- expect(T::Coerce[T.nilable(Integer)].new.from(nil) || 1).to eql 1
141
- expect(T::Coerce[Integer].new.from(2)).to eql 2
142
- expect(T::Coerce[Integer].new.from('1.0')).to eql 1
148
+ expect{TypeCoerce[Integer].new.from(nil)}.to raise_error(TypeError)
149
+ expect(TypeCoerce[T.nilable(Integer)].new.from(nil) || 1).to eql 1
150
+ expect(TypeCoerce[Integer].new.from(2)).to eql 2
151
+ expect(TypeCoerce[Integer].new.from('1.0')).to eql 1
143
152
 
144
- expect{T::Coerce[T.nilable(Integer)].new.from('invalid integer string')}.to raise_error(T::Coerce::CoercionError)
145
- expect(T::Coerce[Float].new.from('1.0')).to eql 1.0
153
+ expect{TypeCoerce[T.nilable(Integer)].new.from('invalid integer string')}.to raise_error(TypeCoerce::CoercionError)
154
+ expect(TypeCoerce[Float].new.from('1.0')).to eql 1.0
146
155
 
147
- expect(T::Coerce[T::Boolean].new.from('false')).to be false
148
- expect(T::Coerce[T::Boolean].new.from('true')).to be true
156
+ expect(TypeCoerce[T::Boolean].new.from('false')).to be false
157
+ expect(TypeCoerce[T::Boolean].new.from('true')).to be true
149
158
 
150
- expect(T::Coerce[T.nilable(Integer)].new.from('')).to be nil
151
- expect{T::Coerce[T.nilable(Integer)].new.from([])}.to raise_error(T::Coerce::CoercionError)
152
- expect(T::Coerce[T.nilable(String)].new.from('')).to eql ''
159
+ expect(TypeCoerce[T.nilable(Integer)].new.from('')).to be nil
160
+ expect{TypeCoerce[T.nilable(Integer)].new.from([])}.to raise_error(TypeCoerce::CoercionError)
161
+ expect(TypeCoerce[T.nilable(String)].new.from('')).to eql ''
153
162
  end
154
163
  end
155
164
 
156
165
  context 'when given custom types' do
157
166
  it 'coerces correctly' do
158
- T.assert_type!(T::Coerce[CustomType].new.from(a: 1), CustomType)
159
- expect(T::Coerce[CustomType].new.from(1).a).to be 1
167
+ obj = TypeCoerce[CustomType].new.from(1)
168
+ T.assert_type!(obj, CustomType)
169
+ expect(obj.a).to be 1
170
+ expect(TypeCoerce[CustomType].new.from(obj)).to be obj
160
171
 
161
- expect{T::Coerce[UnsupportedCustomType].new.from(1)}.to raise_error(ArgumentError)
172
+ expect{TypeCoerce[UnsupportedCustomType].new.from(1)}.to raise_error(ArgumentError)
162
173
  # CustomType2.new(anything) returns Integer 1; 1.is_a?(CustomType2) == false
163
- expect{T::Coerce[CustomType2].new.from(1)}.to raise_error(TypeError)
174
+ expect{TypeCoerce[CustomType2].new.from(1)}.to raise_error(TypeError)
164
175
  end
165
176
  end
166
177
 
167
178
  context 'when dealing with arries' do
168
179
  it 'coreces correctly' do
169
- expect(T::Coerce[T::Array[Integer]].new.from(nil)).to eql []
170
- expect(T::Coerce[T::Array[Integer]].new.from('')).to eql []
171
- expect{T::Coerce[T::Array[Integer]].new.from('not an array')}.to raise_error(T::Coerce::ShapeError)
172
- expect{T::Coerce[T::Array[Integer]].new.from('1')}.to raise_error(T::Coerce::ShapeError)
173
- expect(T::Coerce[T::Array[Integer]].new.from(['1', '2', '3'])).to eql [1, 2, 3]
174
- expect{T::Coerce[T::Array[Integer]].new.from(['1', 'invalid', '3'])}.to raise_error(T::Coerce::CoercionError)
175
- expect{T::Coerce[T::Array[Integer]].new.from({a: 1})}.to raise_error(T::Coerce::CoercionError)
176
-
177
- infos = T::Coerce[T::Array[ParamInfo]].new.from([{name: 'a', skill_ids: []}])
180
+ expect(TypeCoerce[T::Array[Integer]].new.from(nil)).to eql []
181
+ expect(TypeCoerce[T::Array[Integer]].new.from('')).to eql []
182
+ expect{TypeCoerce[T::Array[Integer]].new.from('not an array')}.to raise_error(TypeCoerce::ShapeError)
183
+ expect{TypeCoerce[T::Array[Integer]].new.from('1')}.to raise_error(TypeCoerce::ShapeError)
184
+ expect(TypeCoerce[T::Array[Integer]].new.from(['1', '2', '3'])).to eql [1, 2, 3]
185
+ expect{TypeCoerce[T::Array[Integer]].new.from(['1', 'invalid', '3'])}.to raise_error(TypeCoerce::CoercionError)
186
+ expect{TypeCoerce[T::Array[Integer]].new.from({a: 1})}.to raise_error(TypeCoerce::CoercionError)
187
+
188
+ infos = TypeCoerce[T::Array[ParamInfo]].new.from([{name: 'a', skill_ids: []}])
178
189
  T.assert_type!(infos, T::Array[ParamInfo])
179
190
  expect(infos.first.name).to eql 'a'
180
191
 
181
- infos = T::Coerce[T::Array[ParamInfo]].new.from([{name: 'b', skill_ids: []}])
192
+ infos = TypeCoerce[T::Array[ParamInfo]].new.from([{name: 'b', skill_ids: []}])
182
193
  T.assert_type!(infos, T::Array[ParamInfo])
183
194
  expect(infos.first.name).to eql 'b'
184
195
 
185
196
  expect {
186
- T::Coerce[ParamInfo2].new.from({a: nil, b: nil})
197
+ TypeCoerce[ParamInfo2].new.from({a: nil, b: nil})
187
198
  }.to raise_error(TypeError)
188
199
  end
189
200
  end
190
201
 
202
+ context 'when dealing with hashes' do
203
+ it 'coreces correctly' do
204
+ expect(TypeCoerce[T::Hash[T.untyped, T.untyped]].new.from(nil)).to eql({})
205
+
206
+ expect(TypeCoerce[T::Hash[String, T::Boolean]].new.from({
207
+ a: 'true',
208
+ b: 'false',
209
+ })).to eql({
210
+ 'a' => true,
211
+ 'b' => false,
212
+ })
213
+
214
+ expect(TypeCoerce[HashParams].new.from({
215
+ myhash: {'a' => '1', 'b' => '2'},
216
+ }).myhash).to eql({'a' => 1, 'b' => 2})
217
+
218
+ expect(TypeCoerce[HashParamsWithDefault].new.from({}).myhash).to eql({'a' => 1})
219
+
220
+ expect {
221
+ TypeCoerce[T::Hash[String, T::Boolean]].new.from({
222
+ a: 'invalid',
223
+ b: 'false',
224
+ })
225
+ }.to raise_error(TypeCoerce::CoercionError)
226
+
227
+ expect {
228
+ TypeCoerce[T::Hash[String, Integer]].new.from(1)
229
+ }.to raise_error(TypeCoerce::ShapeError)
230
+ end
231
+ end
232
+
233
+ context 'when dealing with sets' do
234
+ it 'coreces correctly' do
235
+ expect(TypeCoerce[T::Set[Integer]].new.from(
236
+ Set.new(['1', '2', '3'])
237
+ )).to eq Set.new([1, 2, 3])
238
+
239
+ expect {
240
+ TypeCoerce[T::Set[Integer]].new.from(Set.new(['1', 'invalid', '3']))
241
+ }.to raise_error(TypeCoerce::CoercionError)
242
+
243
+ expect {
244
+ TypeCoerce[T::Set[Integer]].new.from(1)
245
+ }.to raise_error(TypeCoerce::ShapeError)
246
+ end
247
+ end
248
+
191
249
  context 'when given a type alias' do
192
250
  MyType = T.type_alias(T::Boolean)
193
251
 
194
252
  it 'coerces correctly' do
195
- expect(T::Coerce[MyType].new.from('false')).to be false
253
+ expect(TypeCoerce[MyType].new.from('false')).to be false
196
254
  end
197
255
  end
256
+
257
+ it 'works with T.untyped' do
258
+ expect(TypeCoerce[T.untyped].new.from(1)).to eql 1
259
+
260
+ obj = CustomType.new(1)
261
+ expect(TypeCoerce[T::Hash[String, T.untyped]].new.from({a: obj})).to eq({'a' => obj})
262
+ end
198
263
  end
@@ -2,7 +2,7 @@
2
2
  require 'sorbet-coerce'
3
3
  require 'sorbet-runtime'
4
4
 
5
- describe T::Coerce do
5
+ describe TypeCoerce do
6
6
  context 'when given nested types' do
7
7
  class User < T::Struct
8
8
  const :id, Integer
@@ -15,7 +15,7 @@ describe T::Coerce do
15
15
  end
16
16
 
17
17
  it 'works with nest T::Struct' do
18
- converted = T::Coerce[NestedParam].new.from({
18
+ converted = TypeCoerce[NestedParam].new.from({
19
19
  users: [{id: '1'}],
20
20
  params: {
21
21
  users: [{id: '2', valid: 'true'}],
@@ -42,16 +42,16 @@ describe T::Coerce do
42
42
 
43
43
  it 'works with nest T::Array' do
44
44
  expect {
45
- T::Coerce[T::Array[T.nilable(Integer)]].new.from(['1', 'invalid', '3'])
46
- }.to raise_error(T::Coerce::CoercionError)
45
+ TypeCoerce[T::Array[T.nilable(Integer)]].new.from(['1', 'invalid', '3'])
46
+ }.to raise_error(TypeCoerce::CoercionError)
47
47
  expect(
48
- T::Coerce[T::Array[T::Array[Integer]]].new.from([nil])
48
+ TypeCoerce[T::Array[T::Array[Integer]]].new.from([nil])
49
49
  ).to eql([[]])
50
50
  expect(
51
- T::Coerce[T::Array[T::Array[Integer]]].new.from([['1'], ['2'], ['3']]),
51
+ TypeCoerce[T::Array[T::Array[Integer]]].new.from([['1'], ['2'], ['3']]),
52
52
  ).to eql [[1], [2], [3]]
53
53
 
54
- expect(T::Coerce[
54
+ expect(TypeCoerce[
55
55
  T::Array[
56
56
  T::Array[
57
57
  T::Array[User]
@@ -59,7 +59,7 @@ describe T::Coerce do
59
59
  ]
60
60
  ].new.from([[[{id: '1'}]]]).flatten.first.id).to eql(1)
61
61
 
62
- expect(T::Coerce[
62
+ expect(TypeCoerce[
63
63
  T::Array[
64
64
  T::Array[
65
65
  T::Array[
@@ -71,9 +71,18 @@ describe T::Coerce do
71
71
  ]
72
72
  ].new.from([[[[[{id: 1}]]]]]).flatten.first.id).to eql 1
73
73
 
74
- expect(T::Coerce[
74
+ expect(TypeCoerce[
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
@@ -2,12 +2,12 @@
2
2
  require 'sorbet-coerce'
3
3
  require 'sorbet-runtime'
4
4
 
5
- describe T::Coerce do
5
+ describe TypeCoerce do
6
6
  context 'when type errors are soft errors' do
7
7
  let(:ignore_error) { Proc.new {} }
8
8
 
9
9
  before(:each) do
10
- allow(T::Coerce::Configuration).to receive(
10
+ allow(TypeCoerce::Configuration).to receive(
11
11
  :raise_coercion_error,
12
12
  ).and_return(false)
13
13
 
@@ -42,24 +42,24 @@ describe T::Coerce do
42
42
 
43
43
  it 'overwrites the global config when inline config is set' do
44
44
  expect {
45
- T::Coerce[Integer].new.from(invalid_arg, raise_coercion_error: true)
46
- }.to raise_error(T::Coerce::CoercionError)
45
+ TypeCoerce[Integer].new.from(invalid_arg, raise_coercion_error: true)
46
+ }.to raise_error(TypeCoerce::CoercionError)
47
47
  end
48
48
 
49
49
  it 'works as expected' do
50
- expect(T::Coerce[Integer].new.from(invalid_arg)).to eql(nil)
50
+ expect(TypeCoerce[Integer].new.from(invalid_arg)).to eql(nil)
51
51
 
52
- expect{T::Coerce[T::Array[Integer]].new.from(1)}.to raise_error(T::Coerce::ShapeError)
53
- expect(T::Coerce[T::Array[Integer]].new.from({a: 1})).to eql([nil])
52
+ expect{TypeCoerce[T::Array[Integer]].new.from(1)}.to raise_error(TypeCoerce::ShapeError)
53
+ expect(TypeCoerce[T::Array[Integer]].new.from({a: 1})).to eql([nil])
54
54
 
55
55
  expect {
56
- T::Coerce[CustomTypeRaisesHardError].new.from(1)
56
+ TypeCoerce[CustomTypeRaisesHardError].new.from(1)
57
57
  }.to raise_error(StandardError)
58
- expect(T::Coerce[CustomTypeDoesNotRiaseHardError].new.from(1)).to eql(1)
58
+ expect(TypeCoerce[CustomTypeDoesNotRiaseHardError].new.from(1)).to eql(1)
59
59
 
60
60
  sorbet_version = Gem.loaded_specs['sorbet-runtime'].version
61
61
  if sorbet_version >= Gem::Version.new('0.4.4948')
62
- expect(T::Coerce[ParamsWithSortError].new.from({a: invalid_arg}).a).to eql(nil)
62
+ expect(TypeCoerce[ParamsWithSortError].new.from({a: invalid_arg}).a).to eql(nil)
63
63
  end
64
64
  end
65
65
  end
@@ -1,22 +1,22 @@
1
1
  # typed: true
2
2
  require 'sorbet-coerce'
3
3
 
4
- T.assert_type!(T::Coerce[Integer].new.from('1'), Integer)
4
+ T.assert_type!(TypeCoerce[Integer].new.from('1'), Integer)
5
5
  T.assert_type!(
6
- T::Coerce[T.nilable(Integer)].new.from('invalid', raise_coercion_error: false),
6
+ TypeCoerce[T.nilable(Integer)].new.from('invalid', raise_coercion_error: false),
7
7
  T.nilable(Integer),
8
8
  )
9
9
 
10
- T::Coerce::Configuration.raise_coercion_error = true
10
+ TypeCoerce::Configuration.raise_coercion_error = true
11
11
  coercion_error = nil
12
12
  begin
13
- T::Coerce[T.nilable(Integer)].new.from('invalid')
14
- rescue T::Coerce::CoercionError => e
13
+ TypeCoerce[T.nilable(Integer)].new.from('invalid')
14
+ rescue TypeCoerce::CoercionError => e
15
15
  coercion_error = e
16
16
  end
17
17
  raise 'no coercion error is raised' unless coercion_error
18
18
 
19
19
  T.assert_type!(
20
- T::Coerce[T.nilable(Integer)].new.from('invalid', raise_coercion_error: false),
20
+ TypeCoerce[T.nilable(Integer)].new.from('invalid', raise_coercion_error: false),
21
21
  T.nilable(Integer),
22
22
  )
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.6
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/configuration.rb
122
- - lib/private/converter.rb
121
+ - lib/bundled_rbi/sorbet-coerce.rbi
123
122
  - lib/sorbet-coerce.rb
124
- - rbi/sorbet-coerce.rbi
123
+ - lib/sorbet-coerce/configuration.rb
124
+ - lib/sorbet-coerce/converter.rb
125
+ - lib/sorbet-coerce/coverter.rbi
125
126
  - spec/coerce_spec.rb
126
127
  - spec/nested_spec.rb
127
128
  - spec/soft_error_spec.rb
@@ -1,20 +0,0 @@
1
- # typed: true
2
- module T
3
- module Coerce
4
- extend T::Sig
5
- extend T::Generic
6
-
7
- Elem = type_member
8
-
9
- sig { params(args: T.untyped, raise_coercion_error: T.nilable(T::Boolean)).returns(Elem) }
10
- def from(args, raise_coercion_error: nil); end
11
- end
12
-
13
- module Private
14
- module Types
15
- class TypeAlias
16
- def aliased_type; end
17
- end
18
- end
19
- end
20
- end