WalEx.Casting.ArrayParser (WalEx v4.7.0)

View Source

Parser for PostgreSQL array literals

Implementation inspired by Supabase Realtime and Sequin

Summary

Functions

Parses a PostgreSQL array literal string into an Elixir list.

Functions

parse(array_string)

Parses a PostgreSQL array literal string into an Elixir list.

Returns all elements as strings - the caller is responsible for any type conversion. NULL values are returned as nil.

Parameters

  • array_string - PostgreSQL array literal (e.g., "{1,2,3}")

Returns

  • {:ok, list} - Successfully parsed array as nested list
  • {:error, reason} - Parsing failed with reason

Examples

# Simple arrays
iex> WalEx.ArrayParser.parse("{1,2,3}")
{:ok, ["1", "2", "3"]}

# Empty arrays
iex> WalEx.ArrayParser.parse("{}")
{:ok, []}

# Arrays with quoted strings
iex> WalEx.ArrayParser.parse("{"hello, world","foo"}")
{:ok, ["hello, world", "foo"]}

# Nested arrays
iex> WalEx.ArrayParser.parse("{{1,2},{3,4}}")
{:ok, [["1", "2"], ["3", "4"]]}

# NULL values
iex> WalEx.ArrayParser.parse("{1,NULL,3}")
{:ok, ["1", nil, "3"]}