Packages
oaspec
0.26.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@openapi@diagnostic_format.erl
-module(oaspec@openapi@diagnostic_format).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oaspec/openapi/diagnostic_format.gleam").
-export([pointer_to_human/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.
?MODULEDOC(
" Human-readable rendering of diagnostic pointer strings. The machine\n"
" layer — `Diagnostic.pointer` itself — is left untouched so future\n"
" `--format json` output or any consumer that wants the raw\n"
" JSON Pointer / dotted path can still read it verbatim. This module\n"
" only owns the CLI presentation transform.\n"
"\n"
" The function is deliberately spec-context free: it never looks a\n"
" path up in a parsed spec. That is a larger follow-up; here the goal\n"
" is simply to turn `paths.~1pets.get.parameters.0` into\n"
" `GET /pets, parameter #0` so a reader does not need to know about\n"
" JSON Pointer escape rules.\n"
).
-file("src/oaspec/openapi/diagnostic_format.gleam", 56).
?DOC(
" JSON Pointer unescaping per RFC 6901: `~1` → `/`, `~0` → `~`.\n"
" Order matters: `~1` must be decoded first so `~01` ends up as `~1`,\n"
" not `/`.\n"
).
-spec unescape_segment(binary()) -> binary().
unescape_segment(S) ->
_pipe = S,
_pipe@1 = gleam@string:replace(_pipe, <<"~1"/utf8>>, <<"/"/utf8>>),
gleam@string:replace(_pipe@1, <<"~0"/utf8>>, <<"~"/utf8>>).
-file("src/oaspec/openapi/diagnostic_format.gleam", 39).
-spec split_pointer(binary()) -> list(binary()).
split_pointer(Pointer) ->
_pipe = Pointer,
_pipe@1 = gleam@string:replace(_pipe, <<"#/"/utf8>>, <<""/utf8>>),
_pipe@2 = gleam@string:replace(_pipe@1, <<"/"/utf8>>, <<"."/utf8>>),
_pipe@3 = gleam@string:split(_pipe@2, <<"."/utf8>>),
_pipe@4 = gleam@list:filter(_pipe@3, fun(S) -> S /= <<""/utf8>> end),
gleam@list:map(_pipe@4, fun unescape_segment/1).
-file("src/oaspec/openapi/diagnostic_format.gleam", 94).
-spec with_tail(binary(), list(binary())) -> binary().
with_tail(Base, Rest) ->
case Rest of
[] ->
Base;
_ ->
<<<<<<Base/binary, " ("/utf8>>/binary,
(gleam@string:join(Rest, <<"."/utf8>>))/binary>>/binary,
")"/utf8>>
end.
-file("src/oaspec/openapi/diagnostic_format.gleam", 101).
-spec default_format(list(binary()), binary()) -> binary().
default_format(Segments, Original) ->
case Segments of
[] ->
Original;
_ ->
gleam@string:join(Segments, <<"."/utf8>>)
end.
-file("src/oaspec/openapi/diagnostic_format.gleam", 62).
-spec format_segments(list(binary()), binary()) -> binary().
format_segments(Segments, Original) ->
case Segments of
[<<"paths"/utf8>>, Path, Method, <<"parameters"/utf8>>, Idx | Rest] ->
with_tail(
<<<<<<<<(string:uppercase(Method))/binary, " "/utf8>>/binary,
Path/binary>>/binary,
", parameter #"/utf8>>/binary,
Idx/binary>>,
Rest
);
[<<"paths"/utf8>>, Path@1, Method@1, <<"requestBody"/utf8>> | Rest@1] ->
with_tail(
<<<<<<(string:uppercase(Method@1))/binary, " "/utf8>>/binary,
Path@1/binary>>/binary,
", requestBody"/utf8>>,
Rest@1
);
[<<"paths"/utf8>>,
Path@2,
Method@2,
<<"responses"/utf8>>,
Status |
Rest@2] ->
with_tail(
<<<<<<<<(string:uppercase(Method@2))/binary, " "/utf8>>/binary,
Path@2/binary>>/binary,
", response "/utf8>>/binary,
Status/binary>>,
Rest@2
);
[<<"paths"/utf8>>, Path@3, Method@3 | Rest@3] ->
with_tail(
<<<<(string:uppercase(Method@3))/binary, " "/utf8>>/binary,
Path@3/binary>>,
Rest@3
);
[<<"components"/utf8>>, <<"schemas"/utf8>>, Name | Rest@4] ->
with_tail(<<"schemas."/utf8, Name/binary>>, Rest@4);
[<<"components"/utf8>>, <<"parameters"/utf8>>, Name@1 | Rest@5] ->
with_tail(<<"parameters."/utf8, Name@1/binary>>, Rest@5);
[<<"components"/utf8>>, <<"responses"/utf8>>, Name@2 | Rest@6] ->
with_tail(<<"responses."/utf8, Name@2/binary>>, Rest@6);
[<<"components"/utf8>>, <<"requestBodies"/utf8>>, Name@3 | Rest@7] ->
with_tail(<<"requestBodies."/utf8, Name@3/binary>>, Rest@7);
[<<"components"/utf8>>, Kind, Name@4 | Rest@8] ->
with_tail(
<<<<Kind/binary, "."/utf8>>/binary, Name@4/binary>>,
Rest@8
);
_ ->
default_format(Segments, Original)
end.
-file("src/oaspec/openapi/diagnostic_format.gleam", 32).
?DOC(
" Translate a diagnostic pointer into a human-readable location string.\n"
"\n"
" Recognised shapes:\n"
" - `paths.<escaped>.<method>.parameters.<idx>[...]`\n"
" → `METHOD /path, parameter #idx[...]`\n"
" - `paths.<escaped>.<method>.requestBody[...]`\n"
" → `METHOD /path, requestBody[...]`\n"
" - `paths.<escaped>.<method>.responses.<status>[...]`\n"
" → `METHOD /path, response <status>[...]`\n"
" - `paths.<escaped>.<method>[...]`\n"
" → `METHOD /path[...]`\n"
" - `components.<kind>.<name>[...]`\n"
" → `<kind>.<name>[...]`\n"
"\n"
" Falls back to the escape-decoded pointer if no pattern matches, so\n"
" callers always get *something* intelligible.\n"
).
-spec pointer_to_human(binary()) -> binary().
pointer_to_human(Pointer) ->
case Pointer of
<<""/utf8>> ->
<<"root"/utf8>>;
_ ->
format_segments(split_pointer(Pointer), Pointer)
end.