Packages
oaspec
0.6.0
0.68.0
0.67.0
0.66.0
0.65.0
0.64.0
0.63.0
0.62.0
0.61.0
0.60.0
0.59.0
0.58.1
0.58.0
0.57.0
0.56.0
0.55.0
0.54.0
0.53.0
0.52.0
0.51.0
0.50.0
0.49.0
0.48.0
0.47.0
0.46.0
0.45.0
0.44.0
0.43.0
0.42.0
0.41.0
0.40.0
0.39.0
0.38.0
0.37.0
0.36.0
0.35.0
0.34.0
0.33.0
0.32.0
0.31.0
0.30.0
0.29.0
0.28.0
0.27.0
0.26.0
0.25.0
0.24.0
0.23.0
0.22.0
0.21.0
0.20.0
0.19.0
0.18.0
0.17.0
0.16.0
0.15.0
0.14.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.0
0.7.0
0.6.3
0.6.1
0.6.0
0.5.0
0.4.0
0.3.0
0.1.3
Generate Gleam code from OpenAPI 3.x specifications
Current section
Files
Jump to
Current section
Files
src/oaspec@codegen@ir_render.erl
-module(oaspec@codegen@ir_render).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oaspec/codegen/ir_render.gleam").
-export([render/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.
-file("src/oaspec/codegen/ir_render.gleam", 40).
?DOC(" Render a type definition to Gleam source.\n").
-spec render_type_def(
gleam@string_tree:string_tree(),
oaspec@codegen@ir:type_def()
) -> gleam@string_tree:string_tree().
render_type_def(Sb, Type_def) ->
case Type_def of
{type_alias, Name, Target} ->
_pipe = Sb,
_pipe@1 = oaspec@util@string_extra:line(
_pipe,
<<<<<<"pub type "/utf8, Name/binary>>/binary, " = "/utf8>>/binary,
Target/binary>>
),
oaspec@util@string_extra:blank_line(_pipe@1);
{record_type, Name@1, Fields} ->
Sb@1 = begin
_pipe@2 = Sb,
oaspec@util@string_extra:line(
_pipe@2,
<<<<"pub type "/utf8, Name@1/binary>>/binary, " {"/utf8>>
)
end,
Field_strs = gleam@list:map(
Fields,
fun(F) ->
<<<<(erlang:element(2, F))/binary, ": "/utf8>>/binary,
(erlang:element(3, F))/binary>>
end
),
Sb@2 = begin
_pipe@3 = Sb@1,
oaspec@util@string_extra:indent(
_pipe@3,
1,
<<<<<<Name@1/binary, "("/utf8>>/binary,
(gleam@string:join(Field_strs, <<", "/utf8>>))/binary>>/binary,
")"/utf8>>
)
end,
_pipe@4 = Sb@2,
_pipe@5 = oaspec@util@string_extra:line(_pipe@4, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@5);
{union_type, Name@2, Variants} ->
Sb@3 = begin
_pipe@6 = Sb,
oaspec@util@string_extra:line(
_pipe@6,
<<<<"pub type "/utf8, Name@2/binary>>/binary, " {"/utf8>>
)
end,
Sb@5 = gleam@list:fold(
Variants,
Sb@3,
fun(Sb@4, Variant) -> case Variant of
{variant_with_type, Vname, Inner_type} ->
_pipe@7 = Sb@4,
oaspec@util@string_extra:indent(
_pipe@7,
1,
<<<<<<Vname/binary, "("/utf8>>/binary,
Inner_type/binary>>/binary,
")"/utf8>>
);
{variant_empty, Vname@1} ->
_pipe@8 = Sb@4,
oaspec@util@string_extra:indent(_pipe@8, 1, Vname@1)
end end
),
_pipe@9 = Sb@5,
_pipe@10 = oaspec@util@string_extra:line(_pipe@9, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@10);
{enum_type, Name@3, Variants@1} ->
Sb@6 = begin
_pipe@11 = Sb,
oaspec@util@string_extra:line(
_pipe@11,
<<<<"pub type "/utf8, Name@3/binary>>/binary, " {"/utf8>>
)
end,
Sb@8 = gleam@list:fold(
Variants@1,
Sb@6,
fun(Sb@7, Variant_name) -> _pipe@12 = Sb@7,
oaspec@util@string_extra:indent(_pipe@12, 1, Variant_name) end
),
_pipe@13 = Sb@8,
_pipe@14 = oaspec@util@string_extra:line(_pipe@13, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@14)
end.
-file("src/oaspec/codegen/ir_render.gleam", 28).
?DOC(" Render a single declaration (doc comment + type definition).\n").
-spec render_declaration(
gleam@string_tree:string_tree(),
oaspec@codegen@ir:declaration()
) -> gleam@string_tree:string_tree().
render_declaration(Sb, Decl) ->
Sb@1 = case erlang:element(2, Decl) of
{some, Doc} ->
_pipe = Sb,
oaspec@util@string_extra:doc_comment(_pipe, Doc);
none ->
Sb
end,
render_type_def(Sb@1, erlang:element(3, Decl)).
-file("src/oaspec/codegen/ir_render.gleam", 14).
?DOC(" Render a complete IR Module to a Gleam source string.\n").
-spec render(oaspec@codegen@ir:module_()) -> binary().
render(Module) ->
Sb = begin
_pipe = oaspec@util@string_extra:file_header(<<"0.6.0"/utf8>>),
oaspec@util@string_extra:imports(_pipe, erlang:element(3, Module))
end,
Sb@2 = gleam@list:fold(
erlang:element(4, Module),
Sb,
fun(Sb@1, Decl) -> render_declaration(Sb@1, Decl) end
),
oaspec@util@string_extra:to_string(Sb@2).