Current section

Files

Jump to
oaspec src oaspec@openapi@diagnostic_format.erl
Raw

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.