Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand union type support #73

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions lib/sorbet-coerce/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,7 @@ def _convert(value, type, raise_coercion_error, coerce_empty_to_nil)
elsif type.is_a?(T::Types::Simple)
_convert(value, type.raw_type, raise_coercion_error, coerce_empty_to_nil)
elsif type.is_a?(T::Types::Union)
true_idx = T.let(nil, T.nilable(Integer))
false_idx = T.let(nil, T.nilable(Integer))
nil_idx = T.let(nil, T.nilable(Integer))

type.types.each_with_index do |t, i|
nil_idx = i if t.is_a?(T::Types::Simple) && t.raw_type == NilClass
true_idx = i if t.is_a?(T::Types::Simple) && t.raw_type == TrueClass
false_idx = i if t.is_a?(T::Types::Simple) && t.raw_type == FalseClass
end

return value unless (
(!true_idx.nil? && !false_idx.nil? && !nil_idx.nil?) || # T.nilable(T::Boolean)
(type.types.length == 2 && (
!nil_idx.nil? || (!true_idx.nil? && !false_idx.nil?) # T.nilable || T::Boolean
))
)

if !true_idx.nil? && !false_idx.nil?
_convert_simple(value, T::Boolean, raise_coercion_error, coerce_empty_to_nil)
else
_convert(value, type.types[nil_idx == 0 ? 1 : 0], raise_coercion_error, coerce_empty_to_nil)
end
_convert_union(value, type, raise_coercion_error, coerce_empty_to_nil)
elsif type.is_a?(T::Types::TypedHash)
return {} if _nil_like?(value, type, coerce_empty_to_nil)

Expand Down Expand Up @@ -117,6 +96,25 @@ def _convert(value, type, raise_coercion_error, coerce_empty_to_nil)
end
end

def _convert_union(value, type, raise_coercion_error, coerce_empty_to_nil)
type.types.each do |t|
# Always raise coercion error so that we can handle it here
if t.is_a?(T::Types::Simple) && (t.raw_type == NilClass || t.raw_type == TrueClass || t.raw_type == FalseClass)
return _convert_simple(value, T::Boolean, true, coerce_empty_to_nil)
end

return _convert(value, t, true, coerce_empty_to_nil)
rescue TypeCoerce::CoercionError, TypeCoerce::ShapeError
# Keep trying to coerce
end

if raise_coercion_error
raise TypeCoerce::CoercionError.new(value, type)
else
value
end
end

def _convert_enum(value, type, raise_coercion_error, coerce_empty_to_nil)
if raise_coercion_error
type.deserialize(value)
Expand Down
24 changes: 14 additions & 10 deletions spec/coerce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ class UnsupportedCustomType
# Does not respond to new
end

class WithSupportedUnion < T::Struct
class WithNilableUnion < T::Struct
const :nilable, T.nilable(String)
const :nilable_boolean, T.nilable(T::Boolean)
end

class WithUnsupportedUnion < T::Struct
const :union, T.any(String, Integer)
class WithComplexUnion < T::Struct
const :union, T.any(WithNilableUnion, Integer, String)
end

class WithFixedArray < T::Struct
Expand Down Expand Up @@ -217,9 +217,9 @@ class Param2 < T::Struct
end

context 'when given union types' do
context 'supported union types' do
context 'nilable types' do
it 'coerces correctly' do
coerced = TypeCoerce[WithSupportedUnion].new.from({
coerced = TypeCoerce[WithNilableUnion].new.from({
nilable: 2,
nilable_boolean: 'true'
})
Expand All @@ -228,16 +228,20 @@ class Param2 < T::Struct
end
end

context 'unsupported union types' do
it 'keeps the values as-is' do
coerced = TypeCoerce[WithUnsupportedUnion].new.from({union: 'a'})
context 'when give complex union types' do
it 'nested types' do
coerced = TypeCoerce[WithComplexUnion].new.from({union: 'a'})
expect(coerced.union).to eq('a')

coerced = TypeCoerce[WithUnsupportedUnion].new.from({union: 2})
coerced = TypeCoerce[WithComplexUnion].new.from({union: 2})
expect(coerced.union).to eq(2)

coerced = TypeCoerce[WithComplexUnion].new.from({union: { nilable: "Test", nilable_boolean: nil } })
expect(coerced.union.nilable).to eq("Test")
expect(coerced.union.nilable_boolean).to eq(nil)

expect do
TypeCoerce[WithUnsupportedUnion].new.from({union: nil})
TypeCoerce[WithComplexUnion].new.from({union: nil})
end.to raise_error(TypeError)
end
end
Expand Down