Current section
Files
Jump to
Current section
Files
src/oas@generator.erl
-module(oas@generator).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([gen_operations_and_top_files/3, gen_schema_file/2, json_decode_error_to_string/1, build/4]).
-file("src/oas/generator.gleam", 22).
-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,
_assert_subject = gleam@list:reduce(
Parts,
fun(Post, Pre) -> {binary_operator, concatenate, Pre, Post} end
),
{ok, A} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"oas/generator"/utf8>>,
function => <<"concat_path"/utf8>>,
line => 66})
end,
A.
-file("src/oas/generator.gleam", 73).
-spec noop1(binary()) -> glance:expression().
noop1(Message) ->
{fn,
[{fn_parameter, {discarded, <<""/utf8>>}, none}],
none,
[{expression, {panic, {some, {string, Message}}}}]}.
-file("src/oas/generator.gleam", 79).
-spec query_to_parts(
list({binary(), boolean(), oas:ref(oas: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", 112).
-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", 127).
-spec let_(binary(), glance:expression()) -> glance:statement().
let_(Var, Value) ->
{assignment, 'let', {pattern_variable, Var}, none, Value}.
-file("src/oas/generator.gleam", 131).
-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", 145).
-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", 160).
-spec gen_request_for_op(
{gleam@http:method(), oas:operation()},
binary(),
list(oas:ref(oas:parameter())),
oas:components(),
list(oas@generator@lift:fields())
) -> {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>>,
module => <<"oas/generator"/utf8>>,
function => <<"gen_request_for_op"/utf8>>,
line => 180})
end,
Module = operations,
{Custom_type@1, Body@1, Internal@2} = 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, Schema}}], _} ->
{Lifted, _, Internal@1} = oas@generator@lift:do_lift(
Schema,
Internal
),
{Custom_type, Arg, Json} = case Lifted of
{named, N} ->
Encoder = oas@generator@schema:to_encoder(
{named, N},
Module
),
{none,
<<"data"/utf8>>,
oas@generator@ast:call(
Encoder,
{variable, <<"data"/utf8>>}
)};
{primitive, P} ->
Encoder@1 = oas@generator@schema:to_encoder(
{primitive, P},
Module
),
{none,
<<"data"/utf8>>,
oas@generator@ast:call(
Encoder@1,
{variable, <<"data"/utf8>>}
)};
{array, I} ->
Encoder@2 = oas@generator@schema:to_encoder(
{array, I},
Module
),
{none,
<<"data"/utf8>>,
oas@generator@ast:call(
Encoder@2,
{variable, <<"data"/utf8>>}
)};
{tuple, Es} ->
Encoder@3 = oas@generator@schema:to_encoder(
{tuple, Es},
Module
),
{none,
<<"data"/utf8>>,
oas@generator@ast:call(
Encoder@3,
{variable, <<"data"/utf8>>}
)};
{compound, {fields, Named, Additional, Required}} ->
Name = oas@generator@ast:name_for_gleam_type(
<<Operation_id/binary, "-request"/utf8>>
),
Type_ = oas@generator@schema:custom_type(
Name,
Named,
Additional,
Required,
Module
),
{{some, Type_},
<<"data"/utf8>>,
oas@generator@schema:fields_to_encode_body(
Named,
Required,
Additional,
Module
)};
{dictionary, F} ->
Encoder@4 = oas@generator@schema:to_encoder(
{dictionary, F},
Module
),
{none,
<<"data"/utf8>>,
oas@generator@ast:call(
Encoder@4,
{variable, <<"data"/utf8>>}
)};
unsupported ->
Encoder@5 = oas@generator@schema:to_encoder(
unsupported,
Module
),
{none,
<<"data"/utf8>>,
oas@generator@ast:call(
Encoder@5,
{variable, <<"data"/utf8>>}
)}
end,
{Custom_type,
{some,
{Arg,
oas@generator@ast:call1(
<<"utils"/utf8>>,
<<"json_to_bits"/utf8>>,
Json
)}},
Internal@1};
{[], []} ->
{none, none, Internal};
{[], [{Unknown@1, _}]} ->
gleam_stdlib:println(
<<"unknown content type: "/utf8, Unknown@1/binary>>
),
{none, none, Internal};
{_, _} ->
gleam_stdlib:println(
<<"multiple content types not supported"/utf8>>
),
{none, none, Internal}
end;
none ->
{none, none, Internal}
end,
Parameters_for_operation_functions = begin
_pipe = path_args(Match@1),
_pipe@1 = lists:append(_pipe, case Body@1 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@1, Q_args)
end,
Parameters_for_top_functions = begin
_pipe@2 = path_args(Match@1),
_pipe@3 = lists:append(_pipe@2, case Body@1 of
{some, {Arg@2, _}} ->
[{function_parameter, none, {named, Arg@2}, none}];
none ->
[]
end),
lists:append(_pipe@3, 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@1 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) ->
{function_parameter, _, {named, Name@2}, _} = case P@1 of
{function_parameter, _, {named, _}, _} -> P@1;
_assert_fail ->
erlang:error(
#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"oas/generator"/utf8>>,
function => <<"gen_request_for_op"/utf8>>,
line => 354}
)
end,
{unlabelled_field, {variable, Name@2}}
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}},
{Fn_, Req_fn, Custom_type@1, Internal@2}.
-file("src/oas/generator.gleam", 445).
-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", 389).
-spec gen_content_handling(
binary(),
gleam@dict:dict(binary(), oas:media_type()),
binary(),
list(oas@generator@lift:fields())
) -> {{glance:expression(),
gleam@option:option(glance:custom_type()),
boolean()},
list(oas@generator@lift:fields())}.
gen_content_handling(Operation_id, Content, Wrapper, Internal) ->
Module = operations,
{Known, Unknown} = get_structure_json_media(Content),
case {Known, Unknown} of
{[{_, {media_type, Schema}}], _} ->
{Lifted, _, Internal@1} = oas@generator@lift:do_lift(
Schema,
Internal
),
{Decoder@1, Resp_type} = case Lifted of
{named, N} ->
{oas@generator@schema:to_decoder({named, N}, Module), none};
{primitive, P} ->
{oas@generator@schema:to_decoder({primitive, P}, Module),
none};
{array, A} ->
{oas@generator@schema:to_decoder({array, A}, Module), none};
{tuple, Of} ->
{oas@generator@schema:to_decoder({tuple, Of}, Module), none};
{compound, {fields, Parameters, Additional, Required}} = Top ->
Name = <<Operation_id/binary, "_response"/utf8>>,
Type_ = oas@generator@schema:custom_type(
Name,
Parameters,
Additional,
Required,
operations
),
Decoder = {block,
oas@generator@schema:gen_top_decoder_needs_name(
Name,
Top,
Module
)},
{Decoder, {some, Type_}};
{dictionary, Values} ->
{oas@generator@schema:to_decoder(
{dictionary, Values},
Module
),
none};
unsupported ->
{oas@generator@schema:to_decoder(unsupported, Module), none}
end,
Action = begin
_pipe = oas@generator@ast:call2(
<<"json"/utf8>>,
<<"parse_bits"/utf8>>,
{variable, <<"body"/utf8>>},
Decoder@1
),
oas@generator@ast:pipe(
_pipe,
oas@generator@ast:call1(
<<"result"/utf8>>,
<<"map"/utf8>>,
{variable, Wrapper}
)
)
end,
{{Action, Resp_type, true}, Internal@1};
{[], []} ->
{just_return_ok_nil(Wrapper), Internal};
{[], [{Unknown@1, _}]} ->
gleam_stdlib:println(
<<"unknown content type: "/utf8, Unknown@1/binary>>
),
{just_return_ok_nil(Wrapper), Internal};
{_, _} ->
gleam_stdlib:println(
<<"multiple content types not supported"/utf8>>
),
{just_return_ok_nil(Wrapper), Internal}
end.
-file("src/oas/generator.gleam", 454).
-spec status_range(list({oas:status(), PHN}), integer(), integer()) -> list({integer(),
PHN}).
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", 464).
-spec gen_response(
{any(), oas:operation()},
oas:components(),
list(oas@generator@lift:fields())
) -> {{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
),
{{Default_branch, Resp_type, Used}, Internal@1} = 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,
{{Branch, none, false}, Internal}
end;
[] ->
Branch@1 = begin
_pipe@3 = {variable, <<"response"/utf8>>},
_pipe@4 = oas@generator@ast:pipe(
_pipe@3,
{variable, <<"Error"/utf8>>}
),
oas@generator@ast:pipe(
_pipe@4,
{variable, <<"Ok"/utf8>>}
)
end,
{{Branch@1, none, false}, Internal}
end
end,
Default_clause = {clause,
[[{pattern_discard, <<""/utf8>>}]],
none,
Default_branch},
{{Used@1, Response_type, Expected_clauses}, Internal@3} = case begin
_pipe@5 = status_range(Responses, 200, 300),
gleam@list:sort(
_pipe@5,
fun(Ra, Rb) ->
gleam@int:compare(erlang:element(1, Ra), erlang:element(1, Rb))
end
)
end of
[] ->
{{Used, Resp_type, []}, Internal@1};
[{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)
),
{{Branch@2, Resp_type@1, U}, Internal@2} = gen_content_handling(
erlang:element(5, Op@1),
Content@2,
<<"Ok"/utf8>>,
Internal@1
),
Clause = {clause,
[[{pattern_int, erlang:integer_to_binary(Status@3)}]],
none,
Branch@2},
{{Used orelse U, Resp_type@1, [Clause]}, Internal@2}
end,
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}},
{{Response_handler, Response_type}, Internal@3}.
-file("src/oas/generator.gleam", 592).
-spec gen_fns(
binary(),
oas:path_item(),
oas:components(),
list(binary()),
{list(glance:custom_type()), list(oas@generator@lift:fields())}
) -> {{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
),
gleam@list:map_fold(
Operations,
Acc,
fun(Acc@1, Op@1) ->
{Custom_types, Internal} = Acc@1,
{Fn_, Req_fn, Custom_type, Internal@1} = gen_request_for_op(
Op@1,
Key,
erlang:element(4, Path_item),
Components,
Internal
),
Custom_types@1 = case Custom_type of
{some, T} ->
[T | Custom_types];
none ->
Custom_types
end,
{{Response_handler, Response_type}, Internal@2} = gen_response(
Op@1,
Components,
Internal@1
),
{{Custom_types@1, Internal@2},
{{[Response_handler, Req_fn], Response_type}, Fn_}}
end
).
-file("src/oas/generator.gleam", 617).
-spec gen_operations(
{binary(), oas:path_item()},
oas:components(),
list(binary()),
{list(glance:custom_type()), list(oas@generator@lift:fields())}
) -> {{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", 623).
-spec defs(list(PLP)) -> list(glance:definition(PLP)).
defs(Xs) ->
gleam@list:map(Xs, fun(_capture) -> {definition, [], _capture} end).
-file("src/oas/generator.gleam", 688).
-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} = gleam@list:map_fold(
Paths,
{Custom_types, Internal},
fun(Acc, Path) ->
gen_operations(Path, erlang:element(7, Spec), Exclude, Acc)
end
),
{Internal_types, Internal_fns} = begin
_pipe = gleam@list:index_map(
Internal@1,
fun(Fields, Index) ->
{fields, Properties, Additional, Required} = Fields,
Name = <<"Internal_"/utf8,
(erlang:integer_to_binary(Index))/binary>>,
Module = operations,
Type_ = oas@generator@schema:custom_type(
Name,
Properties,
Additional,
Required,
operations
),
Encoder = oas@generator@schema:to_encode_fn(
{Name, {compound, Fields}},
Module
),
Decoder = oas@generator@schema:to_decode_fn(
{Name, {compound, Fields}},
Module
),
{Type_, [Encoder, Decoder]}
end
),
gleam@list:unzip(_pipe)
end,
{Operation_functions, Top} = gleam@list:unzip(gleam@list:flatten(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 = gleam@list:flatten(Operation_functions@1),
Modules = [<<Provider/binary, "/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>>],
Operations = begin
_pipe@1 = {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(
gleam@list:flatten(Internal_fns),
Operation_functions@2
),
fun(_capture@2) -> {definition, [], _capture@2} end
)},
glance_printer:print(_pipe@1)
end,
Entry = begin
_pipe@2 = {module,
[],
[],
[],
[],
gleam@list:map(
Top,
fun(_capture@3) -> {definition, [], _capture@3} end
)},
glance_printer:print(_pipe@2)
end,
{Operations, Entry}.
-file("src/oas/generator.gleam", 765).
-spec gen_schema_file(gleam@dict:dict(binary(), oas:schema()), binary()) -> binary().
gen_schema_file(Schemas, Provider) ->
{Custom_types, Type_aliases, Functions} = oas@generator@schema:generate(
Schemas
),
_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, <<Provider/binary, "/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)},
glance_printer:print(_pipe).
-file("src/oas/generator.gleam", 803).
-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", 793).
-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", 627).
-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)
),
Provider
),
gleam_stdlib:identity(_pipe@10)
end,
Schema_module_file = <<Module_path/binary,
"/schema.gleam"/utf8>>,
gleam@result:'try'(
begin
_pipe@11 = simplifile_erl:write_bits(
Schema_module_file,
Content@1
),
_pipe@12 = snag:map_error(
_pipe@11,
fun simplifile:describe_error/1
),
snag:context(
_pipe@12,
<<"Could not write file "/utf8,
Schema_module_file/binary>>
)
end,
fun(_use0@2) ->
nil = _use0@2,
{ok, nil}
end
)
end
)
end
)
end
)
end
)
end
).