Current section
Files
Jump to
Current section
Files
src/oas@generator@schema.erl
-module(oas@generator@schema).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oas/generator/schema.gleam").
-export([custom_type/4, encode_fn/1, to_decoder/1, gen_top_decoder_needs_name/2, to_decode_fn/1, to_encoder/1, fields_to_encode_body/3, do_to_encode_fn/3, to_encode_fn/1, generate/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" This module builds ASTs from schemas.\n"
" It does not do any file generation\n"
).
-file("src/oas/generator/schema.gleam", 121).
?DOC(
" Generate type aliases from OpenAPI lifted schemas.\n"
" All objects are converted to names\n"
).
-spec to_type(oas@generator@lift:schema(oas@generator@lift:content_id())) -> oas@generator@lookup:lookup(glance:type()).
to_type(Lifted) ->
case Lifted of
{named, Ref} ->
{lookup,
Ref,
fun(Mod, Name) ->
{done,
{named_type,
oas@generator@ast:name_for_gleam_type(Name),
Mod,
[]}}
end};
{primitive, Primitive} ->
_pipe = case Primitive 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, []};
always ->
{named_type, <<"Any"/utf8>>, {some, <<"utils"/utf8>>}, []};
never ->
{named_type, <<"Never"/utf8>>, {some, <<"utils"/utf8>>}, []}
end,
{done, _pipe};
{array, Items} ->
oas@generator@lookup:then(
to_type(Items),
fun(Items@1) ->
{done, {named_type, <<"List"/utf8>>, none, [Items@1]}}
end
);
{tuple, Items@2} ->
oas@generator@lookup:then(
oas@generator@lookup:seq(gleam@list:map(Items@2, fun to_type/1)),
fun(Items@3) -> {done, {tuple_type, Items@3}} end
);
{compound, Id} ->
Type_ = oas@generator@lift:content_id_to_type(Id),
{done, {named_type, Type_, none, []}};
{dictionary, Values} ->
oas@generator@lookup:then(
to_type(Values),
fun(Values@1) ->
{done,
{named_type,
<<"Dict"/utf8>>,
{some, <<"dict"/utf8>>},
[{named_type, <<"String"/utf8>>, none, []},
Values@1]}}
end
);
unsupported ->
{done, {named_type, <<"Any"/utf8>>, {some, <<"utils"/utf8>>}, []}}
end.
-file("src/oas/generator/schema.gleam", 76).
?DOC(
" Generate a custom type from an object type.\n"
" TODO make private\n"
).
-spec custom_type(
binary(),
list({binary(),
{oas@generator@lift:schema(oas@generator@lift:content_id()), boolean()}}),
gleam@option:option(oas@generator@lift:schema(oas@generator@lift:content_id())),
list(binary())
) -> oas@generator@lookup:lookup(glance:custom_type()).
custom_type(Name, Properties, Additional, Required) ->
oas@generator@lookup:then(
oas@generator@lookup:seq(
gleam@list:map(
Properties,
fun(Property) ->
{Key, {Schema, Nullable}} = Property,
oas@generator@lookup:then(
to_type(Schema),
fun(Type_) ->
_pipe = {labelled_variant_field,
case not gleam@list:contains(Required, Key)
orelse Nullable of
false ->
Type_;
true ->
{named_type,
<<"Option"/utf8>>,
none,
[Type_]}
end,
oas@generator@ast:name_for_gleam_field_or_var(
Key
)},
{done, _pipe}
end
)
end
)
),
fun(Fields) -> oas@generator@lookup:then(case Additional of
{some, Schema@1} ->
Key@1 = <<"additionalProperties"/utf8>>,
oas@generator@lookup:then(
to_type(Schema@1),
fun(Type_@1) ->
Extra = {labelled_variant_field,
{named_type,
<<"Dict"/utf8>>,
{some, <<"dict"/utf8>>},
[{named_type,
<<"String"/utf8>>,
none,
[]},
Type_@1]},
oas@generator@ast:name_for_gleam_field_or_var(
Key@1
)},
{done, lists:append(Fields, [Extra])}
end
);
none ->
{done, Fields}
end, fun(Fields@1) ->
Name@1 = oas@generator@ast:name_for_gleam_type(Name),
{done,
{custom_type,
Name@1,
public,
false,
[],
[{variant, Name@1, Fields@1}]}}
end) end
).
-file("src/oas/generator/schema.gleam", 363).
-spec encode_fn(binary()) -> binary().
encode_fn(Name) ->
oas@generator@ast:name_for_gleam_field_or_var(
<<Name/binary, "_encode"/utf8>>
).
-file("src/oas/generator/schema.gleam", 566).
-spec decoder(binary()) -> binary().
decoder(Name) ->
oas@generator@ast:name_for_gleam_field_or_var(
<<Name/binary, "_decoder"/utf8>>
).
-file("src/oas/generator/schema.gleam", 571).
?DOC(" Not used anywhere else\n").
-spec alias(binary(), glance:type()) -> glance:type_alias().
alias(To, Type_) ->
{type_alias, To, public, [], Type_}.
-file("src/oas/generator/schema.gleam", 575).
-spec always_decode() -> glance:expression().
always_decode() ->
oas@generator@ast: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/schema.gleam", 590).
-spec never_decode() -> glance:expression().
never_decode() ->
oas@generator@ast:call2(
<<"decode"/utf8>>,
<<"new_primitive_decoder"/utf8>>,
{string, <<"Never"/utf8>>},
{fn,
[{fn_parameter, {discarded, <<""/utf8>>}, none}],
none,
[{expression,
{panic,
{some,
{string,
<<"tried to decode a never decode value"/utf8>>}}}}]}
).
-file("src/oas/generator/schema.gleam", 502).
-spec to_decoder(oas@generator@lift:schema(oas@generator@lift:content_id())) -> oas@generator@lookup:lookup(glance:expression()).
to_decoder(Lifted) ->
case Lifted of
{named, Ref} ->
{lookup,
Ref,
fun(Mod, Name) ->
Func = oas@generator@ast:name_for_gleam_field_or_var(
<<Name/binary, "_decoder"/utf8>>
),
_pipe = case Mod of
none ->
{call, {variable, Func}, []};
{some, Module} ->
oas@generator@ast:call0(Module, Func)
end,
{done, _pipe}
end};
{primitive, Primitive} ->
_pipe@1 = case Primitive of
boolean ->
oas@generator@ast:access(<<"decode"/utf8>>, <<"bool"/utf8>>);
integer ->
oas@generator@ast:access(<<"decode"/utf8>>, <<"int"/utf8>>);
number ->
oas@generator@ast:access(
<<"decode"/utf8>>,
<<"float"/utf8>>
);
string ->
oas@generator@ast:access(
<<"decode"/utf8>>,
<<"string"/utf8>>
);
null ->
always_decode();
always ->
oas@generator@ast:call0(
<<"utils"/utf8>>,
<<"any_decoder"/utf8>>
);
never ->
never_decode()
end,
{done, _pipe@1};
{tuple, Items} ->
oas@generator@lookup:then(
oas@generator@lookup:seq(
gleam@list:index_map(
Items,
fun(Item, Index) ->
oas@generator@lookup:then(
to_decoder(Item),
fun(Item@1) ->
_pipe@2 = {use,
[{pattern_variable,
<<"e"/utf8,
(erlang:integer_to_binary(
Index
))/binary>>}],
oas@generator@ast:call1(
<<"decode"/utf8>>,
<<"then"/utf8>>,
Item@1
)},
{done, _pipe@2}
end
)
end
)
),
fun(Uses) ->
Vars = gleam@list:index_map(
Items,
fun(_, Index@1) ->
{variable,
<<"e"/utf8,
(erlang:integer_to_binary(Index@1))/binary>>}
end
),
_pipe@3 = {block,
lists:append(
Uses,
[{expression,
oas@generator@ast:call1(
<<"decode"/utf8>>,
<<"success"/utf8>>,
{tuple, Vars}
)}]
)},
{done, _pipe@3}
end
);
{array, Items@1} ->
oas@generator@lookup:then(
to_decoder(Items@1),
fun(Items@2) ->
_pipe@4 = oas@generator@ast:call1(
<<"decode"/utf8>>,
<<"list"/utf8>>,
Items@2
),
{done, _pipe@4}
end
);
{compound, Id} ->
Func@1 = <<(oas@generator@lift:content_id_to_fn_prefix(Id))/binary,
"_decoder"/utf8>>,
_pipe@5 = {call, {variable, Func@1}, []},
{done, _pipe@5};
{dictionary, Values} ->
oas@generator@lookup:then(
to_decoder(Values),
fun(Values@1) ->
_pipe@6 = oas@generator@ast:call2(
<<"decode"/utf8>>,
<<"dict"/utf8>>,
oas@generator@ast:access(
<<"decode"/utf8>>,
<<"string"/utf8>>
),
Values@1
),
{done, _pipe@6}
end
);
unsupported ->
_pipe@7 = oas@generator@ast:call0(
<<"utils"/utf8>>,
<<"any_decoder"/utf8>>
),
{done, _pipe@7}
end.
-file("src/oas/generator/schema.gleam", 384).
-spec gen_top_decoder_needs_name(
binary(),
oas@generator@lift:schema(oas@generator@lift:fields())
) -> oas@generator@lookup:lookup(list(glance:statement())).
gen_top_decoder_needs_name(Name, Top) ->
case Top of
{named, N} ->
oas@generator@lookup:then(
to_decoder({named, N}),
fun(Exp) -> _pipe = [{expression, Exp}],
{done, _pipe} end
);
{primitive, P} ->
oas@generator@lookup:then(
to_decoder({primitive, P}),
fun(Exp@1) -> _pipe@1 = [{expression, Exp@1}],
{done, _pipe@1} end
);
{array, Items} ->
oas@generator@lookup:then(
to_decoder({array, Items}),
fun(Items@1) -> _pipe@2 = [{expression, Items@1}],
{done, _pipe@2} end
);
{tuple, Items@2} ->
oas@generator@lookup:then(
to_decoder({tuple, Items@2}),
fun(Items@3) -> _pipe@3 = case Items@3 of
{block, Statements} ->
Statements;
Other ->
[{expression, Other}]
end,
{done, _pipe@3} end
);
{compound, {fields, Properties, Additional, Required}} ->
Type_ = oas@generator@ast:name_for_gleam_type(Name),
oas@generator@lookup:then(
oas@generator@lookup:seq(
gleam@list:map(
Properties,
fun(Property) ->
{Key, {Schema, Nullable}} = Property,
Is_optional = not gleam@list:contains(Required, Key)
orelse Nullable,
oas@generator@lookup:then(
to_decoder(Schema),
fun(Field_decoder) ->
_pipe@4 = {{use,
[{pattern_variable,
oas@generator@ast:name_for_gleam_field_or_var(
Key
)}],
case Is_optional of
false ->
oas@generator@ast:call2(
<<"decode"/utf8>>,
<<"field"/utf8>>,
{string, Key},
Field_decoder
);
true ->
{call,
oas@generator@ast:access(
<<"decode"/utf8>>,
<<"optional_field"/utf8>>
),
[{unlabelled_field,
{string, Key}},
{unlabelled_field,
{variable,
<<"None"/utf8>>}},
{unlabelled_field,
oas@generator@ast:call1(
<<"decode"/utf8>>,
<<"optional"/utf8>>,
Field_decoder
)}]}
end},
{labelled_field,
oas@generator@ast:name_for_gleam_field_or_var(
Key
),
{variable,
oas@generator@ast:name_for_gleam_field_or_var(
Key
)}}},
{done, _pipe@4}
end
)
end
)
),
fun(Zipped) -> oas@generator@lookup:then(case Additional of
{some, Schema@1} ->
oas@generator@lookup:then(
to_decoder(Schema@1),
fun(Schema@2) ->
Key@1 = <<"additionalProperties"/utf8>>,
_pipe@5 = lists:append(
Zipped,
[{{use,
[{pattern_variable,
oas@generator@ast:name_for_gleam_field_or_var(
Key@1
)}],
oas@generator@ast:call2(
<<"utils"/utf8>>,
<<"decode_additional"/utf8>>,
{list,
gleam@list:map(
Properties,
fun(P@1) ->
{K, _} = P@1,
{string,
K}
end
),
none},
Schema@2
)},
{labelled_field,
oas@generator@ast:name_for_gleam_field_or_var(
Key@1
),
{variable,
oas@generator@ast:name_for_gleam_field_or_var(
Key@1
)}}}]
),
{done, _pipe@5}
end
);
none ->
_pipe@6 = Zipped,
{done, _pipe@6}
end, fun(Zipped@1) ->
{Fields, Cons} = gleam@list:unzip(Zipped@1),
Final = {expression,
oas@generator@ast:call1(
<<"decode"/utf8>>,
<<"success"/utf8>>,
case erlang:length(Cons) of
0 ->
{variable, Type_};
_ ->
{call, {variable, Type_}, Cons}
end
)},
_pipe@7 = lists:append(Fields, [Final]),
{done, _pipe@7}
end) end
);
{dictionary, Values} ->
oas@generator@lookup:then(
to_decoder({dictionary, Values}),
fun(Exp@2) -> _pipe@8 = [{expression, Exp@2}],
{done, _pipe@8} end
);
unsupported ->
oas@generator@lookup:then(
to_decoder(unsupported),
fun(Exp@3) -> _pipe@9 = [{expression, Exp@3}],
{done, _pipe@9} end
)
end.
-file("src/oas/generator/schema.gleam", 368).
-spec to_decode_fn(
{binary(), oas@generator@lift:schema(oas@generator@lift:fields())}
) -> oas@generator@lookup:lookup(glance:function_()).
to_decode_fn(Entry) ->
{Name, Top} = Entry,
oas@generator@lookup:then(
gen_top_decoder_needs_name(Name, Top),
fun(Body) ->
_pipe = {function,
decoder(Name),
public,
[],
none,
Body,
{span, 0, 0}},
{done, _pipe}
end
).
-file("src/oas/generator/schema.gleam", 348).
-spec encode_tuple_body(
list(oas@generator@lift:schema(oas@generator@lift:content_id())),
glance:expression()
) -> oas@generator@lookup:lookup(glance:expression()).
encode_tuple_body(Items, Arg) ->
oas@generator@lookup:then(
oas@generator@lookup:seq(
gleam@list:index_map(
Items,
fun(Item, Index) ->
oas@generator@lookup:then(
to_encoder(Item),
fun(Item@1) ->
_pipe = {call,
Item@1,
[{unlabelled_field, {tuple_index, Arg, Index}}]},
{done, _pipe}
end
)
end
)
),
fun(Items@1) ->
_pipe@1 = oas@generator@ast:call1(
<<"utils"/utf8>>,
<<"merge"/utf8>>,
{list, Items@1, none}
),
{done, _pipe@1}
end
).
-file("src/oas/generator/schema.gleam", 285).
?DOC(" This handles a lifted encoder\n").
-spec to_encoder(oas@generator@lift:schema(oas@generator@lift:content_id())) -> oas@generator@lookup:lookup(glance:expression()).
to_encoder(Lifted) ->
case Lifted of
{named, Ref} ->
{lookup,
Ref,
fun(Mod, Name) ->
Func = encode_fn(Name),
_pipe = case Mod of
none ->
{variable, Func};
{some, Module} ->
oas@generator@ast:access(Module, Func)
end,
{done, _pipe}
end};
{primitive, boolean} ->
_pipe@1 = oas@generator@ast:access(<<"json"/utf8>>, <<"bool"/utf8>>),
{done, _pipe@1};
{primitive, integer} ->
_pipe@2 = oas@generator@ast:access(<<"json"/utf8>>, <<"int"/utf8>>),
{done, _pipe@2};
{primitive, number} ->
_pipe@3 = oas@generator@ast:access(
<<"json"/utf8>>,
<<"float"/utf8>>
),
{done, _pipe@3};
{primitive, string} ->
_pipe@4 = oas@generator@ast:access(
<<"json"/utf8>>,
<<"string"/utf8>>
),
{done, _pipe@4};
{primitive, null} ->
_pipe@5 = {fn,
[{fn_parameter,
{discarded, <<""/utf8>>},
{some, {named_type, <<"Nil"/utf8>>, none, []}}}],
none,
[{expression,
oas@generator@ast:call0(
<<"json"/utf8>>,
<<"null"/utf8>>
)}]},
{done, _pipe@5};
{primitive, always} ->
_pipe@6 = oas@generator@ast:access(
<<"utils"/utf8>>,
<<"any_to_json"/utf8>>
),
{done, _pipe@6};
{primitive, never} ->
_pipe@7 = {fn,
[{fn_parameter, {discarded, <<"data"/utf8>>}, none}],
none,
[{expression,
{panic,
{some,
{string,
<<"never value cannot be encoded"/utf8>>}}}}]},
{done, _pipe@7};
{array, Items} ->
oas@generator@lookup:then(
to_encoder(Items),
fun(Items@1) ->
_pipe@8 = {fn_capture,
none,
oas@generator@ast:access(
<<"json"/utf8>>,
<<"array"/utf8>>
),
[],
[{unlabelled_field, Items@1}]},
{done, _pipe@8}
end
);
{tuple, Items@2} ->
oas@generator@lookup:then(
encode_tuple_body(Items@2, {variable, <<"data"/utf8>>}),
fun(Exp) ->
_pipe@9 = {fn,
[{fn_parameter, {named, <<"data"/utf8>>}, none}],
none,
[{expression, Exp}]},
{done, _pipe@9}
end
);
{compound, Some_name} ->
_pipe@10 = {variable,
<<(oas@generator@lift:content_id_to_fn_prefix(Some_name))/binary,
"_encode"/utf8>>},
{done, _pipe@10};
{dictionary, Values} ->
oas@generator@lookup:then(
to_encoder(Values),
fun(Values@1) ->
_pipe@11 = {fn_capture,
none,
oas@generator@ast:access(
<<"utils"/utf8>>,
<<"dict"/utf8>>
),
[],
[{unlabelled_field, Values@1}]},
{done, _pipe@11}
end
);
unsupported ->
_pipe@12 = oas@generator@ast:access(
<<"utils"/utf8>>,
<<"any_to_json"/utf8>>
),
{done, _pipe@12}
end.
-file("src/oas/generator/schema.gleam", 233).
-spec fields_to_encode_body(
list({binary(),
{oas@generator@lift:schema(oas@generator@lift:content_id()), boolean()}}),
list(binary()),
gleam@option:option(oas@generator@lift:schema(oas@generator@lift:content_id()))
) -> oas@generator@lookup:lookup(glance:expression()).
fields_to_encode_body(Properties, Required, Additional) ->
oas@generator@lookup:then(case Additional of
{some, Values} ->
oas@generator@lookup:then(
to_encoder(Values),
fun(Values@1) ->
_pipe = {some,
oas@generator@ast:call1(
<<"dict"/utf8>>,
<<"to_list"/utf8>>,
oas@generator@ast:call2(
<<"dict"/utf8>>,
<<"map_values"/utf8>>,
oas@generator@ast:access(
<<"data"/utf8>>,
<<"additional_properties"/utf8>>
),
{fn,
[{fn_parameter,
{discarded, <<"key"/utf8>>},
none},
{fn_parameter,
{named, <<"value"/utf8>>},
none}],
none,
[{expression,
{call,
Values@1,
[{unlabelled_field,
{variable,
<<"value"/utf8>>}}]}}]}
)
)},
{done, _pipe}
end
);
none ->
{done, none}
end, fun(Additional@1) ->
oas@generator@lookup:then(
oas@generator@lookup:seq(
gleam@list:map(
Properties,
fun(Property) ->
{Key, {Schema, Nullable}} = Property,
Arg = oas@generator@ast:access(
<<"data"/utf8>>,
oas@generator@ast:name_for_gleam_field_or_var(
Key
)
),
oas@generator@lookup:then(
to_encoder(Schema),
fun(Cast) ->
Value = case not gleam@list:contains(
Required,
Key
)
orelse Nullable of
false ->
{call,
Cast,
[{unlabelled_field, Arg}]};
true ->
oas@generator@ast:call2(
<<"json"/utf8>>,
<<"nullable"/utf8>>,
Arg,
Cast
)
end,
{done, {tuple, [{string, Key}, Value]}}
end
)
end
)
),
fun(Properties@1) ->
_pipe@1 = oas@generator@ast:call1(
<<"utils"/utf8>>,
<<"object"/utf8>>,
{list, Properties@1, Additional@1}
),
{done, _pipe@1}
end
)
end).
-file("src/oas/generator/schema.gleam", 172).
-spec do_to_encode_fn(
binary(),
binary(),
oas@generator@lift:schema(oas@generator@lift:fields())
) -> oas@generator@lookup:lookup(glance:function_()).
do_to_encode_fn(Name, Type_, Top) ->
Arg = {variable, <<"data"/utf8>>},
oas@generator@lookup:then(case Top of
{named, N} ->
oas@generator@lookup:then(
to_encoder({named, N}),
fun(Top@1) ->
_pipe = {call, Top@1, [{unlabelled_field, Arg}]},
{done, _pipe}
end
);
{primitive, null} ->
_pipe@1 = oas@generator@ast:call0(
<<"json"/utf8>>,
<<"null"/utf8>>
),
{done, _pipe@1};
{primitive, never} ->
_pipe@2 = {panic,
{some, {string, <<"never value cannot be encoded"/utf8>>}}},
{done, _pipe@2};
{primitive, P} ->
oas@generator@lookup:then(
to_encoder({primitive, P}),
fun(Top@2) ->
_pipe@3 = {call, Top@2, [{unlabelled_field, Arg}]},
{done, _pipe@3}
end
);
{array, Items} ->
oas@generator@lookup:then(
to_encoder(Items),
fun(Items@1) ->
_pipe@4 = oas@generator@ast:call2(
<<"json"/utf8>>,
<<"array"/utf8>>,
Arg,
Items@1
),
{done, _pipe@4}
end
);
{tuple, Items@2} ->
encode_tuple_body(Items@2, Arg);
{compound, {fields, Properties, Additional, Required}} ->
fields_to_encode_body(Properties, Required, Additional);
{dictionary, Values} ->
oas@generator@lookup:then(
to_encoder(Values),
fun(Values@1) ->
_pipe@5 = oas@generator@ast:call2(
<<"utils"/utf8>>,
<<"dict"/utf8>>,
Arg,
Values@1
),
{done, _pipe@5}
end
);
unsupported ->
oas@generator@lookup:then(
to_encoder(unsupported),
fun(Top@3) ->
_pipe@6 = {call, Top@3, [{unlabelled_field, Arg}]},
{done, _pipe@6}
end
)
end, fun(Exp) ->
Ignored = case Top of
{primitive, null} ->
true;
{primitive, never} ->
true;
{compound, {fields, Properties@1, none, _}} ->
gleam@list:is_empty(Properties@1);
_ ->
false
end,
Assignment = case Ignored of
true ->
{discarded, <<"data"/utf8>>};
false ->
{named, <<"data"/utf8>>}
end,
_pipe@7 = {function,
encode_fn(Name),
public,
[{function_parameter,
none,
Assignment,
{some, {named_type, Type_, none, []}}}],
none,
[{expression, Exp}],
{span, 0, 0}},
{done, _pipe@7}
end).
-file("src/oas/generator/schema.gleam", 166).
?DOC(
" This handles top to encoder\n"
" TODO make this private\n"
).
-spec to_encode_fn(
{binary(), oas@generator@lift:schema(oas@generator@lift:fields())}
) -> oas@generator@lookup:lookup(glance:function_()).
to_encode_fn(Entry) ->
{Name, Top} = Entry,
Type_ = oas@generator@ast:name_for_gleam_type(Name),
do_to_encode_fn(Name, Type_, Top).
-file("src/oas/generator/schema.gleam", 15).
?DOC(" From a dictionary of schemas generate all custom types, type aliases, encoders and decoders.\n").
-spec generate(gleam@dict:dict(binary(), oas@json_schema:schema())) -> oas@generator@lookup:lookup({list(glance:custom_type()),
list(glance:type_alias()),
list(glance:function_())}).
generate(Schemas) ->
{Internal, Named} = gleam@list:map_fold(
begin
_pipe = Schemas,
maps:to_list(_pipe)
end,
[],
fun(Acc, Entry) ->
{Name, Schema} = Entry,
{Top, _, Acc@1} = oas@generator@lift:do_lift({inline, Schema}, Acc),
{Acc@1, {Name, Top}}
end
),
Named@1 = lists:append(
Named,
begin
_pipe@1 = Internal,
_pipe@2 = gleam@list:unique(_pipe@1),
_pipe@3 = lists:reverse(_pipe@2),
gleam@list:map(
_pipe@3,
fun(Entry@1) ->
{Hash, Fields} = Entry@1,
{oas@generator@lift:content_id_to_fn_prefix(Hash),
{compound, Fields}}
end
)
end
),
oas@generator@lookup:fold(
Named@1,
{[], [], []},
fun(Acc@2, Entry@2) ->
{Name@1, Top@1} = Entry@2,
{Custom_types, Type_aliases, Fns} = Acc@2,
oas@generator@lookup:then(
to_encode_fn({Name@1, Top@1}),
fun(Enc_fn) ->
oas@generator@lookup:then(
to_decode_fn({Name@1, Top@1}),
fun(Dec_fn) ->
Fns@1 = lists:append(Fns, [Enc_fn, Dec_fn]),
Name@2 = oas@generator@ast:name_for_gleam_type(
Name@1
),
oas@generator@lookup:then(case Top@1 of
{named, Name@3} ->
oas@generator@lookup:then(
to_type({named, Name@3}),
fun(Type_) ->
{done,
{Custom_types,
[alias(Name@3, Type_) |
Type_aliases]}}
end
);
{primitive, Primitive} ->
oas@generator@lookup:then(
to_type({primitive, Primitive}),
fun(Type_@1) ->
{done,
{Custom_types,
[alias(Name@2, Type_@1) |
Type_aliases]}}
end
);
{array, Items} ->
oas@generator@lookup:then(
to_type({array, Items}),
fun(Type_@2) ->
{done,
{Custom_types,
[alias(Name@2, Type_@2) |
Type_aliases]}}
end
);
{tuple, Items@1} ->
oas@generator@lookup:then(
to_type({tuple, Items@1}),
fun(Type_@3) ->
{done,
{Custom_types,
[alias(Name@2, Type_@3) |
Type_aliases]}}
end
);
{compound,
{fields,
Properties,
Additional,
Required}} ->
oas@generator@lookup:then(
custom_type(
Name@2,
Properties,
Additional,
Required
),
fun(Type_@4) ->
{done,
{[Type_@4 | Custom_types],
Type_aliases}}
end
);
{dictionary, Values} ->
oas@generator@lookup:then(
to_type({dictionary, Values}),
fun(Type_@5) ->
{done,
{Custom_types,
[alias(Name@2, Type_@5) |
Type_aliases]}}
end
);
unsupported ->
oas@generator@lookup:then(
to_type(unsupported),
fun(Type_@6) ->
{done,
{Custom_types,
[alias(Name@2, Type_@6) |
Type_aliases]}}
end
)
end, fun(_use0) ->
{Custom_types@1, Type_aliases@1} = _use0,
{done,
{Custom_types@1, Type_aliases@1, Fns@1}}
end)
end
)
end
)
end
).