Current section
Files
Jump to
Current section
Files
lib/ex_shopify_schema/graphql/decimal.ex
defmodule ExShopifySchema.Graphql.Decimal do
@moduledoc "A signed decimal number, which supports arbitrary precision and is serialized as a string.\n\nExample values: `\"29.99\"`, `\"29.999\"`.\n"
@type t() :: Decimal.t()
use Ecto.Type
require Decimal
@impl Ecto.Type
def type, do: :decimal
@impl Ecto.Type
def cast(nil), do: {:ok, nil}
def cast(str) when is_binary(str) do
case Decimal.parse(str) do
{decimal, ""} -> {:ok, decimal}
_other -> :error
end
end
def cast(_data), do: :error
@impl Ecto.Type
def load(data), do: cast(data)
@impl Ecto.Type
def dump(nil), do: {:ok, nil}
def dump(decimal) when Decimal.is_decimal(decimal), do: {:ok, Decimal.to_string(decimal)}
def dump(_data), do: :error
end