-module(gilly@internal@codegen). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/gilly/internal/codegen.gleam"). -export([generate_schemas/2, generate/2, generate_operations/2]). -export_type([optionality/0, config/0, state/0]). -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(false). -type optionality() :: required_only | nullable_only | required_and_nullable. -type config() :: {config, optionality(), integer(), boolean()}. -type state() :: {state, gleam@dict:dict(binary(), gleam@set:set(binary())), gleam@dict:dict(binary(), list(binary()))}. -file("src/gilly/internal/codegen.gleam", 46). ?DOC(false). -spec default_state() -> state(). default_state() -> {state, maps:new(), maps:new()}. -file("src/gilly/internal/codegen.gleam", 50). ?DOC(false). -spec register_enum(state(), binary(), list(binary())) -> state(). register_enum(State, Type_name, Variants) -> {state, erlang:element(2, State), gleam@dict:insert(erlang:element(3, State), Type_name, Variants)}. -file("src/gilly/internal/codegen.gleam", 58). ?DOC(false). -spec import_qualified(state(), binary(), binary()) -> state(). import_qualified(State, Module, Imported) -> Imports = gleam@dict:upsert( erlang:element(2, State), Module, fun(Existing) -> case Existing of {some, Values} -> gleam@set:insert(Values, Imported); none -> gleam@set:from_list([Imported]) end end ), {state, Imports, erlang:element(3, State)}. -file("src/gilly/internal/codegen.gleam", 69). ?DOC(false). -spec import_module(state(), binary()) -> state(). import_module(State, Module) -> Imports = case gleam@dict:has_key(erlang:element(2, State), Module) of true -> erlang:element(2, State); false -> gleam@dict:insert(erlang:element(2, State), Module, gleam@set:new()) end, {state, Imports, erlang:element(3, State)}. -file("src/gilly/internal/codegen.gleam", 77). ?DOC(false). -spec imports_doc(state(), integer()) -> glam@doc:document(). imports_doc(State, Indent) -> Sorted_imports = begin _pipe = maps:to_list(erlang:element(2, State)), gleam@list:sort( _pipe, fun(A, B) -> gleam@string:compare(erlang:element(1, A), erlang:element(1, B)) end ) end, _pipe@6 = gleam@list:map( Sorted_imports, fun(Entry) -> {Module, Qualified} = Entry, Import_line = glam@doc:from_string( <<"import "/utf8, Module/binary>> ), case gleam@set:to_list(Qualified) of [] -> Import_line; Values -> Values_doc = begin _pipe@1 = gleam@list:sort( Values, fun gleam@string:compare/2 ), _pipe@2 = gleam@list:map( _pipe@1, fun glam@doc:from_string/1 ), _pipe@3 = glam@doc:join( _pipe@2, glam@doc:break(<<", "/utf8>>, <<","/utf8>>) ), glam@doc:group(_pipe@3) end, glam@doc:concat( [Import_line, glam@doc:from_string(<<".{"/utf8>>), begin _pipe@4 = glam@doc:concat( [{break, <<""/utf8>>, <<""/utf8>>}, Values_doc] ), _pipe@5 = glam@doc:group(_pipe@4), glam@doc:nest(_pipe@5, Indent) end, {break, <<""/utf8>>, <<""/utf8>>}, glam@doc:from_string(<<"}"/utf8>>)] ) end end ), glam@doc:join(_pipe@6, {line, 1}). -file("src/gilly/internal/codegen.gleam", 301). ?DOC(false). -spec extract_schemas(gilly@openapi@openapi:open_a_p_i()) -> list({binary(), gilly@openapi@schema:schema()}). extract_schemas(Spec) -> _pipe = erlang:element(6, Spec), _pipe@1 = gleam@option:map(_pipe, fun(C) -> erlang:element(2, C) end), gleam@option:unwrap(_pipe@1, []). -file("src/gilly/internal/codegen.gleam", 309). ?DOC(false). -spec api_error_doc(integer()) -> glam@doc:document(). api_error_doc(Indent) -> glam@doc:concat( [glam@doc:from_string(<<"pub type ApiError(err) {"/utf8>>), begin _pipe = {line, 1}, glam@doc:nest(_pipe, Indent) end, begin _pipe@1 = glam@doc:from_string( <<"JsonDecodeError(json.DecodeError)"/utf8>> ), glam@doc:nest(_pipe@1, Indent) end, begin _pipe@2 = {line, 1}, glam@doc:nest(_pipe@2, Indent) end, begin _pipe@3 = glam@doc:from_string(<<"ClientError(err)"/utf8>>), glam@doc:nest(_pipe@3, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ). -file("src/gilly/internal/codegen.gleam", 349). ?DOC(false). -spec client_new_doc(integer(), gleam@option:option(binary())) -> glam@doc:document(). client_new_doc(Indent, Default_base_url) -> Base_url_default = case Default_base_url of {some, Url} -> <<<<"\""/utf8, Url/binary>>/binary, "\""/utf8>>; none -> <<"\"\""/utf8>> end, glam@doc:concat( [glam@doc:from_string(<<"pub fn new("/utf8>>), begin _pipe = {line, 1}, glam@doc:nest(_pipe, Indent) end, begin _pipe@1 = glam@doc:from_string( <<"http_client: fn(request.Request(String)) -> Result(response.Response(String), err),"/utf8>> ), glam@doc:nest(_pipe@1, Indent) end, {line, 1}, glam@doc:from_string(<<") -> Client(err) {"/utf8>>), begin _pipe@2 = {line, 1}, glam@doc:nest(_pipe@2, Indent) end, begin _pipe@3 = glam@doc:from_string( <<<<"Client(http_client:, base_url: "/utf8, Base_url_default/binary>>/binary, ")"/utf8>> ), glam@doc:nest(_pipe@3, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ). -file("src/gilly/internal/codegen.gleam", 374). ?DOC(false). -spec client_with_base_url_doc(integer()) -> glam@doc:document(). client_with_base_url_doc(Indent) -> glam@doc:concat( [glam@doc:from_string(<<"pub fn with_base_url("/utf8>>), begin _pipe = {line, 1}, glam@doc:nest(_pipe, Indent) end, begin _pipe@1 = glam@doc:from_string(<<"client: Client(err),"/utf8>>), glam@doc:nest(_pipe@1, Indent) end, begin _pipe@2 = {line, 1}, glam@doc:nest(_pipe@2, Indent) end, begin _pipe@3 = glam@doc:from_string(<<"base_url: String,"/utf8>>), glam@doc:nest(_pipe@3, Indent) end, {line, 1}, glam@doc:from_string(<<") -> Client(err) {"/utf8>>), begin _pipe@4 = {line, 1}, glam@doc:nest(_pipe@4, Indent) end, begin _pipe@5 = glam@doc:from_string( <<"Client(..client, base_url:)"/utf8>> ), glam@doc:nest(_pipe@5, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ). -file("src/gilly/internal/codegen.gleam", 393). ?DOC(false). -spec default_base_url(gilly@openapi@openapi:open_a_p_i()) -> gleam@option:option(binary()). default_base_url(Spec) -> case erlang:element(4, Spec) of [First | _] -> {some, erlang:element(2, First)}; [] -> none end. -file("src/gilly/internal/codegen.gleam", 660). ?DOC(false). -spec operation_fn_name(gilly@openapi@operation:operation(), binary(), binary()) -> binary(). operation_fn_name(Op, Path, Method) -> case erlang:element(2, Op) of {some, Id} -> justin:snake_case(Id); none -> Clean_path = begin _pipe = gleam@string:replace(Path, <<"{"/utf8>>, <<""/utf8>>), _pipe@1 = gleam@string:replace(_pipe, <<"}"/utf8>>, <<""/utf8>>), gleam@string:replace(_pipe@1, <<"/"/utf8>>, <<"_"/utf8>>) end, justin:snake_case( <<(string:lowercase(Method))/binary, Clean_path/binary>> ) end. -file("src/gilly/internal/codegen.gleam", 750). ?DOC(false). -spec result_or({ok, GDR} | {error, GDS}, fun(() -> {ok, GDR} | {error, GDS})) -> {ok, GDR} | {error, GDS}. result_or(Result, Alternative) -> case Result of {ok, _} -> Result; {error, _} -> Alternative() end. -file("src/gilly/internal/codegen.gleam", 818). ?DOC(false). -spec find_next_param(binary(), list(gilly@openapi@operation:parameter())) -> gleam@option:option({binary(), gilly@openapi@operation:parameter(), binary()}). find_next_param(Path, Params) -> case gleam@string:split_once(Path, <<"{"/utf8>>) of {ok, {Before, Rest}} -> case gleam@string:split_once(Rest, <<"}"/utf8>>) of {ok, {Param_name, After}} -> case gleam@list:find( Params, fun(P) -> erlang:element(2, P) =:= Param_name end ) of {ok, Param} -> {some, {Before, Param, After}}; {error, _} -> none end; {error, _} -> none end; {error, _} -> none end. -file("src/gilly/internal/codegen.gleam", 1180). ?DOC(false). -spec inline_object_type( state(), gilly@openapi@schema:object_schema(), list({binary(), gilly@openapi@schema:schema()}) ) -> {state(), glam@doc:document()}. inline_object_type(State, _, _) -> State@1 = import_qualified( State, <<"gleam/dynamic"/utf8>>, <<"type Dynamic"/utf8>> ), {State@1, glam@doc:from_string(<<"Dynamic"/utf8>>)}. -file("src/gilly/internal/codegen.gleam", 1190). ?DOC(false). -spec wrap_optional(state(), glam@doc:document()) -> {state(), glam@doc:document()}. wrap_optional(State, Inner) -> State@1 = import_qualified( State, <<"gleam/option"/utf8>>, <<"type Option"/utf8>> ), {State@1, glam@doc:concat( [glam@doc:from_string(<<"Option("/utf8>>), Inner, glam@doc:from_string(<<")"/utf8>>)] )}. -file("src/gilly/internal/codegen.gleam", 1519). ?DOC(false). -spec is_nullable(gilly@openapi@schema:schema()) -> boolean(). is_nullable(S) -> case S of {ref, _} -> false; {string, Base, _} -> erlang:element(5, Base); {integer, Base, _} -> erlang:element(5, Base); {number, Base} -> erlang:element(5, Base); {boolean, Base} -> erlang:element(5, Base); {array, Base, _} -> erlang:element(5, Base); {object, Base, _} -> erlang:element(5, Base) end. -file("src/gilly/internal/codegen.gleam", 1507). ?DOC(false). -spec is_field_optional(gilly@openapi@schema:schema(), boolean(), optionality()) -> boolean(). is_field_optional(Prop_schema, In_required, Optionality) -> case Optionality of required_only -> not In_required; nullable_only -> is_nullable(Prop_schema); required_and_nullable -> not In_required orelse is_nullable(Prop_schema) end. -file("src/gilly/internal/codegen.gleam", 1552). ?DOC(false). -spec description_to_comment(binary()) -> glam@doc:document(). description_to_comment(Desc) -> _pipe = gleam@string:split(Desc, <<"\n"/utf8>>), _pipe@1 = gleam@list:map( _pipe, fun(Line) -> glam@doc:from_string( <<"/// "/utf8, (gleam@string:trim(Line))/binary>> ) end ), glam@doc:join(_pipe@1, {line, 1}). -file("src/gilly/internal/codegen.gleam", 322). ?DOC(false). -spec client_type_doc(integer(), gleam@option:option(binary())) -> glam@doc:document(). client_type_doc(Indent, Description) -> Comment = case Description of {some, Desc} -> glam@doc:concat([description_to_comment(Desc), {line, 1}]); none -> {concat, []} end, glam@doc:concat( [Comment, glam@doc:from_string(<<"pub opaque type Client(err) {"/utf8>>), begin _pipe = {line, 1}, glam@doc:nest(_pipe, Indent) end, begin _pipe@1 = glam@doc:from_string(<<"Client("/utf8>>), glam@doc:nest(_pipe@1, Indent) end, begin _pipe@2 = {line, 1}, glam@doc:nest(_pipe@2, Indent * 2) end, begin _pipe@3 = glam@doc:from_string( <<"http_client: fn(request.Request(String)) -> Result(response.Response(String), err),"/utf8>> ), glam@doc:nest(_pipe@3, Indent * 2) end, begin _pipe@4 = {line, 1}, glam@doc:nest(_pipe@4, Indent * 2) end, begin _pipe@5 = glam@doc:from_string(<<"base_url: String,"/utf8>>), glam@doc:nest(_pipe@5, Indent * 2) end, begin _pipe@6 = {line, 1}, glam@doc:nest(_pipe@6, Indent) end, begin _pipe@7 = glam@doc:from_string(<<")"/utf8>>), glam@doc:nest(_pipe@7, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ). -file("src/gilly/internal/codegen.gleam", 1533). ?DOC(false). -spec base_schema_comment(gilly@openapi@schema:base_schema()) -> gleam@option:option(glam@doc:document()). base_schema_comment(Base) -> case erlang:element(4, Base) of {some, Desc} -> {some, description_to_comment(Desc)}; none -> none end. -file("src/gilly/internal/codegen.gleam", 1540). ?DOC(false). -spec schema_description(gilly@openapi@schema:schema()) -> gleam@option:option(glam@doc:document()). schema_description(S) -> case S of {ref, _} -> none; {string, Base, _} -> base_schema_comment(Base); {integer, Base, _} -> base_schema_comment(Base); {number, Base} -> base_schema_comment(Base); {boolean, Base} -> base_schema_comment(Base); {array, Base, _} -> base_schema_comment(Base); {object, Base, _} -> base_schema_comment(Base) end. -file("src/gilly/internal/codegen.gleam", 1560). ?DOC(false). -spec to_type_name(binary()) -> binary(). to_type_name(Name) -> justin:pascal_case(Name). -file("src/gilly/internal/codegen.gleam", 1136). ?DOC(false). -spec string_type(state(), gilly@openapi@schema:string_schema(), binary()) -> {state(), glam@doc:document()}. string_type(State, String_schema, Enum_name_hint) -> case erlang:element(4, String_schema) of {some, Variants} -> Type_name = to_type_name(Enum_name_hint), State@1 = register_enum(State, Type_name, Variants), {State@1, glam@doc:from_string(Type_name)}; none -> case erlang:element(5, String_schema) of {some, <<"date-time"/utf8>>} -> {State, glam@doc:from_string(<<"String"/utf8>>)}; {some, <<"binary"/utf8>>} -> {State, glam@doc:from_string(<<"BitArray"/utf8>>)}; _ -> {State, glam@doc:from_string(<<"String"/utf8>>)} end end. -file("src/gilly/internal/codegen.gleam", 1359). ?DOC(false). -spec string_to_json_fn(state(), gilly@openapi@schema:string_schema(), binary()) -> {state(), glam@doc:document()}. string_to_json_fn(State, String_schema, Enum_name_hint) -> case erlang:element(4, String_schema) of {some, _} -> Type_name = to_type_name(Enum_name_hint), To_json_fn = <<(justin:snake_case(Type_name))/binary, "_to_json"/utf8>>, {State, glam@doc:from_string(To_json_fn)}; none -> {State, glam@doc:from_string(<<"json.string"/utf8>>)} end. -file("src/gilly/internal/codegen.gleam", 1424). ?DOC(false). -spec string_to_json( state(), gilly@openapi@schema:string_schema(), binary(), binary() ) -> {state(), glam@doc:document()}. string_to_json(State, String_schema, Accessor, Enum_name_hint) -> case erlang:element(4, String_schema) of {some, _} -> Type_name = to_type_name(Enum_name_hint), To_string_fn = <<(justin:snake_case(Type_name))/binary, "_to_string"/utf8>>, {State, glam@doc:from_string( <<<<<<<<"json.string("/utf8, To_string_fn/binary>>/binary, "("/utf8>>/binary, Accessor/binary>>/binary, "))"/utf8>> )}; none -> case erlang:element(5, String_schema) of {some, <<"binary"/utf8>>} -> {State, glam@doc:from_string( <<<<"json.string("/utf8, Accessor/binary>>/binary, ")"/utf8>> )}; _ -> {State, glam@doc:from_string( <<<<"json.string("/utf8, Accessor/binary>>/binary, ")"/utf8>> )} end end. -file("src/gilly/internal/codegen.gleam", 1564). ?DOC(false). -spec to_field_name(binary()) -> binary(). to_field_name(Name) -> Snake = justin:snake_case(Name), case Snake of <<"type"/utf8>> -> <<"type_"/utf8>>; <<"fn"/utf8>> -> <<"fn_"/utf8>>; <<"let"/utf8>> -> <<"let_"/utf8>>; <<"case"/utf8>> -> <<"case_"/utf8>>; <<"use"/utf8>> -> <<"use_"/utf8>>; <<"pub"/utf8>> -> <<"pub_"/utf8>>; <<"import"/utf8>> -> <<"import_"/utf8>>; <<"as"/utf8>> -> <<"as_"/utf8>>; _ -> Snake end. -file("src/gilly/internal/codegen.gleam", 784). ?DOC(false). -spec split_path_on_params( binary(), list(gilly@openapi@operation:parameter()), list(glam@doc:document()) ) -> list(glam@doc:document()). split_path_on_params(Remaining, Params, Acc) -> case find_next_param(Remaining, Params) of {some, {Before, Param, After}} -> Param_name = to_field_name(erlang:element(2, Param)), Has_int_type = case erlang:element(6, Param) of {some, {integer, _, _}} -> true; _ -> false end, Before_parts = case Before of <<""/utf8>> -> Acc; _ -> lists:append( Acc, [glam@doc:from_string( <<<<"\""/utf8, Before/binary>>/binary, "\""/utf8>> )] ) end, Param_expr = case Has_int_type of true -> glam@doc:from_string( <<<<"int.to_string("/utf8, Param_name/binary>>/binary, ")"/utf8>> ); false -> glam@doc:from_string(Param_name) end, split_path_on_params( After, Params, lists:append(Before_parts, [Param_expr]) ); none -> case Remaining of <<""/utf8>> -> Acc; _ -> lists:append( Acc, [glam@doc:from_string( <<<<"\""/utf8, Remaining/binary>>/binary, "\""/utf8>> )] ) end end. -file("src/gilly/internal/codegen.gleam", 778). ?DOC(false). -spec build_path_parts(binary(), list(gilly@openapi@operation:parameter())) -> glam@doc:document(). build_path_parts(Path, Params) -> Parts = split_path_on_params(Path, Params, []), glam@doc:join(Parts, glam@doc:from_string(<<" <> "/utf8>>)). -file("src/gilly/internal/codegen.gleam", 760). ?DOC(false). -spec build_url_expression(binary(), list(gilly@openapi@operation:parameter())) -> glam@doc:document(). build_url_expression(Path, Path_params) -> case Path_params of [] -> glam@doc:from_string( <<<<"let assert Ok(req) = request.to(client.base_url <> \""/utf8, Path/binary>>/binary, "\")"/utf8>> ); _ -> Url_parts = build_path_parts(Path, Path_params), glam@doc:concat( [glam@doc:from_string( <<"let assert Ok(req) = request.to(client.base_url <> "/utf8>> ), Url_parts, glam@doc:from_string(<<")"/utf8>>)] ) end. -file("src/gilly/internal/codegen.gleam", 1580). ?DOC(false). -spec ref_to_type_name(binary()) -> binary(). ref_to_type_name(Ref) -> case gleam@string:split(Ref, <<"/"/utf8>>) of [_, _, _, Name | _] -> to_type_name(Name); _ -> to_type_name(Ref) end. -file("src/gilly/internal/codegen.gleam", 1590). ?DOC(false). -spec enum_variant_name(binary(), binary()) -> binary(). enum_variant_name(Type_name, Variant) -> <>. -file("src/gilly/internal/codegen.gleam", 1594). ?DOC(false). -spec enum_decoder_name(binary()) -> binary(). enum_decoder_name(Type_name) -> <<(justin:snake_case(Type_name))/binary, "_decoder"/utf8>>. -file("src/gilly/internal/codegen.gleam", 1223). ?DOC(false). -spec string_decoder(state(), gilly@openapi@schema:string_schema(), binary()) -> {state(), glam@doc:document()}. string_decoder(State, String_schema, Enum_name_hint) -> case erlang:element(4, String_schema) of {some, Variants} -> Type_name = to_type_name(Enum_name_hint), _ = Variants, Decoder_name = enum_decoder_name(Type_name), {State, glam@doc:from_string(<>)}; none -> case erlang:element(5, String_schema) of {some, <<"binary"/utf8>>} -> {State, glam@doc:from_string(<<"decode.bit_array"/utf8>>)}; _ -> {State, glam@doc:from_string(<<"decode.string"/utf8>>)} end end. -file("src/gilly/internal/codegen.gleam", 1767). ?DOC(false). -spec separator_comment(binary()) -> glam@doc:document(). separator_comment(Value) -> _pipe = gleam@string:pad_end( <<<<"// --- "/utf8, Value/binary>>/binary, " "/utf8>>, 80, <<"-"/utf8>> ), glam@doc:from_string(_pipe). -file("src/gilly/internal/codegen.gleam", 1792). ?DOC(false). -spec enum_type_doc(binary(), list(binary()), integer()) -> glam@doc:document(). enum_type_doc(Type_name, Variants, Indent) -> Variant_docs = gleam@list:map( Variants, fun(V) -> glam@doc:from_string(enum_variant_name(Type_name, V)) end ), glam@doc:concat( [glam@doc:from_string( <<<<"pub type "/utf8, Type_name/binary>>/binary, " {"/utf8>> ), begin _pipe = {line, 1}, glam@doc:nest(_pipe, Indent) end, begin _pipe@1 = glam@doc:join(Variant_docs, {line, 1}), glam@doc:nest(_pipe@1, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ). -file("src/gilly/internal/codegen.gleam", 1812). ?DOC(false). -spec enum_to_string_doc(binary(), list(binary()), integer()) -> glam@doc:document(). enum_to_string_doc(Type_name, Variants, Indent) -> Fn_name = <<(justin:snake_case(Type_name))/binary, "_to_string"/utf8>>, Var_name = <<"value"/utf8>>, Case_lines = gleam@list:map( Variants, fun(V) -> glam@doc:concat( [glam@doc:from_string(enum_variant_name(Type_name, V)), glam@doc:from_string(<<" -> "/utf8>>), glam@doc:from_string( <<<<"\""/utf8, V/binary>>/binary, "\""/utf8>> )] ) end ), Case_block = glam@doc:concat( [glam@doc:from_string( <<<<"case "/utf8, Var_name/binary>>/binary, " {"/utf8>> ), begin _pipe = {line, 1}, glam@doc:nest(_pipe, Indent) end, begin _pipe@1 = glam@doc:join(Case_lines, {line, 1}), glam@doc:nest(_pipe@1, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ), glam@doc:concat( [glam@doc:from_string( <<<<<<<<<<<<"pub fn "/utf8, Fn_name/binary>>/binary, "("/utf8>>/binary, Var_name/binary>>/binary, ": "/utf8>>/binary, Type_name/binary>>/binary, ") -> String {"/utf8>> ), begin _pipe@2 = {line, 1}, glam@doc:nest(_pipe@2, Indent) end, begin _pipe@3 = Case_block, glam@doc:nest(_pipe@3, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ). -file("src/gilly/internal/codegen.gleam", 1856). ?DOC(false). -spec enum_to_json_doc(binary(), list(binary()), integer()) -> glam@doc:document(). enum_to_json_doc(Type_name, _, Indent) -> Fn_name = <<(justin:snake_case(Type_name))/binary, "_to_json"/utf8>>, To_string_fn = <<(justin:snake_case(Type_name))/binary, "_to_string"/utf8>>, Var_name = <<"value"/utf8>>, glam@doc:concat( [glam@doc:from_string( <<<<<<<<<<<<"pub fn "/utf8, Fn_name/binary>>/binary, "("/utf8>>/binary, Var_name/binary>>/binary, ": "/utf8>>/binary, Type_name/binary>>/binary, ") -> json.Json {"/utf8>> ), begin _pipe = {line, 1}, glam@doc:nest(_pipe, Indent) end, begin _pipe@1 = glam@doc:from_string( <<<<<<<<"json.string("/utf8, To_string_fn/binary>>/binary, "("/utf8>>/binary, Var_name/binary>>/binary, "))"/utf8>> ), glam@doc:nest(_pipe@1, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ). -file("src/gilly/internal/codegen.gleam", 1883). ?DOC(false). -spec enum_decoder_doc(binary(), list(binary()), integer()) -> glam@doc:document(). enum_decoder_doc(Type_name, Variants, Indent) -> Decoder_name = enum_decoder_name(Type_name), Var_name = <<"value"/utf8>>, Success_cases = gleam@list:map( Variants, fun(V) -> glam@doc:concat( [glam@doc:from_string( <<<<"\""/utf8, V/binary>>/binary, "\" -> "/utf8>> ), glam@doc:from_string( <<<<"decode.success("/utf8, (enum_variant_name(Type_name, V))/binary>>/binary, ")"/utf8>> )] ) end ), First_variant = case Variants of [First | _] -> enum_variant_name(Type_name, First); [] -> <<"Nil"/utf8>> end, Failure_case = glam@doc:concat( [glam@doc:from_string(<<"_ -> "/utf8>>), glam@doc:from_string( <<<<<<<<"decode.failure("/utf8, First_variant/binary>>/binary, ", \""/utf8>>/binary, Type_name/binary>>/binary, "\")"/utf8>> )] ), Case_body = lists:append(Success_cases, [Failure_case]), Case_block = glam@doc:concat( [glam@doc:from_string( <<<<"case "/utf8, Var_name/binary>>/binary, " {"/utf8>> ), begin _pipe = {line, 1}, glam@doc:nest(_pipe, Indent) end, begin _pipe@1 = glam@doc:join(Case_body, {line, 1}), glam@doc:nest(_pipe@1, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ), Fn_body = glam@doc:concat( [glam@doc:from_string( <<<<"use "/utf8, Var_name/binary>>/binary, " <- decode.then(decode.string)"/utf8>> ), {line, 1}, Case_block] ), Return_type = glam@doc:from_string( <<<<"decode.Decoder("/utf8, Type_name/binary>>/binary, ")"/utf8>> ), glam@doc:concat( [glam@doc:from_string( <<<<"pub fn "/utf8, Decoder_name/binary>>/binary, "() -> "/utf8>> ), Return_type, glam@doc:from_string(<<" {"/utf8>>), begin _pipe@2 = {line, 1}, glam@doc:nest(_pipe@2, Indent) end, begin _pipe@3 = Fn_body, glam@doc:nest(_pipe@3, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ). -file("src/gilly/internal/codegen.gleam", 1772). ?DOC(false). -spec enums_doc(state(), integer()) -> glam@doc:document(). enums_doc(State, Indent) -> Sorted_enums = begin _pipe = maps:to_list(erlang:element(3, State)), gleam@list:sort( _pipe, fun(A, B) -> gleam@string:compare(erlang:element(1, A), erlang:element(1, B)) end ) end, _pipe@1 = gleam@list:map( Sorted_enums, fun(Entry) -> {Type_name, Variants} = Entry, glam@doc:concat( [enum_type_doc(Type_name, Variants, Indent), glam@doc:lines(2), enum_to_string_doc(Type_name, Variants, Indent), glam@doc:lines(2), enum_to_json_doc(Type_name, Variants, Indent), glam@doc:lines(2), enum_decoder_doc(Type_name, Variants, Indent)] ) end ), glam@doc:join(_pipe@1, glam@doc:lines(2)). -file("src/gilly/internal/codegen.gleam", 1452). ?DOC(false). -spec array_to_json( state(), gilly@openapi@schema:array_schema(), binary(), list({binary(), gilly@openapi@schema:schema()}), binary() ) -> {state(), glam@doc:document()}. array_to_json(State, Array_schema, Accessor, All_schemas, Enum_name_hint) -> {State@1, Item_encoder} = schema_to_json_expression_inner( State, erlang:element(4, Array_schema), <<"item"/utf8>>, All_schemas, Enum_name_hint ), {State@1, glam@doc:concat( [glam@doc:from_string( <<<<"json.array("/utf8, Accessor/binary>>/binary, ", fn(item) { "/utf8>> ), Item_encoder, glam@doc:from_string(<<" })"/utf8>>)] )}. -file("src/gilly/internal/codegen.gleam", 1395). ?DOC(false). -spec schema_to_json_expression_inner( state(), gilly@openapi@schema:schema(), binary(), list({binary(), gilly@openapi@schema:schema()}), binary() ) -> {state(), glam@doc:document()}. schema_to_json_expression_inner( State, Schema, Accessor, All_schemas, Enum_name_hint ) -> case Schema of {string, _, String_schema} -> string_to_json(State, String_schema, Accessor, Enum_name_hint); {integer, _, _} -> {State, glam@doc:from_string( <<<<"json.int("/utf8, Accessor/binary>>/binary, ")"/utf8>> )}; {number, _} -> {State, glam@doc:from_string( <<<<"json.float("/utf8, Accessor/binary>>/binary, ")"/utf8>> )}; {boolean, _} -> {State, glam@doc:from_string( <<<<"json.bool("/utf8, Accessor/binary>>/binary, ")"/utf8>> )}; {array, _, Array_schema} -> array_to_json( State, Array_schema, Accessor, All_schemas, Enum_name_hint ); {ref, Ref} -> ref_to_json(State, Ref, Accessor, All_schemas); {object, _, _} -> {State, glam@doc:from_string(<<"json.null()"/utf8>>)} end. -file("src/gilly/internal/codegen.gleam", 836). ?DOC(false). -spec build_request_lines( state(), binary(), list(gilly@openapi@operation:parameter()), gleam@option:option({binary(), glam@doc:document(), gilly@openapi@schema:schema()}), list({binary(), gilly@openapi@schema:schema()}) ) -> list(glam@doc:document()). build_request_lines(State, Method, Query_params, Body_param, All_schemas) -> Method_line = glam@doc:from_string( <<<<"let req = request.set_method(req, http."/utf8, Method/binary>>/binary, ")"/utf8>> ), Header_line = glam@doc:from_string( <<"let req = request.prepend_header(req, \"content-type\", \"application/json\")"/utf8>> ), Body_line = case Body_param of {some, {Arg_name, _, Body_schema}} -> {_, Encode_expr} = schema_to_json_expression_inner( State, Body_schema, Arg_name, All_schemas, Arg_name ), Encode_str = glam@doc:to_string(Encode_expr, 80), [glam@doc:from_string( <<<<"let req = request.set_body(req, json.to_string("/utf8, Encode_str/binary>>/binary, "))"/utf8>> )]; none -> [] end, Query_lines = case Query_params of [] -> []; _ -> Required_entries = begin _pipe = gleam@list:filter( Query_params, fun(P) -> erlang:element(5, P) end ), gleam@list:map( _pipe, fun(Param) -> Param_name = to_field_name(erlang:element(2, Param)), glam@doc:from_string( <<<<<<<<"#(\""/utf8, (erlang:element(2, Param))/binary>>/binary, "\", "/utf8>>/binary, Param_name/binary>>/binary, ")"/utf8>> ) end ) end, Optional_entries = begin _pipe@1 = gleam@list:filter( Query_params, fun(P@1) -> not erlang:element(5, P@1) end ), gleam@list:map( _pipe@1, fun(Param@1) -> Param_name@1 = to_field_name(erlang:element(2, Param@1)), glam@doc:from_string( <<<<<<<<"option.map("/utf8, Param_name@1/binary>>/binary, ", fn(v) { #(\""/utf8>>/binary, (erlang:element(2, Param@1))/binary>>/binary, "\", v) })"/utf8>> ) end ) end, Required_list = case Required_entries of [] -> glam@doc:from_string(<<"[]"/utf8>>); _ -> glam@doc:concat( [glam@doc:from_string(<<"["/utf8>>), glam@doc:join( Required_entries, glam@doc:from_string(<<", "/utf8>>) ), glam@doc:from_string(<<"]"/utf8>>)] ) end, Optional_part = case Optional_entries of [] -> []; _ -> [glam@doc:from_string( <<"let query = list.append(query, option.values(["/utf8>> ) | lists:append( gleam@list:map( Optional_entries, fun(Entry) -> glam@doc:concat( [glam@doc:from_string(<<" "/utf8>>), Entry, glam@doc:from_string(<<","/utf8>>)] ) end ), [glam@doc:from_string(<<"]))"/utf8>>)] )] end, lists:append( [[glam@doc:concat( [glam@doc:from_string(<<"let query = "/utf8>>), Required_list] )], Optional_part, [glam@doc:from_string( <<"let req = request.set_query(req, query)"/utf8>> )]] ) end, _pipe@2 = [Method_line, Header_line], _pipe@3 = lists:append(_pipe@2, Body_line), lists:append(_pipe@3, Query_lines). -file("src/gilly/internal/codegen.gleam", 1477). ?DOC(false). -spec ref_to_json( state(), binary(), binary(), list({binary(), gilly@openapi@schema:schema()}) ) -> {state(), glam@doc:document()}. ref_to_json(State, Ref, Accessor, All_schemas) -> Raw_name = case gleam@string:split(Ref, <<"/"/utf8>>) of [_, _, _, Name | _] -> Name; _ -> Ref end, Type_name = to_type_name(Raw_name), case gleam@list:key_find(All_schemas, Raw_name) of {ok, {object, _, _}} -> To_json_fn = <<(justin:snake_case(Type_name))/binary, "_to_json"/utf8>>, {State, glam@doc:from_string( <<<<<>/binary, Accessor/binary>>/binary, ")"/utf8>> )}; {ok, Schema} -> schema_to_json_expression_inner( State, Schema, Accessor, All_schemas, Type_name ); {error, _} -> {State, glam@doc:from_string(<<"json.null()"/utf8>>)} end. -file("src/gilly/internal/codegen.gleam", 1374). ?DOC(false). -spec ref_to_json_fn( state(), binary(), list({binary(), gilly@openapi@schema:schema()}) ) -> {state(), glam@doc:document()}. ref_to_json_fn(State, Ref, All_schemas) -> Raw_name = case gleam@string:split(Ref, <<"/"/utf8>>) of [_, _, _, Name | _] -> Name; _ -> Ref end, Type_name = to_type_name(Raw_name), case gleam@list:key_find(All_schemas, Raw_name) of {ok, {object, _, _}} -> To_json_fn = <<(justin:snake_case(Type_name))/binary, "_to_json"/utf8>>, {State, glam@doc:from_string(To_json_fn)}; {ok, Schema} -> schema_to_json_encoder_fn(State, Schema, All_schemas, Type_name); {error, _} -> {State, glam@doc:from_string(<<"fn(_) { json.null() }"/utf8>>)} end. -file("src/gilly/internal/codegen.gleam", 1325). ?DOC(false). -spec schema_to_json_encoder_fn( state(), gilly@openapi@schema:schema(), list({binary(), gilly@openapi@schema:schema()}), binary() ) -> {state(), glam@doc:document()}. schema_to_json_encoder_fn(State, Schema, All_schemas, Enum_name_hint) -> case Schema of {string, _, String_schema} -> string_to_json_fn(State, String_schema, Enum_name_hint); {integer, _, _} -> {State, glam@doc:from_string(<<"json.int"/utf8>>)}; {number, _} -> {State, glam@doc:from_string(<<"json.float"/utf8>>)}; {boolean, _} -> {State, glam@doc:from_string(<<"json.bool"/utf8>>)}; {array, _, Array_schema} -> {State@1, Item_fn} = schema_to_json_encoder_fn( State, erlang:element(4, Array_schema), All_schemas, Enum_name_hint ), {State@1, glam@doc:concat( [glam@doc:from_string(<<"json.array(_, "/utf8>>), Item_fn, glam@doc:from_string(<<")"/utf8>>)] )}; {ref, Ref} -> ref_to_json_fn(State, Ref, All_schemas); {object, _, _} -> {State, glam@doc:from_string(<<"fn(_) { json.null() }"/utf8>>)} end. -file("src/gilly/internal/codegen.gleam", 1291). ?DOC(false). -spec schema_to_json_expression( state(), gilly@openapi@schema:schema(), binary(), list({binary(), gilly@openapi@schema:schema()}), binary(), boolean() ) -> {state(), glam@doc:document()}. schema_to_json_expression( State, Schema, Accessor, All_schemas, Enum_name_hint, Optional ) -> case Optional of true -> {State@1, Encoder_fn} = schema_to_json_encoder_fn( State, Schema, All_schemas, Enum_name_hint ), {State@1, glam@doc:concat( [glam@doc:from_string( <<<<"json.nullable("/utf8, Accessor/binary>>/binary, ", "/utf8>> ), Encoder_fn, glam@doc:from_string(<<")"/utf8>>)] )}; false -> schema_to_json_expression_inner( State, Schema, Accessor, All_schemas, Enum_name_hint ) end. -file("src/gilly/internal/codegen.gleam", 1694). ?DOC(false). -spec record_to_json_doc( state(), binary(), gilly@openapi@schema:object_schema(), list({binary(), gilly@openapi@schema:schema()}), config() ) -> {state(), glam@doc:document()}. record_to_json_doc(State, Type_name, Object_schema, All_schemas, Config) -> State@1 = import_module(State, <<"gleam/json"/utf8>>), Fn_name = <<(justin:snake_case(Type_name))/binary, "_to_json"/utf8>>, Indent = erlang:element(3, Config), {State@4, Field_pairs} = gleam@list:fold( erlang:element(3, Object_schema), {State@1, []}, fun(Acc, Prop) -> {State@2, Pairs} = Acc, {Prop_name, Prop_schema} = Prop, Field_name = to_field_name(Prop_name), In_required = gleam@list:contains( erlang:element(2, Object_schema), Prop_name ), Optional = is_field_optional( Prop_schema, In_required, erlang:element(2, Config) ), Enum_hint = <>, Accessor = <<"value."/utf8, Field_name/binary>>, {State@3, Json_expr} = schema_to_json_expression( State@2, Prop_schema, Accessor, All_schemas, Enum_hint, Optional ), Pair = glam@doc:concat( [glam@doc:from_string( <<<<"#(\""/utf8, Prop_name/binary>>/binary, "\", "/utf8>> ), Json_expr, glam@doc:from_string(<<")"/utf8>>)] ), {State@3, [Pair | Pairs]} end ), Field_pairs@1 = lists:reverse(Field_pairs), Body = case Field_pairs@1 of [] -> glam@doc:from_string(<<"json.object([])"/utf8>>); _ -> glam@doc:concat( [glam@doc:from_string(<<"json.object(["/utf8>>), begin _pipe = {line, 1}, glam@doc:nest(_pipe, Indent) end, begin _pipe@1 = glam@doc:join( Field_pairs@1, glam@doc:concat( [glam@doc:from_string(<<","/utf8>>), {line, 1}] ) ), glam@doc:nest(_pipe@1, Indent) end, glam@doc:concat( [glam@doc:from_string(<<","/utf8>>), {line, 1}] ), glam@doc:from_string(<<"])"/utf8>>)] ) end, Result = glam@doc:concat( [glam@doc:from_string( <<<<<<<<"pub fn "/utf8, Fn_name/binary>>/binary, "(value: "/utf8>>/binary, Type_name/binary>>/binary, ") -> json.Json {"/utf8>> ), begin _pipe@2 = {line, 1}, glam@doc:nest(_pipe@2, Indent) end, begin _pipe@3 = Body, glam@doc:nest(_pipe@3, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ), {State@4, Result}. -file("src/gilly/internal/codegen.gleam", 1243). ?DOC(false). -spec array_decoder( state(), gilly@openapi@schema:array_schema(), list({binary(), gilly@openapi@schema:schema()}), binary() ) -> {state(), glam@doc:document()}. array_decoder(State, Array_schema, All_schemas, Enum_name_hint) -> {State@1, Items_decoder} = schema_to_inner_decoder( State, erlang:element(4, Array_schema), All_schemas, Enum_name_hint ), {State@1, glam@doc:concat( [glam@doc:from_string(<<"decode.list("/utf8>>), Items_decoder, glam@doc:from_string(<<")"/utf8>>)] )}. -file("src/gilly/internal/codegen.gleam", 1204). ?DOC(false). -spec schema_to_inner_decoder( state(), gilly@openapi@schema:schema(), list({binary(), gilly@openapi@schema:schema()}), binary() ) -> {state(), glam@doc:document()}. schema_to_inner_decoder(State, Schema, All_schemas, Enum_name_hint) -> case Schema of {ref, Ref} -> ref_to_decoder(State, Ref, All_schemas); {string, _, String_schema} -> string_decoder(State, String_schema, Enum_name_hint); {integer, _, _} -> {State, glam@doc:from_string(<<"decode.int"/utf8>>)}; {number, _} -> {State, glam@doc:from_string(<<"decode.float"/utf8>>)}; {boolean, _} -> {State, glam@doc:from_string(<<"decode.bool"/utf8>>)}; {array, _, Array_schema} -> array_decoder(State, Array_schema, All_schemas, Enum_name_hint); {object, _, _} -> {State, glam@doc:from_string(<<"decode.dynamic"/utf8>>)} end. -file("src/gilly/internal/codegen.gleam", 938). ?DOC(false). -spec build_decode_line( state(), gleam@option:option({glam@doc:document(), gilly@openapi@schema:schema()}), list({binary(), gilly@openapi@schema:schema()}) ) -> {state(), glam@doc:document()}. build_decode_line(State, Response_type, All_schemas) -> case Response_type of {some, {_, Resp_schema}} -> {State@1, Decoder_doc} = schema_to_inner_decoder( State, Resp_schema, All_schemas, <<"response"/utf8>> ), Decoder_str = glam@doc:to_string(Decoder_doc, 80), {State@1, glam@doc:concat( [glam@doc:from_string( <<"use resp <- result.try(client.http_client(req) |> result.map_error(ClientError))"/utf8>> ), {line, 1}, glam@doc:from_string( <<<<"json.parse(resp.body, "/utf8, Decoder_str/binary>>/binary, ")"/utf8>> ), {line, 1}, glam@doc:from_string( <<"|> result.map_error(JsonDecodeError)"/utf8>> )] )}; none -> {State, glam@doc:concat( [glam@doc:from_string( <<"use resp <- result.try(client.http_client(req) |> result.map_error(ClientError))"/utf8>> ), {line, 1}, glam@doc:from_string(<<"let _ = resp"/utf8>>), {line, 1}, glam@doc:from_string(<<"Ok(Nil)"/utf8>>)] )} end. -file("src/gilly/internal/codegen.gleam", 1266). ?DOC(false). -spec ref_to_decoder( state(), binary(), list({binary(), gilly@openapi@schema:schema()}) ) -> {state(), glam@doc:document()}. ref_to_decoder(State, Ref, All_schemas) -> Raw_name = case gleam@string:split(Ref, <<"/"/utf8>>) of [_, _, _, Name | _] -> Name; _ -> Ref end, Type_name = to_type_name(Raw_name), case gleam@list:key_find(All_schemas, Raw_name) of {ok, {object, _, _}} -> Decoder_name = <<(justin:snake_case(Type_name))/binary, "_decoder()"/utf8>>, {State, glam@doc:from_string(Decoder_name)}; {ok, Schema} -> schema_to_inner_decoder(State, Schema, All_schemas, Type_name); {error, _} -> {State, glam@doc:from_string(<<"decode.dynamic"/utf8>>)} end. -file("src/gilly/internal/codegen.gleam", 1600). ?DOC(false). -spec record_decoder_doc( state(), binary(), gilly@openapi@schema:object_schema(), list({binary(), gilly@openapi@schema:schema()}), config() ) -> {state(), glam@doc:document()}. record_decoder_doc(State, Type_name, Object_schema, All_schemas, Config) -> State@1 = import_module(State, <<"gleam/dynamic/decode"/utf8>>), Decoder_name = <<(justin:snake_case(Type_name))/binary, "_decoder"/utf8>>, Indent = erlang:element(3, Config), {State@6, Field_lines} = gleam@list:fold( erlang:element(3, Object_schema), {State@1, []}, fun(Acc, Prop) -> {State@2, Lines} = Acc, {Prop_name, Prop_schema} = Prop, Field_name = to_field_name(Prop_name), In_required = gleam@list:contains( erlang:element(2, Object_schema), Prop_name ), Optional = is_field_optional( Prop_schema, In_required, erlang:element(2, Config) ), Enum_hint = <>, {State@3, Inner_decoder} = schema_to_inner_decoder( State@2, Prop_schema, All_schemas, Enum_hint ), {State@5, Line} = case Optional of false -> {State@3, glam@doc:concat( [glam@doc:from_string( <<<<<<<<"use "/utf8, Field_name/binary>>/binary, " <- decode.field(\""/utf8>>/binary, Prop_name/binary>>/binary, "\", "/utf8>> ), Inner_decoder, glam@doc:from_string(<<")"/utf8>>)] )}; true -> State@4 = import_qualified( State@3, <<"gleam/option"/utf8>>, <<"None"/utf8>> ), {State@4, glam@doc:concat( [glam@doc:from_string( <<<<<<<<"use "/utf8, Field_name/binary>>/binary, " <- decode.optional_field(\""/utf8>>/binary, Prop_name/binary>>/binary, "\", None, decode.optional("/utf8>> ), Inner_decoder, glam@doc:from_string(<<"))"/utf8>>)] )} end, {State@5, [Line | Lines]} end ), Field_lines@1 = lists:reverse(Field_lines), Labels = gleam@list:map( erlang:element(3, Object_schema), fun(Prop@1) -> <<(to_field_name(erlang:element(1, Prop@1)))/binary, ":"/utf8>> end ), Success_line = case Labels of [] -> glam@doc:from_string( <<<<"decode.success("/utf8, Type_name/binary>>/binary, ")"/utf8>> ); _ -> glam@doc:from_string( <<<<<<<<"decode.success("/utf8, Type_name/binary>>/binary, "("/utf8>>/binary, (gleam@string:join(Labels, <<", "/utf8>>))/binary>>/binary, "))"/utf8>> ) end, Body_lines = lists:append(Field_lines@1, [Success_line]), Return_type = glam@doc:from_string( <<<<"decode.Decoder("/utf8, Type_name/binary>>/binary, ")"/utf8>> ), Result = glam@doc:concat( [glam@doc:from_string( <<<<"pub fn "/utf8, Decoder_name/binary>>/binary, "() -> "/utf8>> ), Return_type, glam@doc:from_string(<<" {"/utf8>>), begin _pipe = {line, 1}, glam@doc:nest(_pipe, Indent) end, begin _pipe@1 = glam@doc:join(Body_lines, {line, 1}), glam@doc:nest(_pipe@1, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ), {State@6, Result}. -file("src/gilly/internal/codegen.gleam", 1156). ?DOC(false). -spec array_type( state(), gilly@openapi@schema:array_schema(), list({binary(), gilly@openapi@schema:schema()}), binary() ) -> {state(), glam@doc:document()}. array_type(State, Array_schema, All_schemas, Enum_name_hint) -> {State@1, Items_type} = schema_to_gleam_type( State, erlang:element(4, Array_schema), true, All_schemas, Enum_name_hint ), {State@1, glam@doc:concat( [glam@doc:from_string(<<"List("/utf8>>), Items_type, glam@doc:from_string(<<")"/utf8>>)] )}. -file("src/gilly/internal/codegen.gleam", 1110). ?DOC(false). -spec schema_to_gleam_type( state(), gilly@openapi@schema:schema(), boolean(), list({binary(), gilly@openapi@schema:schema()}), binary() ) -> {state(), glam@doc:document()}. schema_to_gleam_type(State, Schema, Required, All_schemas, Enum_name_hint) -> {State@1, Inner} = case Schema of {ref, Ref} -> {State, glam@doc:from_string(ref_to_type_name(Ref))}; {string, _, String_schema} -> string_type(State, String_schema, Enum_name_hint); {integer, _, _} -> {State, glam@doc:from_string(<<"Int"/utf8>>)}; {number, _} -> {State, glam@doc:from_string(<<"Float"/utf8>>)}; {boolean, _} -> {State, glam@doc:from_string(<<"Bool"/utf8>>)}; {array, _, Array_schema} -> array_type(State, Array_schema, All_schemas, Enum_name_hint); {object, _, Object_schema} -> inline_object_type(State, Object_schema, All_schemas) end, case Required of true -> {State@1, Inner}; false -> wrap_optional(State@1, Inner) end. -file("src/gilly/internal/codegen.gleam", 674). ?DOC(false). -spec param_to_type( state(), gilly@openapi@operation:parameter(), list({binary(), gilly@openapi@schema:schema()}) ) -> {state(), glam@doc:document()}. param_to_type(State, Param, All_schemas) -> case erlang:element(6, Param) of {some, {string, _, _}} -> {State, glam@doc:from_string(<<"String"/utf8>>)}; {some, Schema} -> schema_to_gleam_type( State, Schema, erlang:element(5, Param), All_schemas, erlang:element(2, Param) ); none -> {State, glam@doc:from_string(<<"String"/utf8>>)} end. -file("src/gilly/internal/codegen.gleam", 695). ?DOC(false). -spec request_body_param( state(), gilly@openapi@operation:request_body(), list({binary(), gilly@openapi@schema:schema()}) ) -> {state(), gleam@option:option({binary(), glam@doc:document(), gilly@openapi@schema:schema()})}. request_body_param(State, Rb, All_schemas) -> case gleam@list:key_find(erlang:element(4, Rb), <<"application/json"/utf8>>) of {ok, Media_type} -> case erlang:element(2, Media_type) of {some, Body_schema} -> {State@1, Type_doc} = schema_to_gleam_type( State, Body_schema, true, All_schemas, <<"body"/utf8>> ), {State@1, {some, {<<"body"/utf8>>, Type_doc, Body_schema}}}; none -> {State, none} end; {error, _} -> {State, none} end. -file("src/gilly/internal/codegen.gleam", 715). ?DOC(false). -spec response_type_from_op( state(), gilly@openapi@operation:operation(), list({binary(), gilly@openapi@schema:schema()}) ) -> {state(), gleam@option:option({glam@doc:document(), gilly@openapi@schema:schema()})}. response_type_from_op(State, Op, All_schemas) -> Response = begin _pipe = gleam@list:key_find(erlang:element(8, Op), <<"200"/utf8>>), _pipe@1 = result_or( _pipe, fun() -> gleam@list:key_find(erlang:element(8, Op), <<"201"/utf8>>) end ), result_or( _pipe@1, fun() -> gleam@list:key_find(erlang:element(8, Op), <<"default"/utf8>>) end ) end, case Response of {ok, Resp} -> case gleam@list:key_find( erlang:element(3, Resp), <<"application/json"/utf8>> ) of {ok, Media_type} -> case erlang:element(2, Media_type) of {some, Resp_schema} -> {State@1, Type_doc} = schema_to_gleam_type( State, Resp_schema, true, All_schemas, <<"response"/utf8>> ), {State@1, {some, {Type_doc, Resp_schema}}}; none -> {State, none} end; {error, _} -> {State, none} end; {error, _} -> {State, none} end. -file("src/gilly/internal/codegen.gleam", 1002). ?DOC(false). -spec object_type_doc( state(), binary(), gilly@openapi@schema:base_schema(), gilly@openapi@schema:object_schema(), list({binary(), gilly@openapi@schema:schema()}), config() ) -> {state(), glam@doc:document()}. object_type_doc(State, Type_name, Base, Object_schema, All_schemas, Config) -> Comment = base_schema_comment(Base), {State@3, Fields@1} = gleam@list:fold( erlang:element(3, Object_schema), {State, []}, fun(Acc, Prop) -> {State@1, Fields} = Acc, {Prop_name, Prop_schema} = Prop, Field_name = to_field_name(Prop_name), In_required = gleam@list:contains( erlang:element(2, Object_schema), Prop_name ), Optional = is_field_optional( Prop_schema, In_required, erlang:element(2, Config) ), Enum_hint = <>, {State@2, Field_type} = schema_to_gleam_type( State@1, Prop_schema, not Optional, All_schemas, Enum_hint ), Field_line = glam@doc:concat( [glam@doc:from_string(<>), Field_type] ), Field_doc = case schema_description(Prop_schema) of {some, Comment@1} -> glam@doc:concat([Comment@1, {line, 1}, Field_line]); none -> Field_line end, {State@2, [Field_doc | Fields]} end ), Fields@2 = lists:reverse(Fields@1), Indent = erlang:element(3, Config), Body = case Fields@2 of [] -> glam@doc:concat( [glam@doc:from_string( <<<<"pub type "/utf8, Type_name/binary>>/binary, " {"/utf8>> ), begin _pipe = {line, 1}, glam@doc:nest(_pipe, Indent) end, begin _pipe@1 = glam@doc:from_string(Type_name), glam@doc:nest(_pipe@1, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ); _ -> glam@doc:concat( [glam@doc:from_string( <<<<"pub type "/utf8, Type_name/binary>>/binary, " {"/utf8>> ), begin _pipe@2 = {line, 1}, glam@doc:nest(_pipe@2, Indent) end, begin _pipe@5 = glam@doc:concat( [glam@doc:from_string( <> ), begin _pipe@3 = {line, 1}, glam@doc:nest(_pipe@3, Indent) end, begin _pipe@4 = glam@doc:join( Fields@2, glam@doc:concat( [glam@doc:from_string(<<","/utf8>>), {line, 1}] ) ), glam@doc:nest(_pipe@4, Indent) end, glam@doc:concat( [glam@doc:from_string(<<","/utf8>>), {line, 1}] ), glam@doc:from_string(<<")"/utf8>>)] ), glam@doc:nest(_pipe@5, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ) end, Result = case Comment of {some, C} -> glam@doc:concat([C, {line, 1}, Body]); none -> Body end, {State@4, Decoder} = record_decoder_doc( State@3, Type_name, Object_schema, All_schemas, Config ), {State@5, Encoder} = record_to_json_doc( State@4, Type_name, Object_schema, All_schemas, Config ), Result@1 = glam@doc:concat( [Result, glam@doc:lines(2), Decoder, glam@doc:lines(2), Encoder] ), {State@5, Result@1}. -file("src/gilly/internal/codegen.gleam", 1090). ?DOC(false). -spec simple_type_doc( state(), binary(), gilly@openapi@schema:schema(), list({binary(), gilly@openapi@schema:schema()}), config() ) -> {state(), glam@doc:document()}. simple_type_doc(State, Type_name, Schema, All_schemas, Config) -> {State@1, Gleam_type} = schema_to_gleam_type( State, Schema, true, All_schemas, Type_name ), Result = glam@doc:concat( [glam@doc:from_string( <<<<"pub type "/utf8, Type_name/binary>>/binary, " ="/utf8>> ), begin _pipe = {line, 1}, glam@doc:nest(_pipe, erlang:element(3, Config)) end, begin _pipe@1 = Gleam_type, glam@doc:nest(_pipe@1, erlang:element(3, Config)) end] ), {State@1, Result}. -file("src/gilly/internal/codegen.gleam", 978). ?DOC(false). -spec schema_to_type_doc( state(), binary(), gilly@openapi@schema:schema(), list({binary(), gilly@openapi@schema:schema()}), config() ) -> {state(), glam@doc:document()}. schema_to_type_doc(State, Name, Schema, All_schemas, Config) -> Type_name = to_type_name(Name), case Schema of {object, Base, Object_schema} -> object_type_doc( State, Type_name, Base, Object_schema, All_schemas, Config ); _ -> simple_type_doc(State, Type_name, Schema, All_schemas, Config) end. -file("src/gilly/internal/codegen.gleam", 265). ?DOC(false). -spec generate_schema_docs( state(), list({binary(), gilly@openapi@schema:schema()}), config() ) -> {state(), list(glam@doc:document())}. generate_schema_docs(State, Schemas, Config) -> {State@3, Type_docs} = gleam@list:fold( Schemas, {State, []}, fun(Acc, Entry) -> {State@1, Docs} = Acc, {Name, Schema} = Entry, {State@2, Type_doc} = schema_to_type_doc( State@1, Name, Schema, Schemas, Config ), {State@2, [Type_doc | Docs]} end ), {State@3, lists:reverse(Type_docs)}. -file("src/gilly/internal/codegen.gleam", 187). ?DOC(false). -spec generate_schemas(gilly@openapi@openapi:open_a_p_i(), config()) -> binary(). generate_schemas(Spec, Config) -> Schemas = extract_schemas(Spec), State = default_state(), {State@1, Type_docs} = generate_schema_docs(State, Schemas, Config), Types_code = begin _pipe = glam@doc:join(Type_docs, glam@doc:lines(2)), glam@doc:append(_pipe, {line, 1}) end, State@2 = case gleam@dict:is_empty(erlang:element(3, State@1)) of true -> State@1; false -> _pipe@1 = State@1, _pipe@2 = import_module(_pipe@1, <<"gleam/dynamic/decode"/utf8>>), import_module(_pipe@2, <<"gleam/json"/utf8>>) end, Enums_code = case gleam@dict:is_empty(erlang:element(3, State@2)) of true -> {concat, []}; false -> glam@doc:concat( [glam@doc:lines(2), separator_comment(<<"Enums"/utf8>>), glam@doc:lines(2), enums_doc(State@2, erlang:element(3, Config))] ) end, Code = case gleam@dict:is_empty(erlang:element(2, State@2)) of true -> glam@doc:concat([Types_code, Enums_code]); false -> glam@doc:concat( [imports_doc(State@2, erlang:element(3, Config)), glam@doc:lines(2), Types_code, Enums_code] ) end, glam@doc:to_string(Code, 80). -file("src/gilly/internal/codegen.gleam", 429). ?DOC(false). -spec operation_doc( state(), binary(), binary(), gilly@openapi@operation:operation(), list({binary(), gilly@openapi@schema:schema()}), config() ) -> {state(), glam@doc:document()}. operation_doc(State, Path, Method, Op, All_schemas, Config) -> Indent = erlang:element(3, Config), Fn_name = operation_fn_name(Op, Path, Method), Path_params = gleam@list:filter( erlang:element(6, Op), fun(P) -> erlang:element(3, P) =:= path end ), Query_params = begin _pipe = gleam@list:filter( erlang:element(6, Op), fun(P@1) -> erlang:element(3, P@1) =:= 'query' end ), gleam@list:map(_pipe, fun(P@2) -> case erlang:element(4, Config) of true -> {parameter, erlang:element(2, P@2), erlang:element(3, P@2), erlang:element(4, P@2), false, erlang:element(6, P@2)}; false -> P@2 end end) end, {State@3, Body_param, Inline_body_docs, All_schemas@2} = case erlang:element( 7, Op ) of {some, Rb} -> {State@1, Param} = request_body_param(State, Rb, All_schemas), case Param of {some, {Arg_name, _, {object, Base, Obj_schema}}} -> Request_type_name = case erlang:element(2, Op) of {some, Id} -> <<(to_type_name(Id))/binary, "Request"/utf8>>; none -> <<"RequestBody"/utf8>> end, Enums_before = erlang:element(3, State@1), {State@2, Schema_doc} = schema_to_type_doc( State@1, Request_type_name, {object, Base, Obj_schema}, All_schemas, Config ), New_enums = begin _pipe@1 = maps:to_list(erlang:element(3, State@2)), gleam@list:filter( _pipe@1, fun(Entry) -> not gleam@dict:has_key( Enums_before, erlang:element(1, Entry) ) end ) end, Enum_docs = case New_enums of [] -> []; _ -> Temp_state = {state, erlang:element(2, State@2), maps:from_list(New_enums)}, [enums_doc(Temp_state, erlang:element(3, Config))] end, Ref_schema = {ref, <<"#/components/schemas/"/utf8, Request_type_name/binary>>}, Type_doc = glam@doc:from_string(Request_type_name), All_schemas@1 = [{Request_type_name, {object, Base, Obj_schema}} | All_schemas], {State@2, {some, {Arg_name, Type_doc, Ref_schema}}, lists:append([Enum_docs, [Schema_doc]]), All_schemas@1}; _ -> {State@1, Param, [], All_schemas} end; none -> {State, none, [], All_schemas} end, {State@4, Response_type} = response_type_from_op(State@3, Op, All_schemas@2), Client_arg = glam@doc:from_string(<<"client: Client(err)"/utf8>>), {State@7, Param_args} = gleam@list:fold( Path_params, {State@4, []}, fun(Acc, Param@1) -> {State@5, Args} = Acc, {State@6, Type_doc@1} = param_to_type( State@5, Param@1, All_schemas@2 ), Arg = glam@doc:concat( [glam@doc:from_string( <<(to_field_name(erlang:element(2, Param@1)))/binary, ": "/utf8>> ), Type_doc@1] ), {State@6, lists:append(Args, [Arg])} end ), Query_args = gleam@list:map( Query_params, fun(Param@2) -> Type_str = case erlang:element(5, Param@2) of true -> <<"String"/utf8>>; false -> <<"Option(String)"/utf8>> end, glam@doc:from_string( <<<<(to_field_name(erlang:element(2, Param@2)))/binary, ": "/utf8>>/binary, Type_str/binary>> ) end ), {State@8, Body_args} = case Body_param of {some, {Arg_name@1, Type_doc@2, _}} -> Arg@1 = glam@doc:concat( [glam@doc:from_string(<>), Type_doc@2] ), {State@7, [Arg@1]}; none -> {State@7, []} end, All_args = begin _pipe@2 = [Client_arg], _pipe@3 = lists:append(_pipe@2, Param_args), _pipe@4 = lists:append(_pipe@3, Query_args), lists:append(_pipe@4, Body_args) end, Url_expr = build_url_expression(Path, Path_params), State@9 = import_module(State@8, <<"gleam/dynamic/decode"/utf8>>), State@10 = import_module(State@9, <<"gleam/json"/utf8>>), Has_int_path_param = gleam@list:any( Path_params, fun(P@3) -> case erlang:element(6, P@3) of {some, {integer, _, _}} -> true; _ -> false end end ), State@11 = case Has_int_path_param of true -> import_module(State@10, <<"gleam/int"/utf8>>); false -> State@10 end, Has_optional_query = gleam@list:any( Query_params, fun(P@4) -> not erlang:element(5, P@4) end ), State@12 = case Has_optional_query of true -> _pipe@5 = State@11, _pipe@6 = import_module(_pipe@5, <<"gleam/option"/utf8>>), import_module(_pipe@6, <<"gleam/list"/utf8>>); false -> State@11 end, Req_lines = build_request_lines( State@12, Method, Query_params, Body_param, All_schemas@2 ), {State@13, Decode_line} = build_decode_line( State@12, Response_type, All_schemas@2 ), Body_lines = lists:append([[Url_expr], Req_lines, [Decode_line]]), Return_type = case Response_type of {some, {Type_doc@3, _}} -> glam@doc:concat( [glam@doc:from_string(<<"Result("/utf8>>), Type_doc@3, glam@doc:from_string(<<", ApiError(err))"/utf8>>)] ); none -> glam@doc:from_string(<<"Result(Nil, ApiError(err))"/utf8>>) end, Comment = case erlang:element(3, Op) of {some, Summary} -> glam@doc:concat( [glam@doc:from_string(<<"/// "/utf8, Summary/binary>>), {line, 1}] ); none -> case erlang:element(4, Op) of {some, Desc} -> glam@doc:concat([description_to_comment(Desc), {line, 1}]); none -> {concat, []} end end, Fn_doc = glam@doc:concat( [Comment, glam@doc:from_string( <<<<"pub fn "/utf8, Fn_name/binary>>/binary, "("/utf8>> ), begin _pipe@7 = {line, 1}, glam@doc:nest(_pipe@7, Indent) end, begin _pipe@8 = glam@doc:join( All_args, glam@doc:concat( [glam@doc:from_string(<<","/utf8>>), {line, 1}] ) ), glam@doc:nest(_pipe@8, Indent) end, glam@doc:concat([glam@doc:from_string(<<","/utf8>>), {line, 1}]), glam@doc:from_string(<<") -> "/utf8>>), Return_type, glam@doc:from_string(<<" {"/utf8>>), begin _pipe@9 = {line, 1}, glam@doc:nest(_pipe@9, Indent) end, begin _pipe@10 = glam@doc:join(Body_lines, {line, 1}), glam@doc:nest(_pipe@10, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ), Full_doc = case Inline_body_docs of [] -> Fn_doc; _ -> glam@doc:concat( [glam@doc:join(Inline_body_docs, glam@doc:lines(2)), glam@doc:lines(2), Fn_doc] ) end, {State@13, Full_doc}. -file("src/gilly/internal/codegen.gleam", 400). ?DOC(false). -spec path_item_to_docs( state(), binary(), gilly@openapi@openapi:path_item(), list({binary(), gilly@openapi@schema:schema()}), config() ) -> {state(), list(glam@doc:document())}. path_item_to_docs(State, Path, Path_item, All_schemas, Config) -> Methods = [{<<"Get"/utf8>>, erlang:element(2, Path_item)}, {<<"Post"/utf8>>, erlang:element(3, Path_item)}, {<<"Put"/utf8>>, erlang:element(4, Path_item)}, {<<"Delete"/utf8>>, erlang:element(5, Path_item)}, {<<"Patch"/utf8>>, erlang:element(6, Path_item)}], gleam@list:fold( Methods, {State, []}, fun(Acc, Method_entry) -> {State@1, Docs} = Acc, {Method, Op} = Method_entry, case Op of {some, Operation} -> {State@2, Op_doc} = operation_doc( State@1, Path, Method, Operation, All_schemas, Config ), {State@2, lists:append(Docs, [Op_doc])}; none -> {State@1, Docs} end end ). -file("src/gilly/internal/codegen.gleam", 281). ?DOC(false). -spec generate_operation_docs( state(), gilly@openapi@openapi:open_a_p_i(), list({binary(), gilly@openapi@schema:schema()}), config() ) -> {state(), list(glam@doc:document())}. generate_operation_docs(State, Spec, Schemas, Config) -> State@1 = import_module(State, <<"gleam/http/request"/utf8>>), State@2 = import_module(State@1, <<"gleam/http/response"/utf8>>), State@3 = import_module(State@2, <<"gleam/http"/utf8>>), State@4 = import_module(State@3, <<"gleam/result"/utf8>>), gleam@list:fold( erlang:element(5, Spec), {State@4, []}, fun(Acc, Path_entry) -> {State@5, Docs} = Acc, {Path, Path_item} = Path_entry, {State@6, Path_docs} = path_item_to_docs( State@5, Path, Path_item, Schemas, Config ), {State@6, lists:append(Docs, Path_docs)} end ). -file("src/gilly/internal/codegen.gleam", 113). ?DOC(false). -spec generate(gilly@openapi@openapi:open_a_p_i(), config()) -> binary(). generate(Spec, Config) -> Schemas = extract_schemas(Spec), State = default_state(), {State@1, Type_docs} = generate_schema_docs(State, Schemas, Config), Types_code = begin _pipe = glam@doc:join(Type_docs, glam@doc:lines(2)), glam@doc:append(_pipe, {line, 1}) end, State@2 = case gleam@dict:is_empty(erlang:element(3, State@1)) of true -> State@1; false -> _pipe@1 = State@1, _pipe@2 = import_module(_pipe@1, <<"gleam/dynamic/decode"/utf8>>), import_module(_pipe@2, <<"gleam/json"/utf8>>) end, Enums_code = case gleam@dict:is_empty(erlang:element(3, State@2)) of true -> {concat, []}; false -> glam@doc:concat( [glam@doc:lines(2), separator_comment(<<"Enums"/utf8>>), glam@doc:lines(2), enums_doc(State@2, erlang:element(3, Config))] ) end, {State@3, Op_docs} = generate_operation_docs(State@2, Spec, Schemas, Config), Ops_code = case Op_docs of [] -> {concat, []}; _ -> Base_url = default_base_url(Spec), glam@doc:concat( [glam@doc:lines(2), separator_comment(<<"Operations"/utf8>>), glam@doc:lines(2), api_error_doc(erlang:element(3, Config)), glam@doc:lines(2), client_type_doc( erlang:element(3, Config), erlang:element(4, erlang:element(3, Spec)) ), glam@doc:lines(2), client_new_doc(erlang:element(3, Config), Base_url), glam@doc:lines(2), client_with_base_url_doc(erlang:element(3, Config)), glam@doc:lines(2), glam@doc:join(Op_docs, glam@doc:lines(2)), {line, 1}] ) end, Code = case gleam@dict:is_empty(erlang:element(2, State@3)) of true -> glam@doc:concat([Types_code, Enums_code, Ops_code]); false -> glam@doc:concat( [imports_doc(State@3, erlang:element(3, Config)), glam@doc:lines(2), Types_code, Enums_code, Ops_code] ) end, glam@doc:to_string(Code, 80). -file("src/gilly/internal/codegen.gleam", 235). ?DOC(false). -spec generate_operations(gilly@openapi@openapi:open_a_p_i(), config()) -> binary(). generate_operations(Spec, Config) -> Schemas = extract_schemas(Spec), State = default_state(), {State@1, Op_docs} = generate_operation_docs(State, Spec, Schemas, Config), Code = case Op_docs of [] -> {concat, []}; _ -> Base_url = default_base_url(Spec), glam@doc:concat( [imports_doc(State@1, erlang:element(3, Config)), glam@doc:lines(2), api_error_doc(erlang:element(3, Config)), glam@doc:lines(2), client_type_doc( erlang:element(3, Config), erlang:element(4, erlang:element(3, Spec)) ), glam@doc:lines(2), client_new_doc(erlang:element(3, Config), Base_url), glam@doc:lines(2), client_with_base_url_doc(erlang:element(3, Config)), glam@doc:lines(2), glam@doc:join(Op_docs, glam@doc:lines(2)), {line, 1}] ) end, glam@doc:to_string(Code, 80).