-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
function.ex
68 lines (54 loc) · 1.47 KB
/
function.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
defmodule Ash.Type.Function do
@moduledoc """
Represents a function.
If the type would be dumped to a native format, `:erlang.term_to_binary(term, [:safe])` is used.
Please keep in mind, this is not safe to use with external input. This could easily cause you t
More information available here: https://erlang.org/doc/man/erlang.html#binary_to_term-2
"""
use Ash.Type
@constraints [
arity: [
type: :pos_integer,
doc: "Enforces a specific arity on the provided function"
]
]
@impl true
def storage_type, do: :binary
@impl true
def constraints, do: @constraints
@impl true
def apply_constraints(term, constraints) do
if constraints[:arity] && not is_function(term, constraints[:arity]) do
{:error, message: "Expected a function of arity %{arity}", arity: constraints[:arity]}
else
:ok
end
end
@impl true
def cast_input(value, _) when is_function(value) do
{:ok, value}
end
def cast_input(_, _), do: :error
@impl true
# sobelow_skip ["Misc.BinToTerm"]
def cast_stored(value, _) do
case Ecto.Type.load(:binary, value) do
{:ok, val} ->
case :erlang.binary_to_term(val, [:safe]) do
function when is_function(function) ->
function
_ ->
:error
end
other ->
other
end
rescue
_ ->
:error
end
@impl true
def dump_to_native(value, _) do
Ecto.Type.dump(:binary, :erlang.term_to_binary(value))
end
end