Packages
oaspec
0.20.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@codegen@schema_dispatch.erl
-module(oaspec@codegen@schema_dispatch).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oaspec/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_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.
-file("src/oaspec/codegen/schema_dispatch.gleam", 62).
?DOC(
" Map a primitive schema to its to_string expression.\n"
" Returns the expression that converts a value to String for URL encoding.\n"
).
-spec to_string_expr(oaspec@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/codegen/schema_dispatch.gleam", 73).
?DOC(" Map a schema ref to a to_string expression, resolving refs if needed.\n").
-spec schema_ref_to_string_expr(
oaspec@openapi@schema:schema_ref(),
binary(),
oaspec@openapi@spec:open_api_spec(oaspec@openapi@spec:resolved())
) -> binary().
schema_ref_to_string_expr(Ref, Value, Spec) ->
case Ref of
{inline, Schema} ->
to_string_expr(Schema, Value);
{reference, _, Name} ->
case oaspec@openapi@resolver:resolve_schema_ref(Ref, Spec) of
{ok, {string_schema, _, _, Enum_values, _, _, _}} when Enum_values =/= [] ->
<<<<<<<<"encode.encode_"/utf8,
(oaspec@util@naming:to_snake_case(Name))/binary>>/binary,
"_to_string("/utf8>>/binary,
Value/binary>>/binary,
")"/utf8>>;
{ok, Resolved} ->
to_string_expr(Resolved, Value);
{error, _} ->
Value
end
end.
-file("src/oaspec/codegen/schema_dispatch.gleam", 98).
?DOC(
" Map a schema ref to its decoder expression (for inline primitives)\n"
" or decoder function name (for references).\n"
).
-spec decoder_expr(oaspec@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@util@naming:to_snake_case(Name))/binary,
"_decoder()"/utf8>>;
{inline, _} ->
<<"decode.string"/utf8>>
end.
-file("src/oaspec/codegen/schema_dispatch.gleam", 110).
?DOC(" Map a schema ref to its JSON encoder expression.\n").
-spec json_encoder_expr(oaspec@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@util@naming:to_snake_case(Name))/binary>>/binary,
"_json("/utf8>>/binary,
Value/binary>>/binary,
")"/utf8>>;
{inline, _} ->
<<<<"json.string("/utf8, Value/binary>>/binary, ")"/utf8>>
end.
-file("src/oaspec/codegen/schema_dispatch.gleam", 123).
?DOC(" Map a schema ref to its JSON encoder function reference (for higher-order use).\n").
-spec json_encoder_fn(oaspec@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@util@naming:to_snake_case(Name))/binary>>/binary,
"_json"/utf8>>;
{inline, _} ->
<<"json.string"/utf8>>
end.
-file("src/oaspec/codegen/schema_dispatch.gleam", 135).
?DOC(" Map a schema ref to its to_string function reference (for list.map etc).\n").
-spec to_string_fn(
oaspec@openapi@schema:schema_ref(),
oaspec@openapi@spec:open_api_spec(oaspec@openapi@spec:resolved())
) -> binary().
to_string_fn(Ref, Spec) ->
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@openapi@resolver:resolve_schema_ref(Ref, Spec) of
{ok, {string_schema, _, _, Enum_values, _, _, _}} when Enum_values =/= [] ->
<<<<"encode.encode_"/utf8,
(oaspec@util@naming:to_snake_case(Name))/binary>>/binary,
"_to_string"/utf8>>;
{ok, Resolved} ->
to_string_fn({inline, Resolved}, Spec);
{error, _} ->
<<"fn(x) { x }"/utf8>>
end;
{inline, _} ->
<<"fn(x) { x }"/utf8>>
end.
-file("src/oaspec/codegen/schema_dispatch.gleam", 45).
?DOC(" Map a schema ref to a Gleam type string.\n").
-spec schema_ref_type(oaspec@openapi@schema:schema_ref()) -> binary().
schema_ref_type(Ref) ->
case Ref of
{inline, Schema} ->
schema_base_type(Schema);
{reference, _, Name} ->
oaspec@util@naming:schema_to_type_name(Name)
end.
-file("src/oaspec/codegen/schema_dispatch.gleam", 21).
?DOC(" Map a schema object to its Gleam type string (without nullable wrapping).\n").
-spec schema_base_type(oaspec@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/codegen/schema_dispatch.gleam", 36).
?DOC(" Map a schema object to its Gleam type string (with nullable wrapping).\n").
-spec schema_type(oaspec@openapi@schema:schema_object()) -> binary().
schema_type(Schema) ->
Base = schema_base_type(Schema),
case oaspec@openapi@schema:is_nullable(Schema) of
true ->
<<<<"Option("/utf8, Base/binary>>/binary, ")"/utf8>>;
false ->
Base
end.
-file("src/oaspec/codegen/schema_dispatch.gleam", 53).
?DOC(" Map a schema ref to a qualified Gleam type (with types. prefix for refs).\n").
-spec schema_ref_qualified_type(oaspec@openapi@schema:schema_ref()) -> binary().
schema_ref_qualified_type(Ref) ->
case Ref of
{inline, Schema} ->
schema_base_type(Schema);
{reference, _, Name} ->
<<"types."/utf8,
(oaspec@util@naming:schema_to_type_name(Name))/binary>>
end.
-file("src/oaspec/codegen/schema_dispatch.gleam", 155).
?DOC(" Resolve a schema ref and return the base type (for parameter type resolution).\n").
-spec resolve_param_type(
gleam@option:option(oaspec@openapi@schema:schema_ref()),
oaspec@openapi@spec:open_api_spec(oaspec@openapi@spec:resolved())
) -> binary().
resolve_param_type(Schema_ref, Spec) ->
case Schema_ref of
{some, {inline, Schema}} ->
schema_base_type(Schema);
{some, {reference, _, Name} = Ref} ->
case oaspec@openapi@resolver:resolve_schema_ref(Ref, Spec) of
{ok, {array_schema, _, Items, _, _, _}} ->
<<<<"List("/utf8, (schema_ref_type(Items))/binary>>/binary,
")"/utf8>>;
_ ->
<<"types."/utf8,
(oaspec@util@naming:schema_to_type_name(Name))/binary>>
end;
none ->
<<"String"/utf8>>
end.