View Source Dequel.Semantic.Coerce (Dequel v0.7.0)

Type coercion for query values.

Converts string values from the parser into their proper typed equivalents based on field type information from the schema resolver.

Supported Types

TypeInputOutput
:integer"25"25
:float"3.14"3.14
:boolean"true"/"yes"/"1"/"false"/"no"/"0"true/false
:date"2024-01-15"~D[2024-01-15]
:naive_datetime"2024-01-15T10:30:00"~N[2024-01-15 10:30:00]
:utc_datetime"2024-01-15T10:30:00Z"~U[2024-01-15 10:30:00Z]
:decimal"19.99"Decimal.new("19.99")
:string/unknown"foo""foo" (unchanged)

Invalid values are returned unchanged, letting the adapter or runtime handle errors.

Summary

Functions

Coerces a string value to the given type.

Returns date range bounds for partial date strings.

Functions

@spec coerce(term(), atom()) :: term()

Coerces a string value to the given type.

Returns the original value if coercion fails or type is unknown.

Examples

iex> Dequel.Semantic.Coerce.coerce("42", :integer)
42

iex> Dequel.Semantic.Coerce.coerce("-123", :integer)
-123

iex> Dequel.Semantic.Coerce.coerce("invalid", :integer)
"invalid"

iex> Dequel.Semantic.Coerce.coerce("3.14", :float)
3.14

iex> Dequel.Semantic.Coerce.coerce("true", :boolean)
true

iex> Dequel.Semantic.Coerce.coerce("false", :boolean)
false

iex> Dequel.Semantic.Coerce.coerce("1", :boolean)
true

iex> Dequel.Semantic.Coerce.coerce("0", :boolean)
false

iex> Dequel.Semantic.Coerce.coerce("yes", :boolean)
true

iex> Dequel.Semantic.Coerce.coerce("no", :boolean)
false

iex> Dequel.Semantic.Coerce.coerce("2024-01-15", :date)
~D[2024-01-15]

iex> Dequel.Semantic.Coerce.coerce("2024-01-15T10:30:00", :naive_datetime)
~N[2024-01-15 10:30:00]

iex> Dequel.Semantic.Coerce.coerce("2024-01-15T10:30:00Z", :utc_datetime)
~U[2024-01-15 10:30:00Z]

iex> Dequel.Semantic.Coerce.coerce("hello", :string)
"hello"

iex> Dequel.Semantic.Coerce.coerce("hello", :unknown_type)
"hello"

Non-string values pass through unchanged:

iex> Dequel.Semantic.Coerce.coerce(42, :integer)
42

iex> Dequel.Semantic.Coerce.coerce(nil, :string)
nil
Link to this function

date_range(value, date_type)

View Source
@spec date_range(String.t(), atom()) ::
  {:range, term(), term()} | {:exact, term()} | :error

Returns date range bounds for partial date strings.

Used by the Analyzer to expand :== on partial dates to :between ranges.

Examples

iex> Dequel.Semantic.Coerce.date_range("2024-01", :date)
{:range, ~D[2024-01-01], ~D[2024-01-31]}

iex> Dequel.Semantic.Coerce.date_range("2024", :date)
{:range, ~D[2024-01-01], ~D[2024-12-31]}

iex> Dequel.Semantic.Coerce.date_range("2024-01-15", :date)
{:exact, ~D[2024-01-15]}

iex> Dequel.Semantic.Coerce.date_range("hello", :date)
:error

iex> Dequel.Semantic.Coerce.date_range("2024-01", :naive_datetime)
{:range, ~N[2024-01-01 00:00:00], ~N[2024-01-31 23:59:59]}

iex> Dequel.Semantic.Coerce.date_range("2024", :utc_datetime)
{:range, ~U[2024-01-01 00:00:00Z], ~U[2024-12-31 23:59:59Z]}