Current section

Files

Jump to
oaspec src oaspec@codegen@schema_dispatch.erl
Raw

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_ref_type/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.
-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", 97).
?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", 109).
?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", 122).
?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", 134).
?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", 153).
?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.