Current section
Files
Jump to
Current section
Files
src/convert@http@query.erl
-module(convert@http@query).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([decode_value/1, decode/2, encode_value/1, encode/2]).
-export_type([query_decode_error/0]).
-type query_decode_error() :: {key_not_found, binary()} |
{decode_error, list(gleam@dynamic:decode_error())}.
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 75).
-spec encode_dict_key(convert:glitr_value()) -> binary().
encode_dict_key(Key) ->
case Key of
{bool_value, V} ->
gleam@bool:to_string(V);
{float_value, V@1} ->
gleam_stdlib:float_to_string(V@1);
{int_value, V@2} ->
erlang:integer_to_binary(V@2);
{string_value, V@3} ->
V@3;
{bit_array_value, V@4} ->
gleam@bit_array:base64_url_encode(V@4, true);
_ ->
<<""/utf8>>
end.
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 209).
-spec get_value(
list({binary(), binary()}),
binary(),
fun((binary()) -> {ok, convert:glitr_value()} |
{error, query_decode_error()})
) -> {ok, convert:glitr_value()} | {error, query_decode_error()}.
get_value(Query, Key, Callback) ->
_pipe = gleam@list:key_pop(Query, Key),
_pipe@1 = gleam@result:replace_error(_pipe, {key_not_found, Key}),
gleam@result:then(_pipe@1, fun(Q) -> Callback(erlang:element(1, Q)) end).
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 219).
-spec decode_bit_array(list({binary(), binary()}), list(binary())) -> {ok,
convert:glitr_value()} |
{error, query_decode_error()}.
decode_bit_array(Query, Location) ->
get_value(
Query,
gleam@string:join(Location, <<"."/utf8>>),
fun(V) -> _pipe = gleam@bit_array:base64_url_decode(V),
_pipe@1 = gleam@result:map(
_pipe,
fun(Field@0) -> {bit_array_value, Field@0} end
),
gleam@result:replace_error(
_pipe@1,
{decode_error,
[{decode_error,
<<"A base64 encoded bit array"/utf8>>,
V,
Location}]}
) end
).
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 231).
-spec decode_bool(list({binary(), binary()}), list(binary())) -> {ok,
convert:glitr_value()} |
{error, query_decode_error()}.
decode_bool(Query, Location) ->
get_value(
Query,
gleam@string:join(Location, <<"."/utf8>>),
fun(V) -> case V of
<<"True"/utf8>> ->
{ok, {bool_value, true}};
<<"False"/utf8>> ->
{ok, {bool_value, false}};
_ ->
{error,
{decode_error,
[{decode_error,
<<"True or False"/utf8>>,
V,
Location}]}}
end end
).
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 243).
-spec decode_int(list({binary(), binary()}), list(binary())) -> {ok,
convert:glitr_value()} |
{error, query_decode_error()}.
decode_int(Query, Location) ->
get_value(
Query,
gleam@string:join(Location, <<"."/utf8>>),
fun(V) -> _pipe = gleam_stdlib:parse_int(V),
_pipe@1 = gleam@result:map(
_pipe,
fun(Field@0) -> {int_value, Field@0} end
),
gleam@result:replace_error(
_pipe@1,
{decode_error,
[{decode_error,
<<"An integer string representation"/utf8>>,
V,
Location}]}
) end
).
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 257).
-spec decode_float(list({binary(), binary()}), list(binary())) -> {ok,
convert:glitr_value()} |
{error, query_decode_error()}.
decode_float(Query, Location) ->
get_value(
Query,
gleam@string:join(Location, <<"."/utf8>>),
fun(V) -> _pipe = gleam_stdlib:parse_float(V),
_pipe@1 = gleam@result:map(
_pipe,
fun(Field@0) -> {float_value, Field@0} end
),
gleam@result:replace_error(
_pipe@1,
{decode_error,
[{decode_error,
<<"A float string representation"/utf8>>,
V,
Location}]}
) end
).
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 271).
-spec decode_string(list({binary(), binary()}), list(binary())) -> {ok,
convert:glitr_value()} |
{error, query_decode_error()}.
decode_string(Query, Location) ->
get_value(
Query,
gleam@string:join(Location, <<"."/utf8>>),
fun(V) -> {ok, {string_value, V}} end
).
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 355).
-spec decode_dict_key(binary(), convert:glitr_type(), list(binary())) -> {ok,
convert:glitr_value()} |
{error, query_decode_error()}.
decode_dict_key(Key, Key_type, Location) ->
Path = gleam@string:join(Location, <<"."/utf8>>),
gleam@bool:guard(
not gleam_stdlib:string_starts_with(Key, Path),
{error,
{decode_error,
[{decode_error,
<<"A string starting with the path"/utf8>>,
Key,
Location}]}},
fun() ->
Keyvalue = gleam@string:drop_start(Key, string:length(Path) + 1),
case Key_type of
bool ->
case Keyvalue of
<<"True"/utf8>> ->
{ok, {bool_value, true}};
<<"False"/utf8>> ->
{ok, {bool_value, false}};
_ ->
{error,
{decode_error,
[{decode_error,
<<"True or False"/utf8>>,
Key,
Location}]}}
end;
float ->
_pipe = gleam_stdlib:parse_float(Keyvalue),
_pipe@1 = gleam@result:map(
_pipe,
fun(Field@0) -> {float_value, Field@0} end
),
gleam@result:replace_error(
_pipe@1,
{decode_error,
[{decode_error,
<<"A float string representation"/utf8>>,
Key,
Location}]}
);
int ->
_pipe@2 = gleam_stdlib:parse_int(Keyvalue),
_pipe@3 = gleam@result:map(
_pipe@2,
fun(Field@0) -> {int_value, Field@0} end
),
gleam@result:replace_error(
_pipe@3,
{decode_error,
[{decode_error,
<<"An integer string representation"/utf8>>,
Key,
Location}]}
);
string ->
{ok, {string_value, Keyvalue}};
bit_array ->
_pipe@4 = gleam@bit_array:base64_url_decode(Keyvalue),
_pipe@5 = gleam@result:map(
_pipe@4,
fun(Field@0) -> {bit_array_value, Field@0} end
),
gleam@result:replace_error(
_pipe@5,
{decode_error,
[{decode_error,
<<"A base64 encoded bit array"/utf8>>,
Key,
Location}]}
);
_ ->
{error,
{decode_error,
[{decode_error,
<<"Unsupported key type"/utf8>>,
Key,
Location}]}}
end
end
).
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 441).
-spec query_decode_error_to_decode_errors(query_decode_error()) -> list(gleam@dynamic:decode_error()).
query_decode_error_to_decode_errors(Err) ->
case Err of
{key_not_found, Key} ->
[{decode_error,
<<"Key not found"/utf8>>,
<<""/utf8>>,
gleam@string:split(Key, <<"."/utf8>>)}];
{decode_error, Errors} ->
Errors
end.
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 188).
-spec decode_sub_value(convert:glitr_type(), list(binary())) -> fun((list({binary(),
binary()})) -> {ok, convert:glitr_value()} | {error, query_decode_error()}).
decode_sub_value(Of, Location) ->
case Of of
bit_array ->
fun(_capture) -> decode_bit_array(_capture, Location) end;
bool ->
fun(_capture@1) -> decode_bool(_capture@1, Location) end;
{dict, K, V} ->
fun(_capture@2) -> decode_dict(_capture@2, K, V, Location) end;
dynamic ->
fun(_) -> {ok, {dynamic_value, gleam_stdlib:identity(nil)}} end;
{enum, Variants} ->
fun(_capture@3) -> decode_enum(_capture@3, Variants, Location) end;
float ->
fun(V@1) -> decode_float(V@1, Location) end;
int ->
fun(V@2) -> decode_int(V@2, Location) end;
{list, Els} ->
fun(_capture@4) -> decode_list(_capture@4, Els, Location, 0, []) end;
null ->
fun(_) -> {ok, null_value} end;
{object, Fields} ->
fun(_capture@5) -> decode_object(_capture@5, Fields, Location) end;
{optional, _} ->
fun(_capture@6) -> decode_optional(_capture@6, Of, Location) end;
{result, Ok, Err} ->
fun(_capture@7) -> decode_result(_capture@7, Ok, Err, Location) end;
string ->
fun(_capture@8) -> decode_string(_capture@8, Location) end
end.
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 326).
-spec decode_dict(
list({binary(), binary()}),
convert:glitr_type(),
convert:glitr_type(),
list(binary())
) -> {ok, convert:glitr_value()} | {error, query_decode_error()}.
decode_dict(Query, Key_type, Value_type, Location) ->
Res = begin
_pipe@1 = gleam@list:filter(
Query,
fun(Query_el) -> _pipe = erlang:element(1, Query_el),
gleam_stdlib:string_starts_with(
_pipe,
gleam@string:join(Location, <<"."/utf8>>)
) end
),
_pipe@2 = gleam@list:map(
_pipe@1,
fun(Query_el@1) ->
Key = decode_dict_key(
erlang:element(1, Query_el@1),
Key_type,
Location
),
Value = (decode_sub_value(
Value_type,
gleam@string:split(
erlang:element(1, Query_el@1),
<<"."/utf8>>
)
))(Query),
case {Key, Value} of
{{ok, K}, {ok, V}} ->
{ok, {K, V}};
{{error, {decode_error, Error}}, _} ->
{error, {decode_error, Error}};
{_, {error, {decode_error, Error}}} ->
{error, {decode_error, Error}};
{{error, {key_not_found, _}}, _} ->
{error, {key_not_found, erlang:element(1, Query_el@1)}};
{_, {error, {key_not_found, _}}} ->
{error, {key_not_found, erlang:element(1, Query_el@1)}}
end
end
),
gleam@result:partition(_pipe@2)
end,
{ok,
{dict_value,
begin
_pipe@3 = erlang:element(1, Res),
maps:from_list(_pipe@3)
end}}.
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 279).
-spec decode_list(
list({binary(), binary()}),
convert:glitr_type(),
list(binary()),
integer(),
list(convert:glitr_value())
) -> {ok, convert:glitr_value()} | {error, query_decode_error()}.
decode_list(Query, Of, Location, Index, Elements) ->
case (decode_sub_value(
Of,
lists:append(Location, [erlang:integer_to_binary(Index)])
))(Query) of
{error, {key_not_found, _}} ->
{ok, {list_value, lists:reverse(Elements)}};
{error, {decode_error, _}} = Err ->
Err;
{ok, V} ->
decode_list(Query, Of, Location, Index + 1, [V | Elements])
end.
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 295).
-spec decode_result(
list({binary(), binary()}),
convert:glitr_type(),
convert:glitr_type(),
list(binary())
) -> {ok, convert:glitr_value()} | {error, query_decode_error()}.
decode_result(Query, Ok_type, Error_type, Location) ->
case {(decode_sub_value(Ok_type, lists:append(Location, [<<"ok"/utf8>>])))(
Query
),
(decode_sub_value(
Error_type,
lists:append(Location, [<<"error"/utf8>>])
))(Query)} of
{{ok, Ok}, _} ->
{ok, {result_value, {ok, Ok}}};
{_, {ok, Error}} ->
{ok, {result_value, {error, Error}}};
{{error, {decode_error, Error@1}}, _} ->
{error, {decode_error, Error@1}};
{_, {error, {decode_error, Error@1}}} ->
{error, {decode_error, Error@1}};
{{error, {key_not_found, _}}, {error, {key_not_found, _}}} ->
{error,
{key_not_found,
<<(gleam@string:join(Location, <<"."/utf8>>))/binary,
".ok"/utf8>>}}
end.
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 314).
-spec decode_optional(
list({binary(), binary()}),
convert:glitr_type(),
list(binary())
) -> {ok, convert:glitr_value()} | {error, query_decode_error()}.
decode_optional(Query, Of, Location) ->
case (decode_sub_value(Of, Location))(Query) of
{ok, V} ->
{ok, {optional_value, {some, V}}};
{error, {key_not_found, _}} ->
{ok, {optional_value, none}};
{error, {decode_error, _}} = Err ->
Err
end.
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 413).
-spec decode_object(
list({binary(), binary()}),
list({binary(), convert:glitr_type()}),
list(binary())
) -> {ok, convert:glitr_value()} | {error, query_decode_error()}.
decode_object(Query, Fields, Location) ->
_pipe = (gleam@list:try_map(
Fields,
fun(F) ->
gleam@result:map(
(decode_sub_value(
erlang:element(2, F),
lists:append(Location, [erlang:element(1, F)])
))(Query),
fun(V) -> {erlang:element(1, F), V} end
)
end
)),
gleam@result:map(_pipe, fun(Field@0) -> {object_value, Field@0} end).
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 428).
-spec decode_enum(
list({binary(), binary()}),
list({binary(), convert:glitr_type()}),
list(binary())
) -> {ok, convert:glitr_value()} | {error, query_decode_error()}.
decode_enum(Query, Variants, Location) ->
_pipe = gleam@list:find_map(
Variants,
fun(Variant) ->
(decode_sub_value(
erlang:element(2, Variant),
lists:append(Location, [erlang:element(1, Variant)])
))(Query)
end
),
gleam@result:replace_error(
_pipe,
{decode_error,
[{decode_error,
<<"One of the enum variants"/utf8>>,
<<""/utf8>>,
Location}]}
).
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 161).
-spec decode_value(convert:glitr_type()) -> fun((list({binary(), binary()})) -> {ok,
convert:glitr_value()} |
{error, list(gleam@dynamic:decode_error())}).
decode_value(Of) ->
Decode_fn = case Of of
bit_array ->
fun(_capture) ->
decode_bit_array(_capture, [<<"bit_array"/utf8>>])
end;
bool ->
fun(_capture@1) -> decode_bool(_capture@1, [<<"bool"/utf8>>]) end;
{dict, K, V} ->
fun(_capture@2) ->
decode_dict(_capture@2, K, V, [<<"dict"/utf8>>])
end;
dynamic ->
fun(Query) ->
{ok, {dynamic_value, gleam_stdlib:identity(Query)}}
end;
{enum, Variants} ->
fun(_capture@3) -> decode_enum(_capture@3, Variants, []) end;
float ->
fun(_capture@4) -> decode_float(_capture@4, [<<"float"/utf8>>]) end;
int ->
fun(_capture@5) -> decode_int(_capture@5, [<<"int"/utf8>>]) end;
{list, Els} ->
fun(_capture@6) ->
decode_list(_capture@6, Els, [<<"list"/utf8>>], 0, [])
end;
null ->
fun(_) -> {ok, null_value} end;
{object, Fields} ->
fun(_capture@7) -> decode_object(_capture@7, Fields, []) end;
{optional, El} ->
fun(_capture@8) ->
decode_optional(_capture@8, El, [<<"optional"/utf8>>])
end;
{result, Ok, Err} ->
fun(_capture@9) ->
decode_result(_capture@9, Ok, Err, [<<"result"/utf8>>])
end;
string ->
fun(_capture@10) ->
decode_string(_capture@10, [<<"string"/utf8>>])
end
end,
fun(Query@1) -> _pipe = Decode_fn(Query@1),
gleam@result:map_error(_pipe, fun query_decode_error_to_decode_errors/1) end.
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 153).
-spec decode(list({binary(), binary()}), convert:converter(FXJ)) -> {ok, FXJ} |
{error, list(gleam@dynamic:decode_error())}.
decode(Query, Converter) ->
_pipe@1 = (decode_value(
begin
_pipe = Converter,
convert:type_def(_pipe)
end
))(Query),
gleam@result:then(_pipe@1, convert:decode(Converter)).
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 128).
-spec encode_sub_value(convert:glitr_value(), list(binary())) -> list({binary(),
binary()}).
encode_sub_value(Val, Path) ->
Prefix = gleam@string:join(Path, <<"."/utf8>>),
case Val of
{bool_value, V} ->
[{Prefix, gleam@bool:to_string(V)}];
{dict_value, V@1} ->
encode_dict(V@1, Path);
{enum_value, Variant, V@2} ->
encode_enum(Variant, V@2, Path);
{float_value, V@3} ->
[{Prefix, gleam_stdlib:float_to_string(V@3)}];
{int_value, V@4} ->
[{Prefix, erlang:integer_to_binary(V@4)}];
{list_value, V@5} ->
encode_list(V@5, Path);
null_value ->
[];
{object_value, V@6} ->
encode_object(V@6, Path);
{optional_value, V@7} ->
encode_optional(V@7, Path);
{result_value, V@8} ->
encode_result(V@8, Path);
{string_value, V@9} ->
[{Prefix, V@9}];
{bit_array_value, V@10} ->
[{Prefix, gleam@bit_array:base64_url_encode(V@10, true)}];
{dynamic_value, _} ->
[]
end.
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 56).
-spec encode_dict(
gleam@dict:dict(convert:glitr_value(), convert:glitr_value()),
list(binary())
) -> list({binary(), binary()}).
encode_dict(Val, Path) ->
Result_partition = begin
_pipe = Val,
_pipe@1 = maps:to_list(_pipe),
_pipe@2 = gleam@list:map(
_pipe@1,
fun(Kv) -> case encode_dict_key(erlang:element(1, Kv)) of
<<""/utf8>> ->
{error, nil};
Key ->
{ok,
encode_sub_value(
erlang:element(2, Kv),
lists:append(Path, [Key])
)}
end end
),
gleam@result:partition(_pipe@2)
end,
_pipe@3 = erlang:element(1, Result_partition),
_pipe@4 = lists:reverse(_pipe@3),
gleam@list:flatten(_pipe@4).
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 46).
-spec encode_object(list({binary(), convert:glitr_value()}), list(binary())) -> list({binary(),
binary()}).
encode_object(Val, Path) ->
_pipe = Val,
gleam@list:flat_map(
_pipe,
fun(Value) ->
encode_sub_value(
erlang:element(2, Value),
lists:append(Path, [erlang:element(1, Value)])
)
end
).
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 86).
-spec encode_list(list(convert:glitr_value()), list(binary())) -> list({binary(),
binary()}).
encode_list(Val, Path) ->
_pipe = Val,
_pipe@1 = gleam@list:index_fold(
_pipe,
[],
fun(Acc, Value, Index) ->
gleam@list:flatten(
[encode_sub_value(
Value,
lists:append(Path, [erlang:integer_to_binary(Index)])
),
Acc]
)
end
),
lists:reverse(_pipe@1).
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 100).
-spec encode_result(
{ok, convert:glitr_value()} | {error, convert:glitr_value()},
list(binary())
) -> list({binary(), binary()}).
encode_result(Val, Path) ->
case Val of
{ok, V} ->
encode_sub_value(V, lists:append(Path, [<<"ok"/utf8>>]));
{error, V@1} ->
encode_sub_value(V@1, lists:append(Path, [<<"error"/utf8>>]))
end.
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 110).
-spec encode_optional(
gleam@option:option(convert:glitr_value()),
list(binary())
) -> list({binary(), binary()}).
encode_optional(Val, Path) ->
case Val of
none ->
[];
{some, V} ->
encode_sub_value(V, Path)
end.
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 120).
-spec encode_enum(binary(), convert:glitr_value(), list(binary())) -> list({binary(),
binary()}).
encode_enum(Variant, V, Path) ->
encode_sub_value(V, lists:append(Path, [Variant])).
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 25).
-spec encode_value(convert:glitr_value()) -> list({binary(), binary()}).
encode_value(Val) ->
case Val of
{bool_value, V} ->
[{<<"bool"/utf8>>, gleam@bool:to_string(V)}];
{dict_value, V@1} ->
encode_dict(V@1, [<<"dict"/utf8>>]);
{enum_value, Variant, V@2} ->
encode_enum(Variant, V@2, []);
{float_value, V@3} ->
[{<<"float"/utf8>>, gleam_stdlib:float_to_string(V@3)}];
{int_value, V@4} ->
[{<<"int"/utf8>>, erlang:integer_to_binary(V@4)}];
{list_value, V@5} ->
encode_list(V@5, [<<"list"/utf8>>]);
null_value ->
[];
{object_value, V@6} ->
encode_object(V@6, []);
{optional_value, V@7} ->
encode_optional(V@7, [<<"optional"/utf8>>]);
{result_value, V@8} ->
encode_result(V@8, [<<"result"/utf8>>]);
{string_value, V@9} ->
[{<<"string"/utf8>>, V@9}];
{bit_array_value, V@10} ->
[{<<"bit_array"/utf8>>,
gleam@bit_array:base64_url_encode(V@10, true)}];
{dynamic_value, _} ->
[]
end.
-file("/home/runner/work/convert_http_query/convert_http_query/src/convert/http/query.gleam", 18).
-spec encode(FWJ, convert:converter(FWJ)) -> list({binary(), binary()}).
encode(Value, Converter) ->
_pipe = Value,
_pipe@1 = (convert:encode(Converter))(_pipe),
encode_value(_pipe@1).