Current section
Files
Jump to
Current section
Files
src/postgleam@decode.erl
-module(postgleam@decode).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/postgleam/decode.gleam").
-export([run/2, success/1, optional/1, int/1, element/3, text/1, bool/1, float/1, bytea/1, uuid/1, uuid_string/1, json/1, jsonb/1, numeric/1, date/1, timestamp/1, timestamptz/1, time/1, timetz/1, interval/1, xml/1, jsonpath/1, money/1, point/1, line/1, lseg/1, box/1, circle/1, path/1, polygon/1, inet/1, macaddr/1, macaddr8/1, bit_string/1]).
-export_type([row_decoder/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-opaque row_decoder(HSW) :: {row_decoder,
fun((list(gleam@option:option(postgleam@value:value()))) -> {ok, HSW} |
{error, postgleam@error:error()})}.
-file("src/postgleam/decode.gleam", 31).
?DOC(" Run a decoder on a row.\n").
-spec run(row_decoder(HTB), list(gleam@option:option(postgleam@value:value()))) -> {ok,
HTB} |
{error, postgleam@error:error()}.
run(Decoder, Row) ->
(erlang:element(2, Decoder))(Row).
-file("src/postgleam/decode.gleam", 66).
?DOC(" Finalize a decoder chain with a value.\n").
-spec success(HTM) -> row_decoder(HTM).
success(Value) ->
{row_decoder, fun(_) -> {ok, Value} end}.
-file("src/postgleam/decode.gleam", 412).
?DOC(
" Make any value decoder nullable (NULL-safe).\n"
" Returns `Ok(None)` for NULL, `Ok(Some(val))` for non-NULL.\n"
).
-spec optional(
fun((gleam@option:option(postgleam@value:value())) -> {ok, HXC} |
{error, postgleam@error:error()})
) -> fun((gleam@option:option(postgleam@value:value())) -> {ok,
gleam@option:option(HXC)} |
{error, postgleam@error:error()}).
optional(Decoder) ->
fun(Val) -> case Val of
none ->
{ok, none};
_ ->
case Decoder(Val) of
{ok, V} ->
{ok, {some, V}};
{error, E} ->
{error, E}
end
end end.
-file("src/postgleam/decode.gleam", 429).
-spec list_at(list(HXG), integer()) -> {ok, HXG} | {error, nil}.
list_at(L, Index) ->
case {L, Index} of
{[X | _], 0} ->
{ok, X};
{[_ | Rest], N} when N > 0 ->
list_at(Rest, N - 1);
{_, _} ->
{error, nil}
end.
-file("src/postgleam/decode.gleam", 437).
-spec value_type_name(postgleam@value:value()) -> binary().
value_type_name(Val) ->
case Val of
null ->
<<"Null"/utf8>>;
{boolean, _} ->
<<"Boolean"/utf8>>;
{integer, _} ->
<<"Integer"/utf8>>;
{float, _} ->
<<"Float"/utf8>>;
pos_infinity ->
<<"PosInfinity"/utf8>>;
neg_infinity ->
<<"NegInfinity"/utf8>>;
na_n ->
<<"NaN"/utf8>>;
{text, _} ->
<<"Text"/utf8>>;
{bytea, _} ->
<<"Bytea"/utf8>>;
{uuid, _} ->
<<"Uuid"/utf8>>;
{oid, _} ->
<<"Oid"/utf8>>;
void ->
<<"Void"/utf8>>;
{array, _} ->
<<"Array"/utf8>>;
{date, _} ->
<<"Date"/utf8>>;
{time, _} ->
<<"Time"/utf8>>;
{time_tz, _, _} ->
<<"TimeTz"/utf8>>;
{timestamp, _} ->
<<"Timestamp"/utf8>>;
{timestamptz, _} ->
<<"Timestamptz"/utf8>>;
{interval, _, _, _} ->
<<"Interval"/utf8>>;
{json, _} ->
<<"Json"/utf8>>;
{jsonb, _} ->
<<"Jsonb"/utf8>>;
{numeric, _} ->
<<"Numeric"/utf8>>;
{point, _, _} ->
<<"Point"/utf8>>;
{inet, _, _, _} ->
<<"Inet"/utf8>>;
{macaddr, _} ->
<<"Macaddr"/utf8>>;
{macaddr8, _} ->
<<"Macaddr8"/utf8>>;
{money, _} ->
<<"Money"/utf8>>;
{xml, _} ->
<<"Xml"/utf8>>;
{jsonpath, _} ->
<<"Jsonpath"/utf8>>;
{bit_string, _, _} ->
<<"BitString"/utf8>>;
{line, _, _, _} ->
<<"Line"/utf8>>;
{lseg, _, _, _, _} ->
<<"Lseg"/utf8>>;
{box, _, _, _, _} ->
<<"Box"/utf8>>;
{path, _, _} ->
<<"Path"/utf8>>;
{polygon, _} ->
<<"Polygon"/utf8>>;
{circle, _, _, _} ->
<<"Circle"/utf8>>
end.
-file("src/postgleam/decode.gleam", 75).
?DOC(" Decode an integer value (int2, int4, int8).\n").
-spec int(gleam@option:option(postgleam@value:value())) -> {ok, integer()} |
{error, postgleam@error:error()}.
int(Val) ->
case Val of
{some, {integer, N}} ->
{ok, N};
{some, Other} ->
{error,
{decode_error,
<<"Expected Integer, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Integer, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 45).
?DOC(
" Decode the element at a given column index using a value decoder.\n"
" Designed for use with Gleam's `use` syntax.\n"
"\n"
" ```gleam\n"
" let decoder = {\n"
" use id <- decode.element(0, decode.int)\n"
" use name <- decode.element(1, decode.text)\n"
" decode.success(#(id, name))\n"
" }\n"
" ```\n"
).
-spec element(
integer(),
fun((gleam@option:option(postgleam@value:value())) -> {ok, HTH} |
{error, postgleam@error:error()}),
fun((HTH) -> row_decoder(HTJ))
) -> row_decoder(HTJ).
element(Index, Decoder, Next) ->
{row_decoder, fun(Row) -> case list_at(Row, Index) of
{ok, Cell} ->
case Decoder(Cell) of
{ok, Val} ->
run(Next(Val), Row);
{error, E} ->
{error, E}
end;
{error, _} ->
{error,
{decode_error,
<<<<"Column index "/utf8,
(erlang:integer_to_binary(Index))/binary>>/binary,
" out of bounds"/utf8>>}}
end end}.
-file("src/postgleam/decode.gleam", 85).
?DOC(" Decode a text/string value.\n").
-spec text(gleam@option:option(postgleam@value:value())) -> {ok, binary()} |
{error, postgleam@error:error()}.
text(Val) ->
case Val of
{some, {text, S}} ->
{ok, S};
{some, Other} ->
{error,
{decode_error,
<<"Expected Text, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Text, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 95).
?DOC(" Decode a boolean value.\n").
-spec bool(gleam@option:option(postgleam@value:value())) -> {ok, boolean()} |
{error, postgleam@error:error()}.
bool(Val) ->
case Val of
{some, {boolean, B}} ->
{ok, B};
{some, Other} ->
{error,
{decode_error,
<<"Expected Boolean, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Boolean, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 107).
?DOC(" Decode a float value (float4, float8).\n").
-spec float(gleam@option:option(postgleam@value:value())) -> {ok, float()} |
{error, postgleam@error:error()}.
float(Val) ->
case Val of
{some, {float, F}} ->
{ok, F};
{some, Other} ->
{error,
{decode_error,
<<"Expected Float, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Float, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 117).
?DOC(" Decode a bytea (binary data) value.\n").
-spec bytea(gleam@option:option(postgleam@value:value())) -> {ok, bitstring()} |
{error, postgleam@error:error()}.
bytea(Val) ->
case Val of
{some, {bytea, B}} ->
{ok, B};
{some, Other} ->
{error,
{decode_error,
<<"Expected Bytea, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Bytea, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 127).
?DOC(" Decode a UUID value (16-byte binary).\n").
-spec uuid(gleam@option:option(postgleam@value:value())) -> {ok, bitstring()} |
{error, postgleam@error:error()}.
uuid(Val) ->
case Val of
{some, {uuid, U}} ->
{ok, U};
{some, Other} ->
{error,
{decode_error,
<<"Expected Uuid, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Uuid, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 137).
?DOC(" Decode a UUID value as a hyphenated string (e.g. \"550e8400-e29b-41d4-a716-446655440000\").\n").
-spec uuid_string(gleam@option:option(postgleam@value:value())) -> {ok,
binary()} |
{error, postgleam@error:error()}.
uuid_string(Val) ->
case Val of
{some, {uuid, _} = V} ->
case postgleam@value:uuid_to_string(V) of
{ok, S} ->
{ok, S};
{error, _} ->
{error,
{decode_error,
<<"Failed to format UUID as string"/utf8>>}}
end;
{some, Other} ->
{error,
{decode_error,
<<"Expected Uuid, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Uuid, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 151).
?DOC(" Decode a JSON value (string).\n").
-spec json(gleam@option:option(postgleam@value:value())) -> {ok, binary()} |
{error, postgleam@error:error()}.
json(Val) ->
case Val of
{some, {json, S}} ->
{ok, S};
{some, Other} ->
{error,
{decode_error,
<<"Expected Json, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Json, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 161).
?DOC(" Decode a JSONB value (string).\n").
-spec jsonb(gleam@option:option(postgleam@value:value())) -> {ok, binary()} |
{error, postgleam@error:error()}.
jsonb(Val) ->
case Val of
{some, {jsonb, S}} ->
{ok, S};
{some, Other} ->
{error,
{decode_error,
<<"Expected Jsonb, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Jsonb, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 171).
?DOC(" Decode a numeric/decimal value (string representation).\n").
-spec numeric(gleam@option:option(postgleam@value:value())) -> {ok, binary()} |
{error, postgleam@error:error()}.
numeric(Val) ->
case Val of
{some, {numeric, N}} ->
{ok, N};
{some, Other} ->
{error,
{decode_error,
<<"Expected Numeric, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Numeric, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 183).
?DOC(" Decode a date value (days since 2000-01-01).\n").
-spec date(gleam@option:option(postgleam@value:value())) -> {ok, integer()} |
{error, postgleam@error:error()}.
date(Val) ->
case Val of
{some, {date, D}} ->
{ok, D};
{some, Other} ->
{error,
{decode_error,
<<"Expected Date, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Date, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 193).
?DOC(" Decode a timestamp value (microseconds since 2000-01-01 00:00:00).\n").
-spec timestamp(gleam@option:option(postgleam@value:value())) -> {ok, integer()} |
{error, postgleam@error:error()}.
timestamp(Val) ->
case Val of
{some, {timestamp, T}} ->
{ok, T};
{some, Other} ->
{error,
{decode_error,
<<"Expected Timestamp, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Timestamp, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 205).
?DOC(" Decode a timestamptz value (microseconds since 2000-01-01 00:00:00 UTC).\n").
-spec timestamptz(gleam@option:option(postgleam@value:value())) -> {ok,
integer()} |
{error, postgleam@error:error()}.
timestamptz(Val) ->
case Val of
{some, {timestamptz, T}} ->
{ok, T};
{some, Other} ->
{error,
{decode_error,
<<"Expected Timestamptz, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Timestamptz, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 217).
?DOC(" Decode a time value (microseconds since midnight).\n").
-spec time(gleam@option:option(postgleam@value:value())) -> {ok, integer()} |
{error, postgleam@error:error()}.
time(Val) ->
case Val of
{some, {time, T}} ->
{ok, T};
{some, Other} ->
{error,
{decode_error,
<<"Expected Time, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Time, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 227).
?DOC(" Decode a timetz value as #(microseconds, tz_offset_seconds).\n").
-spec timetz(gleam@option:option(postgleam@value:value())) -> {ok,
{integer(), integer()}} |
{error, postgleam@error:error()}.
timetz(Val) ->
case Val of
{some, {time_tz, Us, Tz}} ->
{ok, {Us, Tz}};
{some, Other} ->
{error,
{decode_error,
<<"Expected TimeTz, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected TimeTz, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 239).
?DOC(" Decode an interval value as #(microseconds, days, months).\n").
-spec interval(gleam@option:option(postgleam@value:value())) -> {ok,
{integer(), integer(), integer()}} |
{error, postgleam@error:error()}.
interval(Val) ->
case Val of
{some, {interval, Us, Days, Months}} ->
{ok, {Us, Days, Months}};
{some, Other} ->
{error,
{decode_error,
<<"Expected Interval, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Interval, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 251).
?DOC(" Decode an XML value (string).\n").
-spec xml(gleam@option:option(postgleam@value:value())) -> {ok, binary()} |
{error, postgleam@error:error()}.
xml(Val) ->
case Val of
{some, {xml, S}} ->
{ok, S};
{some, Other} ->
{error,
{decode_error,
<<"Expected Xml, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Xml, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 261).
?DOC(" Decode a JSONPath value (string).\n").
-spec jsonpath(gleam@option:option(postgleam@value:value())) -> {ok, binary()} |
{error, postgleam@error:error()}.
jsonpath(Val) ->
case Val of
{some, {jsonpath, S}} ->
{ok, S};
{some, Other} ->
{error,
{decode_error,
<<"Expected Jsonpath, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Jsonpath, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 273).
?DOC(" Decode a money value (int64 cents).\n").
-spec money(gleam@option:option(postgleam@value:value())) -> {ok, integer()} |
{error, postgleam@error:error()}.
money(Val) ->
case Val of
{some, {money, N}} ->
{ok, N};
{some, Other} ->
{error,
{decode_error,
<<"Expected Money, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Money, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 283).
?DOC(" Decode a point value as #(x, y).\n").
-spec point(gleam@option:option(postgleam@value:value())) -> {ok,
{float(), float()}} |
{error, postgleam@error:error()}.
point(Val) ->
case Val of
{some, {point, X, Y}} ->
{ok, {X, Y}};
{some, Other} ->
{error,
{decode_error,
<<"Expected Point, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Point, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 293).
?DOC(" Decode a line value as #(a, b, c) coefficients.\n").
-spec line(gleam@option:option(postgleam@value:value())) -> {ok,
{float(), float(), float()}} |
{error, postgleam@error:error()}.
line(Val) ->
case Val of
{some, {line, A, B, C}} ->
{ok, {A, B, C}};
{some, Other} ->
{error,
{decode_error,
<<"Expected Line, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Line, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 303).
?DOC(" Decode a line segment as #(x1, y1, x2, y2).\n").
-spec lseg(gleam@option:option(postgleam@value:value())) -> {ok,
{float(), float(), float(), float()}} |
{error, postgleam@error:error()}.
lseg(Val) ->
case Val of
{some, {lseg, X1, Y1, X2, Y2}} ->
{ok, {X1, Y1, X2, Y2}};
{some, Other} ->
{error,
{decode_error,
<<"Expected Lseg, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Lseg, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 315).
?DOC(" Decode a box as #(x1, y1, x2, y2) (upper-right, lower-left).\n").
-spec box(gleam@option:option(postgleam@value:value())) -> {ok,
{float(), float(), float(), float()}} |
{error, postgleam@error:error()}.
box(Val) ->
case Val of
{some, {box, X1, Y1, X2, Y2}} ->
{ok, {X1, Y1, X2, Y2}};
{some, Other} ->
{error,
{decode_error,
<<"Expected Box, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Box, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 327).
?DOC(" Decode a circle as #(x, y, radius).\n").
-spec circle(gleam@option:option(postgleam@value:value())) -> {ok,
{float(), float(), float()}} |
{error, postgleam@error:error()}.
circle(Val) ->
case Val of
{some, {circle, X, Y, R}} ->
{ok, {X, Y, R}};
{some, Other} ->
{error,
{decode_error,
<<"Expected Circle, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Circle, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 339).
?DOC(" Decode a path as #(closed, points).\n").
-spec path(gleam@option:option(postgleam@value:value())) -> {ok,
{boolean(), list({float(), float()})}} |
{error, postgleam@error:error()}.
path(Val) ->
case Val of
{some, {path, Closed, Pts}} ->
{ok, {Closed, Pts}};
{some, Other} ->
{error,
{decode_error,
<<"Expected Path, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Path, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 351).
?DOC(" Decode a polygon as a list of vertices.\n").
-spec polygon(gleam@option:option(postgleam@value:value())) -> {ok,
list({float(), float()})} |
{error, postgleam@error:error()}.
polygon(Val) ->
case Val of
{some, {polygon, Pts}} ->
{ok, Pts};
{some, Other} ->
{error,
{decode_error,
<<"Expected Polygon, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Polygon, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 365).
?DOC(" Decode an inet/cidr value as #(family, address, netmask).\n").
-spec inet(gleam@option:option(postgleam@value:value())) -> {ok,
{integer(), bitstring(), integer()}} |
{error, postgleam@error:error()}.
inet(Val) ->
case Val of
{some, {inet, Family, Addr, Mask}} ->
{ok, {Family, Addr, Mask}};
{some, Other} ->
{error,
{decode_error,
<<"Expected Inet, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Inet, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 375).
?DOC(" Decode a macaddr value (6-byte binary).\n").
-spec macaddr(gleam@option:option(postgleam@value:value())) -> {ok, bitstring()} |
{error, postgleam@error:error()}.
macaddr(Val) ->
case Val of
{some, {macaddr, B}} ->
{ok, B};
{some, Other} ->
{error,
{decode_error,
<<"Expected Macaddr, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Macaddr, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 387).
?DOC(" Decode a macaddr8 value (8-byte binary).\n").
-spec macaddr8(gleam@option:option(postgleam@value:value())) -> {ok,
bitstring()} |
{error, postgleam@error:error()}.
macaddr8(Val) ->
case Val of
{some, {macaddr8, B}} ->
{ok, B};
{some, Other} ->
{error,
{decode_error,
<<"Expected Macaddr8, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected Macaddr8, got NULL"/utf8>>}}
end.
-file("src/postgleam/decode.gleam", 399).
?DOC(" Decode a bit/varbit value as #(bit_count, data).\n").
-spec bit_string(gleam@option:option(postgleam@value:value())) -> {ok,
{integer(), bitstring()}} |
{error, postgleam@error:error()}.
bit_string(Val) ->
case Val of
{some, {bit_string, Count, Data}} ->
{ok, {Count, Data}};
{some, Other} ->
{error,
{decode_error,
<<"Expected BitString, got "/utf8,
(value_type_name(Other))/binary>>}};
none ->
{error, {decode_error, <<"Expected BitString, got NULL"/utf8>>}}
end.