Skip to content

Commit

Permalink
Require recent decimal versions
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Oct 1, 2023
1 parent f7256f1 commit 4fff8f3
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 22 deletions.
13 changes: 1 addition & 12 deletions lib/ecto/changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3152,7 +3152,7 @@ defmodule Ecto.Changeset do
_spec_function,
%Decimal{} = target_value
) do
result = Decimal.compare(value, target_value) |> normalize_compare()
result = Decimal.compare(value, target_value)

case decimal_compare(result, spec_key) do
true -> nil
Expand All @@ -3175,17 +3175,6 @@ defmodule Ecto.Changeset do
end
end

# TODO: Remove me once we support Decimal 2.0 only
# Support mismatch between API for Decimal.compare/2 for versions 1.6 and 2.0
defp normalize_compare(result) do
case result do
%Decimal{coef: 1, sign: -1} -> :lt
%Decimal{coef: 0} -> :eq
%Decimal{coef: 1, sign: 1} -> :gt
_ -> result
end
end

defp decimal_new(term) when is_float(term), do: Decimal.from_float(term)
defp decimal_new(term), do: Decimal.new(term)

Expand Down
8 changes: 1 addition & 7 deletions lib/ecto/query/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1128,13 +1128,7 @@ defmodule Ecto.Query.Builder do
@doc """
Negates the given number.
"""
# TODO: Remove check when we depend on decimal v2.0
if Code.ensure_loaded?(Decimal) and function_exported?(Decimal, :negate, 1) do
def negate!(%Decimal{} = decimal), do: Decimal.negate(decimal)
else
def negate!(%Decimal{} = decimal), do: Decimal.minus(decimal)
end

def negate!(%Decimal{} = decimal), do: Decimal.negate(decimal)
def negate!(number) when is_number(number), do: -number

@doc """
Expand Down
2 changes: 0 additions & 2 deletions lib/ecto/type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -858,8 +858,6 @@ defmodule Ecto.Type do

defp cast_decimal(term) when is_binary(term) do
case Decimal.parse(term) do
{:ok, decimal} -> check_decimal(decimal, false)
# The following two clauses exist to support earlier versions of Decimal.
{decimal, ""} -> check_decimal(decimal, false)
{_, remainder} when is_binary(remainder) and byte_size(remainder) > 0 -> :error
:error -> :error
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Ecto.MixProject do
defp deps do
[
{:telemetry, "~> 0.4 or ~> 1.0"},
{:decimal, "~> 1.6 or ~> 2.0"},
{:decimal, "~> 2.0"},
{:jason, "~> 1.0", optional: true},
{:ex_doc, "~> 0.20", only: :docs}
]
Expand Down

0 comments on commit 4fff8f3

Please sign in to comment.