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@naming.erl
Raw

src/atproto_codegen@naming.erl

-module(atproto_codegen@naming).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/atproto_codegen/naming.gleam").
-export([module_subpath/2, module_alias/2, parse_ref/2, ref_type_name/2, def_type_name/2, field_name/1, qualifier/3]).
-export_type([ref/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(" Naming + at-uri ref resolution: NSIDs <-> Gleam module/type/field names.\n").
-type ref() :: {ref, binary(), binary()}.
-file("src/atproto_codegen/naming.gleam", 8).
-spec strip_prefix(list(binary()), binary()) -> binary().
strip_prefix(Prefixes, Nsid) ->
case gleam@list:find(
Prefixes,
fun(_capture) -> gleam_stdlib:string_starts_with(Nsid, _capture) end
) of
{ok, Prefix} ->
gleam@string:drop_start(Nsid, string:length(Prefix));
{error, nil} ->
Nsid
end.
-file("src/atproto_codegen/naming.gleam", 16).
-spec segments(list(binary()), binary()) -> list(binary()).
segments(Prefixes, Nsid) ->
_pipe = strip_prefix(Prefixes, Nsid),
_pipe@1 = gleam@string:split(_pipe, <<"."/utf8>>),
gleam@list:map(_pipe@1, fun justin:snake_case/1).
-file("src/atproto_codegen/naming.gleam", 22).
-spec module_subpath(list(binary()), binary()) -> binary().
module_subpath(Prefixes, Nsid) ->
_pipe = segments(Prefixes, Nsid),
gleam@string:join(_pipe, <<"/"/utf8>>).
-file("src/atproto_codegen/naming.gleam", 26).
-spec module_alias(list(binary()), binary()) -> binary().
module_alias(Prefixes, Nsid) ->
_pipe = segments(Prefixes, Nsid),
gleam@string:join(_pipe, <<"_"/utf8>>).
-file("src/atproto_codegen/naming.gleam", 34).
-spec parse_ref(binary(), binary()) -> ref().
parse_ref(Current, Ref) ->
case gleam@string:split(Ref, <<"#"/utf8>>) of
[<<""/utf8>>, Def_name] ->
{ref, Current, Def_name};
[Nsid, Def_name@1] ->
{ref, Nsid, Def_name@1};
[Nsid@1] ->
{ref, Nsid@1, <<"main"/utf8>>};
_ ->
{ref, Current, Ref}
end.
-file("src/atproto_codegen/naming.gleam", 43).
-spec ref_type_name(list(binary()), ref()) -> binary().
ref_type_name(Prefixes, Ref) ->
case erlang:element(3, Ref) of
<<"main"/utf8>> ->
justin:pascal_case(module_alias(Prefixes, erlang:element(2, Ref)));
Name ->
justin:pascal_case(Name)
end.
-file("src/atproto_codegen/naming.gleam", 51).
?DOC(" A `main` object def takes the module's name, matching how refs to it are emitted (`ref_type_name` resolves bare NSIDs to #main).\n").
-spec def_type_name(list(binary()), atproto_codegen@lower:flat_def()) -> binary().
def_type_name(Prefixes, Def) ->
case Def of
{flat_record, Nsid, _} ->
justin:pascal_case(module_alias(Prefixes, Nsid));
{flat_object, Nsid@1, <<"main"/utf8>>, _} ->
justin:pascal_case(module_alias(Prefixes, Nsid@1));
{flat_object, _, Name, _} ->
justin:pascal_case(Name);
_ ->
<<""/utf8>>
end.
-file("src/atproto_codegen/naming.gleam", 66).
-spec field_name(binary()) -> binary().
field_name(Json_name) ->
Snake = justin:snake_case(Json_name),
case gleam@list:contains(
[<<"as"/utf8>>,
<<"assert"/utf8>>,
<<"case"/utf8>>,
<<"const"/utf8>>,
<<"echo"/utf8>>,
<<"fn"/utf8>>,
<<"if"/utf8>>,
<<"import"/utf8>>,
<<"let"/utf8>>,
<<"opaque"/utf8>>,
<<"panic"/utf8>>,
<<"pub"/utf8>>,
<<"todo"/utf8>>,
<<"type"/utf8>>,
<<"use"/utf8>>],
Snake
) of
true ->
<<Snake/binary, "_"/utf8>>;
false ->
Snake
end.
-file("src/atproto_codegen/naming.gleam", 74).
-spec qualifier(list(binary()), binary(), binary()) -> binary().
qualifier(Prefixes, Current, Nsid) ->
case Nsid =:= Current of
true ->
<<""/utf8>>;
false ->
<<(module_alias(Prefixes, Nsid))/binary, "."/utf8>>
end.