Current section

Files

Jump to
postgleam src postgleam@value.erl
Raw

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_to_string/1, uuid_from_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()} |
{macaddr8, bitstring()} |
{money, integer()} |
{xml, binary()} |
{jsonpath, binary()} |
{bit_string, integer(), bitstring()} |
{line, float(), float(), float()} |
{lseg, float(), float(), float(), float()} |
{box, float(), float(), float(), float()} |
{path, boolean(), list({float(), float()})} |
{polygon, list({float(), float()})} |
{circle, float(), float(), float()}.
-file("src/postgleam/value.gleam", 149).
-spec hex_nibble(integer()) -> integer().
hex_nibble(N) ->
case N < 10 of
true ->
N + 16#30;
false ->
N + 16#57
end.
-file("src/postgleam/value.gleam", 141).
-spec hex_hi(integer()) -> integer().
hex_hi(B) ->
hex_nibble(B div 16).
-file("src/postgleam/value.gleam", 145).
-spec hex_lo(integer()) -> integer().
hex_lo(B) ->
hex_nibble(B rem 16).
-file("src/postgleam/value.gleam", 115).
?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>>} ->
Result = <<(hex_hi(A)),
(hex_lo(A)),
(hex_hi(B)),
(hex_lo(B)),
(hex_hi(C)),
(hex_lo(C)),
(hex_hi(D)),
(hex_lo(D)),
16#2D,
(hex_hi(E)),
(hex_lo(E)),
(hex_hi(F)),
(hex_lo(F)),
16#2D,
(hex_hi(G)),
(hex_lo(G)),
(hex_hi(H)),
(hex_lo(H)),
16#2D,
(hex_hi(I)),
(hex_lo(I)),
(hex_hi(J)),
(hex_lo(J)),
16#2D,
(hex_hi(K)),
(hex_lo(K)),
(hex_hi(L)),
(hex_lo(L)),
(hex_hi(M)),
(hex_lo(M)),
(hex_hi(N)),
(hex_lo(N)),
(hex_hi(O)),
(hex_lo(O)),
(hex_hi(P)),
(hex_lo(P))>>,
case gleam@bit_array:to_string(Result) of
{ok, S} ->
{ok, S};
{error, _} ->
{error, nil}
end;
_ ->
{error, nil}
end.
-file("src/postgleam/value.gleam", 175).
-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", 156).
-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", 86).
?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.