Current section
Files
Jump to
Current section
Files
src/qs.erl
-module(qs).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([parse_key_value/1, split_and_parse/1, join_key_value/3, serialize_with/2, serialize/1, empty/0, parse/1, get/2, has_key/2, insert/3, merge/2, delete/2]).
-spec parse_key_value(binary()) -> {ok, {binary(), binary()}} |
{error, binary()}.
parse_key_value(Segment) ->
_pipe = Segment,
_pipe@1 = gleam@uri:percent_decode(_pipe),
_pipe@2 = gleam@result:unwrap(_pipe@1, Segment),
_pipe@3 = gleam@string:split_once(_pipe@2, <<"="/utf8>>),
gleam@result:replace_error(
_pipe@3,
begin
_pipe@4 = <<"Unable to parse "/utf8>>,
gleam@string:append(_pipe@4, Segment)
end
).
-spec split_and_parse(binary()) -> {ok, list({binary(), binary()})} |
{error, binary()}.
split_and_parse(Qs) ->
_pipe = Qs,
_pipe@1 = gleam@string:replace(_pipe, <<"?"/utf8>>, <<""/utf8>>),
_pipe@2 = gleam@string:split(_pipe@1, <<"&"/utf8>>),
_pipe@3 = gleam@list:map(_pipe@2, fun parse_key_value/1),
_pipe@4 = gleam@result:values(_pipe@3),
{ok, _pipe@4}.
-spec add_key_value(
gleam@dict:dict(binary(), list(binary())),
{binary(), binary()}
) -> gleam@dict:dict(binary(), list(binary())).
add_key_value(Query, Key_value) ->
{Key, Value} = Key_value,
Updater = fun(Res) -> case Res of
{some, Existing} ->
lists:append(Existing, [Value]);
none ->
[Value]
end end,
gleam@dict:upsert(Query, Key, Updater).
-spec join_key_value(binary(), binary(), binary()) -> binary().
join_key_value(Key, Value, Join) ->
<<<<(gleam@uri:percent_encode(Key))/binary, Join/binary>>/binary,
(gleam@uri:percent_encode(Value))/binary>>.
-spec serialize_key_value({binary(), list(binary())}) -> list(binary()).
serialize_key_value(Key_value) ->
{Key, Values} = Key_value,
gleam@list:map(
Values,
fun(Value) -> join_key_value(Key, Value, <<"="/utf8>>) end
).
-spec add_question_mark(binary()) -> binary().
add_question_mark(Query) ->
_pipe = <<"?"/utf8>>,
gleam@string:append(_pipe, Query).
-spec serialize_with(
gleam@dict:dict(binary(), FWS),
fun(({binary(), FWS}) -> list(binary()))
) -> binary().
serialize_with(Query, Serialize_key_value) ->
_pipe = Query,
_pipe@1 = maps:to_list(_pipe),
_pipe@2 = gleam@list:flat_map(_pipe@1, Serialize_key_value),
_pipe@3 = gleam@string:join(_pipe@2, <<"&"/utf8>>),
add_question_mark(_pipe@3).
-spec serialize(gleam@dict:dict(binary(), list(binary()))) -> binary().
serialize(Query) ->
serialize_with(Query, fun serialize_key_value/1).
-spec empty() -> gleam@dict:dict(binary(), any()).
empty() ->
gleam@dict:new().
-spec parse(binary()) -> {ok, gleam@dict:dict(binary(), list(binary()))} |
{error, binary()}.
parse(Qs) ->
gleam@result:then(
split_and_parse(Qs),
fun(Key_values) ->
_pipe = gleam@list:fold(Key_values, empty(), fun add_key_value/2),
{ok, _pipe}
end
).
-spec get(gleam@dict:dict(binary(), FWZ), binary()) -> {ok, FWZ} |
{error, binary()}.
get(Query, Key) ->
Error = <<"Invalid key "/utf8, Key/binary>>,
_pipe = gleam@dict:get(Query, Key),
gleam@result:replace_error(_pipe, Error).
-spec has_key(gleam@dict:dict(binary(), any()), binary()) -> boolean().
has_key(Query, Key) ->
gleam@dict:has_key(Query, Key).
-spec insert(gleam@dict:dict(binary(), FXF), binary(), FXF) -> gleam@dict:dict(binary(), FXF).
insert(Query, Key, Value) ->
gleam@dict:insert(Query, Key, Value).
-spec merge(gleam@dict:dict(binary(), FXI), gleam@dict:dict(binary(), FXI)) -> gleam@dict:dict(binary(), FXI).
merge(A, B) ->
gleam@dict:merge(A, B).
-spec delete(gleam@dict:dict(binary(), FXM), binary()) -> gleam@dict:dict(binary(), FXM).
delete(Query, Key) ->
gleam@dict:delete(Query, Key).