Current section
Files
Jump to
Current section
Files
src/postgleam@value.erl
-module(postgleam@value).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/postgleam/value.gleam").
-export([uuid_from_string/1, uuid_to_string/1]).
-export_type([value/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type value() :: null |
{boolean, boolean()} |
{integer, integer()} |
{float, float()} |
pos_infinity |
neg_infinity |
na_n |
{text, binary()} |
{bytea, bitstring()} |
{uuid, bitstring()} |
{oid, integer()} |
void |
{array, list(gleam@option:option(value()))} |
{date, integer()} |
{time, integer()} |
{time_tz, integer(), integer()} |
{timestamp, integer()} |
{timestamptz, integer()} |
{interval, integer(), integer(), integer()} |
{json, binary()} |
{jsonb, binary()} |
{numeric, binary()} |
{point, float(), float()} |
{inet, integer(), bitstring(), integer()} |
{macaddr, bitstring()}.
-file("src/postgleam/value.gleam", 143).
-spec hex_byte(integer()) -> {ok, integer()} | {error, nil}.
hex_byte(B) ->
case B of
_ when (B >= 16#30) andalso (B =< 16#39) ->
{ok, B - 16#30};
_ when (B >= 16#61) andalso (B =< 16#66) ->
{ok, (B - 16#61) + 10};
_ when (B >= 16#41) andalso (B =< 16#46) ->
{ok, (B - 16#41) + 10};
_ ->
{error, nil}
end.
-file("src/postgleam/value.gleam", 124).
-spec decode_hex_bytes(bitstring(), bitstring()) -> {ok, value()} | {error, nil}.
decode_hex_bytes(Input, Acc) ->
case Input of
<<>> ->
case erlang:byte_size(Acc) of
16 ->
{ok, {uuid, Acc}};
_ ->
{error, nil}
end;
<<Hi, Lo, Rest/binary>> ->
case {hex_byte(Hi), hex_byte(Lo)} of
{{ok, H}, {ok, L}} ->
decode_hex_bytes(Rest, <<Acc/bitstring, (((H * 16) + L))>>);
{_, _} ->
{error, nil}
end;
_ ->
{error, nil}
end.
-file("src/postgleam/value.gleam", 65).
?DOC(
" Parse a UUID string into a Uuid value.\n"
" Accepts formats: \"550e8400-e29b-41d4-a716-446655440000\"\n"
" or \"550e8400e29b41d4a716446655440000\" (with or without dashes).\n"
).
-spec uuid_from_string(binary()) -> {ok, value()} | {error, nil}.
uuid_from_string(S) ->
Bytes = gleam_stdlib:identity(S),
case Bytes of
<<A1,
A2,
A3,
A4,
A5,
A6,
A7,
A8,
16#2D,
B1,
B2,
B3,
B4,
16#2D,
C1,
C2,
C3,
C4,
16#2D,
D1,
D2,
D3,
D4,
16#2D,
E1,
E2,
E3,
E4,
E5,
E6,
E7,
E8,
E9,
E10,
E11,
E12>> ->
decode_hex_bytes(
<<A1,
A2,
A3,
A4,
A5,
A6,
A7,
A8,
B1,
B2,
B3,
B4,
C1,
C2,
C3,
C4,
D1,
D2,
D3,
D4,
E1,
E2,
E3,
E4,
E5,
E6,
E7,
E8,
E9,
E10,
E11,
E12>>,
<<>>
);
_ ->
case erlang:byte_size(Bytes) of
32 ->
decode_hex_bytes(Bytes, <<>>);
_ ->
{error, nil}
end
end.
-file("src/postgleam/value.gleam", 154).
-spec byte_to_hex(integer()) -> binary().
byte_to_hex(B) ->
Hi = gleam@string:slice(<<"0123456789abcdef"/utf8>>, B div 16, 1),
Lo = gleam@string:slice(<<"0123456789abcdef"/utf8>>, B rem 16, 1),
<<Hi/binary, Lo/binary>>.
-file("src/postgleam/value.gleam", 94).
?DOC(
" Format a Uuid value as a hyphenated string.\n"
" Returns \"550e8400-e29b-41d4-a716-446655440000\" format.\n"
).
-spec uuid_to_string(value()) -> {ok, binary()} | {error, nil}.
uuid_to_string(Val) ->
case Val of
{uuid, <<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P>>} ->
{ok,
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<(byte_to_hex(A))/binary,
(byte_to_hex(
B
))/binary>>/binary,
(byte_to_hex(
C
))/binary>>/binary,
(byte_to_hex(
D
))/binary>>/binary,
"-"/utf8>>/binary,
(byte_to_hex(
E
))/binary>>/binary,
(byte_to_hex(
F
))/binary>>/binary,
"-"/utf8>>/binary,
(byte_to_hex(G))/binary>>/binary,
(byte_to_hex(H))/binary>>/binary,
"-"/utf8>>/binary,
(byte_to_hex(I))/binary>>/binary,
(byte_to_hex(J))/binary>>/binary,
"-"/utf8>>/binary,
(byte_to_hex(K))/binary>>/binary,
(byte_to_hex(L))/binary>>/binary,
(byte_to_hex(M))/binary>>/binary,
(byte_to_hex(N))/binary>>/binary,
(byte_to_hex(O))/binary>>/binary,
(byte_to_hex(P))/binary>>};
_ ->
{error, nil}
end.