Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
defmodule FileSizeEcto.Type.ByteWithUnit do | ||
@behaviour Ecto.Type | ||
|
||
alias FileSize.Convertible | ||
alias FileSize.InvalidUnitError | ||
alias FileSizeEcto.Type.Byte, as: ByteType | ||
|
||
@impl true | ||
def type, do: :map | ||
|
||
@impl true | ||
def cast(term) | ||
|
||
def cast(%{"bytes" => _, "unit" => _} = term) do | ||
load(term) | ||
end | ||
|
||
def cast(term) do | ||
ByteType.cast(term) | ||
end | ||
|
||
@impl true | ||
def dump(term) do | ||
case cast(term) do | ||
{:ok, size} -> | ||
{:ok, %{ | ||
"bytes" => Convertible.normalized_value(size), | ||
"unit" => to_string(size.unit) | ||
}} | ||
|
||
_ -> | ||
:error | ||
end | ||
end | ||
|
||
@impl true | ||
def load(term) | ||
|
||
def load(%{"bytes" => bytes, "unit" => unit_str}) | ||
when is_integer(bytes) and is_binary(unit_strs) do | ||
unit = String.to_existing_atom(unit_str) | ||
{:ok, FileSize.from_bytes(bytes, unit)} | ||
rescue | ||
error in [ArgumentError, InvalidUnitError] -> | ||
:error | ||
end | ||
|
||
def load(_term), do: :error | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters