Created
July 5, 2022 12:28
-
-
Save zachdaniel/c99e0dc86669447b69d91f26f54e14af to your computer and use it in GitHub Desktop.
This file contains hidden or 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
defmodule MyApp.Types.StringLiteral do | |
@moduledoc "An ash type for a string that can only take a single value. Used in codegen to create literal values." | |
use Ash.Type | |
@constraints [ | |
value: [ | |
type: :string, | |
doc: "The string literal value", | |
required: true | |
] | |
] | |
@impl true | |
def constraints, do: @constraints | |
@impl true | |
def apply_constraints(_, _), do: :ok | |
@impl true | |
def storage_type, do: :string | |
@impl true | |
def cast_input(nil, _), do: {:ok, nil} | |
def cast_input(value, constraints) do | |
value = to_string(value) | |
if value == constraints[:value] do | |
{:ok, value} | |
else | |
:error | |
end | |
end | |
@impl true | |
def cast_stored(nil, _), do: {:ok, nil} | |
def cast_stored(value, constraints) do | |
value = to_string(value) | |
if value == constraints[:value] do | |
{:ok, value} | |
else | |
:error | |
end | |
end | |
@impl true | |
def dump_to_native(value, _) do | |
{:ok, value} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment