Packages
gleam_bson
0.27.0
0.27.0
retired
0.26.0
retired
0.25.0
retired
0.24.0
retired
0.23.0
retired
0.22.0
retired
0.21.0
retired
0.20.0
retired
0.19.0
retired
0.18.0
retired
0.17.0
retired
0.16.0
retired
0.15.0
retired
0.14.0
retired
0.13.0
retired
0.12.0
retired
0.11.0
retired
0.10.1
retired
0.10.0
retired
0.9.0
retired
0.8.0
retired
0.7.0
retired
0.6.0
retired
0.5.0
retired
0.4.0
retired
0.3.0
retired
0.2.0
retired
0.1.0
retired
bson encoder and decoder for gleam
Retired package: Renamed - Republished as bison
Current section
Files
Jump to
Current section
Files
src/bson@encoder.erl
-module(bson@encoder).
-compile([no_auto_import, nowarn_unused_vars]).
-export([encode/1]).
-export_type([entity/0]).
-type entity() :: {entity, bson@kind:kind(), bitstring()}.
-spec null() -> entity().
null() ->
{entity, {kind, <<16#0A>>}, <<>>}.
-spec min() -> entity().
min() ->
{entity, {kind, <<16#FF>>}, <<>>}.
-spec max() -> entity().
max() ->
{entity, {kind, <<16#7F>>}, <<>>}.
-spec js(binary()) -> entity().
js(Value) ->
Length = gleam@bit_string:byte_size(<<Value/binary>>) + 1,
{entity, {kind, <<16#0D>>}, <<Length:32/little, Value/binary, 0>>}.
-spec string(binary()) -> entity().
string(Value) ->
Length = gleam@bit_string:byte_size(<<Value/binary>>) + 1,
{entity, {kind, <<16#02>>}, <<Length:32/little, Value/binary, 0>>}.
-spec double(float()) -> entity().
double(Value) ->
{entity, {kind, <<16#01>>}, <<Value/little-float>>}.
-spec boolean(boolean()) -> entity().
boolean(Value) ->
case Value of
true ->
{entity, {kind, <<16#08>>}, <<1>>};
false ->
{entity, {kind, <<16#08>>}, <<0>>}
end.
-spec int32(integer()) -> entity().
int32(Value) ->
{entity, {kind, <<16#10>>}, <<Value:32/little>>}.
-spec int64(integer()) -> entity().
int64(Value) ->
{entity, {kind, <<16#12>>}, <<Value:64/little>>}.
-spec datetime(birl@time:date_time()) -> entity().
datetime(Value) ->
{duration, Value@1} = birl@time:difference(
Value,
{date_time, 0, 0, none, none}
),
Value@2 = Value@1 div 1000,
{entity, {kind, <<16#09>>}, <<Value@2:64/little>>}.
-spec object_id(bson@object_id:object_id()) -> entity().
object_id(Value) ->
{entity, {kind, <<16#07>>}, bson@object_id:to_bit_string(Value)}.
-spec timestamp(integer()) -> entity().
timestamp(Value) ->
{entity, {kind, <<16#11>>}, <<Value:64/little>>}.
-spec md5(bson@md5:md5()) -> entity().
md5(Value) ->
Value@1 = bson@md5:to_bit_string(Value),
Length = gleam@bit_string:byte_size(Value@1),
{entity,
{kind, <<16#05>>},
begin
_pipe = [<<Length:32/little>>,
erlang:element(2, {sub_kind, <<16#5>>}),
Value@1],
gleam@bit_string:concat(_pipe)
end}.
-spec uuid(bson@uuid:uuid()) -> entity().
uuid(Value) ->
Value@1 = bson@uuid:to_bit_string(Value),
Length = gleam@bit_string:byte_size(Value@1),
{entity,
{kind, <<16#05>>},
begin
_pipe = [<<Length:32/little>>,
erlang:element(2, {sub_kind, <<16#4>>}),
Value@1],
gleam@bit_string:concat(_pipe)
end}.
-spec custom(bson@custom:custom()) -> entity().
custom(Value) ->
{Code, Value@1} = bson@custom:to_bit_string_with_code(Value),
Length = gleam@bit_string:byte_size(Value@1),
{entity,
{kind, <<16#05>>},
begin
_pipe = [<<Length:32/little>>, <<Code>>, Value@1],
gleam@bit_string:concat(_pipe)
end}.
-spec generic(bson@generic:generic()) -> entity().
generic(Value) ->
Value@1 = bson@generic:to_bit_string(Value),
Length = gleam@bit_string:byte_size(Value@1),
{entity,
{kind, <<16#05>>},
begin
_pipe = [<<Length:32/little>>,
erlang:element(2, {sub_kind, <<16#0>>}),
Value@1],
gleam@bit_string:concat(_pipe)
end}.
-spec regex(binary(), binary()) -> entity().
regex(Pattern, Options) ->
{entity,
{kind, <<16#0B>>},
begin
_pipe = [<<Pattern/binary, 0, Options/binary, 0>>],
gleam@bit_string:concat(_pipe)
end}.
-spec encode_kv({binary(), bson@value:value()}) -> bitstring().
encode_kv(Pair) ->
Key = <<(erlang:element(1, Pair))/binary, 0>>,
Value@15 = case erlang:element(2, Pair) of
null ->
null();
min ->
min();
max ->
max();
{js, Value} ->
js(Value);
{str, Value@1} ->
string(Value@1);
{array, Value@2} ->
array(Value@2);
{int32, Value@3} ->
int32(Value@3);
{int64, Value@4} ->
int64(Value@4);
{double, Value@5} ->
double(Value@5);
{boolean, Value@6} ->
boolean(Value@6);
{document, Value@7} ->
document(Value@7);
{date_time, Value@8} ->
datetime(Value@8);
{object_id, Value@9} ->
object_id(Value@9);
{timestamp, Value@10} ->
timestamp(Value@10);
{binary, {md5, Value@11}} ->
md5(Value@11);
{binary, {uuid, Value@12}} ->
uuid(Value@12);
{binary, {custom, Value@13}} ->
custom(Value@13);
{binary, {generic, Value@14}} ->
generic(Value@14);
{regex, {Pattern, Options}} ->
regex(Pattern, Options)
end,
case Value@15 of
{entity, Kind, Value@16} ->
_pipe = [erlang:element(2, Kind), Key, Value@16],
gleam@bit_string:concat(_pipe)
end.
-spec document(list({binary(), bson@value:value()})) -> entity().
document(Doc) ->
Doc@1 = begin
_pipe = Doc,
_pipe@1 = gleam@list:map(_pipe, fun encode_kv/1),
gleam@bit_string:concat(_pipe@1)
end,
Size = gleam@bit_string:byte_size(Doc@1) + 5,
{entity,
{kind, <<16#03>>},
begin
_pipe@2 = [<<Size:32/little>>, Doc@1, <<0>>],
gleam@bit_string:concat(_pipe@2)
end}.
-spec encode(list({binary(), bson@value:value()})) -> bitstring().
encode(Doc) ->
case document(Doc) of
{entity, _, Value} ->
Value
end.
-spec array(list(bson@value:value())) -> entity().
array(Value) ->
case begin
_pipe = gleam@list:index_map(
Value,
fun(Index, Item) -> {gleam@int:to_string(Index), Item} end
),
document(_pipe)
end of
{entity, _, Value@1} ->
{entity, {kind, <<16#04>>}, Value@1}
end.