sorbet-coerce 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/private/converter.rb +32 -13
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b782e5a5b86602bca93180834ff7ed40f1fc4f05b112aeff37bc7a64963e9f3a
4
- data.tar.gz: cae4b199fe1f9f504641101995a55b2e56e16f8260da8a0b2289f59544fc8af3
3
+ metadata.gz: 66e3f4b05deb745fbcff9b66bbb7ddfd1d10529ce292e9d27eaacc9bb7b8983f
4
+ data.tar.gz: 8a3e7c12cf1233854e369f3c6d02f3182ed4977ffee07a8bdd5bc223f02d216b
5
5
  SHA512:
6
- metadata.gz: d9523a1ce9514a8203377b133b1f56f56c23e241bc01f47018588d11d096a5e1ba87ccb6b4372faea3f6efcf4384478241f1da198739bd0972943b187dd9c00e
7
- data.tar.gz: 694ac86c81038acac9b8289dd5ebda5934c1f0f42d0c4a8a4f4d83bd01a58af272c61e95dedf5ff42da0b2a2aba4e5c5a1562d21006c0b51532b5a59f2f76851
6
+ metadata.gz: cbea704d16c04d30e695c5d65c0963e31288f1a70b5ba205fac12ca48bc65e8977882450927dc062ab26eab6d68c9f0861bf5f1931dec1ba9ebbe55a068c5f83
7
+ data.tar.gz: '09dda06d9da527001aca7fbbb4bd311385647317150a2efb0fd4fa07e1bfaed22130d4e0d36c4d35e17908cb972203764039a9537314db0507cea0ae595652ba'
@@ -27,17 +27,32 @@ module T::Private
27
27
  if type.is_a?(T::Types::TypedArray)
28
28
  _convert_to_a(value, type.type)
29
29
  elsif type.is_a?(T::Types::Simple)
30
- _convert_simple(value, type.raw_type)
30
+ _convert(value, type.raw_type)
31
31
  elsif type.is_a?(T::Types::Union)
32
- raw_types = type.types.map(&:raw_type)
33
- raise ArgumentError.new(
34
- 'the only supported union type is T.nilable',
35
- ) unless raw_types.length == 2 && raw_types.include?(NilClass)
32
+ true_idx = T.let(nil, T.nilable(Integer))
33
+ false_idx = T.let(nil, T.nilable(Integer))
34
+ nil_idx = T.let(nil, T.nilable(Integer))
35
+
36
+ type.types.each_with_index do |t, i|
37
+ nil_idx = i if t.is_a?(T::Types::Simple) && t.raw_type == NilClass
38
+ true_idx = i if t.is_a?(T::Types::Simple) && t.raw_type == TrueClass
39
+ false_idx = i if t.is_a?(T::Types::Simple) && t.raw_type == FalseClass
40
+ end
36
41
 
37
- _convert(
38
- value,
39
- raw_types.select { |t| t != NilClass }.first,
42
+ raise ArgumentError.new(
43
+ 'the only supported union types are T.nilable and T::Boolean',
44
+ ) unless (
45
+ (!true_idx.nil? && !false_idx.nil? && !nil_idx.nil?) || # T.nilable(T::Boolean)
46
+ (type.types.length == 2 && (
47
+ !nil_idx.nil? || (!true_idx.nil? && !false_idx.nil?) # T.nilable || T::Boolean
48
+ ))
40
49
  )
50
+
51
+ if !true_idx.nil? && !false_idx.nil?
52
+ _convert_simple(value, T::Boolean)
53
+ else
54
+ _convert(value, type.types[nil_idx == 0 ? 1 : 0])
55
+ end
41
56
  elsif type < T::Struct
42
57
  args = _build_args(value, type.props)
43
58
  begin
@@ -53,11 +68,14 @@ module T::Private
53
68
  sig { params(value: T.untyped, type: T.untyped).returns(T.untyped) }
54
69
  def _convert_simple(value, type)
55
70
  return nil if value.nil?
56
- return value if value.is_a?(type)
57
- safe_type_rule = if type == T::Boolean
58
- SafeType::Boolean.strict
71
+ safe_type_rule = T.let(nil, T.untyped)
72
+
73
+ if type == T::Boolean
74
+ safe_type_rule = SafeType::Boolean.strict
75
+ elsif value.is_a?(type)
76
+ return value
59
77
  elsif PRIMITIVE_TYPES.include?(type)
60
- SafeType.const_get(type.name).strict
78
+ safe_type_rule = SafeType.const_get(type.name).strict
61
79
  else
62
80
  safe_type_rule = type
63
81
  end
@@ -74,7 +92,7 @@ module T::Private
74
92
 
75
93
  sig { params(ary: T.untyped, type: T.untyped).returns(T.untyped) }
76
94
  def _convert_to_a(ary, type)
77
- ary = [ary] unless ary.respond_to?(:map)
95
+ ary = [ary] unless ary.is_a?(::Array)
78
96
  T.send(
79
97
  'let',
80
98
  ary.map { |value| _convert(value, type) },
@@ -86,6 +104,7 @@ module T::Private
86
104
 
87
105
  sig { params(args: T.untyped, props: T.untyped).returns(T.untyped) }
88
106
  def _build_args(args, props)
107
+ return {} if args.nil?
89
108
  args.map { |name, value|
90
109
  key = name.to_sym
91
110
  [
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.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chan Zuckerberg Initiative