-module(gilly@gilly). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/gilly/gilly.gleam"). -export([new/0, with_optionality/2, with_indent/2, with_optional_query_params/2, generate_code/2, generate_code_from_file/2]). -export_type([builder/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. -opaque builder() :: {builder, gilly@internal@codegen:optionality(), integer(), boolean()}. -file("src/gilly/gilly.gleam", 22). ?DOC(" Create a new Gilly Builder with default configuration.\n"). -spec new() -> builder(). new() -> {builder, required_only, 2, false}. -file("src/gilly/gilly.gleam", 31). ?DOC(" Optionality determines how we decide which fields are optional in the generated code.\n"). -spec with_optionality(builder(), gilly@internal@codegen:optionality()) -> builder(). with_optionality(Builder, Optionality) -> {builder, Optionality, erlang:element(3, Builder), erlang:element(4, Builder)}. -file("src/gilly/gilly.gleam", 36). ?DOC(" Indent configures the number of spaces used for indentation in the generated code.\n"). -spec with_indent(builder(), integer()) -> builder(). with_indent(Builder, Indent) -> {builder, erlang:element(2, Builder), Indent, erlang:element(4, Builder)}. -file("src/gilly/gilly.gleam", 42). ?DOC( " When setting `optional_query_params` to `True`, all query parameters in the generated client will be optional, regardless of whether they are marked as required in the OpenAPI spec or not. \n" " This is useful for non-conforming OpenAPI specs or to allow more flexibility for SDK end-users.\n" ). -spec with_optional_query_params(builder(), boolean()) -> builder(). with_optional_query_params(Builder, Optional_query_params) -> {builder, erlang:element(2, Builder), erlang:element(3, Builder), Optional_query_params}. -file("src/gilly/gilly.gleam", 49). -spec to_codegen_config(builder()) -> gilly@internal@codegen:config(). to_codegen_config(Builder) -> {config, erlang:element(2, Builder), erlang:element(3, Builder), erlang:element(4, Builder)}. -file("src/gilly/gilly.gleam", 77). ?DOC( " Generate Gleam code from an OpenAPI specification record.\n" " See's gilly/openapi/openapi.gleam for more info on building OpenAPI records.\n" ). -spec generate_code(builder(), gilly@openapi@openapi:open_a_p_i()) -> binary(). generate_code(Builder, Spec) -> gilly@internal@codegen:generate(Spec, to_codegen_config(Builder)). -file("src/gilly/gilly.gleam", 90). -spec is_supported_file_type(binary()) -> boolean(). is_supported_file_type(Source) -> Extension = begin _pipe = gleam@string:split(Source, <<"."/utf8>>), _pipe@1 = gleam@list:last(_pipe), gleam@result:unwrap(_pipe@1, <<""/utf8>>) end, gleam@list:contains([<<"json"/utf8>>], Extension). -file("src/gilly/gilly.gleam", 81). -spec read_file(binary()) -> {ok, binary()} | {error, gilly@internal@error:error()}. read_file(Source) -> gleam@bool:guard( not is_supported_file_type(Source), {error, {unsupported_file_type, Source, [<<"json"/utf8>>]}}, fun() -> _pipe = simplifile:read(Source), gleam@result:map_error( _pipe, fun(_capture) -> {reading_file, Source, _capture} end ) end ). -file("src/gilly/gilly.gleam", 103). ?DOC(" Generate a header comment for the generated code file.\n"). -spec generate_header(binary(), binary()) -> binary(). generate_header(Version, Source) -> _pipe = <<"// Code generated by Gilly () from OpenAPI specification at ''. DO NOT EDIT. // To regenerate, run: gilly "/utf8>>, _pipe@1 = gleam@string:replace(_pipe, <<""/utf8>>, Version), gleam@string:replace(_pipe@1, <<""/utf8>>, Source). -file("src/gilly/gilly.gleam", 61). ?DOC( " Generate Gleam code from an OpenAPI specification file.\n" " \n" " Source should be a path to a JSON file containing the OpenAPI spec.\n" " Returns the generated code as a string, or an error if something goes wrong.\n" ). -spec generate_code_from_file(builder(), binary()) -> {ok, binary()} | {error, gilly@internal@error:error()}. generate_code_from_file(Builder, Source) -> gleam@result:'try'( read_file(Source), fun(Content) -> gleam@result:'try'( begin _pipe = gilly@openapi@openapi:from_json_string(Content), gleam@result:map_error( _pipe, fun(_capture) -> {parsing_open_a_p_i, Source, _capture} end ) end, fun(Spec) -> Code = generate_code(Builder, Spec), Header = generate_header(<<"0.2.0"/utf8>>, Source), {ok, <<<
>/binary, Code/binary>>} end ) end ).