Skip to content

Instantly share code, notes, and snippets.

@kipcole9
Created September 23, 2021 04:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kipcole9/0c22bc507301302d8035651b1d35325b to your computer and use it in GitHub Desktop.
Save kipcole9/0c22bc507301302d8035651b1d35325b to your computer and use it in GitHub Desktop.
if Code.ensure_loaded?(Ecto.Type) do
defmodule Bitstring do
@moduledoc """
Implements the Ecto.Type behaviour for the Postgres
type "bit varying"
"""
use Ecto.Type
def type do
:"bit varying"
end
# When loading from the database
def load(tuple, loader \\ nil, params \\ [])
def load(nil, _loader, _params) do
{:ok, nil}
end
def load(bitstring, _loader, _params) do
{:ok, bitstring}
end
# Dumping to the database.
def dump(bitstring, dumper \\ nil, params \\ [])
def dump(bitstring, _dumper, _params) when is_bitstring(bitstring) do
{:ok, bitstring}
end
def dump(nil, _, _) do
{:ok, nil}
end
def dump(_, _, _) do
:error
end
# Casting in changesets
def cast(bitstring, params \\ [])
def cast(nil, _params) do
{:ok, nil}
end
def cast(bitstring, _params) when is_bitstring(bitstring) do
{:ok, bitstring}
end
def cast(_bitstring, _params) do
:error
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment