Current section
Files
Jump to
Current section
Files
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_of/1, union_type_name/3, gleam_type/4, encoder_expr/4, decoder_expr/4]).
-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(
" Per-field expressions: the Gleam type, encoder, and decoder for one\n"
" lexicon field type, plus the predicates the header/emission logic keys on.\n"
).
-file("src/atproto_codegen/emit/exprs.gleam", 17).
-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", 23).
-spec type_uses_helper(atproto_codegen@lexicon:field_type()) -> boolean().
type_uses_helper(Ft) ->
case Ft of
t_cid_link ->
true;
t_bytes ->
true;
t_unknown ->
true;
{t_union, _} ->
true;
{t_array, Inner} ->
type_uses_helper(Inner);
_ ->
false
end.
-file("src/atproto_codegen/emit/exprs.gleam", 31).
-spec type_uses_blob(atproto_codegen@lexicon:field_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", 39).
-spec type_uses_unknown(atproto_codegen@lexicon:field_type()) -> boolean().
type_uses_unknown(Ft) ->
case Ft of
t_unknown ->
true;
{t_union, _} ->
true;
{t_array, Inner} ->
type_uses_unknown(Inner);
_ ->
false
end.
-file("src/atproto_codegen/emit/exprs.gleam", 47).
-spec union_of(atproto_codegen@lexicon:field_type()) -> gleam@option:option(list(binary())).
union_of(Ft) ->
case Ft of
{t_union, Refs} ->
{some, Refs};
{t_array, Inner} ->
union_of(Inner);
_ ->
none
end.
-file("src/atproto_codegen/emit/exprs.gleam", 56).
?DOC(" The synthesized name for a property's inline union type.\n").
-spec union_type_name(
atproto_codegen@config:config(),
atproto_codegen@lexicon:def(),
atproto_codegen@lexicon:property()
) -> binary().
union_type_name(Cfg, Def, Prop) ->
<<(atproto_codegen@naming:def_type_name(erlang:element(5, Cfg), Def))/binary,
(justin:pascal_case(erlang:element(2, Prop)))/binary>>.
-file("src/atproto_codegen/emit/exprs.gleam", 60).
-spec gleam_type(
atproto_codegen@config:config(),
binary(),
binary(),
atproto_codegen@lexicon:field_type()
) -> binary().
gleam_type(Cfg, Current, Union_name, Ft) ->
case Ft of
t_string ->
<<"String"/utf8>>;
t_int ->
<<"Int"/utf8>>;
t_bool ->
<<"Bool"/utf8>>;
t_number ->
<<"Float"/utf8>>;
{t_ref, Ref} ->
R = atproto_codegen@naming:parse_ref(Current, Ref),
<<(atproto_codegen@naming:qualifier(
erlang:element(5, Cfg),
Current,
erlang:element(2, R)
))/binary,
(atproto_codegen@naming:ref_type_name(erlang:element(5, Cfg), R))/binary>>;
{t_array, Inner} ->
<<<<"List("/utf8,
(gleam_type(Cfg, 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", 87).
-spec encoder_expr(
atproto_codegen@config:config(),
binary(),
binary(),
atproto_codegen@lexicon:field_type()
) -> binary().
encoder_expr(Cfg, Current, Union_name, Ft) ->
case Ft of
t_string ->
<<"json.string"/utf8>>;
t_int ->
<<"json.int"/utf8>>;
t_bool ->
<<"json.bool"/utf8>>;
t_number ->
<<"json.float"/utf8>>;
{t_ref, Ref} ->
R = atproto_codegen@naming:parse_ref(Current, Ref),
<<<<(atproto_codegen@naming:qualifier(
erlang:element(5, Cfg),
Current,
erlang:element(2, R)
))/binary,
"encode_"/utf8>>/binary,
(justin:snake_case(
atproto_codegen@naming:ref_type_name(
erlang:element(5, Cfg),
R
)
))/binary>>;
{t_array, Inner} ->
<<<<"fn(items) { json.array(items, "/utf8,
(encoder_expr(Cfg, 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", 117).
-spec decoder_expr(
atproto_codegen@config:config(),
binary(),
binary(),
atproto_codegen@lexicon:field_type()
) -> binary().
decoder_expr(Cfg, Current, Union_name, Ft) ->
case Ft of
t_string ->
<<"decode.string"/utf8>>;
t_int ->
<<"decode.int"/utf8>>;
t_bool ->
<<"decode.bool"/utf8>>;
t_number ->
<<"decode.float"/utf8>>;
{t_ref, Ref} ->
R = atproto_codegen@naming:parse_ref(Current, Ref),
<<<<(atproto_codegen@naming:qualifier(
erlang:element(5, Cfg),
Current,
erlang:element(2, R)
))/binary,
(justin:snake_case(
atproto_codegen@naming:ref_type_name(
erlang:element(5, Cfg),
R
)
))/binary>>/binary,
"_decoder()"/utf8>>;
{t_array, Inner} ->
<<<<"decode.list("/utf8,
(decoder_expr(Cfg, 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.