-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
map.ex
37 lines (28 loc) · 830 Bytes
/
map.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
defmodule Ash.Type.Map do
@moduledoc """
Represents a map stored in the database.
In postgres, for example, this represents binary encoded json
A builtin type that can be referenced via `:map`
"""
use Ash.Type
@impl true
def storage_type, do: :map
@impl true
def cast_input("", _), do: {:ok, nil}
def cast_input(value, _) when is_binary(value) do
case Jason.decode(value) do
{:ok, value} ->
{:ok, value}
_ ->
:error
end
end
def cast_input(value, _) when is_map(value), do: {:ok, value}
def cast_input(_, _), do: :error
@impl true
def cast_stored(value, _) when is_map(value), do: {:ok, value}
def cast_stored(_, _), do: :error
@impl true
def dump_to_native(value, _) when is_map(value), do: {:ok, value}
def dump_to_native(_, _), do: :error
end