Packages
oaspec
0.11.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@client_response.erl
-module(oaspec@codegen@client_response).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oaspec/codegen/client_response.gleam").
-export([generate_multi_content_response/6, inline_schema_to_decoder/1, get_response_decode_expr/4, generate_single_content_response/7, array_item_to_string_fn/2, deep_object_array_item_to_string/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/client_response.gleam", 77).
?DOC(
" Generate response handling for multiple content types.\n"
" Since the response variant uses String for multi-content (to stay type-safe),\n"
" all branches return resp.body directly.\n"
).
-spec generate_multi_content_response(
gleam@string_tree:string_tree(),
oaspec@util@http:http_status_code(),
binary(),
list({binary(), oaspec@openapi@spec:media_type()}),
binary(),
oaspec@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_multi_content_response(Sb, Status_code, Variant_name, _, _, _) ->
_pipe = Sb,
oaspec@util@string_extra:indent(
_pipe,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(Status_code))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
"(resp.body))"/utf8>>
).
-file("src/oaspec/codegen/client_response.gleam", 136).
?DOC(
" Convert an inline schema to a decoder expression for use in generated client.\n"
" Uses dyn_decode (gleam/dynamic/decode) to avoid collision with the generated\n"
" decode module.\n"
).
-spec inline_schema_to_decoder(oaspec@openapi@schema:schema_object()) -> binary().
inline_schema_to_decoder(S) ->
case S of
{string_schema, _, _, _, _, _, _} ->
<<"dyn_decode.string"/utf8>>;
{integer_schema, _, _, _, _, _, _, _} ->
<<"dyn_decode.int"/utf8>>;
{number_schema, _, _, _, _, _, _, _} ->
<<"dyn_decode.float"/utf8>>;
{boolean_schema, _} ->
<<"dyn_decode.bool"/utf8>>;
_ ->
<<"dyn_decode.string"/utf8>>
end.
-file("src/oaspec/codegen/client_response.gleam", 97).
?DOC(" Get the decode expression for a response body.\n").
-spec get_response_decode_expr(
oaspec@openapi@schema:schema_ref(),
binary(),
oaspec@util@http:http_status_code(),
oaspec@codegen@context:context()
) -> binary().
get_response_decode_expr(Schema_ref, Op_id, Status_code, _) ->
case Schema_ref of
{reference, _, Name} ->
<<<<"decode.decode_"/utf8,
(oaspec@util@naming:to_snake_case(Name))/binary>>/binary,
"(resp.body)"/utf8>>;
{inline, {array_schema, _, Items, _, _, _}} ->
case Items of
{reference, _, Name@1} ->
<<<<"decode.decode_"/utf8,
(oaspec@util@naming:to_snake_case(Name@1))/binary>>/binary,
"_list(resp.body)"/utf8>>;
{inline, Inner} ->
Inner_decoder = inline_schema_to_decoder(Inner),
<<<<"json.parse(resp.body, decode.list("/utf8,
Inner_decoder/binary>>/binary,
"))"/utf8>>
end;
{inline, {string_schema, _, _, _, _, _, _}} ->
<<"json.parse(resp.body, dyn_decode.string)"/utf8>>;
{inline, {integer_schema, _, _, _, _, _, _, _}} ->
<<"json.parse(resp.body, dyn_decode.int)"/utf8>>;
{inline, {number_schema, _, _, _, _, _, _, _}} ->
<<"json.parse(resp.body, dyn_decode.float)"/utf8>>;
{inline, {boolean_schema, _}} ->
<<"json.parse(resp.body, dyn_decode.bool)"/utf8>>;
{inline, _} ->
Fn_name = <<<<<<"decode_"/utf8,
(oaspec@util@naming:to_snake_case(Op_id))/binary>>/binary,
"_response_"/utf8>>/binary,
(oaspec@util@naming:to_snake_case(
oaspec@util@http:status_code_suffix(Status_code)
))/binary>>,
<<<<"decode."/utf8, Fn_name/binary>>/binary, "(resp.body)"/utf8>>
end.
-file("src/oaspec/codegen/client_response.gleam", 11).
?DOC(" Generate response handling for a single content type.\n").
-spec generate_single_content_response(
gleam@string_tree:string_tree(),
oaspec@util@http:http_status_code(),
binary(),
binary(),
oaspec@openapi@spec:media_type(),
binary(),
oaspec@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_single_content_response(
Sb,
Status_code,
Variant_name,
Media_type_name,
Media_type,
Op_id,
Ctx
) ->
case Media_type_name of
<<"text/plain"/utf8>> ->
case erlang:element(2, Media_type) of
{some, _} ->
_pipe = Sb,
oaspec@util@string_extra:indent(
_pipe,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
"(resp.body))"/utf8>>
);
_ ->
_pipe@1 = Sb,
oaspec@util@string_extra:indent(
_pipe@1,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
")"/utf8>>
)
end;
<<"application/xml"/utf8>> ->
case erlang:element(2, Media_type) of
{some, _} ->
_pipe = Sb,
oaspec@util@string_extra:indent(
_pipe,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
"(resp.body))"/utf8>>
);
_ ->
_pipe@1 = Sb,
oaspec@util@string_extra:indent(
_pipe@1,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
")"/utf8>>
)
end;
<<"text/xml"/utf8>> ->
case erlang:element(2, Media_type) of
{some, _} ->
_pipe = Sb,
oaspec@util@string_extra:indent(
_pipe,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
"(resp.body))"/utf8>>
);
_ ->
_pipe@1 = Sb,
oaspec@util@string_extra:indent(
_pipe@1,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
")"/utf8>>
)
end;
<<"application/octet-stream"/utf8>> ->
case erlang:element(2, Media_type) of
{some, _} ->
_pipe = Sb,
oaspec@util@string_extra:indent(
_pipe,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
"(resp.body))"/utf8>>
);
_ ->
_pipe@1 = Sb,
oaspec@util@string_extra:indent(
_pipe@1,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
")"/utf8>>
)
end;
_ ->
case erlang:element(2, Media_type) of
{some, Schema_ref} ->
Decode_expr = get_response_decode_expr(
Schema_ref,
Op_id,
Status_code,
Ctx
),
_pipe@2 = Sb,
_pipe@3 = oaspec@util@string_extra:indent(
_pipe@2,
4,
<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> {"/utf8>>
),
_pipe@4 = oaspec@util@string_extra:indent(
_pipe@3,
5,
<<<<"case "/utf8, Decode_expr/binary>>/binary,
" {"/utf8>>
),
_pipe@5 = oaspec@util@string_extra:indent(
_pipe@4,
6,
<<<<"Ok(decoded) -> Ok("/utf8, Variant_name/binary>>/binary,
"(decoded))"/utf8>>
),
_pipe@6 = oaspec@util@string_extra:indent(
_pipe@5,
6,
<<"Error(_) -> Error(DecodeError(detail: \"Failed to decode response body\"))"/utf8>>
),
_pipe@7 = oaspec@util@string_extra:indent(
_pipe@6,
5,
<<"}"/utf8>>
),
oaspec@util@string_extra:indent(_pipe@7, 4, <<"}"/utf8>>);
_ ->
_pipe@8 = Sb,
oaspec@util@string_extra:indent(
_pipe@8,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
")"/utf8>>
)
end
end.
-file("src/oaspec/codegen/client_response.gleam", 148).
?DOC(
" Return a function expression that converts an array item to String.\n"
" Used in generated code: `list.map(param, <fn>)`.\n"
).
-spec array_item_to_string_fn(
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context()
) -> binary().
array_item_to_string_fn(Items, Ctx) ->
oaspec@codegen@schema_dispatch:to_string_fn(Items, erlang:element(2, Ctx)).
-file("src/oaspec/codegen/client_response.gleam", 153).
?DOC(" Convert a deepObject array item to a string expression.\n").
-spec deep_object_array_item_to_string(
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context()
) -> binary().
deep_object_array_item_to_string(Prop_ref, Ctx) ->
case Prop_ref of
{inline, {array_schema, _, Items, _, _, _}} ->
oaspec@codegen@schema_dispatch:schema_ref_to_string_expr(
Items,
<<"item"/utf8>>,
erlang:element(2, Ctx)
);
_ ->
<<"item"/utf8>>
end.