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]).
-export([gen_operations_and_top_files/3, gen_schema_file/2, json_decode_error_to_string/1, build/4]).
-file("src/oas/generator.gleam", 64).
-spec escape_key(binary()) -> binary().
escape_key(Key) ->
Key@1 = case Key of
<<"type"/utf8>> ->
<<"type_"/utf8>>;
<<"auto"/utf8>> ->
<<"auto_"/utf8>>;
<<"import"/utf8>> ->
<<"import_"/utf8>>;
<<"base"/utf8>> ->
<<"base_"/utf8>>;
<<"path"/utf8>> ->
<<"path_"/utf8>>;
<<"method"/utf8>> ->
<<"method_"/utf8>>;
<<"token"/utf8>> ->
<<"token_"/utf8>>;
_ ->
Key
end,
_pipe = case Key@1 of
<<"_"/utf8, Key@2/binary>> ->
Key@2;
_ ->
Key@1
end,
_pipe@1 = gleam@string:replace(_pipe, <<"-"/utf8>>, <<"minus"/utf8>>),
_pipe@2 = gleam@string:replace(_pipe@1, <<"+"/utf8>>, <<"plus"/utf8>>),
gleam@string:replace(_pipe@2, <<"1"/utf8>>, <<"one"/utf8>>).
-file("src/oas/generator.gleam", 130).
-spec var_name(binary()) -> binary().
var_name(From) ->
escape_key(justin:snake_case(From)).
-file("src/oas/generator.gleam", 134).
-spec alias(binary(), glance:type()) -> glance:type_alias().
alias(To, Type_) ->
{type_alias, To, public, [], Type_}.
-file("src/oas/generator.gleam", 160).
-spec array_type(oas:ref(oas:schema()), gleam@option:option(binary())) -> glance:type().
array_type(Items, Module) ->
Inner@2 = case Items of
{ref, <<"#/components/schemas/"/utf8, Inner/binary>>, _, _} ->
{named_type, justin:pascal_case(Inner), Module, []};
{ref, _, _, _} ->
erlang:error(#{gleam_error => panic,
message => <<"unexpected ref"/utf8>>,
module => <<"oas/generator"/utf8>>,
function => <<"array_type"/utf8>>,
line => 164});
{inline, Inner@1} ->
case Inner@1 of
{boolean, _, _, _, _} ->
{named_type, <<"Bool"/utf8>>, none, []};
{integer, _, _, _, _, _, _, _, _, _} ->
{named_type, <<"Int"/utf8>>, none, []};
{number, _, _, _, _, _, _, _, _, _} ->
{named_type, <<"Float"/utf8>>, none, []};
{string, _, _, _, _, _, _, _, _} ->
{named_type, <<"String"/utf8>>, none, []};
{null, _, _, _} ->
{named_type, <<"Nil"/utf8>>, none, []};
{array, _, _, _, Items@1, _, _, _, _} ->
array_type(Items@1, Module);
{object, _, _, _, _, _, _} ->
{named_type, <<"Nil"/utf8>>, none, []};
{any_of, _} ->
{named_type, <<"Nil"/utf8>>, none, []};
{all_of, _} ->
{named_type, <<"Nil"/utf8>>, none, []};
{one_of, _} ->
{named_type, <<"Nil"/utf8>>, none, []};
always_passes ->
{named_type,
<<"Dynamic"/utf8>>,
{some, <<"dynamic"/utf8>>},
[]};
always_fails ->
{named_type, <<"Nil"/utf8>>, none, []}
end
end,
{named_type, <<"List"/utf8>>, none, [Inner@2]}.
-file("src/oas/generator.gleam", 188).
-spec noop1(binary()) -> glance:expression().
noop1(Message) ->
{fn,
[{fn_parameter, {discarded, <<""/utf8>>}, none}],
none,
[{expression, {panic, {some, {string, Message}}}}]}.
-file("src/oas/generator.gleam", 349).
-spec is_nullable(oas:ref(oas:schema())) -> boolean().
is_nullable(Schema) ->
case Schema of
{ref, _, _, _} ->
false;
{inline, Schema@1} ->
case Schema@1 of
{boolean, Nullable, _, _, _} ->
Nullable;
{integer, _, _, _, _, _, Nullable@1, _, _, _} ->
Nullable@1;
{number, _, _, _, _, _, Nullable@2, _, _, _} ->
Nullable@2;
{string, _, _, _, _, Nullable@3, _, _, _} ->
Nullable@3;
{null, _, _, _} ->
false;
{array, _, _, _, _, Nullable@4, _, _, _} ->
Nullable@4;
{object, _, _, Nullable@5, _, _, _} ->
Nullable@5;
{all_of, _} ->
false;
{any_of, _} ->
false;
{one_of, _} ->
false;
always_passes ->
false;
always_fails ->
false
end
end.
-file("src/oas/generator.gleam", 86).
-spec is_optional({NTS, oas:ref(oas:schema())}, list(NTS)) -> boolean().
is_optional(Property, Required) ->
{Key, Schema} = Property,
not gleam@list:contains(Required, Key) orelse is_nullable(Schema).
-file("src/oas/generator.gleam", 91).
-spec gen_object(
list({binary(), oas:ref(oas:schema())}),
list(binary()),
binary(),
gleam@option:option(binary())
) -> glance:custom_type().
gen_object(Properties, Required, Name, Module) ->
Fields = gleam@list:map(
Properties,
fun(Property) ->
{Key, Schema} = Property,
Type_ = case Schema of
{ref, <<"#/components/schemas/"/utf8, Name@1/binary>>, _, _} ->
{named_type, justin:pascal_case(Name@1), Module, []};
{ref, _, _, _} ->
erlang:error(#{gleam_error => panic,
message => <<"unsupported ref"/utf8>>,
module => <<"oas/generator"/utf8>>,
function => <<"gen_object"/utf8>>,
line => 99});
{inline, Schema@1} ->
case Schema@1 of
{boolean, _, _, _, _} ->
{named_type, <<"Bool"/utf8>>, none, []};
{integer, _, _, _, _, _, _, _, _, _} ->
{named_type, <<"Int"/utf8>>, none, []};
{number, _, _, _, _, _, _, _, _, _} ->
{named_type, <<"Float"/utf8>>, none, []};
{string, _, _, _, _, _, _, _, _} ->
{named_type, <<"String"/utf8>>, none, []};
{null, _, _, _} ->
{named_type, <<"Nil"/utf8>>, none, []};
{array, _, _, _, Items, _, _, _, _} ->
array_type(Items, Module);
{object, _, _, _, _, _, _} ->
{named_type, <<"Nil"/utf8>>, none, []};
{any_of, _} ->
{named_type, <<"Nil"/utf8>>, none, []};
{all_of, _} ->
{named_type, <<"Nil"/utf8>>, none, []};
{one_of, _} ->
{named_type, <<"Nil"/utf8>>, none, []};
always_passes ->
{named_type,
<<"Dynamic"/utf8>>,
{some, <<"dynamic"/utf8>>},
[]};
always_fails ->
{named_type, <<"Nil"/utf8>>, none, []}
end
end,
{labelled_variant_field, case is_optional(Property, Required) of
false ->
Type_;
true ->
{named_type, <<"Option"/utf8>>, none, [Type_]}
end, escape_key(justin:snake_case(Key))}
end
),
{custom_type, Name, public, false, [], [{variant, Name, Fields}]}.
-file("src/oas/generator.gleam", 139).
-spec schema_to_type(binary(), oas:schema()) -> {ok, glance:custom_type()} |
{error, glance:type_alias()}.
schema_to_type(Name, Schema) ->
Type_ = justin:pascal_case(Name),
case Schema of
{boolean, _, _, _, _} ->
{error, alias(Type_, {named_type, <<"Bool"/utf8>>, none, []})};
{integer, _, _, _, _, _, _, _, _, _} ->
{error, alias(Type_, {named_type, <<"Int"/utf8>>, none, []})};
{number, _, _, _, _, _, _, _, _, _} ->
{error, alias(Type_, {named_type, <<"Float"/utf8>>, none, []})};
{string, _, _, _, _, _, _, _, _} ->
{error, alias(Type_, {named_type, <<"String"/utf8>>, none, []})};
{null, _, _, _} ->
{error, alias(Type_, {named_type, <<"Nil"/utf8>>, none, []})};
{array, _, _, _, Items, _, _, _, _} ->
{error, alias(Type_, array_type(Items, none))};
{object, Properties, Required, _, _, _, _} ->
{ok, gen_object(maps:to_list(Properties), Required, Type_, none)};
{all_of, _} ->
{error, alias(Type_, {named_type, <<"Nil"/utf8>>, none, []})};
{any_of, _} ->
{error, alias(Type_, {named_type, <<"Nil"/utf8>>, none, []})};
{one_of, _} ->
{error, alias(Type_, {named_type, <<"Nil"/utf8>>, none, []})};
always_passes ->
{error,
alias(
Type_,
{named_type,
<<"Dynamic"/utf8>>,
{some, <<"dynamic"/utf8>>},
[]}
)};
always_fails ->
{error, alias(Type_, {named_type, <<"Nil"/utf8>>, none, []})}
end.
-file("src/oas/generator.gleam", 512).
-spec access(binary(), binary()) -> glance:expression().
access(Object_or_mod, Field) ->
{field_access, {variable, Object_or_mod}, Field}.
-file("src/oas/generator.gleam", 516).
-spec call0(binary(), binary()) -> glance:expression().
call0(M, F) ->
{call, access(M, F), []}.
-file("src/oas/generator.gleam", 520).
-spec call1(binary(), binary(), glance:expression()) -> glance:expression().
call1(M, F, A) ->
{call, access(M, F), [{unlabelled_field, A}]}.
-file("src/oas/generator.gleam", 524).
-spec call2(binary(), binary(), glance:expression(), glance:expression()) -> glance:expression().
call2(M, F, A, B) ->
{call, access(M, F), [{unlabelled_field, A}, {unlabelled_field, B}]}.
-file("src/oas/generator.gleam", 319).
-spec always_decode() -> glance:expression().
always_decode() ->
call2(
<<"decode"/utf8>>,
<<"new_primitive_decoder"/utf8>>,
{string, <<"Nil"/utf8>>},
{fn,
[{fn_parameter, {discarded, <<""/utf8>>}, none}],
none,
[{expression,
{call,
{variable, <<"Ok"/utf8>>},
[{unlabelled_field, {variable, <<"Nil"/utf8>>}}]}}]}
).
-file("src/oas/generator.gleam", 334).
-spec dynamic_decode() -> glance:expression().
dynamic_decode() ->
call2(
<<"decode"/utf8>>,
<<"new_primitive_decoder"/utf8>>,
{string, <<"Dynamic"/utf8>>},
{fn,
[{fn_parameter, {named, <<"raw"/utf8>>}, none}],
none,
[{expression,
{call,
{variable, <<"Ok"/utf8>>},
[{unlabelled_field, {variable, <<"raw"/utf8>>}}]}}]}
).
-file("src/oas/generator.gleam", 531).
-spec pipe(glance:expression(), glance:expression()) -> glance:expression().
pipe(A, B) ->
{binary_operator, pipe, A, B}.
-file("src/oas/generator.gleam", 568).
-spec gen_method(gleam@http:method()) -> glance:expression().
gen_method(Method) ->
case Method of
get ->
access(<<"http"/utf8>>, <<"Get"/utf8>>);
post ->
access(<<"http"/utf8>>, <<"Post"/utf8>>);
head ->
access(<<"http"/utf8>>, <<"Head"/utf8>>);
put ->
access(<<"http"/utf8>>, <<"Put"/utf8>>);
delete ->
access(<<"http"/utf8>>, <<"Delete"/utf8>>);
trace ->
access(<<"http"/utf8>>, <<"Trace"/utf8>>);
connect ->
access(<<"http"/utf8>>, <<"Connect"/utf8>>);
options ->
access(<<"http"/utf8>>, <<"Options"/utf8>>);
patch ->
access(<<"http"/utf8>>, <<"Patch"/utf8>>);
{other, Other} ->
call1(<<"http"/utf8>>, <<"Other"/utf8>>, {string, Other})
end.
-file("src/oas/generator.gleam", 583).
-spec let_(binary(), glance:expression()) -> glance:statement().
let_(Var, Value) ->
{assignment, 'let', {pattern_variable, Var}, none, Value}.
-file("src/oas/generator.gleam", 601).
-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", 792).
-spec safe_snake_case(binary()) -> binary().
safe_snake_case(In) ->
_pipe = justin:snake_case(In),
_pipe@1 = gleam@string:replace(_pipe, <<"/"/utf8>>, <<"_"/utf8>>),
escape_key(_pipe@1).
-file("src/oas/generator.gleam", 19).
-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, safe_snake_case(Name)} |
Prev@1],
{<<""/utf8>>, Prev@2};
{integer, _, _, _, _, _, _, _, _, _} ->
Prev@3 = [{string, <<Current/binary, "/"/utf8>>} |
Prev],
Prev@4 = [call1(
<<"int"/utf8>>,
<<"to_string"/utf8>>,
{variable, safe_snake_case(Name)}
) |
Prev@3],
{<<""/utf8>>, Prev@4};
_ ->
Prev@5 = [{string, <<Current/binary, "/"/utf8>>} |
Prev],
Prev@6 = [{variable, safe_snake_case(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 => 57})
end,
A.
-file("src/oas/generator.gleam", 184).
-spec encode_fn(binary()) -> binary().
encode_fn(Name) ->
<<(safe_snake_case(Name))/binary, "_encode"/utf8>>.
-file("src/oas/generator.gleam", 287).
-spec array_encoder(oas:ref(oas:schema()), gleam@option:option(binary())) -> glance:expression().
array_encoder(Items, Top_level) ->
Mapper = case Items of
{ref, <<"#/components/schemas/"/utf8, Inner/binary>>, _, _} ->
{variable, encode_fn(Inner)};
{ref, _, _, _} ->
erlang:error(#{gleam_error => panic,
message => <<"unexpected ref"/utf8>>,
module => <<"oas/generator"/utf8>>,
function => <<"array_encoder"/utf8>>,
line => 291});
{inline, Items@1} ->
case Items@1 of
{boolean, _, _, _, _} ->
access(<<"json"/utf8>>, <<"bool"/utf8>>);
{integer, _, _, _, _, _, _, _, _, _} ->
access(<<"json"/utf8>>, <<"int"/utf8>>);
{number, _, _, _, _, _, _, _, _, _} ->
access(<<"json"/utf8>>, <<"float"/utf8>>);
{string, _, _, _, _, _, _, _, _} ->
access(<<"json"/utf8>>, <<"string"/utf8>>);
{null, _, _, _} ->
noop1(<<"null in array"/utf8>>);
{array, _, _, _, Items@2, _, _, _, _} ->
array_encoder(Items@2, none);
{object, _, _, _, _, _, _} ->
noop1(<<"object in array"/utf8>>);
{all_of, _} ->
noop1(<<"Alloas.AllOf in array"/utf8>>);
{any_of, _} ->
noop1(<<"Anyoas.AnyOf in array"/utf8>>);
{one_of, _} ->
noop1(<<"Oneoas.OneOf in array"/utf8>>);
always_passes ->
access(<<"utils"/utf8>>, <<"dynamic_to_json"/utf8>>);
always_fails ->
noop1(<<"AlwaysFails in array"/utf8>>)
end
end,
Arg = case Top_level of
{some, Var} ->
{variable, Var};
none ->
{variable, <<"_"/utf8>>}
end,
call2(<<"json"/utf8>>, <<"array"/utf8>>, Arg, Mapper).
-file("src/oas/generator.gleam", 194).
-spec schema_to_encoder({binary(), oas:schema()}) -> glance:function_().
schema_to_encoder(Entry) ->
{Name, Schema} = Entry,
Type_ = justin:pascal_case(Name),
Exp = case Schema of
{boolean, _, _, _, _} ->
call1(<<"json"/utf8>>, <<"bool"/utf8>>, {variable, <<"data"/utf8>>});
{integer, _, _, _, _, _, _, _, _, _} ->
call1(<<"json"/utf8>>, <<"int"/utf8>>, {variable, <<"data"/utf8>>});
{number, _, _, _, _, _, _, _, _, _} ->
call1(
<<"json"/utf8>>,
<<"float"/utf8>>,
{variable, <<"data"/utf8>>}
);
{string, _, _, _, _, _, _, _, _} ->
call1(
<<"json"/utf8>>,
<<"string"/utf8>>,
{variable, <<"data"/utf8>>}
);
{null, _, _, _} ->
access(<<"json"/utf8>>, <<"null"/utf8>>);
{array, _, _, _, Items, _, _, _, _} ->
array_encoder(Items, {some, <<"data"/utf8>>});
{object, Properties, Required, _, _, _, _} ->
Properties@1 = maps:to_list(Properties),
call1(
<<"json"/utf8>>,
<<"object"/utf8>>,
{list,
gleam@list:map(
Properties@1,
fun(P) ->
{Key, Schema@1} = P,
{tuple,
[{string, Key},
begin
Arg = access(
<<"data"/utf8>>,
var_name(Key)
),
Cast = case Schema@1 of
{ref,
<<"#/components/schemas/"/utf8, Named/binary>>,
_,
_} ->
{variable, encode_fn(Named)};
{ref, Ref, _, _} ->
noop1(
<<"unknown ref name: "/utf8,
Ref/binary>>
);
{inline, {boolean, _, _, _, _}} ->
access(
<<"json"/utf8>>,
<<"bool"/utf8>>
);
{inline,
{integer,
_,
_,
_,
_,
_,
_,
_,
_,
_}} ->
access(
<<"json"/utf8>>,
<<"int"/utf8>>
);
{inline,
{number,
_,
_,
_,
_,
_,
_,
_,
_,
_}} ->
access(
<<"json"/utf8>>,
<<"float"/utf8>>
);
{inline,
{string, _, _, _, _, _, _, _, _}} ->
access(
<<"json"/utf8>>,
<<"string"/utf8>>
);
{inline, {null, _, _, _}} ->
noop1(
<<"encode null in field"/utf8>>
);
{inline,
{array,
_,
_,
_,
Items@1,
_,
_,
_,
_}} ->
array_encoder(Items@1, none);
{inline, {object, _, _, _, _, _, _}} ->
noop1(
<<"Literal object inside field"/utf8>>
);
{inline, {all_of, _}} ->
noop1(
<<"AllOf inside field"/utf8>>
);
{inline, {any_of, _}} ->
noop1(
<<"AnyOf inside field"/utf8>>
);
{inline, {one_of, _}} ->
noop1(
<<"OneOf inside field"/utf8>>
);
{inline, always_passes} ->
access(
<<"utils"/utf8>>,
<<"dynamic_to_json"/utf8>>
);
{inline, always_fails} ->
noop1(
<<"AlwaysFails inside field"/utf8>>
)
end,
case is_optional(P, Required) of
false ->
{call,
Cast,
[{unlabelled_field, Arg}]};
true ->
call2(
<<"json"/utf8>>,
<<"nullable"/utf8>>,
Arg,
Cast
)
end
end]}
end
),
none}
);
{all_of, _} ->
{panic, {some, {string, <<"AllOf"/utf8>>}}};
{any_of, _} ->
{panic, {some, {string, <<"AnyOf"/utf8>>}}};
{one_of, _} ->
{panic, {some, {string, <<"OneOf"/utf8>>}}};
always_passes ->
call1(
<<"utils"/utf8>>,
<<"dynamic_to_json"/utf8>>,
{variable, <<"data"/utf8>>}
);
always_fails ->
{panic, {some, {string, <<"Aloas.AlwaysFails"/utf8>>}}}
end,
Ignored = case Schema of
{null, _, _, _} ->
true;
{object, Properties@2, _, _, _, _, _} ->
gleam@dict:is_empty(Properties@2);
{all_of, _} ->
true;
{any_of, _} ->
true;
{one_of, _} ->
true;
_ ->
false
end,
Assignment = case Ignored of
true ->
{discarded, <<"data"/utf8>>};
false ->
{named, <<"data"/utf8>>}
end,
{function,
encode_fn(Name),
public,
[{function_parameter,
none,
Assignment,
{some, {named_type, Type_, none, []}}}],
none,
[{expression, Exp}],
{span, 0, 0}}.
-file("src/oas/generator.gleam", 315).
-spec decoder(binary()) -> binary().
decoder(Name) ->
<<(safe_snake_case(Name))/binary, "_decoder"/utf8>>.
-file("src/oas/generator.gleam", 468).
-spec array_decoder(oas:ref(oas:schema()), gleam@option:option(binary())) -> glance:expression().
array_decoder(Items, Module) ->
Exp = case Items of
{ref, <<"#/components/schemas/"/utf8, Inner/binary>>, _, _} ->
case Module of
{some, M} ->
call0(M, decoder(Inner));
none ->
{call, {variable, decoder(Inner)}, []}
end;
{ref, _, _, _} ->
erlang:error(#{gleam_error => panic,
message => <<"what is this s ref"/utf8>>,
module => <<"oas/generator"/utf8>>,
function => <<"array_decoder"/utf8>>,
line => 475});
{inline, Inner@1} ->
case Inner@1 of
{boolean, _, _, _, _} ->
access(<<"decode"/utf8>>, <<"bool"/utf8>>);
{integer, _, _, _, _, _, _, _, _, _} ->
access(<<"decode"/utf8>>, <<"int"/utf8>>);
{number, _, _, _, _, _, _, _, _, _} ->
access(<<"decode"/utf8>>, <<"float"/utf8>>);
{string, _, _, _, _, _, _, _, _} ->
access(<<"decode"/utf8>>, <<"string"/utf8>>);
{null, _, _, _} ->
always_decode();
{array, _, _, _, Items@1, _, _, _, _} ->
array_decoder(Items@1, Module);
{object, _, _, _, _, _, _} ->
always_decode();
{all_of, _} ->
always_decode();
{any_of, _} ->
always_decode();
{one_of, _} ->
always_decode();
always_passes ->
dynamic_decode();
always_fails ->
always_decode()
end
end,
call1(<<"decode"/utf8>>, <<"list"/utf8>>, Exp).
-file("src/oas/generator.gleam", 384).
-spec schema_to_decoder(binary(), oas:schema(), gleam@option:option(binary())) -> list(glance:statement()).
schema_to_decoder(Name, Schema, Module) ->
Type_ = justin:pascal_case(Name),
case Schema of
{boolean, _, _, _, _} ->
[{expression, access(<<"decode"/utf8>>, <<"bool"/utf8>>)}];
{integer, _, _, _, _, _, _, _, _, _} ->
[{expression, access(<<"decode"/utf8>>, <<"int"/utf8>>)}];
{number, _, _, _, _, _, _, _, _, _} ->
[{expression, access(<<"decode"/utf8>>, <<"float"/utf8>>)}];
{string, _, _, _, _, _, _, _, _} ->
[{expression, access(<<"decode"/utf8>>, <<"string"/utf8>>)}];
{null, _, _, _} ->
[{expression, always_decode()}];
{array, _, _, _, Items, _, _, _, _} ->
[{expression, array_decoder(Items, Module)}];
{object, Properties, Required, _, _, _, _} ->
Properties@1 = maps:to_list(Properties),
{Fields, Cons} = begin
_pipe = gleam@list:map(
Properties@1,
fun(Property) ->
{Key, Schema@1} = Property,
Field_decoder = case Schema@1 of
{ref,
<<"#/components/schemas/"/utf8, S/binary>>,
_,
_} ->
case Module of
{some, M} ->
call0(M, decoder(S));
none ->
{call, {variable, decoder(S)}, []}
end;
{ref, _, _, _} ->
erlang:error(#{gleam_error => panic,
message => <<"unknown ref"/utf8>>,
module => <<"oas/generator"/utf8>>,
function => <<"schema_to_decoder"/utf8>>,
line => 405});
{inline, {boolean, _, _, _, _}} ->
access(<<"decode"/utf8>>, <<"bool"/utf8>>);
{inline, {integer, _, _, _, _, _, _, _, _, _}} ->
access(<<"decode"/utf8>>, <<"int"/utf8>>);
{inline, {number, _, _, _, _, _, _, _, _, _}} ->
access(<<"decode"/utf8>>, <<"float"/utf8>>);
{inline, {string, _, _, _, _, _, _, _, _}} ->
access(<<"decode"/utf8>>, <<"string"/utf8>>);
{inline, {null, _, _, _}} ->
always_decode();
{inline, {array, _, _, _, Items@1, _, _, _, _}} ->
array_decoder(Items@1, Module);
{inline, {object, _, _, _, _, _, _}} ->
always_decode();
{inline, {all_of, _}} ->
always_decode();
{inline, {any_of, _}} ->
always_decode();
{inline, {one_of, _}} ->
always_decode();
{inline, always_passes} ->
dynamic_decode();
{inline, always_fails} ->
always_decode()
end,
Is_optional = not gleam@list:contains(Required, Key)
orelse is_nullable(Schema@1),
{{use,
[{pattern_variable, var_name(Key)}],
case Is_optional of
false ->
call2(
<<"decode"/utf8>>,
<<"field"/utf8>>,
{string, Key},
Field_decoder
);
true ->
{call,
access(
<<"decode"/utf8>>,
<<"optional_field"/utf8>>
),
[{unlabelled_field, {string, Key}},
{unlabelled_field,
{variable, <<"None"/utf8>>}},
{unlabelled_field,
call1(
<<"decode"/utf8>>,
<<"optional"/utf8>>,
Field_decoder
)}]}
end},
{labelled_field,
var_name(Key),
{variable, var_name(Key)}}}
end
),
gleam@list:unzip(_pipe)
end,
Final = {expression,
call1(
<<"decode"/utf8>>,
<<"success"/utf8>>,
case erlang:length(Cons) of
0 ->
{variable, Type_};
_ ->
{call, {variable, Type_}, Cons}
end
)},
lists:append(Fields, [Final]);
{all_of, _} ->
[{expression, {panic, {some, {string, <<"AllOf"/utf8>>}}}}];
{any_of, _} ->
[{expression, {panic, {some, {string, <<"AnyOf"/utf8>>}}}}];
{one_of, _} ->
[{expression, {panic, {some, {string, <<"OneOf"/utf8>>}}}}];
always_passes ->
[{expression, dynamic_decode()}];
always_fails ->
[{expression,
{panic, {some, {string, <<"Alwoas.AlwaysFails"/utf8>>}}}}]
end.
-file("src/oas/generator.gleam", 370).
-spec schema_to_decode_fn({binary(), oas:schema()}) -> glance:function_().
schema_to_decode_fn(Entry) ->
{Name, Schema} = Entry,
Body = schema_to_decoder(Name, Schema, none),
{function, decoder(Name), public, [], none, Body, {span, 0, 0}}.
-file("src/oas/generator.gleam", 496).
-spec gen_schema(gleam@dict:dict(binary(), oas:schema())) -> {list(glance:custom_type()),
list(glance:type_alias()),
list(glance:function_())}.
gen_schema(Schemas) ->
gleam@dict:fold(
Schemas,
{[], [], []},
fun(Acc, Name, Schema) ->
{Custom_types, Type_aliases, Fns} = Acc,
Fns@1 = lists:append(
Fns,
[schema_to_encoder({Name, Schema}),
schema_to_decode_fn({Name, Schema})]
),
{Custom_types@1, Type_aliases@1} = case schema_to_type(Name, Schema) of
{error, Alias} ->
{Custom_types, [Alias | Type_aliases]};
{ok, Custom_type} ->
{[Custom_type | Custom_types], Type_aliases}
end,
{Custom_types@1, Type_aliases@1, Fns@1}
end
).
-file("src/oas/generator.gleam", 535).
-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@1 = gleam@list:map(
Parameters,
fun(P) ->
{Key, Required, Schema} = P,
Schema@1 = oas:fetch_schema(Schema, erlang:element(2, Components)),
Key@1 = begin
_pipe = safe_snake_case(Key),
escape_key(_pipe)
end,
Arg = {function_parameter, {some, Key@1}, {named, Key@1}, none},
Var = {variable, Key@1},
Mapper = case Schema@1 of
{boolean, _, _, _, _} ->
{some, access(<<"bool"/utf8>>, <<"to_string"/utf8>>)};
{integer, _, _, _, _, _, _, _, _, _} ->
{some, access(<<"int"/utf8>>, <<"to_string"/utf8>>)};
{number, _, _, _, _, _, _, _, _, _} ->
{some, access(<<"float"/utf8>>, <<"to_string"/utf8>>)};
{string, _, _, _, _, _, _, _, _} ->
none;
_ ->
{some, noop1(<<"query parameter is not supported"/utf8>>)}
end,
Value = case Required of
true ->
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} ->
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@1).
-file("src/oas/generator.gleam", 587).
-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, safe_snake_case(Name)},
none}}
end end).
-file("src/oas/generator.gleam", 616).
-spec gen_request_for_op(
{gleam@http:method(), oas:operation()},
binary(),
list(oas:ref(oas:parameter())),
oas:components()
) -> {glance:function_(), glance:function_()}.
gen_request_for_op(Op_entry, Pattern, Path_parameters, Components) ->
{Method, Op} = Op_entry,
{operation, _, _, _, Id, Op_parameters, _, _} = Op,
Id@1 = safe_snake_case(Id),
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 => 636})
end,
Body@1 = 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}}], _} ->
case Schema of
{ref,
<<"#/components/schemas/"/utf8, Name/binary>>,
_,
_} ->
Arg = safe_snake_case(Name),
Encode = call1(
<<"utils"/utf8>>,
<<"json_to_bits"/utf8>>,
call1(
<<"schema"/utf8>>,
encode_fn(Name),
{variable, Arg}
)
),
{some, {Arg, Encode}};
_ ->
{some,
{<<"data"/utf8>>, {variable, <<"data"/utf8>>}}}
end;
{[], []} ->
none;
{[], [{Unknown@1, _}]} ->
gleam_stdlib:println(
<<"unknown content type: "/utf8, Unknown@1/binary>>
),
none;
{_, _} ->
gleam_stdlib:println(
<<"multiple content types not supported"/utf8>>
),
none
end;
none ->
none
end,
Parameters@2 = begin
_pipe = path_args(Match@1),
_pipe@1 = lists:append(_pipe, case Body@1 of
{some, {Arg@1, _}} ->
[{function_parameter, none, {named, Arg@1}, none}];
none ->
[]
end),
lists:append(_pipe@1, Q_args)
end,
Op_request = <<Id@1/binary, "_request"/utf8>>,
Op_response = <<Id@1/binary, "_response"/utf8>>,
Req_fn = {function,
Op_request,
public,
[{function_parameter, none, {named, <<"base"/utf8>>}, none} |
Parameters@2],
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@1}} ->
[let_(<<"body"/utf8>>, Encode@1),
{expression,
pipe(
{variable, <<"base"/utf8>>},
pipe(
call1(
<<"utils"/utf8>>,
<<"set_method"/utf8>>,
{variable, <<"method"/utf8>>}
),
pipe(
call1(
<<"utils"/utf8>>,
<<"append_path"/utf8>>,
{variable, <<"path"/utf8>>}
),
pipe(
call1(
<<"utils"/utf8>>,
<<"set_query"/utf8>>,
{variable, <<"query"/utf8>>}
),
call2(
<<"utils"/utf8>>,
<<"set_body"/utf8>>,
{string,
<<"application/json"/utf8>>},
{variable, <<"body"/utf8>>}
)
)
)
)
)}];
none ->
[{expression,
pipe(
{variable, <<"base"/utf8>>},
pipe(
call1(
<<"utils"/utf8>>,
<<"set_method"/utf8>>,
{variable, <<"method"/utf8>>}
),
pipe(
call1(
<<"utils"/utf8>>,
<<"append_path"/utf8>>,
{variable, <<"path"/utf8>>}
),
call1(
<<"utils"/utf8>>,
<<"set_query"/utf8>>,
{variable, <<"query"/utf8>>}
)
)
)
)}]
end],
{span, 0, 0}},
Fn_ = {function,
Id@1,
public,
[{function_parameter, none, {named, <<"token"/utf8>>}, none} |
Parameters@2],
none,
[let_(
<<"request"/utf8>>,
{call,
{variable, <<"base_request"/utf8>>},
[{unlabelled_field, {variable, <<"token"/utf8>>}}]}
),
let_(
<<"request"/utf8>>,
{call,
access(<<"operations"/utf8>>, Op_request),
[{unlabelled_field, {variable, <<"request"/utf8>>}} |
gleam@list:map(
Parameters@2,
fun(P) ->
{function_parameter, _, {named, Name@1}, _} = case P of
{function_parameter, _, {named, _}, _} -> P;
_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 => 761}
)
end,
{unlabelled_field, {variable, Name@1}}
end
)]}
),
{use,
[{pattern_variable, <<"response"/utf8>>}],
call1(
<<"t"/utf8>>,
<<"do"/utf8>>,
call1(
<<"t"/utf8>>,
<<"fetch"/utf8>>,
{variable, <<"request"/utf8>>}
)
)},
{use,
[{pattern_variable, <<"data"/utf8>>}],
call1(
<<"t"/utf8>>,
<<"try"/utf8>>,
{call,
{variable, <<"handle_errors"/utf8>>},
[{unlabelled_field,
call1(
<<"operations"/utf8>>,
Op_response,
{variable, <<"response"/utf8>>}
)}]}
)},
{expression,
call1(
<<"t"/utf8>>,
<<"Done"/utf8>>,
{variable, <<"data"/utf8>>}
)}],
{span, 0, 0}},
{Fn_, Req_fn}.
-file("src/oas/generator.gleam", 816).
-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>>}}]},
pipe(_pipe, {variable, <<"Ok"/utf8>>})
end,
{Thing, none, false}.
-file("src/oas/generator.gleam", 825).
-spec gen_json_content_handling(binary(), oas:ref(oas:schema()), binary()) -> {glance:expression(),
gleam@option:option(glance:custom_type()),
boolean()}.
gen_json_content_handling(Operation_id, Schema, Wrapper) ->
{Resp_type@1, Decoder@1} = case Schema of
{inline,
{array,
_,
_,
_,
{ref, <<"#/components/schemas/"/utf8, Name/binary>>, _, _},
_,
_,
_,
_}} ->
{none,
call1(
<<"decode"/utf8>>,
<<"list"/utf8>>,
call0(
<<"schema"/utf8>>,
<<(safe_snake_case(Name))/binary, "_decoder"/utf8>>
)
)};
{ref, <<"#/components/schemas/"/utf8, Name@1/binary>>, _, _} ->
{none,
call0(
<<"schema"/utf8>>,
<<(safe_snake_case(Name@1))/binary, "_decoder"/utf8>>
)};
{inline, {object, Properties, Required, _, _, _, _} = Schema@1} ->
Name@2 = <<Operation_id/binary, "_response"/utf8>>,
Resp_type = gen_object(
maps:to_list(Properties),
Required,
justin:pascal_case(Name@2),
{some, <<"schema"/utf8>>}
),
Decoder = begin
_pipe = schema_to_decoder(
Name@2,
Schema@1,
{some, <<"schema"/utf8>>}
),
{block, _pipe}
end,
{{some, Resp_type}, Decoder};
{inline, always_passes} ->
{none, dynamic_decode()};
_ ->
{none,
call2(
<<"decode"/utf8>>,
<<"failure"/utf8>>,
{variable, <<"Nil"/utf8>>},
{string, <<"Unsupported schema"/utf8>>}
)}
end,
Action = begin
_pipe@1 = call2(
<<"json"/utf8>>,
<<"parse_bits"/utf8>>,
{variable, <<"body"/utf8>>},
Decoder@1
),
pipe(
_pipe@1,
call1(<<"result"/utf8>>, <<"map"/utf8>>, {variable, Wrapper})
)
end,
{Action, Resp_type@1, true}.
-file("src/oas/generator.gleam", 798).
-spec gen_content_handling(
binary(),
gleam@dict:dict(binary(), oas:media_type()),
binary()
) -> {glance:expression(), gleam@option:option(glance:custom_type()), boolean()}.
gen_content_handling(Operation_id, Content, Wrapper) ->
{Known, Unknown} = get_structure_json_media(Content),
case {Known, Unknown} of
{[{_, {media_type, Schema}}], _} ->
gen_json_content_handling(Operation_id, Schema, Wrapper);
{[], []} ->
just_return_ok_nil(Wrapper);
{[], [{Unknown@1, _}]} ->
gleam_stdlib:println(
<<"unknown content type: "/utf8, Unknown@1/binary>>
),
just_return_ok_nil(Wrapper);
{_, _} ->
gleam_stdlib:println(
<<"multiple content types not supported"/utf8>>
),
just_return_ok_nil(Wrapper)
end.
-file("src/oas/generator.gleam", 880).
-spec status_range(list({oas:status(), OKE}), integer(), integer()) -> list({integer(),
OKE}).
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", 890).
-spec gen_response({any(), oas:operation()}, oas:components()) -> {glance:function_(),
gleam@option:option(glance:custom_type())}.
gen_response(Operation, Components) ->
{_, 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} = 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>>
);
{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>>
);
{error, _} ->
Branch = begin
_pipe@1 = {variable, <<"response"/utf8>>},
_pipe@2 = pipe(
_pipe@1,
{variable, <<"Error"/utf8>>}
),
pipe(_pipe@2, {variable, <<"Ok"/utf8>>})
end,
{Branch, none, false}
end;
[] ->
Branch@1 = begin
_pipe@3 = {variable, <<"response"/utf8>>},
_pipe@4 = pipe(_pipe@3, {variable, <<"Error"/utf8>>}),
pipe(_pipe@4, {variable, <<"Ok"/utf8>>})
end,
{Branch@1, none, false}
end
end,
Default_clause = {clause,
[[{pattern_discard, <<""/utf8>>}]],
none,
Default_branch},
{Used@1, Response_type, Expected_clauses} = 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, []};
[{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} = gen_content_handling(
erlang:element(5, Op@1),
Content@2,
<<"Ok"/utf8>>
),
Clause = {clause,
[[{pattern_int, erlang:integer_to_binary(Status@3)}]],
none,
Branch@2},
{Used orelse U, Resp_type@1, [Clause]}
end,
Response_handler = {function,
<<(safe_snake_case(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}.
-file("src/oas/generator.gleam", 1018).
-spec gen_fns(binary(), oas:path_item(), oas:components(), list(binary())) -> list({{list(glance:function_()),
gleam@option:option(glance:custom_type())},
glance:function_()}).
gen_fns(Key, Path_item, Components, Exclude) ->
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(
Operations,
fun(Op@1) ->
{Fn_, Req_fn} = gen_request_for_op(
Op@1,
Key,
erlang:element(4, Path_item),
Components
),
{Response_handler, Response_type} = gen_response(Op@1, Components),
{{[Response_handler, Req_fn], Response_type}, Fn_}
end
).
-file("src/oas/generator.gleam", 1036).
-spec gen_ops({binary(), oas:path_item()}, oas:components(), list(binary())) -> list({{list(glance:function_()),
gleam@option:option(glance:custom_type())},
glance:function_()}).
gen_ops(Op, Components, Exclude) ->
{Key, Path} = Op,
gen_fns(Key, Path, Components, Exclude).
-file("src/oas/generator.gleam", 1042).
-spec defs(list(ONY)) -> list(glance:definition(ONY)).
defs(Xs) ->
gleam@list:map(Xs, fun(_capture) -> {definition, [], _capture} end).
-file("src/oas/generator.gleam", 1107).
-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)),
Fs = gleam@list:flat_map(
Paths,
fun(_capture) -> gen_ops(_capture, erlang:element(7, Spec), Exclude) end
),
{Operation_functions, Top} = gleam@list:unzip(Fs),
{Operation_functions@1, Operation_types} = gleam@list:unzip(
Operation_functions
),
Operation_types@1 = gleam@list:filter_map(
Operation_types,
fun(_capture@1) -> gleam@option:to_result(_capture@1, nil) end
),
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 = {module,
defs(
[{import,
<<"gleam/option"/utf8>>,
none,
[{unqualified_import, <<"Option"/utf8>>, none}],
[{unqualified_import, <<"None"/utf8>>, none}]} |
gleam@list:map(
Modules,
fun(_capture@2) ->
{import, _capture@2, none, [], []}
end
)]
),
defs(Operation_types@1),
[],
[],
gleam@list:map(
Operation_functions@2,
fun(_capture@3) -> {definition, [], _capture@3} end
)},
glance_printer:print(_pipe)
end,
Entry = begin
_pipe@1 = {module,
[],
[],
[],
[],
gleam@list:map(
Top,
fun(_capture@4) -> {definition, [], _capture@4} end
)},
glance_printer:print(_pipe@1)
end,
{Operations, Entry}.
-file("src/oas/generator.gleam", 1153).
-spec gen_schema_file(gleam@dict:dict(binary(), oas:schema()), binary()) -> binary().
gen_schema_file(Schemas, Provider) ->
{Custom_types, Type_aliases, Functions} = gen_schema(Schemas),
_pipe = {module,
[{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", 1190).
-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", 1180).
-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", 1046).
-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
).