Current section

Files

Jump to
oas_generator src oas@generator.erl
Raw

src/oas@generator.erl

-module(oas@generator).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oas/generator.gleam").
-export([run_single_location/2, gen_operations_and_top_files/3, gen_schema_file/1, json_decode_error_to_string/1, build/4]).
-file("src/oas/generator.gleam", 24).
-spec concat_path(list(oas:segment()), binary()) -> glance:expression().
concat_path(Match, Root) ->
{Str, Rev} = gleam@list:fold(
Match,
{Root, []},
fun(Acc, Segment) ->
{Current, Prev} = Acc,
case Segment of
{fixed_segment, S} ->
{<<<<Current/binary, "/"/utf8>>/binary, S/binary>>, Prev};
{match_segment, Name, Schema} ->
case Schema of
{string, _, _, _, _, _, _, _, _} ->
Prev@1 = [{string, <<Current/binary, "/"/utf8>>} |
Prev],
Prev@2 = [{variable,
oas@generator@ast:name_for_gleam_field_or_var(
Name
)} |
Prev@1],
{<<""/utf8>>, Prev@2};
{integer, _, _, _, _, _, _, _, _, _} ->
Prev@3 = [{string, <<Current/binary, "/"/utf8>>} |
Prev],
Prev@4 = [oas@generator@ast:call1(
<<"int"/utf8>>,
<<"to_string"/utf8>>,
{variable,
oas@generator@ast:name_for_gleam_field_or_var(
Name
)}
) |
Prev@3],
{<<""/utf8>>, Prev@4};
_ ->
Prev@5 = [{string, <<Current/binary, "/"/utf8>>} |
Prev],
Prev@6 = [{variable,
oas@generator@ast:name_for_gleam_field_or_var(
Name
)} |
Prev@5],
{<<""/utf8>>, Prev@6}
end
end
end
),
Parts = case Str of
<<""/utf8>> ->
Rev;
_ ->
[{string, Str} | Rev]
end,
A@1 = case gleam@list:reduce(
Parts,
fun(Post, Pre) -> {binary_operator, concatenate, Pre, Post} end
) of
{ok, A} -> A;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"oas/generator"/utf8>>,
function => <<"concat_path"/utf8>>,
line => 68,
value => _assert_fail,
start => 1811,
'end' => 1934,
pattern_start => 1822,
pattern_end => 1827})
end,
A@1.
-file("src/oas/generator.gleam", 75).
-spec noop1(binary()) -> glance:expression().
noop1(Message) ->
{fn,
[{fn_parameter, {discarded, <<""/utf8>>}, none}],
none,
[{expression, {panic, {some, {string, Message}}}}]}.
-file("src/oas/generator.gleam", 81).
-spec query_to_parts(
list({binary(), boolean(), oas@json_schema:ref(oas@json_schema:schema())}),
oas:components()
) -> {list(glance:function_parameter()), list(glance:expression())}.
query_to_parts(Parameters, Components) ->
_pipe = gleam@list:map(
Parameters,
fun(P) ->
{Key, Required, Schema} = P,
Schema@1 = oas:fetch_schema(Schema, erlang:element(2, Components)),
Key@1 = oas@generator@ast:name_for_gleam_field_or_var(Key),
Arg = {function_parameter, {some, Key@1}, {named, Key@1}, none},
Var = {variable, Key@1},
Mapper = case Schema@1 of
{boolean, _, _, _, _} ->
{some,
oas@generator@ast:access(
<<"bool"/utf8>>,
<<"to_string"/utf8>>
)};
{integer, _, _, _, _, _, _, _, _, _} ->
{some,
oas@generator@ast:access(
<<"int"/utf8>>,
<<"to_string"/utf8>>
)};
{number, _, _, _, _, _, _, _, _, _} ->
{some,
oas@generator@ast:access(
<<"float"/utf8>>,
<<"to_string"/utf8>>
)};
{string, _, _, _, _, _, _, _, _} ->
none;
_ ->
{some, noop1(<<"query parameter is not supported"/utf8>>)}
end,
Value = case Required of
true ->
oas@generator@ast:call1(
<<"option"/utf8>>,
<<"Some"/utf8>>,
case Mapper of
{some, M} ->
{call, M, [{unlabelled_field, Var}]};
none ->
Var
end
);
false ->
case Mapper of
{some, Mapper@1} ->
oas@generator@ast:call2(
<<"option"/utf8>>,
<<"map"/utf8>>,
Var,
Mapper@1
);
none ->
Var
end
end,
Tuple = {tuple, [{string, Key@1}, Value]},
{Arg, Tuple}
end
),
gleam@list:unzip(_pipe).
-file("src/oas/generator.gleam", 114).
-spec gen_method(gleam@http:method()) -> glance:expression().
gen_method(Method) ->
case Method of
get ->
oas@generator@ast:access(<<"http"/utf8>>, <<"Get"/utf8>>);
post ->
oas@generator@ast:access(<<"http"/utf8>>, <<"Post"/utf8>>);
head ->
oas@generator@ast:access(<<"http"/utf8>>, <<"Head"/utf8>>);
put ->
oas@generator@ast:access(<<"http"/utf8>>, <<"Put"/utf8>>);
delete ->
oas@generator@ast:access(<<"http"/utf8>>, <<"Delete"/utf8>>);
trace ->
oas@generator@ast:access(<<"http"/utf8>>, <<"Trace"/utf8>>);
connect ->
oas@generator@ast:access(<<"http"/utf8>>, <<"Connect"/utf8>>);
options ->
oas@generator@ast:access(<<"http"/utf8>>, <<"Options"/utf8>>);
patch ->
oas@generator@ast:access(<<"http"/utf8>>, <<"Patch"/utf8>>);
{other, Other} ->
oas@generator@ast:call1(
<<"http"/utf8>>,
<<"Other"/utf8>>,
{string, Other}
)
end.
-file("src/oas/generator.gleam", 129).
-spec let_(binary(), glance:expression()) -> glance:statement().
let_(Var, Value) ->
{assignment, 'let', {pattern_variable, Var}, none, Value}.
-file("src/oas/generator.gleam", 133).
-spec path_args(list(oas:segment())) -> list(glance:function_parameter()).
path_args(Match) ->
gleam@list:filter_map(Match, fun(Segment) -> case Segment of
{fixed_segment, _} ->
{error, nil};
{match_segment, Name, _} ->
{ok,
{function_parameter,
none,
{named,
oas@generator@ast:name_for_gleam_field_or_var(
Name
)},
none}}
end end).
-file("src/oas/generator.gleam", 147).
-spec get_structure_json_media(gleam@dict:dict(binary(), oas:media_type())) -> {list({gleam@option:option(binary()),
oas:media_type()}),
list({binary(), oas:media_type()})}.
get_structure_json_media(Content) ->
gleam@dict:fold(
Content,
{[], []},
fun(Acc, Key, Value) ->
{Known, Unknown} = Acc,
case Key of
<<"application/json"/utf8>> ->
{[{none, Value} | Known], Unknown};
<<"application/"/utf8, Rest/binary>> ->
case gleam@string:split_once(Rest, <<"+json"/utf8>>) of
{ok, {Pre, <<""/utf8>>}} ->
{[{{some, Pre}, Value} | Known], Unknown};
_ ->
{Known, [{Key, Value} | Unknown]}
end;
_ ->
{Known, [{Key, Value} | Unknown]}
end
end
).
-file("src/oas/generator.gleam", 162).
-spec gen_request_for_op(
{gleam@http:method(), oas:operation()},
binary(),
list(oas@json_schema:ref(oas:parameter())),
oas:components(),
list(oas@generator@lift:fields())
) -> oas@generator@lookup:lookup({glance:function_(),
glance:function_(),
gleam@option:option(glance:custom_type()),
list(oas@generator@lift:fields())}).
gen_request_for_op(Op_entry, Pattern, Path_parameters, Components, Internal) ->
{Method, Op} = Op_entry,
{operation, _, _, _, Operation_id, Op_parameters, _, _} = Op,
Parameters = lists:append(Op_parameters, Path_parameters),
Parameters@1 = gleam@list:map(
Parameters,
fun(_capture) ->
oas:fetch_parameter(_capture, erlang:element(4, Components))
end
),
{Q_args, Q_params} = query_to_parts(
oas:query_parameters(Parameters@1),
Components
),
Match@1 = case oas:gather_match(Pattern, Parameters@1, Components) of
{ok, Match} ->
Match;
{error, _} ->
erlang:error(#{gleam_error => panic,
message => <<"could not find all parameters in match"/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"oas/generator"/utf8>>,
function => <<"gen_request_for_op"/utf8>>,
line => 182})
end,
oas@generator@lookup:then(case erlang:element(7, Op) of
{some, Body} ->
{request_body, _, Content, _} = oas:fetch_request_body(
Body,
erlang:element(5, Components)
),
{Known, Unknown} = get_structure_json_media(Content),
case {Known, Unknown} of
{[{_, {media_type, {some, Schema}}}], _} ->
{Lifted, _, Internal@1} = oas@generator@lift:do_lift(
Schema,
Internal
),
oas@generator@lookup:then(case Lifted of
{named, N} ->
oas@generator@lookup:then(
oas@generator@schema:to_encoder(
{named, N}
),
fun(Encoder) ->
_pipe = {none,
<<"data"/utf8>>,
oas@generator@ast:call(
Encoder,
{variable, <<"data"/utf8>>}
)},
{done, _pipe}
end
);
{primitive, P} ->
oas@generator@lookup:then(
oas@generator@schema:to_encoder(
{primitive, P}
),
fun(Encoder@1) ->
_pipe@1 = {none,
<<"data"/utf8>>,
oas@generator@ast:call(
Encoder@1,
{variable, <<"data"/utf8>>}
)},
{done, _pipe@1}
end
);
{array, I} ->
oas@generator@lookup:then(
oas@generator@schema:to_encoder(
{array, I}
),
fun(Encoder@2) ->
_pipe@2 = {none,
<<"data"/utf8>>,
oas@generator@ast:call(
Encoder@2,
{variable, <<"data"/utf8>>}
)},
{done, _pipe@2}
end
);
{tuple, Es} ->
oas@generator@lookup:then(
oas@generator@schema:to_encoder(
{tuple, Es}
),
fun(Encoder@3) ->
_pipe@3 = {none,
<<"data"/utf8>>,
oas@generator@ast:call(
Encoder@3,
{variable, <<"data"/utf8>>}
)},
{done, _pipe@3}
end
);
{compound,
{fields, Named, Additional, Required}} ->
Name = oas@generator@ast:name_for_gleam_type(
<<Operation_id/binary, "-request"/utf8>>
),
oas@generator@lookup:then(
oas@generator@schema:custom_type(
Name,
Named,
Additional,
Required
),
fun(Type_) ->
oas@generator@lookup:then(
oas@generator@schema:fields_to_encode_body(
Named,
Required,
Additional
),
fun(Body@1) ->
_pipe@4 = {{some, Type_},
<<"data"/utf8>>,
Body@1},
{done, _pipe@4}
end
)
end
);
{dictionary, F} ->
oas@generator@lookup:then(
oas@generator@schema:to_encoder(
{dictionary, F}
),
fun(Encoder@4) ->
_pipe@5 = {none,
<<"data"/utf8>>,
oas@generator@ast:call(
Encoder@4,
{variable, <<"data"/utf8>>}
)},
{done, _pipe@5}
end
);
unsupported ->
oas@generator@lookup:then(
oas@generator@schema:to_encoder(
unsupported
),
fun(Encoder@5) ->
_pipe@6 = {none,
<<"data"/utf8>>,
oas@generator@ast:call(
Encoder@5,
{variable, <<"data"/utf8>>}
)},
{done, _pipe@6}
end
)
end, fun(_use0) ->
{Custom_type, Arg, Json} = _use0,
_pipe@7 = {Custom_type,
{some,
{Arg,
oas@generator@ast:call1(
<<"utils"/utf8>>,
<<"json_to_bits"/utf8>>,
Json
)}},
Internal@1},
{done, _pipe@7}
end);
{[], []} ->
_pipe@8 = {none, none, Internal},
{done, _pipe@8};
{[], [{Unknown@1, _}]} ->
gleam_stdlib:println(
<<"unknown content type: "/utf8, Unknown@1/binary>>
),
_pipe@9 = {none, none, Internal},
{done, _pipe@9};
{_, _} ->
gleam_stdlib:println(
<<"multiple content types not supported"/utf8>>
),
_pipe@10 = {none, none, Internal},
{done, _pipe@10}
end;
none ->
_pipe@11 = {none, none, Internal},
{done, _pipe@11}
end, fun(_use0@1) ->
{Custom_type@1, Body@2, Internal@2} = _use0@1,
Parameters_for_operation_functions = begin
_pipe@12 = path_args(Match@1),
_pipe@13 = lists:append(_pipe@12, case Body@2 of
{some, {Arg@1, _}} ->
[{function_parameter,
none,
{named, Arg@1},
case Custom_type@1 of
{some,
{custom_type, Name@1, _, _, _, _}} ->
{some,
{named_type, Name@1, none, []}};
none ->
none
end}];
none ->
[]
end),
lists:append(_pipe@13, Q_args)
end,
Parameters_for_top_functions = begin
_pipe@14 = path_args(Match@1),
_pipe@15 = lists:append(_pipe@14, case Body@2 of
{some, {Arg@2, _}} ->
[{function_parameter, none, {named, Arg@2}, none}];
none ->
[]
end),
lists:append(_pipe@15, Q_args)
end,
Fn_name = oas@generator@ast:name_for_gleam_field_or_var(
Operation_id
),
Op_request = <<Fn_name/binary, "_request"/utf8>>,
Op_response = <<Fn_name/binary, "_response"/utf8>>,
Req_fn = {function,
Op_request,
public,
[{function_parameter, none, {named, <<"base"/utf8>>}, none} |
Parameters_for_operation_functions],
none,
[let_(<<"method"/utf8>>, gen_method(Method)),
let_(<<"path"/utf8>>, concat_path(Match@1, <<""/utf8>>)),
let_(<<"query"/utf8>>, {list, Q_params, none}) |
case Body@2 of
{some, {_, Encode}} ->
[let_(<<"body"/utf8>>, Encode),
{expression,
oas@generator@ast:pipe(
{variable, <<"base"/utf8>>},
oas@generator@ast:pipe(
oas@generator@ast:call1(
<<"utils"/utf8>>,
<<"set_method"/utf8>>,
{variable, <<"method"/utf8>>}
),
oas@generator@ast:pipe(
oas@generator@ast:call1(
<<"utils"/utf8>>,
<<"append_path"/utf8>>,
{variable, <<"path"/utf8>>}
),
oas@generator@ast:pipe(
oas@generator@ast:call1(
<<"utils"/utf8>>,
<<"set_query"/utf8>>,
{variable,
<<"query"/utf8>>}
),
oas@generator@ast:call2(
<<"utils"/utf8>>,
<<"set_body"/utf8>>,
{string,
<<"application/json"/utf8>>},
{variable,
<<"body"/utf8>>}
)
)
)
)
)}];
none ->
[{expression,
oas@generator@ast:pipe(
{variable, <<"base"/utf8>>},
oas@generator@ast:pipe(
oas@generator@ast:call1(
<<"utils"/utf8>>,
<<"set_method"/utf8>>,
{variable, <<"method"/utf8>>}
),
oas@generator@ast:pipe(
oas@generator@ast:call1(
<<"utils"/utf8>>,
<<"append_path"/utf8>>,
{variable, <<"path"/utf8>>}
),
oas@generator@ast:call1(
<<"utils"/utf8>>,
<<"set_query"/utf8>>,
{variable, <<"query"/utf8>>}
)
)
)
)}]
end],
{span, 0, 0}},
Fn_ = {function,
Fn_name,
public,
[{function_parameter, none, {named, <<"token"/utf8>>}, none} |
Parameters_for_top_functions],
none,
[let_(
<<"request"/utf8>>,
{call,
{variable, <<"base_request"/utf8>>},
[{unlabelled_field, {variable, <<"token"/utf8>>}}]}
),
let_(
<<"request"/utf8>>,
{call,
oas@generator@ast:access(
<<"operations"/utf8>>,
Op_request
),
[{unlabelled_field, {variable, <<"request"/utf8>>}} |
gleam@list:map(
Parameters_for_top_functions,
fun(P@1) ->
Name@3 = case P@1 of
{function_parameter,
_,
{named, Name@2},
_} -> Name@2;
_assert_fail ->
erlang:error(
#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"oas/generator"/utf8>>,
function => <<"gen_request_for_op"/utf8>>,
line => 363,
value => _assert_fail,
start => 11608,
'end' => 11693,
pattern_start => 11619,
pattern_end => 11673}
)
end,
{unlabelled_field, {variable, Name@3}}
end
)]}
),
{use,
[{pattern_variable, <<"response"/utf8>>}],
oas@generator@ast:call1(
<<"t"/utf8>>,
<<"do"/utf8>>,
oas@generator@ast:call1(
<<"t"/utf8>>,
<<"fetch"/utf8>>,
{variable, <<"request"/utf8>>}
)
)},
{use,
[{pattern_variable, <<"data"/utf8>>}],
oas@generator@ast:call1(
<<"t"/utf8>>,
<<"try"/utf8>>,
{call,
{variable, <<"handle_errors"/utf8>>},
[{unlabelled_field,
oas@generator@ast:call1(
<<"operations"/utf8>>,
Op_response,
{variable, <<"response"/utf8>>}
)}]}
)},
{expression,
oas@generator@ast:call1(
<<"t"/utf8>>,
<<"Done"/utf8>>,
{variable, <<"data"/utf8>>}
)}],
{span, 0, 0}},
_pipe@16 = {Fn_, Req_fn, Custom_type@1, Internal@2},
{done, _pipe@16}
end).
-file("src/oas/generator.gleam", 464).
-spec just_return_ok_nil(binary()) -> {glance:expression(),
gleam@option:option(any()),
boolean()}.
just_return_ok_nil(Wrapper) ->
Thing = begin
_pipe = {call,
{variable, Wrapper},
[{unlabelled_field, {variable, <<"Nil"/utf8>>}}]},
oas@generator@ast:pipe(_pipe, {variable, <<"Ok"/utf8>>})
end,
{Thing, none, false}.
-file("src/oas/generator.gleam", 398).
-spec gen_content_handling(
binary(),
gleam@dict:dict(binary(), oas:media_type()),
binary(),
list(oas@generator@lift:fields())
) -> oas@generator@lookup:lookup({{glance:expression(),
gleam@option:option(glance:custom_type()),
boolean()},
list(oas@generator@lift:fields())}).
gen_content_handling(Operation_id, Content, Wrapper, Internal) ->
{Known, Unknown} = get_structure_json_media(Content),
case {Known, Unknown} of
{[{_, {media_type, {some, Schema}}}], _} ->
{Lifted, _, Internal@1} = oas@generator@lift:do_lift(
Schema,
Internal
),
oas@generator@lookup:then(case Lifted of
{named, N} ->
oas@generator@lookup:then(
oas@generator@schema:to_decoder({named, N}),
fun(Decoder) -> _pipe = {Decoder, none},
{done, _pipe} end
);
{primitive, P} ->
oas@generator@lookup:then(
oas@generator@schema:to_decoder({primitive, P}),
fun(Decoder@1) -> _pipe@1 = {Decoder@1, none},
{done, _pipe@1} end
);
{array, A} ->
oas@generator@lookup:then(
oas@generator@schema:to_decoder({array, A}),
fun(Decoder@2) -> _pipe@2 = {Decoder@2, none},
{done, _pipe@2} end
);
{tuple, Of} ->
oas@generator@lookup:then(
oas@generator@schema:to_decoder({tuple, Of}),
fun(Decoder@3) -> _pipe@3 = {Decoder@3, none},
{done, _pipe@3} end
);
{compound, {fields, Parameters, Additional, Required}} = Top ->
Name = <<Operation_id/binary, "_response"/utf8>>,
oas@generator@lookup:then(
oas@generator@schema:custom_type(
Name,
Parameters,
Additional,
Required
),
fun(Type_) ->
oas@generator@lookup:then(
oas@generator@schema:gen_top_decoder_needs_name(
Name,
Top
),
fun(Decoder@4) ->
Decoder@5 = {block, Decoder@4},
_pipe@4 = {Decoder@5, {some, Type_}},
{done, _pipe@4}
end
)
end
);
{dictionary, Values} ->
oas@generator@lookup:then(
oas@generator@schema:to_decoder(
{dictionary, Values}
),
fun(Decoder@6) -> _pipe@5 = {Decoder@6, none},
{done, _pipe@5} end
);
unsupported ->
oas@generator@lookup:then(
oas@generator@schema:to_decoder(unsupported),
fun(Decoder@7) -> _pipe@6 = {Decoder@7, none},
{done, _pipe@6} end
)
end, fun(_use0) ->
{Decoder@8, Resp_type} = _use0,
Action = begin
_pipe@7 = oas@generator@ast:call2(
<<"json"/utf8>>,
<<"parse_bits"/utf8>>,
{variable, <<"body"/utf8>>},
Decoder@8
),
oas@generator@ast:pipe(
_pipe@7,
oas@generator@ast:call1(
<<"result"/utf8>>,
<<"map"/utf8>>,
{variable, Wrapper}
)
)
end,
_pipe@8 = {{Action, Resp_type, true}, Internal@1},
{done, _pipe@8}
end);
{[], []} ->
_pipe@9 = {just_return_ok_nil(Wrapper), Internal},
{done, _pipe@9};
{[], [{Unknown@1, _}]} ->
gleam_stdlib:println(
<<"unknown content type: "/utf8, Unknown@1/binary>>
),
_pipe@10 = {just_return_ok_nil(Wrapper), Internal},
{done, _pipe@10};
{_, _} ->
gleam_stdlib:println(
<<"multiple content types not supported"/utf8>>
),
_pipe@11 = {just_return_ok_nil(Wrapper), Internal},
{done, _pipe@11}
end.
-file("src/oas/generator.gleam", 473).
-spec status_range(list({oas:status(), QTQ}), integer(), integer()) -> list({integer(),
QTQ}).
status_range(Responses, Above_equal, Bellow) ->
gleam@list:filter_map(
Responses,
fun(Response) ->
{Status, Response@1} = Response,
case Status of
{status, I} when (Above_equal =< I) andalso (I < Bellow) ->
{ok, {I, Response@1}};
_ ->
{error, nil}
end
end
).
-file("src/oas/generator.gleam", 483).
-spec gen_response(
{any(), oas:operation()},
oas:components(),
list(oas@generator@lift:fields())
) -> oas@generator@lookup:lookup({{glance:function_(),
gleam@option:option(glance:custom_type())},
list(oas@generator@lift:fields())}).
gen_response(Operation, Components, Internal) ->
{_, Op} = Operation,
Op@1 = Op,
Responses = begin
_pipe = erlang:element(8, Op@1),
maps:to_list(_pipe)
end,
Default = gleam@list:find_map(
Responses,
fun(Response) ->
{Status, Response@1} = Response,
case Status of
default ->
{ok, Response@1};
_ ->
{error, nil}
end
end
),
oas@generator@lookup:then(case Default of
{ok, Response@2} ->
{response, _, _, Content} = oas:fetch_response(
Response@2,
erlang:element(3, Components)
),
gen_content_handling(
erlang:element(5, Op@1),
Content,
<<"Error"/utf8>>,
Internal
);
{error, nil} ->
case status_range(Responses, 400, 600) of
[{Status@1, First} | Rest] ->
case gleam@list:try_fold(
Rest,
[Status@1],
fun(Acc, This) ->
{Status@2, This@1} = This,
case {First, This@1} of
{{ref, A, _, _}, {ref, B, _, _}} when A =:= B ->
{ok, [Status@2 | Acc]};
{{inline, {response, _, _, A@1}},
{inline, {response, _, _, B@1}}} when A@1 =:= B@1 ->
{ok, [Status@2 | Acc]};
{_, _} ->
{error, nil}
end
end
) of
{ok, _} ->
{response, _, _, Content@1} = oas:fetch_response(
First,
erlang:element(3, Components)
),
gen_content_handling(
erlang:element(5, Op@1),
Content@1,
<<"Error"/utf8>>,
Internal
);
{error, _} ->
Branch = begin
_pipe@1 = {variable, <<"response"/utf8>>},
_pipe@2 = oas@generator@ast:pipe(
_pipe@1,
{variable, <<"Error"/utf8>>}
),
oas@generator@ast:pipe(
_pipe@2,
{variable, <<"Ok"/utf8>>}
)
end,
_pipe@3 = {{Branch, none, false}, Internal},
{done, _pipe@3}
end;
[] ->
Branch@1 = begin
_pipe@4 = {variable, <<"response"/utf8>>},
_pipe@5 = oas@generator@ast:pipe(
_pipe@4,
{variable, <<"Error"/utf8>>}
),
oas@generator@ast:pipe(
_pipe@5,
{variable, <<"Ok"/utf8>>}
)
end,
_pipe@6 = {{Branch@1, none, false}, Internal},
{done, _pipe@6}
end
end, fun(_use0) ->
{{Default_branch, Resp_type, Used}, Internal@1} = _use0,
Default_clause = {clause,
[[{pattern_discard, <<""/utf8>>}]],
none,
Default_branch},
oas@generator@lookup:then(
case begin
_pipe@7 = status_range(Responses, 200, 300),
gleam@list:sort(
_pipe@7,
fun(Ra, Rb) ->
gleam@int:compare(
erlang:element(1, Ra),
erlang:element(1, Rb)
)
end
)
end of
[] ->
_pipe@8 = {{Used, Resp_type, []}, Internal@1},
{done, _pipe@8};
[{Status@3, First@1} | More] ->
case More of
[] ->
nil;
_ ->
gleam_stdlib:print(
<<"Doesn't support multiple ok statuses"/utf8>>
),
nil
end,
{response, _, _, Content@2} = oas:fetch_response(
First@1,
erlang:element(3, Components)
),
oas@generator@lookup:then(
gen_content_handling(
erlang:element(5, Op@1),
Content@2,
<<"Ok"/utf8>>,
Internal@1
),
fun(_use0@1) ->
{{Branch@2, Resp_type@1, U}, Internal@2} = _use0@1,
Clause = {clause,
[[{pattern_int,
erlang:integer_to_binary(
Status@3
)}]],
none,
Branch@2},
_pipe@9 = {{Used orelse U,
Resp_type@1,
[Clause]},
Internal@2},
{done, _pipe@9}
end
)
end,
fun(_use0@2) ->
{{Used@1, Response_type, Expected_clauses}, Internal@3} = _use0@2,
Response_handler = {function,
oas@generator@ast:name_for_gleam_field_or_var(
<<(erlang:element(5, Op@1))/binary,
"_response"/utf8>>
),
public,
[{function_parameter,
none,
{named, <<"response"/utf8>>},
none}],
none,
[{assignment,
'let',
{pattern_constructor,
{some, <<"response"/utf8>>},
<<".Response"/utf8>>,
[{shorthand_field, <<"status"/utf8>>} |
case Used@1 of
true ->
[{shorthand_field,
<<"body"/utf8>>}];
false ->
[]
end],
true},
none,
{variable, <<"response"/utf8>>}},
{expression,
{'case',
[{variable, <<"status"/utf8>>}],
lists:append(
Expected_clauses,
[Default_clause]
)}}],
{span, 0, 0}},
_pipe@10 = {{Response_handler, Response_type}, Internal@3},
{done, _pipe@10}
end
)
end).
-file("src/oas/generator.gleam", 618).
-spec gen_fns(
binary(),
oas:path_item(),
oas:components(),
list(binary()),
{list(glance:custom_type()), list(oas@generator@lift:fields())}
) -> oas@generator@lookup:lookup({{list(glance:custom_type()),
list(oas@generator@lift:fields())},
list({{list(glance:function_()), gleam@option:option(glance:custom_type())},
glance:function_()})}).
gen_fns(Key, Path_item, Components, Exclude, Acc) ->
Operations = gleam@list:filter(
erlang:element(5, Path_item),
fun(Op) ->
not gleam@list:contains(
Exclude,
erlang:element(5, (erlang:element(2, Op)))
)
end
),
oas@generator@lookup:map_fold(
Operations,
Acc,
fun(Acc@1, Op@1) ->
{Custom_types, Internal} = Acc@1,
oas@generator@lookup:then(
gen_request_for_op(
Op@1,
Key,
erlang:element(4, Path_item),
Components,
Internal
),
fun(_use0) ->
{Fn_, Req_fn, Custom_type, Internal@1} = _use0,
Custom_types@1 = case Custom_type of
{some, T} ->
[T | Custom_types];
none ->
Custom_types
end,
oas@generator@lookup:then(
gen_response(Op@1, Components, Internal@1),
fun(_use0@1) ->
{{Response_handler, Response_type}, Internal@2} = _use0@1,
_pipe = {{Custom_types@1, Internal@2},
{{[Response_handler, Req_fn], Response_type},
Fn_}},
{done, _pipe}
end
)
end
)
end
).
-file("src/oas/generator.gleam", 652).
-spec gen_operations(
{binary(), oas:path_item()},
oas:components(),
list(binary()),
{list(glance:custom_type()), list(oas@generator@lift:fields())}
) -> oas@generator@lookup:lookup({{list(glance:custom_type()),
list(oas@generator@lift:fields())},
list({{list(glance:function_()), gleam@option:option(glance:custom_type())},
glance:function_()})}).
gen_operations(Op, Components, Exclude, Acc) ->
{Key, Path} = Op,
gen_fns(Key, Path, Components, Exclude, Acc).
-file("src/oas/generator.gleam", 658).
-spec defs(list(QYT)) -> list(glance:definition(QYT)).
defs(Xs) ->
gleam@list:map(Xs, fun(_capture) -> {definition, [], _capture} end).
-file("src/oas/generator.gleam", 662).
-spec run_legacy(oas@generator@lookup:lookup(QZD), oas@generator@misc:module_()) -> QZD.
run_legacy(Lookup, Module) ->
case Lookup of
{lookup, <<"#/components/schemas/"/utf8, Inner/binary>>, Resume} ->
Next = case Module of
schema ->
Resume(none, Inner);
operations ->
Resume({some, <<"schema"/utf8>>}, Inner)
end,
run_legacy(Next, Module);
{lookup, Ref, _} ->
erlang:error(#{gleam_error => panic,
message => (<<"Unknown ref: "/utf8, Ref/binary>>),
file => <<?FILEPATH/utf8>>,
module => <<"oas/generator"/utf8>>,
function => <<"run_legacy"/utf8>>,
line => 671});
{done, Value} ->
Value
end.
-file("src/oas/generator.gleam", 676).
-spec run_single_location(oas@generator@lookup:lookup(QZM), binary()) -> {ok,
QZM} |
{error, binary()}.
run_single_location(Lookup, Prefix) ->
case Lookup of
{lookup, Ref, Resume} ->
case gleam@string:split_once(Ref, Prefix) of
{ok, {<<""/utf8>>, Inner}} ->
_pipe = Resume(none, Inner),
run_single_location(_pipe, Prefix);
_ ->
{error, <<"Unknown ref: "/utf8, Ref/binary>>}
end;
{done, Value} ->
{ok, Value}
end.
-file("src/oas/generator.gleam", 752).
-spec gen_operations_and_top_files(oas:document(), binary(), list(binary())) -> {binary(),
binary()}.
gen_operations_and_top_files(Spec, Provider, Exclude) ->
Paths = maps:to_list(erlang:element(6, Spec)),
Custom_types = [],
Internal = [],
{{Custom_types@1, Internal@1}, Fs} = run_legacy(
oas@generator@lookup:map_fold(
Paths,
{Custom_types, Internal},
fun(Acc, Path) ->
gen_operations(Path, erlang:element(7, Spec), Exclude, Acc)
end
),
operations
),
Zipped = run_legacy(
oas@generator@lookup:seq(
gleam@list:index_map(
Internal@1,
fun(Fields, Index) ->
{fields, Properties, Additional, Required} = Fields,
Name = <<"Internal_"/utf8,
(erlang:integer_to_binary(Index))/binary>>,
oas@generator@lookup:then(
oas@generator@schema:custom_type(
Name,
Properties,
Additional,
Required
),
fun(Type_) ->
oas@generator@lookup:then(
oas@generator@schema:to_encode_fn(
{Name, {compound, Fields}}
),
fun(Encoder) ->
oas@generator@lookup:then(
oas@generator@schema:to_decode_fn(
{Name, {compound, Fields}}
),
fun(Decoder) ->
_pipe = {Type_, [Encoder, Decoder]},
{done, _pipe}
end
)
end
)
end
)
end
)
),
operations
),
{Internal_types, Internal_fns} = begin
_pipe@1 = Zipped,
gleam@list:unzip(_pipe@1)
end,
{Operation_functions, Top} = gleam@list:unzip(lists:append(Fs)),
{Operation_functions@1, Operation_types} = gleam@list:unzip(
Operation_functions
),
Operation_types@1 = gleam@list:filter_map(
Operation_types,
fun(_capture) -> gleam@option:to_result(_capture, nil) end
),
Operation_types@2 = lists:append(Operation_types@1, Custom_types@1),
Operation_functions@2 = lists:append(Operation_functions@1),
Modules = [<<"oas/generator/utils"/utf8>>,
<<Provider/binary, "/schema"/utf8>>,
<<"gleam/http"/utf8>>,
<<"gleam/http/response"/utf8>>,
<<"gleam/int"/utf8>>,
<<"gleam/float"/utf8>>,
<<"gleam/json"/utf8>>,
<<"gleam/dynamic/decode"/utf8>>,
<<"gleam/result"/utf8>>,
<<"gleam/bool"/utf8>>,
<<"gleam/dict"/utf8>>],
Operations = begin
_pipe@2 = {module,
defs(
[{import,
<<"gleam/option"/utf8>>,
none,
[{unqualified_import, <<"Option"/utf8>>, none}],
[{unqualified_import, <<"None"/utf8>>, none}]} |
gleam@list:map(
Modules,
fun(_capture@1) ->
{import, _capture@1, none, [], []}
end
)]
),
defs(lists:append(Internal_types, Operation_types@2)),
[],
[],
gleam@list:map(
lists:append(lists:append(Internal_fns), Operation_functions@2),
fun(_capture@2) -> {definition, [], _capture@2} end
)},
glance_printer:print(_pipe@2)
end,
Entry = begin
_pipe@3 = {module,
[],
[],
[],
[],
gleam@list:map(
Top,
fun(_capture@3) -> {definition, [], _capture@3} end
)},
glance_printer:print(_pipe@3)
end,
{Operations, Entry}.
-file("src/oas/generator.gleam", 841).
-spec gen_schema_file(gleam@dict:dict(binary(), oas@json_schema:schema())) -> oas@generator@lookup:lookup(binary()).
gen_schema_file(Schemas) ->
oas@generator@lookup:then(
oas@generator@schema:generate(Schemas),
fun(_use0) ->
{Custom_types, Type_aliases, Functions} = _use0,
_pipe = {module,
[{definition, [], {import, <<"gleam/dict"/utf8>>, none, [], []}},
{definition,
[],
{import, <<"gleam/dynamic/decode"/utf8>>, none, [], []}},
{definition,
[],
{import, <<"gleam/dynamic"/utf8>>, none, [], []}},
{definition,
[],
{import, <<"gleam/json"/utf8>>, none, [], []}},
{definition,
[],
{import, <<"oas/generator/utils"/utf8>>, none, [], []}},
{definition,
[],
{import,
<<"gleam/option"/utf8>>,
none,
[{unqualified_import, <<"Option"/utf8>>, none}],
[{unqualified_import, <<"None"/utf8>>, none}]}}],
defs(Custom_types),
defs(Type_aliases),
[],
defs(Functions)},
_pipe@1 = glance_printer:print(_pipe),
{done, _pipe@1}
end
).
-file("src/oas/generator.gleam", 882).
-spec print_decode_errors(list(gleam@dynamic@decode:decode_error())) -> binary().
print_decode_errors(Errors) ->
_pipe = gleam@list:map(
Errors,
fun(Error) ->
{decode_error, Expected, Found, Path} = Error,
<<<<<<<<<<"expected "/utf8, Expected/binary>>/binary,
" found "/utf8>>/binary,
Found/binary>>/binary,
" at "/utf8>>/binary,
(gleam@string:join(Path, <<"/"/utf8>>))/binary>>
end
),
gleam@string:join(_pipe, <<"\n"/utf8>>).
-file("src/oas/generator.gleam", 872).
-spec json_decode_error_to_string(gleam@json:decode_error()) -> binary().
json_decode_error_to_string(Error) ->
case Error of
unexpected_end_of_input ->
<<"UnexpectedEndOfInput"/utf8>>;
{unexpected_byte, Str} ->
<<"UnexpectedByte "/utf8, Str/binary>>;
{unexpected_sequence, Str@1} ->
<<"UnexpectedSequence "/utf8, Str@1/binary>>;
{unable_to_decode, Errors} ->
<<"UnableToDecode "/utf8, (print_decode_errors(Errors))/binary>>
end.
-file("src/oas/generator.gleam", 690).
-spec build(binary(), binary(), binary(), list(binary())) -> {ok, nil} |
{error, snag:snag()}.
build(Spec_src, Project_path, Provider, Exclude) ->
Module_path = <<<<Project_path/binary, "/src/"/utf8>>/binary,
Provider/binary>>,
gleam@result:'try'(
begin
_pipe = simplifile:read(Spec_src),
_pipe@1 = snag:map_error(_pipe, fun simplifile:describe_error/1),
snag:context(
_pipe@1,
<<"Could not read file "/utf8, Spec_src/binary>>
)
end,
fun(File) ->
gleam@result:'try'(
begin
_pipe@2 = gleam@json:parse(File, oas:decoder()),
snag:map_error(_pipe@2, fun json_decode_error_to_string/1)
end,
fun(Spec) ->
{Operations, Entry} = gen_operations_and_top_files(
Spec,
Provider,
Exclude
),
Operations_module_file = <<Module_path/binary,
"/operations.gleam"/utf8>>,
gleam@result:'try'(
begin
_pipe@4 = simplifile_erl:write_bits(
Operations_module_file,
begin
_pipe@3 = Operations,
gleam_stdlib:identity(_pipe@3)
end
),
_pipe@5 = snag:map_error(
_pipe@4,
fun simplifile:describe_error/1
),
snag:context(
_pipe@5,
<<"Could not write file "/utf8,
Operations_module_file/binary>>
)
end,
fun(_use0) ->
nil = _use0,
gleam@result:'try'(
begin
_pipe@6 = simplifile:read(
<<Module_path/binary, ".gleam"/utf8>>
),
_pipe@7 = snag:map_error(
_pipe@6,
fun simplifile:describe_error/1
),
snag:context(
_pipe@7,
<<<<"Could not read file "/utf8,
Module_path/binary>>/binary,
".gleam"/utf8>>
)
end,
fun(Mod) ->
Split = <<"// GENERATED ---"/utf8>>,
Pre@1 = case gleam@string:split_once(
Mod,
Split
) of
{ok, {Pre, _}} ->
Pre;
{error, nil} ->
Mod
end,
Content = <<Pre@1/binary,
Split/binary,
"----------\n\n"/utf8,
Entry/binary>>,
gleam@result:'try'(
begin
_pipe@8 = simplifile_erl:write_bits(
<<Module_path/binary,
".gleam"/utf8>>,
Content
),
_pipe@9 = snag:map_error(
_pipe@8,
fun simplifile:describe_error/1
),
snag:context(
_pipe@9,
<<<<"Could not write file "/utf8,
Module_path/binary>>/binary,
".gleam"/utf8>>
)
end,
fun(_use0@1) ->
nil = _use0@1,
Content@1 = begin
_pipe@10 = gen_schema_file(
erlang:element(
2,
erlang:element(7, Spec)
)
),
_pipe@11 = run_legacy(
_pipe@10,
schema
),
gleam_stdlib:identity(_pipe@11)
end,
Schema_module_file = <<Module_path/binary,
"/schema.gleam"/utf8>>,
gleam@result:'try'(
begin
_pipe@12 = simplifile_erl:write_bits(
Schema_module_file,
Content@1
),
_pipe@13 = snag:map_error(
_pipe@12,
fun simplifile:describe_error/1
),
snag:context(
_pipe@13,
<<"Could not write file "/utf8,
Schema_module_file/binary>>
)
end,
fun(_use0@2) ->
nil = _use0@2,
{ok, nil}
end
)
end
)
end
)
end
)
end
)
end
).