Skip to content

Commit

Permalink
improvement: make tsvector type selectable
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Dec 18, 2024
1 parent ff1e10e commit db941cc
Showing 1 changed file with 53 additions and 2 deletions.
55 changes: 53 additions & 2 deletions lib/types/tsvector.ex
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

0 comments on commit db941cc

Please sign in to comment.