Packages
oaspec
0.61.0
0.68.0
0.67.0
0.66.0
0.65.0
0.64.0
0.63.0
0.62.0
0.61.0
0.60.0
0.59.0
0.58.1
0.58.0
0.57.0
0.56.0
0.55.0
0.54.0
0.53.0
0.52.0
0.51.0
0.50.0
0.49.0
0.48.0
0.47.0
0.46.0
0.45.0
0.44.0
0.43.0
0.42.0
0.41.0
0.40.0
0.39.0
0.38.0
0.37.0
0.36.0
0.35.0
0.34.0
0.33.0
0.32.0
0.31.0
0.30.0
0.29.0
0.28.0
0.27.0
0.26.0
0.25.0
0.24.0
0.23.0
0.22.0
0.21.0
0.20.0
0.19.0
0.18.0
0.17.0
0.16.0
0.15.0
0.14.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.0
0.7.0
0.6.3
0.6.1
0.6.0
0.5.0
0.4.0
0.3.0
0.1.3
Generate Gleam code from OpenAPI 3.x specifications
Current section
Files
Jump to
Current section
Files
src/oaspec@internal@codegen@schema_dispatch.erl
-module(oaspec@internal@codegen@schema_dispatch).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oaspec/internal/codegen/schema_dispatch.gleam").
-export([to_string_expr/2, schema_ref_to_string_expr/3, decoder_expr/1, json_encoder_expr/2, json_encoder_fn/1, to_string_fn/2, schema_type_qualified/1, schema_base_type/1, schema_type/1, schema_ref_qualified_type/1, resolve_param_type/2]).
-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).
-file("src/oaspec/internal/codegen/schema_dispatch.gleam", 86).
?DOC(false).
-spec to_string_expr(oaspec@internal@openapi@schema:schema_object(), binary()) -> binary().
to_string_expr(Schema, Value) ->
case Schema of
{integer_schema, _, _, _, _, _, _, _} ->
<<<<"int.to_string("/utf8, Value/binary>>/binary, ")"/utf8>>;
{number_schema, _, _, _, _, _, _, _} ->
<<<<"float.to_string("/utf8, Value/binary>>/binary, ")"/utf8>>;
{boolean_schema, _} ->
<<<<"bool.to_string("/utf8, Value/binary>>/binary, ")"/utf8>>;
{string_schema, _, _, _, _, _, _} ->
Value;
_ ->
Value
end.
-file("src/oaspec/internal/codegen/schema_dispatch.gleam", 97).
?DOC(false).
-spec schema_ref_to_string_expr(
oaspec@internal@openapi@schema:schema_ref(),
binary(),
oaspec@internal@codegen@context:context()
) -> binary().
schema_ref_to_string_expr(Ref, Value, Ctx) ->
case Ref of
{inline, Schema} ->
to_string_expr(Schema, Value);
{reference, _, Name} ->
case oaspec@internal@codegen@context:resolve_schema_ref(Ref, Ctx) of
{ok, {string_schema, _, _, Enum_values, _, _, _}} when Enum_values =/= [] ->
<<<<<<<<"encode.encode_"/utf8,
(oaspec@internal@util@naming:to_snake_case(
Name
))/binary>>/binary,
"_to_string("/utf8>>/binary,
Value/binary>>/binary,
")"/utf8>>;
{ok, {object_schema, _, _, _, _, _, _}} ->
<<<<<<<<"encode.encode_"/utf8,
(oaspec@internal@util@naming:to_snake_case(
Name
))/binary>>/binary,
"("/utf8>>/binary,
Value/binary>>/binary,
")"/utf8>>;
{ok, {one_of_schema, _, _, _}} ->
<<<<<<<<"encode.encode_"/utf8,
(oaspec@internal@util@naming:to_snake_case(
Name
))/binary>>/binary,
"("/utf8>>/binary,
Value/binary>>/binary,
")"/utf8>>;
{ok, {any_of_schema, _, _, _}} ->
<<<<<<<<"encode.encode_"/utf8,
(oaspec@internal@util@naming:to_snake_case(
Name
))/binary>>/binary,
"("/utf8>>/binary,
Value/binary>>/binary,
")"/utf8>>;
{ok, {all_of_schema, _, _}} ->
<<<<<<<<"encode.encode_"/utf8,
(oaspec@internal@util@naming:to_snake_case(
Name
))/binary>>/binary,
"("/utf8>>/binary,
Value/binary>>/binary,
")"/utf8>>;
{ok, Resolved} ->
Inner = to_string_expr(Resolved, Value),
case oaspec@internal@openapi@schema:is_nullable(Resolved) of
true ->
<<<<"option.unwrap("/utf8, Inner/binary>>/binary,
", \"\")"/utf8>>;
false ->
Inner
end;
{error, _} ->
Value
end
end.
-file("src/oaspec/internal/codegen/schema_dispatch.gleam", 154).
?DOC(false).
-spec decoder_expr(oaspec@internal@openapi@schema:schema_ref()) -> binary().
decoder_expr(Ref) ->
case Ref of
{inline, {string_schema, _, _, _, _, _, _}} ->
<<"decode.string"/utf8>>;
{inline, {integer_schema, _, _, _, _, _, _, _}} ->
<<"decode.int"/utf8>>;
{inline, {number_schema, _, _, _, _, _, _, _}} ->
<<"decode.float"/utf8>>;
{inline, {boolean_schema, _}} ->
<<"decode.bool"/utf8>>;
{reference, _, Name} ->
<<(oaspec@internal@util@naming:to_snake_case(Name))/binary,
"_decoder()"/utf8>>;
{inline, _} ->
<<"dyn_decode.dynamic"/utf8>>
end.
-file("src/oaspec/internal/codegen/schema_dispatch.gleam", 168).
?DOC(false).
-spec json_encoder_expr(oaspec@internal@openapi@schema:schema_ref(), binary()) -> binary().
json_encoder_expr(Ref, Value) ->
case Ref of
{inline, {string_schema, _, _, _, _, _, _}} ->
<<<<"json.string("/utf8, Value/binary>>/binary, ")"/utf8>>;
{inline, {integer_schema, _, _, _, _, _, _, _}} ->
<<<<"json.int("/utf8, Value/binary>>/binary, ")"/utf8>>;
{inline, {number_schema, _, _, _, _, _, _, _}} ->
<<<<"json.float("/utf8, Value/binary>>/binary, ")"/utf8>>;
{inline, {boolean_schema, _}} ->
<<<<"json.bool("/utf8, Value/binary>>/binary, ")"/utf8>>;
{reference, _, Name} ->
<<<<<<<<"encode_"/utf8,
(oaspec@internal@util@naming:to_snake_case(Name))/binary>>/binary,
"_json("/utf8>>/binary,
Value/binary>>/binary,
")"/utf8>>;
{inline, _} ->
<<"json.null()"/utf8>>
end.
-file("src/oaspec/internal/codegen/schema_dispatch.gleam", 185).
?DOC(false).
-spec json_encoder_fn(oaspec@internal@openapi@schema:schema_ref()) -> binary().
json_encoder_fn(Ref) ->
case Ref of
{inline, {string_schema, _, _, _, _, _, _}} ->
<<"json.string"/utf8>>;
{inline, {integer_schema, _, _, _, _, _, _, _}} ->
<<"json.int"/utf8>>;
{inline, {number_schema, _, _, _, _, _, _, _}} ->
<<"json.float"/utf8>>;
{inline, {boolean_schema, _}} ->
<<"json.bool"/utf8>>;
{reference, _, Name} ->
<<<<"encode_"/utf8,
(oaspec@internal@util@naming:to_snake_case(Name))/binary>>/binary,
"_json"/utf8>>;
{inline, _} ->
<<"fn(_) { json.null() }"/utf8>>
end.
-file("src/oaspec/internal/codegen/schema_dispatch.gleam", 199).
?DOC(false).
-spec to_string_fn(
oaspec@internal@openapi@schema:schema_ref(),
oaspec@internal@codegen@context:context()
) -> binary().
to_string_fn(Ref, Ctx) ->
case Ref of
{inline, {string_schema, _, _, _, _, _, _}} ->
<<"fn(x) { x }"/utf8>>;
{inline, {integer_schema, _, _, _, _, _, _, _}} ->
<<"int.to_string"/utf8>>;
{inline, {number_schema, _, _, _, _, _, _, _}} ->
<<"float.to_string"/utf8>>;
{inline, {boolean_schema, _}} ->
<<"bool.to_string"/utf8>>;
{reference, _, Name} ->
case oaspec@internal@codegen@context:resolve_schema_ref(Ref, Ctx) of
{ok, {string_schema, _, _, Enum_values, _, _, _}} when Enum_values =/= [] ->
<<<<"encode.encode_"/utf8,
(oaspec@internal@util@naming:to_snake_case(Name))/binary>>/binary,
"_to_string"/utf8>>;
{ok, Resolved} ->
to_string_fn({inline, Resolved}, Ctx);
{error, _} ->
<<"fn(x) { x }"/utf8>>
end;
{inline, _} ->
<<"fn(_) { \"\" }"/utf8>>
end.
-file("src/oaspec/internal/codegen/schema_dispatch.gleam", 77).
?DOC(false).
-spec schema_ref_qualified_type_recursive(
oaspec@internal@openapi@schema:schema_ref()
) -> binary().
schema_ref_qualified_type_recursive(Ref) ->
case Ref of
{inline, S} ->
schema_type_qualified(S);
{reference, _, Name} ->
<<"types."/utf8,
(oaspec@internal@util@naming:schema_to_type_name(Name))/binary>>
end.
-file("src/oaspec/internal/codegen/schema_dispatch.gleam", 65).
?DOC(false).
-spec schema_type_qualified(oaspec@internal@openapi@schema:schema_object()) -> binary().
schema_type_qualified(Schema) ->
Base = case Schema of
{array_schema, _, Items, _, _, _} ->
<<<<"List("/utf8,
(schema_ref_qualified_type_recursive(Items))/binary>>/binary,
")"/utf8>>;
_ ->
schema_base_type(Schema)
end,
case oaspec@internal@openapi@schema:is_nullable(Schema) of
true ->
<<<<"Option("/utf8, Base/binary>>/binary, ")"/utf8>>;
false ->
Base
end.
-file("src/oaspec/internal/codegen/schema_dispatch.gleam", 44).
?DOC(false).
-spec schema_ref_type(oaspec@internal@openapi@schema:schema_ref()) -> binary().
schema_ref_type(Ref) ->
case Ref of
{inline, Schema} ->
schema_base_type(Schema);
{reference, _, Name} ->
oaspec@internal@util@naming:schema_to_type_name(Name)
end.
-file("src/oaspec/internal/codegen/schema_dispatch.gleam", 20).
?DOC(false).
-spec schema_base_type(oaspec@internal@openapi@schema:schema_object()) -> binary().
schema_base_type(Schema) ->
case Schema of
{string_schema, _, _, _, _, _, _} ->
<<"String"/utf8>>;
{integer_schema, _, _, _, _, _, _, _} ->
<<"Int"/utf8>>;
{number_schema, _, _, _, _, _, _, _} ->
<<"Float"/utf8>>;
{boolean_schema, _} ->
<<"Bool"/utf8>>;
{array_schema, _, Items, _, _, _} ->
<<<<"List("/utf8, (schema_ref_type(Items))/binary>>/binary,
")"/utf8>>;
{object_schema, _, _, _, _, _, _} ->
<<"String"/utf8>>;
{all_of_schema, _, _} ->
<<"String"/utf8>>;
{one_of_schema, _, _, _} ->
<<"String"/utf8>>;
{any_of_schema, _, _, _} ->
<<"String"/utf8>>
end.
-file("src/oaspec/internal/codegen/schema_dispatch.gleam", 35).
?DOC(false).
-spec schema_type(oaspec@internal@openapi@schema:schema_object()) -> binary().
schema_type(Schema) ->
Base = schema_base_type(Schema),
case oaspec@internal@openapi@schema:is_nullable(Schema) of
true ->
<<<<"Option("/utf8, Base/binary>>/binary, ")"/utf8>>;
false ->
Base
end.
-file("src/oaspec/internal/codegen/schema_dispatch.gleam", 52).
?DOC(false).
-spec schema_ref_qualified_type(oaspec@internal@openapi@schema:schema_ref()) -> binary().
schema_ref_qualified_type(Ref) ->
case Ref of
{inline, Schema} ->
schema_base_type(Schema);
{reference, _, Name} ->
<<"types."/utf8,
(oaspec@internal@util@naming:schema_to_type_name(Name))/binary>>
end.
-file("src/oaspec/internal/codegen/schema_dispatch.gleam", 219).
?DOC(false).
-spec resolve_param_type(
gleam@option:option(oaspec@internal@openapi@schema:schema_ref()),
oaspec@internal@codegen@context:context()
) -> binary().
resolve_param_type(Schema_ref, Ctx) ->
case Schema_ref of
{some, {inline, {array_schema, _, Items, _, _, _}}} ->
<<<<"List("/utf8,
(schema_ref_qualified_type_recursive(Items))/binary>>/binary,
")"/utf8>>;
{some, {inline, Schema}} ->
schema_base_type(Schema);
{some, {reference, _, Name} = Ref} ->
case oaspec@internal@codegen@context:resolve_schema_ref(Ref, Ctx) of
{ok, {array_schema, _, Items@1, _, _, _}} ->
<<<<"List("/utf8,
(schema_ref_qualified_type_recursive(Items@1))/binary>>/binary,
")"/utf8>>;
_ ->
<<"types."/utf8,
(oaspec@internal@util@naming:schema_to_type_name(Name))/binary>>
end;
none ->
<<"String"/utf8>>
end.