Skip to content

Commit

Permalink
add support for new decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
artemeff committed Dec 25, 2024
1 parent b294d93 commit e2590f0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/construct/type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,11 @@ defmodule Construct.Type do
def cast(:reference, term) when is_reference(term), do: {:ok, term}

def cast(:decimal, term) when is_binary(term) do
validate_decimal(apply(Decimal, :parse, [term]))
case apply(Decimal, :parse, [term]) do
{:ok, term} -> validate_decimal({:ok, term})
{%{__struct__: Decimal} = term, _} -> validate_decimal({:ok, term})
:error -> :error
end
end
def cast(:decimal, term) when is_integer(term) do
{:ok, apply(Decimal, :new, [term])}
Expand Down
4 changes: 2 additions & 2 deletions test/construct/type_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ defmodule Construct.TypeTest do
== Type.cast(:decimal, 1)
assert {:ok, Decimal.new("1")}
== Type.cast(:decimal, Decimal.new("1"))
assert :error
assert {:ok, Decimal.new("NaN")}
== Type.cast(:decimal, "nan")
assert :error
assert {:ok, Decimal.new("NaN")}
== Type.cast(:decimal, Decimal.new("NaN"))
assert :error
== Type.cast(:decimal, Decimal.new("Infinity"))
Expand Down

0 comments on commit e2590f0

Please sign in to comment.