-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement: make tsvector type selectable
- Loading branch information
1 parent
ff1e10e
commit db941cc
Showing
1 changed file
with
53 additions
and
2 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 |
---|---|---|
@@ -1,12 +1,63 @@ | ||
defmodule AshPostgres.Tsvector do | ||
@moduledoc """ | ||
A thin wrapper around `:string` for working with tsvector types in calculations. | ||
A type for representing postgres' tsvectors. | ||
A calculation of this type cannot be selected, but may be used in calculations. | ||
Values will be a list of `Postgrex.Lexeme` | ||
""" | ||
|
||
use Ash.Type.NewType, subtype_of: :term | ||
|
||
@impl true | ||
def storage_type(_), do: :tsvector | ||
|
||
@impl true | ||
def cast_input(nil, _) do | ||
{:ok, nil} | ||
end | ||
|
||
def cast_input(values, _) when is_list(values) do | ||
if Enum.all?(values, &is_struct(&1, Postgrex.Lexeme)) do | ||
{:ok, values} | ||
else | ||
:error | ||
end | ||
end | ||
|
||
def cast_input(_, _) do | ||
:error | ||
end | ||
|
||
@impl true | ||
def dump_to_native(nil, _) do | ||
{:ok, nil} | ||
end | ||
|
||
def dump_to_native(values, _) when is_list(values) do | ||
if Enum.all?(values, &is_struct(&1, Postgrex.Lexeme)) do | ||
{:ok, values} | ||
else | ||
:error | ||
end | ||
end | ||
|
||
def dump_to_native(_, _) do | ||
:error | ||
end | ||
|
||
@impl true | ||
def cast_stored(nil, _) do | ||
{:ok, nil} | ||
end | ||
|
||
def cast_stored(values, _) when is_list(values) do | ||
if Enum.all?(values, &is_struct(&1, Postgrex.Lexeme)) do | ||
{:ok, values} | ||
else | ||
:error | ||
end | ||
end | ||
|
||
def cast_stored(_, _) do | ||
:error | ||
end | ||
end |