Current section
Files
Jump to
Current section
Files
src/atproto_openapi@emit.erl
-module(atproto_openapi@emit).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/atproto_openapi/emit.gleam").
-export([assemble/4]).
-export_type([document/0]).
-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(false).
-type document() :: {document,
gleam@json:json(),
integer(),
integer(),
list(binary())}.
-file("src/atproto_openapi/emit.gleam", 207).
?DOC(false).
-spec 'maybe'(
binary(),
gleam@option:option(JBA),
fun((JBA) -> gleam@json:json())
) -> list({binary(), gleam@json:json()}).
'maybe'(Key, Value, To_json) ->
case Value of
{some, V} ->
[{Key, To_json(V)}];
none ->
[]
end.
-file("src/atproto_openapi/emit.gleam", 197).
?DOC(false).
-spec subscription_json({binary(), gleam@option:option(binary())}) -> gleam@json:json().
subscription_json(Entry) ->
{Nsid, Description} = Entry,
gleam@json:object(
lists:append(
[[{<<"nsid"/utf8>>, gleam@json:string(Nsid)}],
'maybe'(
<<"description"/utf8>>,
Description,
fun gleam@json:string/1
)]
)
).
-file("src/atproto_openapi/emit.gleam", 186).
?DOC(false).
-spec subscriptions_field(list({binary(), gleam@option:option(binary())})) -> list({binary(),
gleam@json:json()}).
subscriptions_field(Subscriptions) ->
case Subscriptions of
[] ->
[];
_ ->
[{<<"x-atproto-subscriptions"/utf8>>,
gleam@json:array(Subscriptions, fun subscription_json/1)}]
end.
-file("src/atproto_openapi/emit.gleam", 218).
?DOC(false).
-spec sorted_by_key(list({binary(), gleam@json:json()})) -> list({binary(),
gleam@json:json()}).
sorted_by_key(Entries) ->
gleam@list:sort(
Entries,
fun(A, B) ->
gleam@string:compare(erlang:element(1, A), erlang:element(1, B))
end
).
-file("src/atproto_openapi/emit.gleam", 170).
?DOC(false).
-spec components_field(list({atproto_openapi@refs:ref(), gleam@json:json()})) -> list({binary(),
gleam@json:json()}).
components_field(Entries) ->
case Entries of
[] ->
[];
_ ->
Keyed = gleam@list:map(
Entries,
fun(Entry) ->
{atproto_openapi@refs:component_key(
erlang:element(1, Entry)
),
erlang:element(2, Entry)}
end
),
[{<<"components"/utf8>>,
gleam@json:object(
[{<<"schemas"/utf8>>,
gleam@json:object(sorted_by_key(Keyed))}]
)}]
end.
-file("src/atproto_openapi/emit.gleam", 156).
?DOC(false).
-spec servers_field(list(binary())) -> list({binary(), gleam@json:json()}).
servers_field(Servers) ->
case Servers of
[] ->
[];
_ ->
[{<<"servers"/utf8>>,
gleam@json:array(
Servers,
fun(Url) ->
gleam@json:object(
[{<<"url"/utf8>>, gleam@json:string(Url)}]
)
end
)}]
end.
-file("src/atproto_openapi/emit.gleam", 149).
?DOC(false).
-spec info_json(binary(), binary()) -> gleam@json:json().
info_json(Title, Version) ->
gleam@json:object(
[{<<"title"/utf8>>, gleam@json:string(Title)},
{<<"version"/utf8>>, gleam@json:string(Version)}]
).
-file("src/atproto_openapi/emit.gleam", 119).
?DOC(false).
-spec doc_subscription_entry(atproto_lexicon@ast:lexicon_doc()) -> gleam@option:option({binary(),
gleam@option:option(binary())}).
doc_subscription_entry(Doc) ->
case gleam@list:key_find(erlang:element(6, Doc), <<"main"/utf8>>) of
{ok, {def_subscription, S}} ->
{some, {erlang:element(3, Doc), erlang:element(2, S)}};
_ ->
none
end.
-file("src/atproto_openapi/emit.gleam", 139).
?DOC(false).
-spec check_collisions(list({atproto_openapi@refs:ref(), gleam@json:json()})) -> {ok,
nil} |
{error, atproto_openapi@refs:ref_error()}.
check_collisions(Entries) ->
All_refs = gleam@list:map(
Entries,
fun(Entry) -> erlang:element(1, Entry) end
),
case atproto_openapi@refs:find_collisions(All_refs) of
[] ->
{ok, nil};
[First | _] ->
{error, First}
end.
-file("src/atproto_openapi/emit.gleam", 86).
?DOC(false).
-spec method_kind(atproto_lexicon@ast:primary_def()) -> boolean().
method_kind(Def) ->
case Def of
{def_query, _} ->
true;
{def_procedure, _} ->
true;
{def_subscription, _} ->
true;
{def_record, _} ->
false;
{def_object, _} ->
false;
{def_array, _} ->
false;
{def_token, _} ->
false;
{def_string, _} ->
false;
{def_integer, _} ->
false;
{def_boolean, _} ->
false;
{def_bytes, _} ->
false;
{def_cid_link, _} ->
false;
{def_unknown, _} ->
false;
{def_blob, _} ->
false
end.
-file("src/atproto_openapi/emit.gleam", 128).
?DOC(false).
-spec doc_schema_entries(atproto_lexicon@ast:lexicon_doc()) -> {ok,
list({atproto_openapi@refs:ref(), gleam@json:json()})} |
{error, atproto_openapi@refs:ref_error()}.
doc_schema_entries(Doc) ->
_pipe = erlang:element(6, Doc),
_pipe@1 = gleam@list:filter(
_pipe,
fun(Entry) -> not method_kind(erlang:element(2, Entry)) end
),
gleam@list:try_map(
_pipe@1,
fun(Entry@1) ->
{Name, Def} = Entry@1,
Own = {ref, erlang:element(3, Doc), Name},
gleam@result:'try'(
atproto_openapi@schema:primary_def(Def, Own),
fun(Entry_json) -> {ok, {Own, Entry_json}} end
)
end
).
-file("src/atproto_openapi/emit.gleam", 103).
?DOC(false).
-spec doc_path_entry(atproto_lexicon@ast:lexicon_doc()) -> {ok,
gleam@option:option({binary(), gleam@json:json()})} |
{error, atproto_openapi@refs:ref_error()}.
doc_path_entry(Doc) ->
case gleam@list:key_find(erlang:element(6, Doc), <<"main"/utf8>>) of
{ok, {def_query, Q}} ->
gleam@result:'try'(
atproto_openapi@paths:query_path_item(erlang:element(3, Doc), Q),
fun(Item) ->
{ok,
{some,
{atproto_openapi@paths:path_for(
erlang:element(3, Doc)
),
Item}}}
end
);
{ok, {def_procedure, P}} ->
gleam@result:'try'(
atproto_openapi@paths:procedure_path_item(
erlang:element(3, Doc),
P
),
fun(Item@1) ->
{ok,
{some,
{atproto_openapi@paths:path_for(
erlang:element(3, Doc)
),
Item@1}}}
end
);
_ ->
{ok, none}
end.
-file("src/atproto_openapi/emit.gleam", 46).
?DOC(false).
-spec assemble(
list(atproto_lexicon@ast:lexicon_doc()),
binary(),
binary(),
list(binary())
) -> {ok, document()} | {error, atproto_openapi@refs:ref_error()}.
assemble(Docs, Title, Version, Servers) ->
gleam@result:'try'(
gleam@list:try_map(Docs, fun doc_path_entry/1),
fun(Path_opts) ->
Path_entries = gleam@option:values(Path_opts),
gleam@result:'try'(
gleam@list:try_map(Docs, fun doc_schema_entries/1),
fun(Schema_lists) ->
Schema_entries = lists:append(Schema_lists),
gleam@result:'try'(
check_collisions(Schema_entries),
fun(_) ->
Subscriptions = gleam@list:filter_map(
Docs,
fun(Doc) ->
gleam@option:to_result(
doc_subscription_entry(Doc),
nil
)
end
),
Document_json = gleam@json:object(
lists:append(
[[{<<"openapi"/utf8>>,
gleam@json:string(
<<"3.1.0"/utf8>>
)}],
[{<<"info"/utf8>>,
info_json(Title, Version)}],
servers_field(Servers),
[{<<"paths"/utf8>>,
gleam@json:object(
sorted_by_key(Path_entries)
)}],
components_field(Schema_entries),
subscriptions_field(Subscriptions)]
)
),
{ok,
{document,
Document_json,
erlang:length(Path_entries),
erlang:length(Schema_entries),
gleam@list:map(
Subscriptions,
fun(S) -> erlang:element(1, S) end
)}}
end
)
end
)
end
).