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 +4 -4
- data/lib/bundled_rbi/sorbet-coerce.rbi +17 -0
- data/lib/sorbet-coerce.rb +5 -5
- data/lib/{configuration.rb → sorbet-coerce/configuration.rb} +2 -2
- data/lib/{private → sorbet-coerce}/converter.rb +36 -9
- data/lib/sorbet-coerce/coverter.rbi +10 -0
- data/spec/coerce_spec.rb +102 -37
- data/spec/nested_spec.rb +18 -9
- data/spec/soft_error_spec.rb +10 -10
- data/spec/sorbet_test_cases.rb +6 -6
- metadata +5 -4
- data/rbi/sorbet-coerce.rbi +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 61b6f639daa4889060a9540e4fd31f7fbfe19529dac7246a91dc4ea9355b9d70
|
4
|
+
data.tar.gz: 7d0ec3164d26cbf16bc45e94820f02142197191e3cc7ae4db7e8c21db351a0f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/sorbet-coerce.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
# typed: strict
|
2
|
-
|
3
|
-
require '
|
2
|
+
require 'sorbet-coerce/configuration'
|
3
|
+
require 'sorbet-coerce/converter'
|
4
4
|
require 'safe_type'
|
5
5
|
|
6
|
-
module
|
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(
|
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 =
|
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
|
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
|
-
|
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
|
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::
|
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
|
-
|
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
|
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
|
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
|
134
|
+
raise TypeCoerce::ShapeError.new(args, type)
|
108
135
|
end
|
109
136
|
|
110
137
|
props = type.props
|
data/spec/coerce_spec.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
require 'sorbet-coerce'
|
3
3
|
require 'sorbet-runtime'
|
4
4
|
|
5
|
-
describe
|
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
|
-
|
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
|
-
|
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
|
-
|
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(
|
114
|
-
expect(
|
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
|
-
|
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!(
|
134
|
-
T.assert_type!(
|
135
|
-
T.assert_type!(
|
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{
|
140
|
-
expect(
|
141
|
-
expect(
|
142
|
-
expect(
|
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{
|
145
|
-
expect(
|
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(
|
148
|
-
expect(
|
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(
|
151
|
-
expect{
|
152
|
-
expect(
|
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
|
-
|
159
|
-
|
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{
|
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{
|
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(
|
170
|
-
expect(
|
171
|
-
expect{
|
172
|
-
expect{
|
173
|
-
expect(
|
174
|
-
expect{
|
175
|
-
expect{
|
176
|
-
|
177
|
-
infos =
|
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 =
|
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
|
-
|
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(
|
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
|
data/spec/nested_spec.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
require 'sorbet-coerce'
|
3
3
|
require 'sorbet-runtime'
|
4
4
|
|
5
|
-
describe
|
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 =
|
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
|
-
|
46
|
-
}.to raise_error(
|
45
|
+
TypeCoerce[T::Array[T.nilable(Integer)]].new.from(['1', 'invalid', '3'])
|
46
|
+
}.to raise_error(TypeCoerce::CoercionError)
|
47
47
|
expect(
|
48
|
-
|
48
|
+
TypeCoerce[T::Array[T::Array[Integer]]].new.from([nil])
|
49
49
|
).to eql([[]])
|
50
50
|
expect(
|
51
|
-
|
51
|
+
TypeCoerce[T::Array[T::Array[Integer]]].new.from([['1'], ['2'], ['3']]),
|
52
52
|
).to eql [[1], [2], [3]]
|
53
53
|
|
54
|
-
expect(
|
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(
|
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(
|
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
|
data/spec/soft_error_spec.rb
CHANGED
@@ -2,12 +2,12 @@
|
|
2
2
|
require 'sorbet-coerce'
|
3
3
|
require 'sorbet-runtime'
|
4
4
|
|
5
|
-
describe
|
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(
|
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
|
-
|
46
|
-
}.to raise_error(
|
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(
|
50
|
+
expect(TypeCoerce[Integer].new.from(invalid_arg)).to eql(nil)
|
51
51
|
|
52
|
-
expect{
|
53
|
-
expect(
|
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
|
-
|
56
|
+
TypeCoerce[CustomTypeRaisesHardError].new.from(1)
|
57
57
|
}.to raise_error(StandardError)
|
58
|
-
expect(
|
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(
|
62
|
+
expect(TypeCoerce[ParamsWithSortError].new.from({a: invalid_arg}).a).to eql(nil)
|
63
63
|
end
|
64
64
|
end
|
65
65
|
end
|
data/spec/sorbet_test_cases.rb
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
# typed: true
|
2
2
|
require 'sorbet-coerce'
|
3
3
|
|
4
|
-
T.assert_type!(
|
4
|
+
T.assert_type!(TypeCoerce[Integer].new.from('1'), Integer)
|
5
5
|
T.assert_type!(
|
6
|
-
|
6
|
+
TypeCoerce[T.nilable(Integer)].new.from('invalid', raise_coercion_error: false),
|
7
7
|
T.nilable(Integer),
|
8
8
|
)
|
9
9
|
|
10
|
-
|
10
|
+
TypeCoerce::Configuration.raise_coercion_error = true
|
11
11
|
coercion_error = nil
|
12
12
|
begin
|
13
|
-
|
14
|
-
rescue
|
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
|
-
|
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.
|
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/
|
122
|
-
- lib/private/converter.rb
|
121
|
+
- lib/bundled_rbi/sorbet-coerce.rbi
|
123
122
|
- lib/sorbet-coerce.rb
|
124
|
-
-
|
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
|
data/rbi/sorbet-coerce.rbi
DELETED
@@ -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
|