Packages
oaspec
0.6.1
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@util@http.erl
-module(oaspec@util@http).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oaspec/util/http.gleam").
-export([status_code_suffix/1, status_code_to_int_pattern/1, status_code_to_int/1, sort_response_entries/1]).
-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/util/http.gleam", 7).
?DOC(
" Shared HTTP status code utilities for code generation.\n"
" Get a human-readable suffix for a given HTTP status code.\n"
" Used to build anonymous type names like \"ListPetsResponseOk\".\n"
).
-spec status_code_suffix(binary()) -> binary().
status_code_suffix(Code) ->
case Code of
<<"200"/utf8>> ->
<<"Ok"/utf8>>;
<<"201"/utf8>> ->
<<"Created"/utf8>>;
<<"204"/utf8>> ->
<<"NoContent"/utf8>>;
<<"400"/utf8>> ->
<<"BadRequest"/utf8>>;
<<"401"/utf8>> ->
<<"Unauthorized"/utf8>>;
<<"403"/utf8>> ->
<<"Forbidden"/utf8>>;
<<"404"/utf8>> ->
<<"NotFound"/utf8>>;
<<"409"/utf8>> ->
<<"Conflict"/utf8>>;
<<"422"/utf8>> ->
<<"UnprocessableEntity"/utf8>>;
<<"500"/utf8>> ->
<<"InternalServerError"/utf8>>;
<<"default"/utf8>> ->
<<"Default"/utf8>>;
<<"1XX"/utf8>> ->
<<"Status1xx"/utf8>>;
<<"1xx"/utf8>> ->
<<"Status1xx"/utf8>>;
<<"2XX"/utf8>> ->
<<"Status2xx"/utf8>>;
<<"2xx"/utf8>> ->
<<"Status2xx"/utf8>>;
<<"3XX"/utf8>> ->
<<"Status3xx"/utf8>>;
<<"3xx"/utf8>> ->
<<"Status3xx"/utf8>>;
<<"4XX"/utf8>> ->
<<"Status4xx"/utf8>>;
<<"4xx"/utf8>> ->
<<"Status4xx"/utf8>>;
<<"5XX"/utf8>> ->
<<"Status5xx"/utf8>>;
<<"5xx"/utf8>> ->
<<"Status5xx"/utf8>>;
Other ->
<<"Status"/utf8, Other/binary>>
end.
-file("src/oaspec/util/http.gleam", 32).
?DOC(
" Convert a status code string to a valid Gleam case pattern.\n"
" Exact codes become integer literals, range codes become guard expressions,\n"
" and \"default\" becomes \"_\" (catch-all).\n"
).
-spec status_code_to_int_pattern(binary()) -> binary().
status_code_to_int_pattern(Code) ->
case Code of
<<"default"/utf8>> ->
<<"_"/utf8>>;
<<"1XX"/utf8>> ->
<<"status if status >= 100 && status <= 199"/utf8>>;
<<"1xx"/utf8>> ->
<<"status if status >= 100 && status <= 199"/utf8>>;
<<"2XX"/utf8>> ->
<<"status if status >= 200 && status <= 299"/utf8>>;
<<"2xx"/utf8>> ->
<<"status if status >= 200 && status <= 299"/utf8>>;
<<"3XX"/utf8>> ->
<<"status if status >= 300 && status <= 399"/utf8>>;
<<"3xx"/utf8>> ->
<<"status if status >= 300 && status <= 399"/utf8>>;
<<"4XX"/utf8>> ->
<<"status if status >= 400 && status <= 499"/utf8>>;
<<"4xx"/utf8>> ->
<<"status if status >= 400 && status <= 499"/utf8>>;
<<"5XX"/utf8>> ->
<<"status if status >= 500 && status <= 599"/utf8>>;
<<"5xx"/utf8>> ->
<<"status if status >= 500 && status <= 599"/utf8>>;
_ ->
Code
end.
-file("src/oaspec/util/http.gleam", 55).
?DOC(
" Convert a status code string to an integer string for use in ServerResponse.\n"
" Range codes and \"default\" map to a representative status code.\n"
).
-spec status_code_to_int(binary()) -> binary().
status_code_to_int(Code) ->
case Code of
<<"default"/utf8>> ->
<<"500"/utf8>>;
<<"1XX"/utf8>> ->
<<"100"/utf8>>;
<<"1xx"/utf8>> ->
<<"100"/utf8>>;
<<"2XX"/utf8>> ->
<<"200"/utf8>>;
<<"2xx"/utf8>> ->
<<"200"/utf8>>;
<<"3XX"/utf8>> ->
<<"300"/utf8>>;
<<"3xx"/utf8>> ->
<<"300"/utf8>>;
<<"4XX"/utf8>> ->
<<"400"/utf8>>;
<<"4xx"/utf8>> ->
<<"400"/utf8>>;
<<"5XX"/utf8>> ->
<<"500"/utf8>>;
<<"5xx"/utf8>> ->
<<"500"/utf8>>;
_ ->
Code
end.
-file("src/oaspec/util/http.gleam", 67).
-spec status_sort_priority(binary()) -> integer().
status_sort_priority(Code) ->
case Code of
<<"default"/utf8>> ->
9999;
<<"1XX"/utf8>> ->
1100;
<<"1xx"/utf8>> ->
1100;
<<"2XX"/utf8>> ->
1200;
<<"2xx"/utf8>> ->
1200;
<<"3XX"/utf8>> ->
1300;
<<"3xx"/utf8>> ->
1300;
<<"4XX"/utf8>> ->
1400;
<<"4xx"/utf8>> ->
1400;
<<"5XX"/utf8>> ->
1500;
<<"5xx"/utf8>> ->
1500;
_ ->
case gleam_stdlib:parse_int(Code) of
{ok, N} ->
N;
{error, _} ->
9998
end
end.
-file("src/oaspec/util/http.gleam", 47).
?DOC(
" Sort response entries so that exact codes come before ranges,\n"
" and ranges come before the default catch-all. This ensures\n"
" correct pattern matching order in generated case expressions.\n"
).
-spec sort_response_entries(list({binary(), HXG})) -> list({binary(), HXG}).
sort_response_entries(Entries) ->
gleam@list:sort(
Entries,
fun(A, B) ->
gleam@int:compare(
status_sort_priority(erlang:element(1, A)),
status_sort_priority(erlang:element(1, B))
)
end
).