-module(atproto_codegen@emit@def). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/atproto_codegen/emit/def.gleam"). -export([emit_type/5, emit_fields/5, emit_zero/6, emit_encoder/3, emit_decoder/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( " Emission for one record/object def: its type, `_fields` helper (object\n" " entries without `$type`, composable by union encoders), encoder,\n" " decoder, and (only for defs reachable as a closed union's fallback\n" " member) a mechanical `_zero()` helper.\n" ). -file("src/atproto_codegen/emit/def.gleam", 17). -spec emit_type( atproto_codegen@config:config(), binary(), atproto_codegen@lower:flat_def(), binary(), list(atproto_codegen@lower:flat_field()) ) -> binary(). emit_type(Cfg, Nsid, Def, Name, Props) -> case Props of [] -> <<<<<<<<"pub type "/utf8, Name/binary>>/binary, " {\n "/utf8>>/binary, Name/binary>>/binary, "\n}"/utf8>>; _ -> Fields = gleam@list:map( Props, fun(P) -> Union_name = atproto_codegen@emit@exprs:union_type_name( Cfg, Def, P ), T = atproto_codegen@emit@exprs:gleam_type( Cfg, Nsid, Union_name, erlang:element(3, P) ), T@1 = case erlang:element(4, P) of true -> T; false -> <<<<"Option("/utf8, T/binary>>/binary, ")"/utf8>> end, <<<<<<<<" "/utf8, (atproto_codegen@naming:field_name( erlang:element(2, P) ))/binary>>/binary, ": "/utf8>>/binary, T@1/binary>>/binary, ","/utf8>> end ), <<<<<<<<<<<<"pub type "/utf8, Name/binary>>/binary, " {\n "/utf8>>/binary, Name/binary>>/binary, "(\n"/utf8>>/binary, (gleam@string:join(Fields, <<"\n"/utf8>>))/binary>>/binary, "\n )\n}"/utf8>> end. -file("src/atproto_codegen/emit/def.gleam", 100). -spec flatten(list(binary())) -> binary(). flatten(Groups) -> <<<<"list.flatten([\n "/utf8, (gleam@string:join(Groups, <<",\n "/utf8>>))/binary>>/binary, ",\n ])"/utf8>>. -file("src/atproto_codegen/emit/def.gleam", 50). ?DOC( " The def's JSON entries without `$type`, so callers (the plain encoder, and\n" " union encoders that inject a discriminator) compose the final object.\n" ). -spec emit_fields( atproto_codegen@config:config(), binary(), atproto_codegen@lower:flat_def(), binary(), list(atproto_codegen@lower:flat_field()) ) -> binary(). emit_fields(Cfg, Nsid, Def, Name, Props) -> {Required, Optional} = gleam@list:partition( Props, fun(P) -> erlang:element(4, P) end ), Required_entries = gleam@list:map( Required, fun(P@1) -> <<<<<<<<<<<<"#(\""/utf8, (atproto_codegen@emit@exprs:escape( erlang:element(2, P@1) ))/binary>>/binary, "\", "/utf8>>/binary, (atproto_codegen@emit@exprs:encoder_expr( Cfg, Nsid, atproto_codegen@emit@exprs:union_type_name( Cfg, Def, P@1 ), erlang:element(3, P@1) ))/binary>>/binary, "(value."/utf8>>/binary, (atproto_codegen@naming:field_name(erlang:element(2, P@1)))/binary>>/binary, "))"/utf8>> end ), Optional_groups = gleam@list:map( Optional, fun(P@2) -> <<<<<<<<<<<<"internal.opt(\""/utf8, (atproto_codegen@emit@exprs:escape( erlang:element(2, P@2) ))/binary>>/binary, "\", value."/utf8>>/binary, (atproto_codegen@naming:field_name( erlang:element(2, P@2) ))/binary>>/binary, ", "/utf8>>/binary, (atproto_codegen@emit@exprs:encoder_expr( Cfg, Nsid, atproto_codegen@emit@exprs:union_type_name( Cfg, Def, P@2 ), erlang:element(3, P@2) ))/binary>>/binary, ")"/utf8>> end ), Inline = <<<<"["/utf8, (gleam@string:join(Required_entries, <<", "/utf8>>))/binary>>/binary, "]"/utf8>>, Body = case {Required_entries, Optional_groups} of {_, []} -> Inline; {[], [Only]} -> Only; {[], Groups} -> flatten(Groups); {_, Groups@1} -> flatten([Inline | Groups@1]) end, Value_arg = case Props of [] -> <<"_value"/utf8>>; _ -> <<"value"/utf8>> end, <<<<<<<<<<<<<<<<"pub fn "/utf8, (justin:snake_case(Name))/binary>>/binary, "_fields("/utf8>>/binary, Value_arg/binary>>/binary, ": "/utf8>>/binary, Name/binary>>/binary, ") -> List(#(String, json.Json)) {\n "/utf8>>/binary, Body/binary>>/binary, "\n}"/utf8>>. -file("src/atproto_codegen/emit/def.gleam", 105). ?DOC(" The def's `_zero()` helper; emitted only for fallback members.\n"). -spec emit_zero( atproto_codegen@emit@zero:zero_plan(), atproto_codegen@config:config(), binary(), atproto_codegen@lower:flat_def(), binary(), list(atproto_codegen@lower:flat_field()) ) -> binary(). emit_zero(Plan, Cfg, Nsid, Def, Name, Props) -> Snake = justin:snake_case(Name), case Props of [] -> <<<<<<<<<<<<"pub fn "/utf8, Snake/binary>>/binary, "_zero() -> "/utf8>>/binary, Name/binary>>/binary, " {\n "/utf8>>/binary, Name/binary>>/binary, "\n}"/utf8>>; _ -> Fields = gleam@list:map( Props, fun(P) -> Value = case erlang:element(4, P) of true -> atproto_codegen@emit@exprs:zero_expr( Plan, Cfg, Nsid, atproto_codegen@emit@exprs:union_type_name( Cfg, Def, P ), erlang:element(3, P) ); false -> <<"option.None"/utf8>> end, <<<<<<<<" "/utf8, (atproto_codegen@naming:field_name( erlang:element(2, P) ))/binary>>/binary, ": "/utf8>>/binary, Value/binary>>/binary, ","/utf8>> end ), <<<<<<<<<<<<<<<<"pub fn "/utf8, Snake/binary>>/binary, "_zero() -> "/utf8>>/binary, Name/binary>>/binary, " {\n "/utf8>>/binary, Name/binary>>/binary, "(\n"/utf8>>/binary, (gleam@string:join(Fields, <<"\n"/utf8>>))/binary>>/binary, "\n )\n}"/utf8>> end. -file("src/atproto_codegen/emit/def.gleam", 146). -spec emit_encoder(binary(), binary(), boolean()) -> binary(). emit_encoder(Nsid, Name, Is_record) -> Snake = justin:snake_case(Name), Body = case Is_record of true -> <<<<<<<<"json.object([\n #(\"$type\", json.string(\""/utf8, (atproto_codegen@emit@exprs:escape(Nsid))/binary>>/binary, "\")),\n .."/utf8>>/binary, Snake/binary>>/binary, "_fields(value)\n ])"/utf8>>; false -> <<<<"json.object("/utf8, Snake/binary>>/binary, "_fields(value))"/utf8>> end, <<<<<<<<<<<<"pub fn encode_"/utf8, Snake/binary>>/binary, "(value: "/utf8>>/binary, Name/binary>>/binary, ") -> json.Json {\n "/utf8>>/binary, Body/binary>>/binary, "\n}"/utf8>>. -file("src/atproto_codegen/emit/def.gleam", 166). -spec emit_decoder( atproto_codegen@config:config(), binary(), atproto_codegen@lower:flat_def(), binary(), list(atproto_codegen@lower:flat_field()) ) -> binary(). emit_decoder(Cfg, Nsid, Def, Name, Props) -> Uses = gleam@list:map( Props, fun(P) -> Var = atproto_codegen@naming:field_name(erlang:element(2, P)), Union_name = atproto_codegen@emit@exprs:union_type_name(Cfg, Def, P), case erlang:element(4, P) of true -> <<<<<<<<<<<<" use "/utf8, Var/binary>>/binary, " <- decode.field(\""/utf8>>/binary, (atproto_codegen@emit@exprs:escape( erlang:element(2, P) ))/binary>>/binary, "\", "/utf8>>/binary, (atproto_codegen@emit@exprs:decoder_expr( Cfg, Nsid, Union_name, erlang:element(3, P) ))/binary>>/binary, ")"/utf8>>; false -> <<<<<<<<<<<<" use "/utf8, Var/binary>>/binary, " <- decode.optional_field(\""/utf8>>/binary, (atproto_codegen@emit@exprs:escape( erlang:element(2, P) ))/binary>>/binary, "\", option.None, decode.optional("/utf8>>/binary, (atproto_codegen@emit@exprs:decoder_expr( Cfg, Nsid, Union_name, erlang:element(3, P) ))/binary>>/binary, "))"/utf8>> end end ), Constructor = case Props of [] -> <<<<" decode.success("/utf8, Name/binary>>/binary, ")"/utf8>>; _ -> <<<<<<<<" decode.success("/utf8, Name/binary>>/binary, "("/utf8>>/binary, (gleam@string:join( gleam@list:map( Props, fun(P@1) -> <<(atproto_codegen@naming:field_name( erlang:element(2, P@1) ))/binary, ":"/utf8>> end ), <<", "/utf8>> ))/binary>>/binary, "))"/utf8>> end, <<<<<<<<<<<<<<<<"pub fn "/utf8, (justin:snake_case(Name))/binary>>/binary, "_decoder() -> decode.Decoder("/utf8>>/binary, Name/binary>>/binary, ") {\n"/utf8>>/binary, (gleam@string:join(Uses, <<"\n"/utf8>>))/binary>>/binary, "\n"/utf8>>/binary, Constructor/binary>>/binary, "\n}"/utf8>>.