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

Enable conversion of Float's to BigNum's #52

Open
wants to merge 2 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
2 changes: 1 addition & 1 deletion lib/sorbet-coerce/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def _convert_simple(value, type, raise_coercion_error)
elsif value.is_a?(type)
return value
elsif type == BigDecimal
return BigDecimal(value)
return BigDecimal(value, 0)
Copy link
Contributor

@diego-silva diego-silva Jun 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not fully sure this is what we would want 🤔

irb(main):001:0> BigDecimal(123.321, 0).to_digits
=> "123.320999999999997953636921010911464691"

maybe we are better off calling to_s on the value?

Suggested change
return BigDecimal(value, 0)
return BigDecimal(value.to_s)
irb(main):002:0> BigDecimal(123.321.to_s).to_digits
=> "123.321"

However, we probably still need to check what to do with nils or things that don't respond to #to_s 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to value.to_s

we probably still need to check what to do with nils or things that don't respond to #to_s

That's already taken care of on L125.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not need to be part of this PR, but we should also catch argument errors like

ArgumentError (invalid value for BigDecimal(): "a")

in this method, so the error can be properly re-raised and being treated properly by the raise_coercion_error flag.

elsif PRIMITIVE_TYPES.include?(type)
safe_type_rule = SafeType.const_get(type.name).strict
else
Expand Down
3 changes: 3 additions & 0 deletions spec/coerce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ class Param2 < T::Struct
T.assert_type!(TypeCoerce[Integer].new.from(1), Integer)
T.assert_type!(TypeCoerce[Integer].new.from('1.0'), Integer)
T.assert_type!(TypeCoerce[T.nilable(Integer)].new.from(nil), T.nilable(Integer))
T.assert_type!(TypeCoerce[BigDecimal].new.from('1.0'), BigDecimal)
end

it 'coreces correctly' do
Expand All @@ -176,6 +177,8 @@ class Param2 < T::Struct
expect(TypeCoerce[T.nilable(Integer)].new.from('')).to be nil
expect{TypeCoerce[T.nilable(Integer)].new.from([])}.to raise_error(TypeCoerce::CoercionError)
expect(TypeCoerce[T.nilable(String)].new.from('')).to eql ''

expect(TypeCoerce[BigDecimal].new.from(123.321)).to eql BigDecimal(123.321, 0)
end
end

Expand Down