-module(convert@http@query). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -define(FILEPATH, "src/convert/http/query.gleam"). -export([decode/2, decode_value/1, 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:decode_error())}. -file("src/convert/http/query.gleam", 73). -spec encode_dict_key(convert:glitr_value()) -> {ok, binary()} | {error, nil}. encode_dict_key(Key) -> case Key of {bool_value, V} -> _pipe = gleam@bool:to_string(V), {ok, _pipe}; {float_value, V@1} -> _pipe@1 = gleam_stdlib:float_to_string(V@1), {ok, _pipe@1}; {int_value, V@2} -> _pipe@2 = erlang:integer_to_binary(V@2), {ok, _pipe@2}; {string_value, V@3} -> {ok, V@3}; {bit_array_value, V@4} -> _pipe@3 = gleam@bit_array:base64_url_encode(V@4, true), {ok, _pipe@3}; _ -> {error, nil} end. -file("src/convert/http/query.gleam", 208). -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("src/convert/http/query.gleam", 270). -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("src/convert/http/query.gleam", 218). -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("src/convert/http/query.gleam", 148). -spec decode(list({binary(), binary()}), convert:converter(ENU)) -> {ok, ENU} | {error, list(gleam@dynamic@decode: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("src/convert/http/query.gleam", 156). -spec decode_value(convert:glitr_type()) -> fun((list({binary(), binary()})) -> {ok, convert:glitr_value()} | {error, list(gleam@dynamic@decode: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) -> _pipe = Query, _pipe@1 = gleam@list:map( _pipe, fun(El) -> {gleam_stdlib:identity(erlang:element(1, El)), gleam_stdlib:identity(erlang:element(2, El))} end ), _pipe@2 = gleam@dynamic:properties(_pipe@1), _pipe@3 = {dynamic_value, _pipe@2}, {ok, _pipe@3} 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@1} -> fun(_capture@8) -> decode_optional(_capture@8, El@1, [<<"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@4 = Decode_fn(Query@1), gleam@result:map_error( _pipe@4, fun query_decode_error_to_decode_errors/1 ) end. -file("src/convert/http/query.gleam", 230). -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("src/convert/http/query.gleam", 242). -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("src/convert/http/query.gleam", 256). -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("src/convert/http/query.gleam", 354). -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("src/convert/http/query.gleam", 440). -spec query_decode_error_to_decode_errors(query_decode_error()) -> list(gleam@dynamic@decode: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("src/convert/http/query.gleam", 278). -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("src/convert/http/query.gleam", 187). -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@dynamic: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("src/convert/http/query.gleam", 294). -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("src/convert/http/query.gleam", 313). -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("src/convert/http/query.gleam", 325). -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("src/convert/http/query.gleam", 412). -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 = begin 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 ) end, gleam@result:map(_pipe, fun(Field@0) -> {object_value, Field@0} end). -file("src/convert/http/query.gleam", 427). -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("src/convert/http/query.gleam", 123). -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("src/convert/http/query.gleam", 57). -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@3 = gleam@list:map( _pipe@1, fun(Kv) -> _pipe@2 = encode_dict_key(erlang:element(1, Kv)), gleam@result:map( _pipe@2, fun(Key) -> encode_sub_value( erlang:element(2, Kv), lists:append(Path, [Key]) ) end ) end ), gleam@result:partition(_pipe@3) end, _pipe@4 = erlang:element(1, Result_partition), _pipe@5 = lists:reverse(_pipe@4), gleam@list:flatten(_pipe@5). -file("src/convert/http/query.gleam", 47). -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("src/convert/http/query.gleam", 84). -spec encode_list(list(convert:glitr_value()), list(binary())) -> list({binary(), binary()}). encode_list(Val, Path) -> _pipe = Val, _pipe@1 = gleam@list:index_map( _pipe, fun(Value, Index) -> encode_sub_value( Value, lists:append(Path, [erlang:integer_to_binary(Index)]) ) end ), gleam@list:flatten(_pipe@1). -file("src/convert/http/query.gleam", 95). -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("src/convert/http/query.gleam", 105). -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("src/convert/http/query.gleam", 115). -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("src/convert/http/query.gleam", 26). -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("src/convert/http/query.gleam", 19). -spec encode(EMS, convert:converter(EMS)) -> list({binary(), binary()}). encode(Value, Converter) -> _pipe = Value, _pipe@1 = (convert:encode(Converter))(_pipe), encode_value(_pipe@1).