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.
- checksums.yaml +4 -4
- data/lib/private/converter.rb +32 -13
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 66e3f4b05deb745fbcff9b66bbb7ddfd1d10529ce292e9d27eaacc9bb7b8983f
|
4
|
+
data.tar.gz: 8a3e7c12cf1233854e369f3c6d02f3182ed4977ffee07a8bdd5bc223f02d216b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cbea704d16c04d30e695c5d65c0963e31288f1a70b5ba205fac12ca48bc65e8977882450927dc062ab26eab6d68c9f0861bf5f1931dec1ba9ebbe55a068c5f83
|
7
|
+
data.tar.gz: '09dda06d9da527001aca7fbbb4bd311385647317150a2efb0fd4fa07e1bfaed22130d4e0d36c4d35e17908cb972203764039a9537314db0507cea0ae595652ba'
|
data/lib/private/converter.rb
CHANGED
@@ -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
|
-
|
30
|
+
_convert(value, type.raw_type)
|
31
31
|
elsif type.is_a?(T::Types::Union)
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
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
|
-
|
38
|
-
|
39
|
-
|
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
|
-
|
57
|
-
|
58
|
-
|
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.
|
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
|
[
|