PhoenixKitSync.ConnectionNotifier.Prepare (PhoenixKitSync v0.2.4)

Copy Markdown View Source

Record-transformation and value-preparation helpers shared across the import paths in ConnectionNotifier.

Three concerns:

  1. value/1 and value/3 — convert JSON-transported scalars back into Postgrex-ready terms. ISO8601 strings become DateTime / Date / Time. Numeric strings are parsed only when the target column is numeric (the 3-arity form): a %Decimal{} for numeric/decimal, a float for double precision/real. The broad 1-arity form (Decimal only) is kept for the PK/unique-key lookup paths where values are known keys, not free-text.
  2. numeric_column_types/1 — maps each numeric column of a table to :decimal or :float. Cached once per table-import by the importer; empty map on error (safe "don't coerce" fallback).
  3. Record field helpers (get_field/2, put_field/3, drop_field/2, normalize_keys/1) — records arrive with either string or atom keys depending on whether they came through JSON or internal code. These helpers access/modify fields uniformly without creating atoms from untrusted data.

Extracted from ConnectionNotifier in 2026-04. All functions are pure or depend only on SchemaInspector (for column introspection) — none touch HTTP, the socket, or the websocket client.

Summary

Functions

Maps each numeric column of table_name to the term a numeric string in it should become: :decimal for numeric/decimal, :float for double precision/real. Empty map on error.

Functions

drop_field(record, field)

@spec drop_field(map(), String.t()) :: map()

get_field(record, field)

@spec get_field(map(), String.t()) :: any()

normalize_keys(record)

@spec normalize_keys(map()) :: map()

numeric_column_types(table_name)

@spec numeric_column_types(String.t()) :: %{required(String.t()) => :decimal | :float}

Maps each numeric column of table_name to the term a numeric string in it should become: :decimal for numeric/decimal, :float for double precision/real. Empty map on error.

put_field(record, field, value)

@spec put_field(map(), String.t(), any()) :: map()

value(value)

@spec value(any()) :: any()

value(value, column, column_types)

@spec value(any(), String.t(), %{required(String.t()) => :decimal | :float}) :: any()