Lantern.Coercion (Lantern v0.6.0)

Copy Markdown View Source

Pure helpers for moving values between Postgres and the UI.

Two concerns live here:

  • cast_expr/2 — given a column's information_schema data_type (and udt_name for arrays / user-defined types), returns the Postgres type expression a text parameter should be cast to on write, or nil when the column is already textual and needs no cast.

  • display / edit formatting — Postgrex returns native Elixir terms (Date, Decimal, NaiveDateTime, …). display/1 renders them for the grid; edit_value/1 renders them for an editable text input.

Summary

Functions

Returns the cast target for a column, or nil if no cast is needed.

Formats a value for a specific HTML control.

Renders a Postgrex value for read-only display.

Like display/1, but uses the column's Postgres type to render binaries accurately. Pass the information_schema data_type (e.g. "uuid", "bytea") as the second argument; without it, 16-byte binaries are optimistically rendered as UUIDs (since a UUID PK is by far the more common case to render in a grid).

Renders a value for an editable text input. NULL becomes an empty string.

Maps a Postgres data_type to the kind of form control to render.

Functions

cast_expr(data_type, udt_name \\ nil)

@spec cast_expr(String.t(), String.t() | nil) :: String.t() | nil

Returns the cast target for a column, or nil if no cast is needed.

Examples

iex> Lantern.Coercion.cast_expr("integer", "int4")
"integer"

iex> Lantern.Coercion.cast_expr("text", "text")
nil

iex> Lantern.Coercion.cast_expr("ARRAY", "_int4")
"int4[]"

control_value(value, arg2)

@spec control_value(term(), atom()) :: String.t()

Formats a value for a specific HTML control.

Date/time controls need ISO shapes (YYYY-MM-DD, YYYY-MM-DDTHH:MM:SS, HH:MM:SS); everything else falls back to edit_value/1.

display(value)

@spec display(term()) :: String.t() | :null

Renders a Postgrex value for read-only display.

Returns :null for nil so callers can distinguish SQL NULL from an empty string; every other value becomes a String.t().

display(value, arg2)

@spec display(term(), String.t() | nil) :: String.t() | :null

Like display/1, but uses the column's Postgres type to render binaries accurately. Pass the information_schema data_type (e.g. "uuid", "bytea") as the second argument; without it, 16-byte binaries are optimistically rendered as UUIDs (since a UUID PK is by far the more common case to render in a grid).

edit_value(value, type \\ nil)

@spec edit_value(term(), String.t() | nil) :: String.t()

Renders a value for an editable text input. NULL becomes an empty string.

Pass the column's Postgres type (e.g. "uuid", "bytea") as the second argument so binary values are disambiguated. Without it, 16-byte binaries are treated as UUIDs.

input_type(t)

@spec input_type(String.t()) :: atom()

Maps a Postgres data_type to the kind of form control to render.

Examples

iex> Lantern.Coercion.input_type("boolean")
:boolean

iex> Lantern.Coercion.input_type("timestamp without time zone")
:datetime

iex> Lantern.Coercion.input_type("text")
:text