Current section
Files
Jump to
Current section
Files
src/atproto_codegen@module_header.erl
-module(atproto_codegen@module_header).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/atproto_codegen/module_header.gleam").
-export([ext_import_line/2, referenced_nsids/1, header/3]).
-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).
-file("src/atproto_codegen/module_header.gleam", 61).
?DOC(false).
-spec ext_import_line(atproto_codegen@config:config(), binary()) -> binary().
ext_import_line(Config, Nsid) ->
Subpath = atproto_codegen@naming:module_subpath(
erlang:element(5, Config),
Nsid
),
Alias = atproto_codegen@naming:module_alias(erlang:element(5, Config), Nsid),
Default_name = begin
_pipe = gleam@string:split(Subpath, <<"/"/utf8>>),
_pipe@1 = gleam@list:last(_pipe),
gleam@result:unwrap(_pipe@1, Subpath)
end,
Rename = case Alias =:= Default_name of
true ->
<<""/utf8>>;
false ->
<<" as "/utf8, Alias/binary>>
end,
<<<<<<<<"import "/utf8, (erlang:element(4, Config))/binary>>/binary,
"/"/utf8>>/binary,
Subpath/binary>>/binary,
Rename/binary>>.
-file("src/atproto_codegen/module_header.gleam", 74).
?DOC(false).
-spec def_uses_flatten(atproto_codegen@lower:flat_def()) -> boolean().
def_uses_flatten(Def) ->
Props = atproto_codegen@lower:properties_of(Def),
Optional = gleam@list:count(Props, fun(P) -> not erlang:element(4, P) end),
Required = erlang:length(Props) - Optional,
(Optional >= 1) andalso ((Required >= 1) orelse (Optional >= 2)).
-file("src/atproto_codegen/module_header.gleam", 91).
?DOC(false).
-spec ref_nsid(binary()) -> list(binary()).
ref_nsid(Ref) ->
R = atproto_codegen@naming:parse_ref(<<""/utf8>>, Ref),
case erlang:element(2, R) of
<<""/utf8>> ->
[];
Nsid ->
[Nsid]
end.
-file("src/atproto_codegen/module_header.gleam", 82).
?DOC(false).
-spec referenced_nsids(atproto_codegen@lower:flat_type()) -> list(binary()).
referenced_nsids(Ft) ->
case Ft of
{t_ref, Ref} ->
ref_nsid(Ref);
{t_union, Refs, _} ->
gleam@list:flat_map(Refs, fun ref_nsid/1);
{t_array, Inner} ->
referenced_nsids(Inner);
_ ->
[]
end.
-file("src/atproto_codegen/module_header.gleam", 15).
?DOC(false).
-spec header(
atproto_codegen@config:config(),
binary(),
list(atproto_codegen@lower:flat_def())
) -> binary().
header(Config, Nsid, Defs) ->
Props = gleam@list:flat_map(Defs, fun atproto_codegen@lower:properties_of/1),
Needs_option = gleam@list:any(Props, fun(P) -> not erlang:element(4, P) end),
Uses_helper = gleam@list:any(
Props,
fun(P@1) ->
atproto_codegen@emit@exprs:type_uses_helper(erlang:element(3, P@1))
end
),
Uses_unknown = gleam@list:any(
Props,
fun(P@2) ->
atproto_codegen@emit@exprs:type_uses_unknown(erlang:element(3, P@2))
end
),
Uses_blob = gleam@list:any(
Props,
fun(P@3) ->
atproto_codegen@emit@exprs:type_uses_blob(erlang:element(3, P@3))
end
),
Externals = begin
_pipe = Props,
_pipe@1 = gleam@list:flat_map(
_pipe,
fun(P@4) -> referenced_nsids(erlang:element(3, P@4)) end
),
_pipe@2 = gleam@list:filter(_pipe@1, fun(N) -> N /= Nsid end),
_pipe@3 = gleam@list:unique(_pipe@2),
gleam@list:sort(_pipe@3, fun gleam@string:compare/2)
end,
Uses_list = gleam@list:any(Defs, fun def_uses_flatten/1),
Base = case Uses_list of
true ->
[<<"import gleam/dynamic/decode"/utf8>>,
<<"import gleam/json"/utf8>>,
<<"import gleam/list"/utf8>>];
false ->
[<<"import gleam/dynamic/decode"/utf8>>,
<<"import gleam/json"/utf8>>]
end,
Blob = case Uses_blob of
true ->
[<<"import atproto/blob"/utf8>>];
false ->
[]
end,
Dyn = case Uses_unknown of
true ->
[<<"import gleam/dynamic"/utf8>>];
false ->
[]
end,
Opt = case Needs_option of
true ->
[<<"import gleam/option.{type Option}"/utf8>>];
false ->
[]
end,
Internal = case Needs_option orelse Uses_helper of
true ->
[<<<<"import "/utf8, (erlang:element(4, Config))/binary>>/binary,
"/internal"/utf8>>];
false ->
[]
end,
Ext = gleam@list:map(
Externals,
fun(_capture) -> ext_import_line(Config, _capture) end
),
<<(gleam@string:join(
lists:append([Blob, Base, Dyn, Opt, Internal, Ext]),
<<"\n"/utf8>>
))/binary,
"\n"/utf8>>.