sorbet-coerce 0.2.7 → 0.3.0

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: 017a474302bad34d6523e0140f0a221419874ddb6488f5cde3075458c54a245e
4
- data.tar.gz: '0822f50c595a31bd377ff359d7e05f42ef60a2c4a9dd46915527dcf47c344081'
3
+ metadata.gz: b3a8032d0ba46f76ce47d4ec30e3e486005028d61a06c4f1047d6691f3e995f4
4
+ data.tar.gz: 6ec0fd090fbce8cb3889d69ce10bfa07b74b928425af2f9a4ca47bf60d1d1889
5
5
  SHA512:
6
- metadata.gz: e105f8b039d77562b9a03ed58cc55cebb2e55a526917d1965c2fce7e410869d8bb9ea1d94e50f8086d298bf818eb63b5fb6293326e073621f6dc66450d6632c8
7
- data.tar.gz: 2c0be667253b77c2e84930cf1fa526b01ed79c3cb603b708e2256958d3def7679adb275cbeccee3f5e7e6649408445c43d3bb6892281fe42efaa511464963fbc
6
+ metadata.gz: 4ec5337779c9eece9610d73a037662d36d2dc9e50cfc77dd2762fb323f90dcb6edca41ee1337c12074243b1a1587450753a8afce49f5923aaf4ee1f88cc0bb2b
7
+ data.tar.gz: 84a9967bac7c2fb07d7c0f7530f1c4063a687019cc66e5787d0d69b1973884cf7e32d245ff2e99cb258abb963ae037f68e7dcc84c78d5c8f1b253d5b2f0b123a
@@ -1,4 +1,4 @@
1
- # typed: true
1
+ # typed: false
2
2
  module SafeType
3
3
  class CoercionError < StandardError; end
4
4
  end
@@ -100,6 +100,10 @@ class TypeCoerce::Converter
100
100
 
101
101
  args = _build_args(value, type, raise_coercion_error)
102
102
  type.new(args)
103
+ elsif type.respond_to?(:<) && type < T::Enum
104
+ return value if value.is_a?(type)
105
+
106
+ _convert_enum(value, type, raise_coercion_error)
103
107
  else
104
108
  return value if value.is_a?(type)
105
109
 
@@ -107,6 +111,16 @@ class TypeCoerce::Converter
107
111
  end
108
112
  end
109
113
 
114
+ def _convert_enum(value, type, raise_coercion_error)
115
+ if raise_coercion_error
116
+ type.deserialize(value)
117
+ else
118
+ type.try_deserialize(value)
119
+ end
120
+ rescue KeyError
121
+ raise TypeCoerce::CoercionError.new(value, type)
122
+ end
123
+
110
124
  def _convert_simple(value, type, raise_coercion_error)
111
125
  return nil if _nil_like?(value, type)
112
126
 
@@ -35,6 +35,17 @@ describe TypeCoerce do
35
35
  const :myhash, T::Hash[String, Integer], default: Hash['a' => 1]
36
36
  end
37
37
 
38
+ class TestEnum < T::Enum
39
+ enums do
40
+ Test = new
41
+ Other = new
42
+ end
43
+ end
44
+
45
+ class WithEnum < T::Struct
46
+ const :myenum, TestEnum
47
+ end
48
+
38
49
  class CustomType
39
50
  attr_reader :a
40
51
 
@@ -254,6 +265,24 @@ describe TypeCoerce do
254
265
  end
255
266
  end
256
267
 
268
+ context 'when dealing with enums' do
269
+ it 'coerces a serialized enum correctly' do
270
+ coerced = TypeCoerce[WithEnum].new.from(myenum: "test")
271
+ expect(coerced.myenum).to eq(TestEnum::Test)
272
+ end
273
+
274
+ it 'handles a real enum correctly' do
275
+ coerced = TypeCoerce[WithEnum].new.from(myenum: TestEnum::Test)
276
+ expect(coerced.myenum).to eq(TestEnum::Test)
277
+ end
278
+
279
+ it 'handles bad enum' do
280
+ expect {
281
+ TypeCoerce[WithEnum].new.from(myenum: "bad_key")
282
+ }.to raise_error(TypeCoerce::CoercionError)
283
+ end
284
+ end
285
+
257
286
  it 'works with T.untyped' do
258
287
  expect(TypeCoerce[T.untyped].new.from(1)).to eql 1
259
288
 
@@ -4,6 +4,12 @@ require 'sorbet-runtime'
4
4
 
5
5
  describe TypeCoerce do
6
6
  context 'when type errors are soft errors' do
7
+ class SoftErrorTestEnum < T::Enum
8
+ enums do
9
+ Other = new
10
+ end
11
+ end
12
+
7
13
  let(:ignore_error) { Proc.new {} }
8
14
 
9
15
  before(:each) do
@@ -48,6 +54,7 @@ describe TypeCoerce do
48
54
 
49
55
  it 'works as expected' do
50
56
  expect(TypeCoerce[Integer].new.from(invalid_arg)).to eql(nil)
57
+ expect(TypeCoerce[SoftErrorTestEnum].new.from('bad_key')).to eql(nil)
51
58
 
52
59
  expect{TypeCoerce[T::Array[Integer]].new.from(1)}.to raise_error(TypeCoerce::ShapeError)
53
60
  expect(TypeCoerce[T::Array[Integer]].new.from({a: 1})).to eql([nil])
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.7
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chan Zuckerberg Initiative
@@ -76,40 +76,40 @@ dependencies:
76
76
  name: rspec
77
77
  requirement: !ruby/object:Gem::Requirement
78
78
  requirements:
79
- - - "~>"
79
+ - - ">="
80
80
  - !ruby/object:Gem::Version
81
81
  version: '3.8'
82
- - - ">="
82
+ - - "~>"
83
83
  - !ruby/object:Gem::Version
84
84
  version: '3.8'
85
85
  type: :development
86
86
  prerelease: false
87
87
  version_requirements: !ruby/object:Gem::Requirement
88
88
  requirements:
89
- - - "~>"
89
+ - - ">="
90
90
  - !ruby/object:Gem::Version
91
91
  version: '3.8'
92
- - - ">="
92
+ - - "~>"
93
93
  - !ruby/object:Gem::Version
94
94
  version: '3.8'
95
95
  - !ruby/object:Gem::Dependency
96
96
  name: byebug
97
97
  requirement: !ruby/object:Gem::Requirement
98
98
  requirements:
99
- - - "~>"
99
+ - - ">="
100
100
  - !ruby/object:Gem::Version
101
101
  version: 11.0.1
102
- - - ">="
102
+ - - "~>"
103
103
  - !ruby/object:Gem::Version
104
104
  version: 11.0.1
105
105
  type: :development
106
106
  prerelease: false
107
107
  version_requirements: !ruby/object:Gem::Requirement
108
108
  requirements:
109
- - - "~>"
109
+ - - ">="
110
110
  - !ruby/object:Gem::Version
111
111
  version: 11.0.1
112
- - - ">="
112
+ - - "~>"
113
113
  - !ruby/object:Gem::Version
114
114
  version: 11.0.1
115
115
  description:
@@ -146,8 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
146
  - !ruby/object:Gem::Version
147
147
  version: '0'
148
148
  requirements: []
149
- rubyforge_project:
150
- rubygems_version: 2.7.6.2
149
+ rubygems_version: 3.0.8
151
150
  signing_key:
152
151
  specification_version: 4
153
152
  summary: A type coercion lib works with Sorbet's static type checker and type definitions;