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

src/atproto_codegen@emit@exprs.erl

-module(atproto_codegen@emit@exprs).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/atproto_codegen/emit/exprs.gleam").
-export([escape/1, type_uses_helper/1, type_uses_blob/1, type_uses_unknown/1, union_type_name/3, gleam_type/4, encoder_expr/4, decoder_expr/4, zero_expr/5]).
-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/emit/exprs.gleam", 14).
?DOC(false).
-spec escape(binary()) -> binary().
escape(Value) ->
_pipe = Value,
_pipe@1 = gleam@string:replace(_pipe, <<"\\"/utf8>>, <<"\\\\"/utf8>>),
gleam@string:replace(_pipe@1, <<"\""/utf8>>, <<"\\\""/utf8>>).
-file("src/atproto_codegen/emit/exprs.gleam", 21).
?DOC(false).
-spec type_uses_helper(atproto_codegen@lower:flat_type()) -> boolean().
type_uses_helper(Ft) ->
case Ft of
t_cid_link ->
true;
t_bytes ->
true;
t_unknown ->
true;
{t_union, _, open} ->
true;
{t_union, _, closed} ->
false;
{t_array, Inner} ->
type_uses_helper(Inner);
_ ->
false
end.
-file("src/atproto_codegen/emit/exprs.gleam", 31).
?DOC(false).
-spec type_uses_blob(atproto_codegen@lower:flat_type()) -> boolean().
type_uses_blob(Ft) ->
case Ft of
t_blob ->
true;
{t_array, Inner} ->
type_uses_blob(Inner);
_ ->
false
end.
-file("src/atproto_codegen/emit/exprs.gleam", 40).
?DOC(false).
-spec type_uses_unknown(atproto_codegen@lower:flat_type()) -> boolean().
type_uses_unknown(Ft) ->
case Ft of
t_unknown ->
true;
{t_union, _, open} ->
true;
{t_union, _, closed} ->
false;
{t_array, Inner} ->
type_uses_unknown(Inner);
_ ->
false
end.
-file("src/atproto_codegen/emit/exprs.gleam", 51).
?DOC(false).
-spec union_type_name(
atproto_codegen@config:config(),
atproto_codegen@lower:emittable_def(),
atproto_codegen@lower:flat_field()
) -> binary().
union_type_name(Config, Def, Prop) ->
<<(atproto_codegen@naming:def_type_name(erlang:element(5, Config), Def))/binary,
(justin:pascal_case(erlang:element(2, Prop)))/binary>>.
-file("src/atproto_codegen/emit/exprs.gleam", 60).
?DOC(false).
-spec gleam_type(
atproto_codegen@config:config(),
binary(),
binary(),
atproto_codegen@lower:flat_type()
) -> binary().
gleam_type(Config, Current, Union_name, Ft) ->
case Ft of
t_string ->
<<"String"/utf8>>;
t_int ->
<<"Int"/utf8>>;
t_bool ->
<<"Bool"/utf8>>;
{t_ref, Ref} ->
R = atproto_codegen@naming:parse_ref(Current, Ref),
<<(atproto_codegen@naming:qualifier(
erlang:element(5, Config),
Current,
erlang:element(2, R)
))/binary,
(atproto_codegen@naming:ref_type_name(
erlang:element(5, Config),
R
))/binary>>;
{t_array, Inner} ->
<<<<"List("/utf8,
(gleam_type(Config, Current, Union_name, Inner))/binary>>/binary,
")"/utf8>>;
t_cid_link ->
<<"String"/utf8>>;
t_bytes ->
<<"BitArray"/utf8>>;
t_blob ->
<<"blob.Blob"/utf8>>;
t_unknown ->
<<"dynamic.Dynamic"/utf8>>;
{t_union, _, _} ->
Union_name;
{t_unsupported, Kind} ->
<<"Dynamic_"/utf8, Kind/binary>>
end.
-file("src/atproto_codegen/emit/exprs.gleam", 86).
?DOC(false).
-spec encoder_expr(
atproto_codegen@config:config(),
binary(),
binary(),
atproto_codegen@lower:flat_type()
) -> binary().
encoder_expr(Config, Current, Union_name, Ft) ->
case Ft of
t_string ->
<<"json.string"/utf8>>;
t_int ->
<<"json.int"/utf8>>;
t_bool ->
<<"json.bool"/utf8>>;
{t_ref, Ref} ->
R = atproto_codegen@naming:parse_ref(Current, Ref),
<<<<(atproto_codegen@naming:qualifier(
erlang:element(5, Config),
Current,
erlang:element(2, R)
))/binary,
"encode_"/utf8>>/binary,
(justin:snake_case(
atproto_codegen@naming:ref_type_name(
erlang:element(5, Config),
R
)
))/binary>>;
{t_array, Inner} ->
<<<<"fn(items) { json.array(items, "/utf8,
(encoder_expr(Config, Current, Union_name, Inner))/binary>>/binary,
") }"/utf8>>;
t_cid_link ->
<<"internal.encode_cid_link"/utf8>>;
t_bytes ->
<<"internal.encode_bytes"/utf8>>;
t_blob ->
<<"blob.encode_blob"/utf8>>;
t_unknown ->
<<"internal.dynamic_to_json"/utf8>>;
{t_union, _, _} ->
<<"encode_"/utf8, (justin:snake_case(Union_name))/binary>>;
{t_unsupported, _} ->
<<"json.string"/utf8>>
end.
-file("src/atproto_codegen/emit/exprs.gleam", 115).
?DOC(false).
-spec decoder_expr(
atproto_codegen@config:config(),
binary(),
binary(),
atproto_codegen@lower:flat_type()
) -> binary().
decoder_expr(Config, Current, Union_name, Ft) ->
case Ft of
t_string ->
<<"decode.string"/utf8>>;
t_int ->
<<"decode.int"/utf8>>;
t_bool ->
<<"decode.bool"/utf8>>;
{t_ref, Ref} ->
R = atproto_codegen@naming:parse_ref(Current, Ref),
<<<<(atproto_codegen@naming:qualifier(
erlang:element(5, Config),
Current,
erlang:element(2, R)
))/binary,
(justin:snake_case(
atproto_codegen@naming:ref_type_name(
erlang:element(5, Config),
R
)
))/binary>>/binary,
"_decoder()"/utf8>>;
{t_array, Inner} ->
<<<<"decode.list("/utf8,
(decoder_expr(Config, Current, Union_name, Inner))/binary>>/binary,
")"/utf8>>;
t_cid_link ->
<<"internal.cid_link_decoder()"/utf8>>;
t_bytes ->
<<"internal.bytes_decoder()"/utf8>>;
t_blob ->
<<"blob.blob_decoder()"/utf8>>;
t_unknown ->
<<"decode.dynamic"/utf8>>;
{t_union, _, _} ->
<<(justin:snake_case(Union_name))/binary, "_decoder()"/utf8>>;
{t_unsupported, _} ->
<<"decode.string"/utf8>>
end.
-file("src/atproto_codegen/emit/exprs.gleam", 143).
?DOC(false).
-spec zero_expr(
atproto_codegen@emit@zero:zero_plan(),
atproto_codegen@config:config(),
binary(),
binary(),
atproto_codegen@lower:flat_type()
) -> binary().
zero_expr(Plan, Config, Current, Union_name, Ft) ->
case Ft of
t_string ->
<<"\"\""/utf8>>;
t_int ->
<<"0"/utf8>>;
t_bool ->
<<"False"/utf8>>;
{t_ref, Ref} ->
R = atproto_codegen@naming:parse_ref(Current, Ref),
<<<<(atproto_codegen@naming:qualifier(
erlang:element(5, Config),
Current,
erlang:element(2, R)
))/binary,
(justin:snake_case(
atproto_codegen@naming:ref_type_name(
erlang:element(5, Config),
R
)
))/binary>>/binary,
"_zero()"/utf8>>;
{t_array, _} ->
<<"[]"/utf8>>;
t_cid_link ->
<<"\"\""/utf8>>;
t_bytes ->
<<"<<>>"/utf8>>;
t_blob ->
<<"blob.Blob(\"\", \"\", 0)"/utf8>>;
t_unknown ->
<<"dynamic.nil()"/utf8>>;
{t_union, _, open} ->
<<Union_name/binary, "Other(dynamic.nil())"/utf8>>;
{t_union, Refs, closed} ->
Ref@1 = atproto_codegen@emit@zero:choose_fallback(
Plan,
Current,
Refs
),
R@1 = atproto_codegen@naming:parse_ref(Current, Ref@1),
<<<<<<<<Union_name/binary,
(atproto_codegen@naming:ref_type_name(
erlang:element(5, Config),
R@1
))/binary>>/binary,
"("/utf8>>/binary,
(zero_expr(
Plan,
Config,
Current,
Union_name,
{t_ref, Ref@1}
))/binary>>/binary,
")"/utf8>>;
{t_unsupported, _} ->
<<"dynamic.nil()"/utf8>>
end.