-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, schema_usage/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 schema_usage() :: request_only | response_only | both | unreferenced. -type state() :: {state, gleam@dict:dict(binary(), gleam@set:set(binary())), gleam@dict:dict(binary(), list(binary())), gleam@dict:dict(binary(), schema_usage())}. -file("src/gilly/internal/codegen.gleam", 62). ?DOC(false). -spec default_state() -> state(). default_state() -> {state, maps:new(), maps:new(), maps:new()}. -file("src/gilly/internal/codegen.gleam", 66). ?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), erlang:element(4, State)}. -file("src/gilly/internal/codegen.gleam", 74). ?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), erlang:element(4, State)}. -file("src/gilly/internal/codegen.gleam", 85). ?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), erlang:element(4, State)}. -file("src/gilly/internal/codegen.gleam", 93). ?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", 321). ?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", 330). ?DOC(false). -spec ref_to_raw_name(binary()) -> gleam@option:option(binary()). ref_to_raw_name(Ref) -> case gleam@string:split(Ref, <<"/"/utf8>>) of [_, _, _, Name | _] -> {some, Name}; _ -> none end. -file("src/gilly/internal/codegen.gleam", 338). ?DOC(false). -spec merge_usage(schema_usage(), schema_usage()) -> schema_usage(). merge_usage(Existing, New) -> case {Existing, New} of {both, _} -> both; {_, both} -> both; {request_only, response_only} -> both; {response_only, request_only} -> both; {request_only, request_only} -> request_only; {response_only, response_only} -> response_only; {unreferenced, Other} -> Other; {Other, unreferenced} -> Other end. -file("src/gilly/internal/codegen.gleam", 349). ?DOC(false). -spec record_schema_ref( gleam@dict:dict(binary(), schema_usage()), gilly@openapi@schema:schema(), schema_usage() ) -> gleam@dict:dict(binary(), schema_usage()). record_schema_ref(Usages, Schema, Usage) -> case Schema of {ref, Ref} -> case ref_to_raw_name(Ref) of {some, Name} -> gleam@dict:upsert( Usages, Name, fun(Existing) -> case Existing of {some, Prev} -> merge_usage(Prev, Usage); none -> Usage end end ); none -> Usages end; _ -> Usages end. -file("src/gilly/internal/codegen.gleam", 435). ?DOC(false). -spec get_schema_usage(state(), binary()) -> schema_usage(). get_schema_usage(State, Raw_name) -> case gleam_stdlib:map_get(erlang:element(4, State), Raw_name) of {ok, Usage} -> Usage; {error, _} -> unreferenced end. -file("src/gilly/internal/codegen.gleam", 444). ?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", 484). ?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", 509). ?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", 528). ?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", 1015). ?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", 1048). ?DOC(false). -spec query_param_to_string_expr(gilly@openapi@operation:parameter()) -> binary(). query_param_to_string_expr(Param) -> case erlang:element(6, Param) of {some, {integer, _, _}} -> <<"int.to_string(v)"/utf8>>; {some, {number, _}} -> <<"float.to_string(v)"/utf8>>; {some, {boolean, _}} -> <<"bool.to_string(v)"/utf8>>; _ -> <<"v"/utf8>> end. -file("src/gilly/internal/codegen.gleam", 1112). ?DOC(false). -spec result_or({ok, GEI} | {error, GEJ}, fun(() -> {ok, GEI} | {error, GEJ})) -> {ok, GEI} | {error, GEJ}. result_or(Result, Alternative) -> case Result of {ok, _} -> Result; {error, _} -> Alternative() end. -file("src/gilly/internal/codegen.gleam", 394). ?DOC(false). -spec collect_operation_usages( gleam@dict:dict(binary(), schema_usage()), gilly@openapi@operation:operation() ) -> gleam@dict:dict(binary(), schema_usage()). collect_operation_usages(Usages, Op) -> Usages@1 = case erlang:element(7, Op) of {some, Rb} -> 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} -> record_schema_ref(Usages, Body_schema, request_only); none -> Usages end; {error, _} -> Usages end; none -> Usages end, 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@1} -> case erlang:element(2, Media_type@1) of {some, Resp_schema} -> record_schema_ref( Usages@1, Resp_schema, response_only ); none -> Usages@1 end; {error, _} -> Usages@1 end; {error, _} -> Usages@1 end. -file("src/gilly/internal/codegen.gleam", 371). ?DOC(false). -spec collect_schema_usages(gilly@openapi@openapi:open_a_p_i()) -> gleam@dict:dict(binary(), schema_usage()). collect_schema_usages(Spec) -> gleam@list:fold( erlang:element(5, Spec), maps:new(), fun(Usages, Path_entry) -> {_, Path_item} = Path_entry, Operations = begin _pipe = [erlang:element(2, Path_item), erlang:element(3, Path_item), erlang:element(4, Path_item), erlang:element(5, Path_item), erlang:element(6, Path_item)], gleam@list:filter_map(_pipe, fun(Op) -> case Op of {some, O} -> {ok, O}; none -> {error, nil} end end) end, gleam@list:fold( Operations, Usages, fun(Usages@1, Op@1) -> collect_operation_usages(Usages@1, Op@1) end ) end ). -file("src/gilly/internal/codegen.gleam", 1190). ?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", 1677). ?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", 1687). ?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", 2022). ?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", 2004). ?DOC(false). -spec is_field_optional(gilly@openapi@schema:schema(), boolean(), optionality()) -> boolean(). is_field_optional(Prop_schema, In_required, Optionality) -> case Prop_schema of {object, _, _} -> true; _ -> 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 end. -file("src/gilly/internal/codegen.gleam", 2055). ?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", 457). ?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", 2036). ?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", 2043). ?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", 2063). ?DOC(false). -spec to_type_name(binary()) -> binary(). to_type_name(Name) -> justin:pascal_case(Name). -file("src/gilly/internal/codegen.gleam", 1633). ?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", 1856). ?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", 1921). ?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", 2067). ?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", 1154). ?DOC(false). -spec split_path_on_params( binary(), list(gilly@openapi@operation:parameter()), binary(), list(glam@doc:document()) ) -> list(glam@doc:document()). split_path_on_params(Remaining, Params, Params_prefix, Acc) -> case find_next_param(Remaining, Params) of {some, {Before, Param, After}} -> Param_name = <>, 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, Params_prefix, 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", 1144). ?DOC(false). -spec build_path_parts( binary(), list(gilly@openapi@operation:parameter()), binary() ) -> glam@doc:document(). build_path_parts(Path, Params, Params_prefix) -> Parts = split_path_on_params(Path, Params, Params_prefix, []), glam@doc:join(Parts, glam@doc:from_string(<<" <> "/utf8>>)). -file("src/gilly/internal/codegen.gleam", 1122). ?DOC(false). -spec build_url_expression( binary(), list(gilly@openapi@operation:parameter()), binary() ) -> glam@doc:document(). build_url_expression(Path, Path_params, Params_prefix) -> 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, Params_prefix), 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", 2083). ?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", 2093). ?DOC(false). -spec enum_variant_name(binary(), binary()) -> binary(). enum_variant_name(Type_name, Variant) -> <>. -file("src/gilly/internal/codegen.gleam", 2097). ?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", 1720). ?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", 2449). ?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", 2474). ?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", 2494). ?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", 2538). ?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", 2565). ?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", 2454). ?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", 1949). ?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", 1892). ?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", 1208). ?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()}), binary() ) -> list(glam@doc:document()). build_request_lines( State, Method, Query_params, Body_param, All_schemas, Params_prefix ) -> 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 [] -> []; _ -> Is_array_param = fun(P) -> case erlang:element(6, P) of {some, {array, _, _}} -> true; _ -> false end end, Required_scalar_entries = begin _pipe = gleam@list:filter( Query_params, fun(P@1) -> erlang:element(5, P@1) andalso not Is_array_param(P@1) end ), gleam@list:map( _pipe, fun(Param) -> Param_name = <>, Value_expr = case query_param_to_string_expr(Param) of <<"v"/utf8>> -> Param_name; Conv -> gleam@string:replace( Conv, <<"v"/utf8>>, Param_name ) end, glam@doc:from_string( <<<<<<<<"#(\""/utf8, (erlang:element(2, Param))/binary>>/binary, "\", "/utf8>>/binary, Value_expr/binary>>/binary, ")"/utf8>> ) end ) end, Required_array_lines = begin _pipe@1 = gleam@list:filter( Query_params, fun(P@2) -> erlang:element(5, P@2) andalso Is_array_param(P@2) end ), gleam@list:map( _pipe@1, fun(Param@1) -> Param_name@1 = <>, Value_expr@1 = query_param_to_string_expr(Param@1), glam@doc:from_string( <<<<<<<<<<<<"let query = list.append(query, list.map("/utf8, Param_name@1/binary>>/binary, ", fn(v) { #(\""/utf8>>/binary, (erlang:element(2, Param@1))/binary>>/binary, "\", "/utf8>>/binary, Value_expr@1/binary>>/binary, ") }))"/utf8>> ) end ) end, Optional_scalar_entries = begin _pipe@2 = gleam@list:filter( Query_params, fun(P@3) -> not erlang:element(5, P@3) andalso not Is_array_param( P@3 ) end ), gleam@list:map( _pipe@2, fun(Param@2) -> Param_name@2 = <>, Value_expr@2 = query_param_to_string_expr(Param@2), glam@doc:from_string( <<<<<<<<<<<<"option.map("/utf8, Param_name@2/binary>>/binary, ", fn(v) { #(\""/utf8>>/binary, (erlang:element(2, Param@2))/binary>>/binary, "\", "/utf8>>/binary, Value_expr@2/binary>>/binary, ") })"/utf8>> ) end ) end, Optional_array_lines = begin _pipe@3 = gleam@list:filter( Query_params, fun(P@4) -> not erlang:element(5, P@4) andalso Is_array_param(P@4) end ), gleam@list:map( _pipe@3, fun(Param@3) -> Param_name@3 = <>, Value_expr@3 = query_param_to_string_expr(Param@3), glam@doc:from_string( <<<<<<<<<<<<"let query = list.append(query, "/utf8, Param_name@3/binary>>/binary, " |> option.unwrap([]) |> list.map(fn(v) { #(\""/utf8>>/binary, (erlang:element(2, Param@3))/binary>>/binary, "\", "/utf8>>/binary, Value_expr@3/binary>>/binary, ") }))"/utf8>> ) end ) end, Required_list = case Required_scalar_entries of [] -> glam@doc:from_string(<<"[]"/utf8>>); _ -> glam@doc:concat( [glam@doc:from_string(<<"["/utf8>>), glam@doc:join( Required_scalar_entries, glam@doc:from_string(<<", "/utf8>>) ), glam@doc:from_string(<<"]"/utf8>>)] ) end, Optional_scalar_part = case Optional_scalar_entries of [] -> []; _ -> [glam@doc:from_string( <<"let query = list.append(query, option.values(["/utf8>> ) | lists:append( gleam@list:map( Optional_scalar_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] )], Required_array_lines, Optional_scalar_part, Optional_array_lines, [glam@doc:from_string( <<"let req = request.set_query(req, query)"/utf8>> )]] ) end, _pipe@4 = [Method_line, Header_line], _pipe@5 = lists:append(_pipe@4, Body_line), lists:append(_pipe@5, Query_lines). -file("src/gilly/internal/codegen.gleam", 1974). ?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", 1871). ?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", 1822). ?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", 1788). ?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", 2197). ?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", 1740). ?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", 1701). ?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", 1361). ?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", 1763). ?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", 2103). ?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", 1653). ?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", 1607). ?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", 1029). ?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, true, All_schemas, erlang:element(2, Param) ); none -> {State, glam@doc:from_string(<<"String"/utf8>>)} end. -file("src/gilly/internal/codegen.gleam", 826). ?DOC(false). -spec operation_params_docs( state(), binary(), list(gilly@openapi@operation:parameter()), list(gilly@openapi@operation:parameter()), list({binary(), gilly@openapi@schema:schema()}), config() ) -> {state(), list(glam@doc:document())}. operation_params_docs( State, Type_name, Path_params, Query_params, All_schemas, Config ) -> Indent = erlang:element(3, Config), All_params = lists:append(Path_params, Query_params), {State@3, Fields@1} = gleam@list:fold( All_params, {State, []}, fun(Acc, Param) -> {State@1, Fields} = Acc, Field_name = to_field_name(erlang:element(2, Param)), {State@2, Type_doc} = param_to_type(State@1, Param, All_schemas), Field_doc = case erlang:element(5, Param) of true -> glam@doc:concat( [glam@doc:from_string(<>), Type_doc] ); false -> glam@doc:concat( [glam@doc:from_string( <> ), Type_doc, glam@doc:from_string(<<")"/utf8>>)] ) end, {State@2, lists:append(Fields, [Field_doc])} end ), Has_optional = gleam@list:any( All_params, fun(P) -> not erlang:element(5, P) end ), State@4 = case Has_optional of true -> import_qualified( State@3, <<"gleam/option"/utf8>>, <<"type Option"/utf8>> ); false -> State@3 end, Type_doc@1 = case Fields@1 of [] -> glam@doc:concat( [glam@doc:from_string( <<<<"pub opaque 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 opaque 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@1, 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, Required_params = gleam@list:filter( All_params, fun(P@1) -> erlang:element(5, P@1) end ), Optional_params = gleam@list:filter( All_params, fun(P@2) -> not erlang:element(5, P@2) end ), State@5 = case gleam@list:is_empty(Optional_params) of true -> State@4; false -> import_qualified(State@4, <<"gleam/option"/utf8>>, <<"None"/utf8>>) end, {State@8, Required_args} = gleam@list:fold( Required_params, {State@5, []}, fun(Acc@1, Param@1) -> {State@6, Args} = Acc@1, Field_name@1 = to_field_name(erlang:element(2, Param@1)), {State@7, Type_doc@2} = param_to_type(State@6, Param@1, All_schemas), Arg = glam@doc:concat( [glam@doc:from_string( <<<<<>/binary, Field_name@1/binary>>/binary, ": "/utf8>> ), Type_doc@2] ), {State@7, lists:append(Args, [Arg])} end ), Field_inits = gleam@list:map( All_params, fun(Param@2) -> Field_name@2 = to_field_name(erlang:element(2, Param@2)), case erlang:element(5, Param@2) of true -> <>; false -> <> end end ), Constructor_body = glam@doc:from_string( <<<<<>/binary, (gleam@string:join(Field_inits, <<", "/utf8>>))/binary>>/binary, ")"/utf8>> ), Fn_name = <<"new_"/utf8, (justin:snake_case(Type_name))/binary>>, Constructor_doc = case Required_args of [] -> glam@doc:concat( [glam@doc:from_string( <<<<<<<<"pub fn "/utf8, Fn_name/binary>>/binary, "() -> "/utf8>>/binary, Type_name/binary>>/binary, " {"/utf8>> ), begin _pipe@6 = {line, 1}, glam@doc:nest(_pipe@6, Indent) end, begin _pipe@7 = Constructor_body, glam@doc:nest(_pipe@7, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ); _ -> glam@doc:concat( [glam@doc:from_string( <<<<"pub fn "/utf8, Fn_name/binary>>/binary, "("/utf8>> ), begin _pipe@8 = {line, 1}, glam@doc:nest(_pipe@8, Indent) end, begin _pipe@9 = glam@doc:join( Required_args, glam@doc:concat( [glam@doc:from_string(<<","/utf8>>), {line, 1}] ) ), glam@doc:nest(_pipe@9, Indent) end, glam@doc:concat( [glam@doc:from_string(<<","/utf8>>), {line, 1}] ), glam@doc:from_string( <<<<") -> "/utf8, Type_name/binary>>/binary, " {"/utf8>> ), begin _pipe@10 = {line, 1}, glam@doc:nest(_pipe@10, Indent) end, begin _pipe@11 = Constructor_body, glam@doc:nest(_pipe@11, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ) end, Var_name = justin:snake_case(Type_name), {State@12, Setters@1} = gleam@list:fold( Optional_params, {State@8, []}, fun(Acc@2, Param@3) -> {State@9, Setters} = Acc@2, Field_name@3 = to_field_name(erlang:element(2, Param@3)), Setter_fn_name = <<<<(justin:snake_case(Type_name))/binary, "_with_"/utf8>>/binary, Field_name@3/binary>>, {State@10, Inner_type} = param_to_type( State@9, Param@3, All_schemas ), State@11 = import_qualified( State@10, <<"gleam/option"/utf8>>, <<"Some"/utf8>> ), Comment_part = case erlang:element(4, Param@3) of {some, Desc} -> [description_to_comment(Desc), {line, 1}]; none -> [] end, Setter_doc = glam@doc:concat( lists:append( Comment_part, [glam@doc:from_string( <<<<"pub fn "/utf8, Setter_fn_name/binary>>/binary, "("/utf8>> ), begin _pipe@12 = {line, 1}, glam@doc:nest(_pipe@12, Indent) end, begin _pipe@13 = glam@doc:from_string( <<<<<>/binary, Type_name/binary>>/binary, ","/utf8>> ), glam@doc:nest(_pipe@13, Indent) end, begin _pipe@14 = {line, 1}, glam@doc:nest(_pipe@14, Indent) end, begin _pipe@15 = glam@doc:concat( [glam@doc:from_string( <<<<<>/binary, Field_name@3/binary>>/binary, ": "/utf8>> ), Inner_type, glam@doc:from_string(<<","/utf8>>)] ), glam@doc:nest(_pipe@15, Indent) end, {line, 1}, glam@doc:from_string( <<<<") -> "/utf8, Type_name/binary>>/binary, " {"/utf8>> ), begin _pipe@16 = {line, 1}, glam@doc:nest(_pipe@16, Indent) end, begin _pipe@17 = glam@doc:from_string( <<<<<<<<<<<<<>/binary, Var_name/binary>>/binary, ", "/utf8>>/binary, Field_name@3/binary>>/binary, ": Some("/utf8>>/binary, Field_name@3/binary>>/binary, "))"/utf8>> ), glam@doc:nest(_pipe@17, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ) ), {State@11, lists:append(Setters, [Setter_doc])} end ), All_docs = begin _pipe@18 = [Type_doc@1, Constructor_doc], lists:append(_pipe@18, Setters@1) end, {State@12, All_docs}. -file("src/gilly/internal/codegen.gleam", 1057). ?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", 1077). ?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", 1587). ?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", 2270). ?DOC(false). -spec record_new_doc( state(), binary(), gilly@openapi@schema:object_schema(), list({binary(), gilly@openapi@schema:schema()}), config() ) -> {state(), glam@doc:document()}. record_new_doc(State, Type_name, Object_schema, All_schemas, Config) -> Fn_name = <<"new_"/utf8, (justin:snake_case(Type_name))/binary>>, Indent = erlang:element(3, Config), Field_info = gleam@list:map( erlang:element(3, Object_schema), fun(Prop) -> {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) ), {Prop_name, Field_name, Prop_schema, Optional} end ), Has_optional = gleam@list:any( Field_info, fun(F) -> erlang:element(4, F) end ), State@1 = case Has_optional of true -> import_qualified(State, <<"gleam/option"/utf8>>, <<"None"/utf8>>); false -> State end, {State@4, Required_args} = gleam@list:fold( Field_info, {State@1, []}, fun(Acc, F@1) -> {State@2, Args} = Acc, {Prop_name@1, Field_name@1, Prop_schema@1, Optional@1} = F@1, case Optional@1 of true -> {State@2, Args}; false -> Enum_hint = <>, {State@3, Field_type} = schema_to_gleam_type( State@2, Prop_schema@1, true, All_schemas, Enum_hint ), Arg = glam@doc:concat( [glam@doc:from_string( <<<<<>/binary, Field_name@1/binary>>/binary, ": "/utf8>> ), Field_type] ), {State@3, lists:append(Args, [Arg])} end end ), Field_inits = gleam@list:map( Field_info, fun(F@2) -> {_, Field_name@2, _, Optional@2} = F@2, case Optional@2 of true -> <>; false -> <> end end ), Constructor_body = case Field_inits of [] -> glam@doc:from_string(Type_name); _ -> glam@doc:from_string( <<<<<>/binary, (gleam@string:join(Field_inits, <<", "/utf8>>))/binary>>/binary, ")"/utf8>> ) end, Result = case Required_args of [] -> glam@doc:concat( [glam@doc:from_string( <<<<<<<<"pub fn "/utf8, Fn_name/binary>>/binary, "() -> "/utf8>>/binary, Type_name/binary>>/binary, " {"/utf8>> ), begin _pipe = {line, 1}, glam@doc:nest(_pipe, Indent) end, begin _pipe@1 = Constructor_body, 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>> ), begin _pipe@2 = {line, 1}, glam@doc:nest(_pipe@2, Indent) end, begin _pipe@3 = glam@doc:join( Required_args, glam@doc:concat( [glam@doc:from_string(<<","/utf8>>), {line, 1}] ) ), glam@doc:nest(_pipe@3, Indent) end, glam@doc:concat( [glam@doc:from_string(<<","/utf8>>), {line, 1}] ), glam@doc:from_string( <<<<") -> "/utf8, Type_name/binary>>/binary, " {"/utf8>> ), begin _pipe@4 = {line, 1}, glam@doc:nest(_pipe@4, Indent) end, begin _pipe@5 = Constructor_body, glam@doc:nest(_pipe@5, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ) end, {State@4, Result}. -file("src/gilly/internal/codegen.gleam", 2374). ?DOC(false). -spec record_with_field_docs( state(), binary(), gilly@openapi@schema:object_schema(), list({binary(), gilly@openapi@schema:schema()}), config() ) -> {state(), list(glam@doc:document())}. record_with_field_docs(State, Type_name, Object_schema, All_schemas, Config) -> Indent = erlang:element(3, Config), Var_name = justin:snake_case(Type_name), gleam@list:fold( erlang:element(3, Object_schema), {State, []}, fun(Acc, Prop) -> {State@1, Docs} = 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) ), case Optional of false -> {State@1, Docs}; true -> Fn_name = <<<<(justin:snake_case(Type_name))/binary, "_with_"/utf8>>/binary, Field_name/binary>>, Enum_hint = <>, {State@2, Inner_type} = schema_to_gleam_type( State@1, Prop_schema, true, All_schemas, Enum_hint ), State@3 = import_qualified( State@2, <<"gleam/option"/utf8>>, <<"Some"/utf8>> ), Comment_part = case schema_description(Prop_schema) of {some, Comment} -> [Comment, {line, 1}]; none -> [] end, Setter_doc = glam@doc:concat( lists:append( Comment_part, [glam@doc:from_string( <<<<"pub fn "/utf8, Fn_name/binary>>/binary, "("/utf8>> ), begin _pipe = {line, 1}, glam@doc:nest(_pipe, Indent) end, begin _pipe@1 = glam@doc:from_string( <<<<<>/binary, Type_name/binary>>/binary, ","/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:concat( [glam@doc:from_string( <<<<<>/binary, Field_name/binary>>/binary, ": "/utf8>> ), Inner_type, glam@doc:from_string(<<","/utf8>>)] ), glam@doc:nest(_pipe@3, Indent) end, {line, 1}, glam@doc:from_string( <<<<") -> "/utf8, Type_name/binary>>/binary, " {"/utf8>> ), begin _pipe@4 = {line, 1}, glam@doc:nest(_pipe@4, Indent) end, begin _pipe@5 = glam@doc:from_string( <<<<<<<<<<<<<>/binary, Var_name/binary>>/binary, ", "/utf8>>/binary, Field_name/binary>>/binary, ": Some("/utf8>>/binary, Field_name/binary>>/binary, "))"/utf8>> ), glam@doc:nest(_pipe@5, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ) ), {State@3, lists:append(Docs, [Setter_doc])} end end ). -file("src/gilly/internal/codegen.gleam", 1427). ?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(), schema_usage() ) -> {state(), glam@doc:document()}. object_type_doc( State, Type_name, Base, Object_schema, All_schemas, Config, Usage ) -> Comment = base_schema_comment(Base), Is_opaque = case Usage of request_only -> true; response_only -> false; both -> false; unreferenced -> false end, Needs_decoder = case Usage of request_only -> false; response_only -> true; both -> true; unreferenced -> true end, Needs_encoder = case Usage of response_only -> false; request_only -> true; both -> true; unreferenced -> true end, Needs_constructor = case Usage of response_only -> false; request_only -> true; both -> true; unreferenced -> true end, Needs_setters = case Usage of response_only -> false; request_only -> true; both -> true; unreferenced -> true end, {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), Type_keyword = case Is_opaque of true -> <<"pub opaque type "/utf8>>; false -> <<"pub type "/utf8>> end, Body = case Fields@2 of [] -> glam@doc:concat( [glam@doc:from_string( <<<>/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( <<<>/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@5, Result@1} = case Needs_decoder of true -> {State@4, Decoder} = record_decoder_doc( State@3, Type_name, Object_schema, All_schemas, Config ), {State@4, glam@doc:concat([Result, glam@doc:lines(2), Decoder])}; false -> {State@3, Result} end, {State@7, Result@2} = case Needs_encoder of true -> {State@6, Encoder} = record_to_json_doc( State@5, Type_name, Object_schema, All_schemas, Config ), {State@6, glam@doc:concat([Result@1, glam@doc:lines(2), Encoder])}; false -> {State@5, Result@1} end, {State@9, Result@3} = case Needs_constructor of true -> {State@8, Constructor} = record_new_doc( State@7, Type_name, Object_schema, All_schemas, Config ), {State@8, glam@doc:concat([Result@2, glam@doc:lines(2), Constructor])}; false -> {State@7, Result@2} end, {State@11, Result@4} = case Needs_setters of true -> {State@10, Setters} = record_with_field_docs( State@9, Type_name, Object_schema, All_schemas, Config ), Setter_docs = case Setters of [] -> {concat, []}; _ -> glam@doc:concat( [glam@doc:lines(2), glam@doc:join(Setters, glam@doc:lines(2))] ) end, {State@10, glam@doc:concat([Result@3, Setter_docs])}; false -> {State@9, Result@3} end, {State@11, Result@4}. -file("src/gilly/internal/codegen.gleam", 1401). ?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), Usage = get_schema_usage(State, Name), case Schema of {object, Base, Object_schema} -> object_type_doc( State, Type_name, Base, Object_schema, All_schemas, Config, Usage ); _ -> simple_type_doc(State, Type_name, Schema, All_schemas, Config) end. -file("src/gilly/internal/codegen.gleam", 285). ?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", 207). ?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", 564). ?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, Has_params = not gleam@list:is_empty(Path_params) orelse not gleam@list:is_empty( Query_params ), {State@4, 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, State@2 = {state, erlang:element(2, State@1), erlang:element(3, State@1), gleam@dict:insert( erlang:element(4, State@1), Request_type_name, request_only )}, Enums_before = erlang:element(3, State@2), {State@3, Schema_doc} = schema_to_type_doc( State@2, Request_type_name, {object, Base, Obj_schema}, All_schemas, Config ), New_enums = begin _pipe@1 = maps:to_list(erlang:element(3, State@3)), 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@3), maps:from_list(New_enums), erlang:element(4, State@3)}, [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@3, {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@5, Response_type} = response_type_from_op(State@4, Op, All_schemas@2), Params_type_name = <<(to_type_name(Fn_name))/binary, "Params"/utf8>>, {State@7, Params_docs, Params_prefix} = case Has_params of true -> {State@6, Pdocs} = operation_params_docs( State@5, Params_type_name, Path_params, Query_params, All_schemas@2, Config ), {State@6, Pdocs, <<"params."/utf8>>}; false -> {State@5, [], <<""/utf8>>} end, Client_arg = glam@doc:from_string(<<"client: Client(err)"/utf8>>), Params_arg = case Has_params of true -> [glam@doc:from_string( <<"params params: "/utf8, Params_type_name/binary>> )]; false -> [] end, {State@8, Body_args} = case Body_param of {some, {Arg_name@1, Type_doc@1, _}} -> Arg = glam@doc:concat( [glam@doc:from_string( <<<<<>/binary, Arg_name@1/binary>>/binary, ": "/utf8>> ), Type_doc@1] ), {State@7, [Arg]}; none -> {State@7, []} end, All_args = begin _pipe@2 = [Client_arg], _pipe@3 = lists:append(_pipe@2, Params_arg), lists:append(_pipe@3, Body_args) end, Url_expr = build_url_expression(Path, Path_params, Params_prefix), 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 ), Has_int_query_param = gleam@list:any( Query_params, fun(P@4) -> case erlang:element(6, P@4) of {some, {integer, _, _}} -> true; _ -> false end end ), State@11 = case Has_int_path_param orelse Has_int_query_param of true -> import_module(State@10, <<"gleam/int"/utf8>>); false -> State@10 end, Has_optional_query = gleam@list:any( Query_params, fun(P@5) -> not erlang:element(5, P@5) end ), State@12 = case Has_optional_query of true -> _pipe@4 = State@11, _pipe@5 = import_module(_pipe@4, <<"gleam/option"/utf8>>), import_module(_pipe@5, <<"gleam/list"/utf8>>); false -> State@11 end, Req_lines = build_request_lines( State@12, Method, Query_params, Body_param, All_schemas@2, Params_prefix ), {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@2, _}} -> glam@doc:concat( [glam@doc:from_string(<<"Result("/utf8>>), Type_doc@2, 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@6 = {line, 1}, glam@doc:nest(_pipe@6, Indent) end, begin _pipe@7 = glam@doc:join( All_args, glam@doc:concat( [glam@doc:from_string(<<","/utf8>>), {line, 1}] ) ), glam@doc:nest(_pipe@7, 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@8 = {line, 1}, glam@doc:nest(_pipe@8, Indent) end, begin _pipe@9 = glam@doc:join(Body_lines, {line, 1}), glam@doc:nest(_pipe@9, Indent) end, {line, 1}, glam@doc:from_string(<<"}"/utf8>>)] ), Prefix_docs = lists:append([Params_docs, Inline_body_docs]), Full_doc = case Prefix_docs of [] -> Fn_doc; _ -> glam@doc:concat( [glam@doc:join(Prefix_docs, glam@doc:lines(2)), glam@doc:lines(2), Fn_doc] ) end, {State@13, Full_doc}. -file("src/gilly/internal/codegen.gleam", 535). ?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", 301). ?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", 129). ?DOC(false). -spec generate(gilly@openapi@openapi:open_a_p_i(), config()) -> binary(). generate(Spec, Config) -> Schemas = extract_schemas(Spec), State = default_state(), Schema_usages = collect_schema_usages(Spec), State@1 = {state, erlang:element(2, State), erlang:element(3, State), Schema_usages}, {State@2, Type_docs} = generate_schema_docs(State@1, Schemas, Config), Types_code = begin _pipe = glam@doc:join(Type_docs, glam@doc:lines(2)), glam@doc:append(_pipe, {line, 1}) end, State@3 = case gleam@dict:is_empty(erlang:element(3, State@2)) of true -> State@2; false -> _pipe@1 = State@2, _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@3)) of true -> {concat, []}; false -> glam@doc:concat( [glam@doc:lines(2), separator_comment(<<"Enums"/utf8>>), glam@doc:lines(2), enums_doc(State@3, erlang:element(3, Config))] ) end, {State@4, Op_docs} = generate_operation_docs(State@3, 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@4)) of true -> glam@doc:concat([Types_code, Enums_code, Ops_code]); false -> glam@doc:concat( [imports_doc(State@4, 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", 255). ?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).