Current section
Files
Jump to
Current section
Files
src/gilly@gilly.erl
-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, with_client_default_parameters/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@common:optionality(),
integer(),
boolean(),
list(binary())}.
-file("src/gilly/gilly.gleam", 29).
?DOC(" Create a new Gilly Builder with default configuration.\n").
-spec new() -> builder().
new() ->
{builder, required_only, 2, false, []}.
-file("src/gilly/gilly.gleam", 39).
?DOC(" Optionality determines how we decide which fields are optional in the generated code.\n").
-spec with_optionality(builder(), gilly@common:optionality()) -> builder().
with_optionality(Builder, Optionality) ->
{builder,
Optionality,
erlang:element(3, Builder),
erlang:element(4, Builder),
erlang:element(5, Builder)}.
-file("src/gilly/gilly.gleam", 44).
?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),
erlang:element(5, Builder)}.
-file("src/gilly/gilly.gleam", 50).
?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,
erlang:element(5, Builder)}.
-file("src/gilly/gilly.gleam", 60).
?DOC(
" Promote certain parameters (by name) to the Client type as defaults.\n"
" When set, these parameters become optional on each Request and are\n"
" automatically filled from the Client unless overridden per-request.\n"
).
-spec with_client_default_parameters(builder(), list(binary())) -> builder().
with_client_default_parameters(Builder, Client_default_parameters) ->
{builder,
erlang:element(2, Builder),
erlang:element(3, Builder),
erlang:element(4, Builder),
Client_default_parameters}.
-file("src/gilly/gilly.gleam", 67).
-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),
erlang:element(5, Builder)}.
-file("src/gilly/gilly.gleam", 96).
?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", 109).
-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", 100).
-spec read_file(binary()) -> {ok, binary()} | {error, gilly@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", 122).
?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 (<version>) from OpenAPI specification at '<source_path>'. DO NOT EDIT.
// To regenerate, run: gilly <path_to_openapi_spec.json>
"/utf8>>,
_pipe@1 = gleam@string:replace(_pipe, <<"<version>"/utf8>>, Version),
gleam@string:replace(_pipe@1, <<"<source_path>"/utf8>>, Source).
-file("src/gilly/gilly.gleam", 80).
?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@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.3.1"/utf8>>, Source),
{ok, <<<<Header/binary, "\n\n"/utf8>>/binary, Code/binary>>}
end
)
end
).