Packages

Generate Gleam types, JSON codecs, and a typed XRPC client from atproto lexicon files.

Current section

Files

Jump to
atproto_codegen src atproto_codegen@plugins@types.erl
Raw

src/atproto_codegen@plugins@types.erl

-module(atproto_codegen@plugins@types).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/atproto_codegen/plugins/types.gleam").
-export([plugin/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(
" The `types` plugin: for each codec unit (a def or a synthesized union\n"
" field type), emits the Gleam type, the JSON-fields helper, and the\n"
" `$type`-tagging encoder. Decoding is the `decode-json` plugin's job; the\n"
" host interleaves the two so the assembled file matches the original\n"
" monolithic emitter's ordering.\n"
).
-file("src/atproto_codegen/plugins/types.gleam", 39).
-spec render_unit(
atproto_codegen@config:config(),
binary(),
atproto_codegen@lower:codec_unit()
) -> binary().
render_unit(Cfg, Nsid, Unit) ->
case Unit of
{def_unit, Def} ->
Name = atproto_codegen@naming:def_type_name(
erlang:element(5, Cfg),
Def
),
Props = atproto_codegen@lower:properties_of(Def),
Is_record = atproto_codegen@lower:is_record(Def),
_pipe = [case Is_record of
true ->
[<<<<"pub const collection = \""/utf8,
(atproto_codegen@emit@exprs:escape(Nsid))/binary>>/binary,
"\""/utf8>>];
false ->
[]
end, [atproto_codegen@emit@def:emit_type(
Cfg,
Nsid,
Def,
Name,
Props
)], [atproto_codegen@emit@def:emit_fields(
Cfg,
Nsid,
Def,
Name,
Props
)], [atproto_codegen@emit@def:emit_encoder(
Nsid,
Name,
Is_record
)]],
_pipe@1 = lists:append(_pipe),
gleam@string:join(_pipe@1, <<"\n\n"/utf8>>);
{union_unit, Def@1, Field, Refs} ->
Name@1 = atproto_codegen@emit@exprs:union_type_name(
Cfg,
Def@1,
Field
),
Parts = atproto_codegen@emit@union:emit_union(
Cfg,
Nsid,
Name@1,
Refs
),
<<<<(erlang:element(2, Parts))/binary, "\n\n"/utf8>>/binary,
(erlang:element(3, Parts))/binary>>
end.
-file("src/atproto_codegen/plugins/types.gleam", 24).
-spec emit(atproto_codegen@plugin:plugin_context()) -> {ok,
list(atproto_codegen@plugin:output_file())} |
{error, atproto_codegen@plugin:codegen_error()}.
emit(Ctx) ->
_pipe = erlang:element(4, Ctx),
_pipe@1 = gleam@list:filter_map(
_pipe,
fun(Lex) ->
case atproto_codegen@lower:codec_units(erlang:element(4, Lex)) of
[] ->
{error, nil};
Units ->
{ok,
{output_file,
<<(atproto_codegen@naming:module_subpath(
erlang:element(5, erlang:element(2, Ctx)),
erlang:element(2, Lex)
))/binary,
".gleam"/utf8>>,
gleam@list:map(
Units,
fun(_capture) ->
render_unit(
erlang:element(2, Ctx),
erlang:element(2, Lex),
_capture
)
end
)}}
end
end
),
{ok, _pipe@1}.
-file("src/atproto_codegen/plugins/types.gleam", 20).
-spec plugin() -> atproto_codegen@plugin:plugin().
plugin() ->
{plugin, <<"types"/utf8>>, [], fun emit/1}.