-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
vector.ex
49 lines (38 loc) · 978 Bytes
/
vector.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
defmodule Ash.Type.Vector do
@moduledoc """
Represents a vector.
A builtin type that can be referenced via `:vector`
"""
use Ash.Type
@impl true
def storage_type(_), do: :vector
@impl true
def generator(_constraints) do
StreamData.list_of(StreamData.float())
end
@impl true
def cast_input(value, _) do
Ash.Vector.new(value)
end
@impl true
def cast_stored(nil, _), do: {:ok, nil}
def cast_stored(%Ash.Vector{} = vector, _) do
{:ok, vector}
end
def cast_stored(value, _) when is_list(value) do
case Ash.Vector.new(value) do
{:ok, vector} -> {:ok, vector}
{:error, _} -> :error
end
end
@impl true
def dump_to_native(nil, _), do: {:ok, nil}
def dump_to_native(%Ash.Vector{data: data}, _) do
{:ok, data}
end
def dump_to_native(value, constraints) when is_list(value) do
with {:ok, value} <- cast_input(value, constraints) do
dump_to_native(value, constraints)
end
end
end