Replicant.Casting.Types (Replicant v4.0.0)

Copy Markdown View Source

Casts PostgreSQL text values into Elixir values for consumers reading Replicant.Change records.

When a parse fails or the type is unknown, the caster generally delivers the original string. Malformed numeric and bytea payloads raise inside cast_record/2; Replicant converts stream failures to a value-free :decode_failure. The timestamp conversion calls DateTime.from_naive!/2 only after parsing a valid naive timestamp.

Cast arrays preserve nesting. Elements use the same rules as scalar columns. NULL elements become nil at any depth. interval and timetz deliver raw strings, including in arrays; the timetz string preserves the offset and fractional seconds.

money returns a Decimal for strict C or en-US forms such as "$1,234.56" or "-$5.00". Other shapes retain the original string. Accept Decimal or a string for money columns when your PostgreSQL locale varies.

Summary

Functions

Casts a PostgreSQL string value to its appropriate Elixir type.

Functions

cast_record(record, type)

@spec cast_record(binary(), term()) :: term()

Casts a PostgreSQL string value to its appropriate Elixir type.

Examples

iex> cast_record("t", "bool")
true

iex> cast_record("123", "int4")
123

iex> cast_record("123.45", "numeric")
#Decimal<123.45>

iex> cast_record("{1,2,3}", "_int4")
[1, 2, 3]

iex> cast_record("2024-01-15T10:30:00Z", "timestamptz")
#DateTime<2024-01-15 10:30:00Z>

Special values like NaN and Infinity are handled:

iex> cast_record("NaN", "float8")
:nan

Returns the original value if casting fails.