Current section
Files
Jump to
Current section
Files
src/deriv@json.erl
-module(deriv@json).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([suppress_option_warnings/0, gen/5]).
-export_type([var_field/0, j_type/0]).
-type var_field() :: {var_field, binary(), glance:type()}.
-type j_type() :: {j_type,
binary(),
gleam@option:option(binary()),
list(j_type())}.
-file("src/deriv/json.gleam", 193).
-spec variant_field(glance:variant_field()) -> var_field().
variant_field(Field) ->
case Field of
{labelled_variant_field, Item, Label} ->
{var_field, Label, Item};
{unlabelled_variant_field, _} ->
erlang:error(#{gleam_error => panic,
message => <<"Not implemented: `glance.UnlabelledVariantField`"/utf8>>,
module => <<"deriv/json"/utf8>>,
function => <<"variant_field"/utf8>>,
line => 196})
end.
-file("src/deriv/json.gleam", 206).
-spec jtype(glance:type()) -> j_type().
jtype(Type_) ->
case Type_ of
{named_type, Name, Module, Ps} ->
{j_type, Name, Module, gleam@list:map(Ps, fun jtype/1)};
_ ->
gleam@io:debug(Type_),
erlang:error(#{gleam_error => panic,
message => <<"Not implemented for `glance.Type` constructor printed above"/utf8>>,
module => <<"deriv/json"/utf8>>,
function => <<"jtype"/utf8>>,
line => 213})
end.
-file("src/deriv/json.gleam", 135).
-spec uses_uuid(glance:custom_type()) -> boolean().
uses_uuid(Type_) ->
_pipe = erlang:element(6, Type_),
gleam@list:any(
_pipe,
fun(Var) ->
gleam@list:any(
erlang:element(3, Var),
fun(Field) ->
Field@1 = variant_field(Field),
Type_@1 = jtype(erlang:element(3, Field@1)),
erlang:element(2, Type_@1) =:= <<"Uuid"/utf8>>
end
)
end
).
-file("src/deriv/json.gleam", 146).
-spec uses_birl_time(glance:custom_type()) -> boolean().
uses_birl_time(Type_) ->
_pipe = erlang:element(6, Type_),
gleam@list:any(
_pipe,
fun(Var) ->
gleam@list:any(
erlang:element(3, Var),
fun(Field) ->
Field@1 = variant_field(Field),
Type_@1 = jtype(erlang:element(3, Field@1)),
erlang:element(2, Type_@1) =:= <<"Time"/utf8>>
end
)
end
).
-file("src/deriv/json.gleam", 122).
-spec needs_util_import(glance:custom_type()) -> boolean().
needs_util_import(Type_) ->
uses_uuid(Type_) orelse uses_birl_time(Type_).
-file("src/deriv/json.gleam", 157).
-spec uses_list(glance:custom_type()) -> boolean().
uses_list(Type_) ->
_pipe = erlang:element(6, Type_),
gleam@list:any(
_pipe,
fun(Var) ->
gleam@list:any(
erlang:element(3, Var),
fun(Field) ->
Field@1 = variant_field(Field),
Type_@1 = jtype(erlang:element(3, Field@1)),
_pipe@1 = erlang:element(2, Type_@1),
gleam_stdlib:string_starts_with(_pipe@1, <<"List"/utf8>>)
end
)
end
).
-file("src/deriv/json.gleam", 127).
-spec needs_list_import(glance:custom_type()) -> boolean().
needs_list_import(Type_) ->
uses_list(Type_).
-file("src/deriv/json.gleam", 44).
-spec gen_imports(list(binary()), glance:custom_type()) -> list(glance:import()).
gen_imports(Opts, Type_) ->
Json_imports = begin
_pipe = [{<<"decode"/utf8>>,
[{import,
<<"decode"/utf8>>,
none,
[{unqualified_import, <<"Decoder"/utf8>>, none}],
[]}]},
{<<"encode"/utf8>>,
[{import,
<<"gleam/json"/utf8>>,
none,
[{unqualified_import, <<"Json"/utf8>>, none}],
[]}]}],
maps:from_list(_pipe)
end,
_pipe@1 = Opts,
_pipe@2 = gleam@list:unique(_pipe@1),
_pipe@3 = gleam@list:map(
_pipe@2,
fun(Opt) -> gleam_stdlib:map_get(Json_imports, Opt) end
),
_pipe@4 = gleam@result:values(_pipe@3),
_pipe@5 = gleam@list:flatten(_pipe@4),
(fun(Imports) -> _pipe@6 = Imports,
_pipe@7 = lists:append(_pipe@6, case needs_util_import(Type_) of
false ->
[];
true ->
[{import, <<"deriv/util"/utf8>>, none, [], []}]
end),
lists:append(_pipe@7, case needs_list_import(Type_) of
false ->
[];
true ->
[{import, <<"gleam/list"/utf8>>, none, [], []}]
end) end)(_pipe@5).
-file("src/deriv/json.gleam", 218).
-spec json_field_name(var_field(), list(deriv@types:deriv_field_opt())) -> binary().
json_field_name(Field, Field_opts) ->
_pipe = Field_opts,
_pipe@1 = gleam@list:find_map(
_pipe,
fun(Opt) -> case erlang:element(2, Opt) of
[<<"json"/utf8>>, <<"named"/utf8>>, Val] ->
{ok, Val};
_ ->
{error, nil}
end end
),
(fun(X) -> case {X, Field} of
{{ok, Val@1}, _} ->
Val@1;
{{error, _}, Field@1} ->
erlang:element(2, Field@1)
end end)(_pipe@1).
-file("src/deriv/json.gleam", 234).
-spec suppress_option_warnings() -> list(gleam@option:option(nil)).
suppress_option_warnings() ->
[none, {some, nil}].
-file("src/deriv/json.gleam", 470).
-spec dummy_location() -> glance:span().
dummy_location() ->
{span, -1, -1}.
-file("src/deriv/json.gleam", 508).
-spec decoder_type_variant_func_name(glance:custom_type(), glance:variant()) -> binary().
decoder_type_variant_func_name(Type_, Variant) ->
<<<<<<"decoder_"/utf8,
(deriv@util:snake_case(erlang:element(2, Type_)))/binary>>/binary,
"_"/utf8>>/binary,
(deriv@util:snake_case(erlang:element(2, Variant)))/binary>>.
-file("src/deriv/json.gleam", 720).
-spec specifies_decoder(list(deriv@types:deriv_field_opt())) -> gleam@option:option(binary()).
specifies_decoder(Opts) ->
_pipe = Opts,
_pipe@1 = lists:reverse(_pipe),
_pipe@2 = gleam@list:find_map(_pipe@1, fun(X) -> case X of
{deriv_field_opt,
[<<"json"/utf8>>, <<"decoder"/utf8>>, Decoder_name]} ->
{ok, Decoder_name};
_ ->
{error, nil}
end end),
gleam@option:from_result(_pipe@2).
-file("src/deriv/json.gleam", 734).
-spec birl_time_decode_expr(deriv@util:birl_time_kind()) -> glance:expression().
birl_time_decode_expr(Birl_time_kind) ->
_pipe = Birl_time_kind,
_pipe@1 = (fun(Kind) -> case Kind of
birl_time_iso8601 ->
<<"decoder_birl_parse"/utf8>>;
birl_time_naive ->
<<"decoder_birl_from_naive"/utf8>>;
birl_time_http ->
<<"decoder_birl_from_http"/utf8>>;
birl_time_unix ->
<<"decoder_birl_from_unix"/utf8>>;
birl_time_unix_milli ->
<<"decoder_birl_from_unix_milli"/utf8>>;
birl_time_unix_micro ->
<<"decoder_birl_from_unix_micro"/utf8>>
end end)(_pipe),
(fun(Func) ->
{call, {field_access, {variable, <<"util"/utf8>>}, Func}, []}
end)(_pipe@1).
-file("src/deriv/json.gleam", 684).
-spec unparameterized_type_decode_expr(
binary(),
deriv@util:birl_time_kind(),
list(deriv@types:deriv_field_opt())
) -> glance:expression().
unparameterized_type_decode_expr(Type_name, Birl_time_kind, Opts) ->
case Type_name of
<<"Int"/utf8>> ->
{field_access, {variable, <<"decode"/utf8>>}, <<"int"/utf8>>};
<<"Float"/utf8>> ->
{field_access, {variable, <<"decode"/utf8>>}, <<"float"/utf8>>};
<<"String"/utf8>> ->
{field_access, {variable, <<"decode"/utf8>>}, <<"string"/utf8>>};
<<"Bool"/utf8>> ->
{field_access, {variable, <<"decode"/utf8>>}, <<"bool"/utf8>>};
<<"Uuid"/utf8>> ->
{call,
{field_access,
{variable, <<"util"/utf8>>},
<<"decoder_uuid"/utf8>>},
[]};
<<"Time"/utf8>> ->
birl_time_decode_expr(Birl_time_kind);
_ ->
{call,
{variable,
<<"decoder_"/utf8,
(deriv@util:snake_case(Type_name))/binary>>},
[]}
end.
-file("src/deriv/json.gleam", 512).
-spec decode_field_expr(
glance:custom_type(),
glance:variant(),
glance:variant_field(),
gleam@dict:dict(deriv@types:deriv_field(), list(deriv@types:deriv_field_opt()))
) -> {binary(), gleam@option:option(binary()), glance:expression()}.
decode_field_expr(Type_, Variant, Field, All_field_opts) ->
Field@1 = variant_field(Field),
T = jtype(erlang:element(3, Field@1)),
Birl_time_kind = deriv@util:birl_time_kind(
Type_,
Variant,
erlang:element(2, Field@1),
All_field_opts
),
Opts = deriv@util:get_field_opts(
All_field_opts,
Type_,
Variant,
erlang:element(2, Field@1)
),
Expr = case {specifies_decoder(Opts), T, erlang:element(4, T)} of
{{some, Decoder_name}, _, _} ->
{call, {variable, Decoder_name}, []};
{_,
{j_type,
<<"Option"/utf8>>,
_,
[{j_type, <<"List"/utf8>>, _, [{j_type, _, _, []} = Param]}]},
_} ->
Inner = begin
_pipe = erlang:element(2, Param),
_pipe@1 = unparameterized_type_decode_expr(
_pipe,
Birl_time_kind,
Opts
),
{unlabelled_field, _pipe@1}
end,
{call,
{field_access,
{variable, <<"decode"/utf8>>},
<<"optional"/utf8>>},
[{unlabelled_field,
{call,
{field_access,
{variable, <<"decode"/utf8>>},
<<"list"/utf8>>},
[Inner]}}]};
{_, _, []} ->
unparameterized_type_decode_expr(
erlang:element(2, T),
Birl_time_kind,
Opts
);
{_, _, [{j_type, _, _, []} = Param@1]} ->
case erlang:element(2, T) of
<<"List"/utf8>> ->
Inner@1 = begin
_pipe@2 = erlang:element(2, Param@1),
_pipe@3 = unparameterized_type_decode_expr(
_pipe@2,
Birl_time_kind,
Opts
),
{unlabelled_field, _pipe@3}
end,
{call,
{field_access,
{variable, <<"decode"/utf8>>},
<<"list"/utf8>>},
[Inner@1]};
<<"Option"/utf8>> ->
Inner@2 = begin
_pipe@4 = erlang:element(2, Param@1),
_pipe@5 = unparameterized_type_decode_expr(
_pipe@4,
Birl_time_kind,
Opts
),
{unlabelled_field, _pipe@5}
end,
{call,
{field_access,
{variable, <<"decode"/utf8>>},
<<"optional"/utf8>>},
[Inner@2]};
_ ->
gleam@io:debug(Field@1),
erlang:error(#{gleam_error => panic,
message => <<"unimplemented"/utf8>>,
module => <<"deriv/json"/utf8>>,
function => <<"decode_field_expr"/utf8>>,
line => 564})
end;
{_, _, [{j_type, _, _, []} = Key_param, {j_type, _, _, []} = Val_param]} ->
case {erlang:element(2, T), erlang:element(2, Key_param)} of
{<<"Dict"/utf8>>, <<"String"/utf8>>} ->
Val_decode_expr = unparameterized_type_decode_expr(
erlang:element(2, Val_param),
Birl_time_kind,
Opts
),
{call,
{field_access,
{variable, <<"decode"/utf8>>},
<<"dict"/utf8>>},
[{unlabelled_field,
{field_access,
{variable, <<"decode"/utf8>>},
<<"string"/utf8>>}},
{unlabelled_field, Val_decode_expr}]};
{_, _} ->
gleam@io:debug(Field@1),
erlang:error(#{gleam_error => panic,
message => <<"unimplemented"/utf8>>,
module => <<"deriv/json"/utf8>>,
function => <<"decode_field_expr"/utf8>>,
line => 586})
end;
{_, _, _} ->
gleam@io:debug(Field@1),
erlang:error(#{gleam_error => panic,
message => <<"unimplemented"/utf8>>,
module => <<"deriv/json"/utf8>>,
function => <<"decode_field_expr"/utf8>>,
line => 591})
end,
Json_field_name = begin
_pipe@6 = All_field_opts,
_pipe@7 = deriv@util:get_field_opts(
_pipe@6,
Type_,
Variant,
erlang:element(2, Field@1)
),
json_field_name(Field@1, _pipe@7)
end,
{erlang:element(2, Field@1), {some, Json_field_name}, Expr}.
-file("src/deriv/json.gleam", 603).
-spec decoder_type_variant_func(
glance:custom_type(),
glance:variant(),
gleam@dict:dict(deriv@types:deriv_field(), list(deriv@types:deriv_field_opt()))
) -> glance:definition(glance:function_()).
decoder_type_variant_func(Type_, Variant, All_field_opts) ->
Name = decoder_type_variant_func_name(Type_, Variant),
Parameters = [],
Return = {some,
{named_type,
<<"Decoder"/utf8>>,
none,
[{named_type, erlang:element(2, Type_), none, []}]}},
Pipe_exprs = begin
_pipe = erlang:element(3, Variant),
gleam@list:map(
_pipe,
fun(_capture) ->
decode_field_expr(Type_, Variant, _capture, All_field_opts)
end
)
end,
Fields = begin
_pipe@1 = Pipe_exprs,
gleam@list:map(
_pipe@1,
fun(X) ->
{Field, _, _} = X,
Field
end
)
end,
Use_exprs = begin
_pipe@2 = Fields,
gleam@list:map(
_pipe@2,
fun(Field@1) ->
{use,
[{pattern_variable, Field@1}],
{field_access,
{variable, <<"decode"/utf8>>},
<<"parameter"/utf8>>}}
end
)
end,
Constr_args = begin
_pipe@3 = Fields,
gleam@list:map(_pipe@3, fun(Field@0) -> {shorthand_field, Field@0} end)
end,
Decode_into_call = {call,
{field_access, {variable, <<"decode"/utf8>>}, <<"into"/utf8>>},
[{unlabelled_field,
{block,
begin
_pipe@4 = Use_exprs,
lists:append(
_pipe@4,
[{expression,
{call,
{variable, erlang:element(2, Variant)},
Constr_args}}]
)
end}}]},
Body = begin
_pipe@7 = gleam@list:fold(
Pipe_exprs,
Decode_into_call,
fun(Acc, X@1) ->
{Field@2, Json_field, Expr} = X@1,
Json_field@1 = begin
_pipe@5 = Json_field,
gleam@option:unwrap(_pipe@5, Field@2)
end,
Call = case gleam@string:split(Json_field@1, <<"."/utf8>>) of
[] ->
erlang:error(#{gleam_error => panic,
message => <<"`panic` expression evaluated."/utf8>>,
module => <<"deriv/json"/utf8>>,
function => <<"decoder_type_variant_func"/utf8>>,
line => 650});
[Json_field@2] ->
{call,
{field_access,
{variable, <<"decode"/utf8>>},
<<"field"/utf8>>},
[{unlabelled_field, {string, Json_field@2}},
{unlabelled_field, Expr}]};
Json_fields ->
Json_fields@1 = begin
_pipe@6 = Json_fields,
gleam@list:map(
_pipe@6,
fun(Str) -> {string, Str} end
)
end,
{call,
{field_access,
{variable, <<"decode"/utf8>>},
<<"subfield"/utf8>>},
[{unlabelled_field, {list, Json_fields@1, none}},
{unlabelled_field, Expr}]}
end,
{binary_operator, pipe, Acc, Call}
end
),
(fun(Expr@1) -> [{expression, Expr@1}] end)(_pipe@7)
end,
{definition,
[],
{function, Name, public, Parameters, Return, Body, dummy_location()}}.
-file("src/deriv/json.gleam", 474).
-spec decoder_type_func(
glance:custom_type(),
gleam@dict:dict(deriv@types:deriv_field(), list(deriv@types:deriv_field_opt()))
) -> list(glance:definition(glance:function_())).
decoder_type_func(Type_, All_field_opts) ->
Variant_funcs = begin
_pipe = erlang:element(6, Type_),
gleam@list:map(
_pipe,
fun(_capture) ->
decoder_type_variant_func(Type_, _capture, All_field_opts)
end
)
end,
Decoder_call_exprs = begin
_pipe@1 = Variant_funcs,
gleam@list:map(
_pipe@1,
fun(Func) -> {call, {variable, deriv@util:func_name(Func)}, []} end
)
end,
Body = [{expression,
{call,
{field_access, {variable, <<"decode"/utf8>>}, <<"one_of"/utf8>>},
[{unlabelled_field, {list, Decoder_call_exprs, none}}]}}],
Type_func = {definition,
[],
{function,
<<"decoder_"/utf8,
(deriv@util:snake_case(erlang:element(2, Type_)))/binary>>,
public,
[],
{some,
{named_type,
<<"Decoder"/utf8>>,
none,
[{named_type, erlang:element(2, Type_), none, []}]}},
Body,
dummy_location()}},
lists:append([Type_func], Variant_funcs).
-file("src/deriv/json.gleam", 169).
-spec gen_json_decoders(
glance:custom_type(),
gleam@dict:dict(deriv@types:deriv_field(), list(deriv@types:deriv_field_opt())),
deriv@types:file()
) -> list(glance:definition(glance:function_())).
gen_json_decoders(Type_, Field_opts, _) ->
decoder_type_func(Type_, Field_opts).
-file("src/deriv/json.gleam", 753).
-spec birl_time_encode_expr(deriv@util:birl_time_kind()) -> glance:expression().
birl_time_encode_expr(Birl_time_kind) ->
_pipe = Birl_time_kind,
_pipe@1 = (fun(Kind) -> case Kind of
birl_time_iso8601 ->
<<"encode_birl_to_iso8601"/utf8>>;
birl_time_naive ->
<<"encode_birl_to_naive"/utf8>>;
birl_time_http ->
<<"encode_birl_to_http"/utf8>>;
birl_time_unix ->
<<"encode_birl_to_unix"/utf8>>;
birl_time_unix_milli ->
<<"encode_birl_to_unix_milli"/utf8>>;
birl_time_unix_micro ->
<<"encode_birl_to_unix_micro"/utf8>>
end end)(_pipe),
(fun(Func) -> {field_access, {variable, <<"util"/utf8>>}, Func} end)(
_pipe@1
).
-file("src/deriv/json.gleam", 236).
-spec unparameterized_type_encode_expr(
binary(),
deriv@util:birl_time_kind(),
gleam@option:option(fun((glance:expression()) -> glance:expression()))
) -> glance:expression().
unparameterized_type_encode_expr(Type_name, Birl_time_kind, Wrap) ->
Expr = case Type_name of
<<"Int"/utf8>> ->
{field_access, {variable, <<"json"/utf8>>}, <<"int"/utf8>>};
<<"Float"/utf8>> ->
{field_access, {variable, <<"json"/utf8>>}, <<"float"/utf8>>};
<<"String"/utf8>> ->
{field_access, {variable, <<"json"/utf8>>}, <<"string"/utf8>>};
<<"Bool"/utf8>> ->
{field_access, {variable, <<"json"/utf8>>}, <<"bool"/utf8>>};
<<"Uuid"/utf8>> ->
{field_access, {variable, <<"util"/utf8>>}, <<"encode_uuid"/utf8>>};
<<"Time"/utf8>> ->
birl_time_encode_expr(Birl_time_kind);
_ ->
{variable,
<<"encode_"/utf8, (deriv@util:snake_case(Type_name))/binary>>}
end,
case Wrap of
none ->
Expr;
{some, F} ->
F(Expr)
end.
-file("src/deriv/json.gleam", 258).
-spec encode_field(
glance:custom_type(),
glance:variant(),
var_field(),
gleam@dict:dict(deriv@types:deriv_field(), list(deriv@types:deriv_field_opt()))
) -> glance:expression().
encode_field(Type_, Variant, Field, All_field_opts) ->
Ftype = jtype(erlang:element(3, Field)),
Birl_time_kind = deriv@util:birl_time_kind(
Type_,
Variant,
erlang:element(2, Field),
All_field_opts
),
case {Ftype, erlang:element(4, Ftype)} of
{{j_type,
<<"Option"/utf8>>,
_,
[{j_type, <<"List"/utf8>>, _, [{j_type, _, _, []} = Param]}]},
_} ->
Param_type_encoder = unparameterized_type_encode_expr(
erlang:element(2, Param),
Birl_time_kind,
none
),
{call,
{field_access, {variable, <<"json"/utf8>>}, <<"nullable"/utf8>>},
[{unlabelled_field,
{field_access,
{variable, <<"value"/utf8>>},
erlang:element(2, Field)}},
{unlabelled_field,
{fn_capture,
none,
{field_access,
{variable, <<"json"/utf8>>},
<<"array"/utf8>>},
[],
[{unlabelled_field, Param_type_encoder}]}}]};
{_, []} ->
unparameterized_type_encode_expr(
erlang:element(2, Ftype),
Birl_time_kind,
{some,
fun(Func_expr) ->
{call,
Func_expr,
[{unlabelled_field,
{field_access,
{variable, <<"value"/utf8>>},
erlang:element(2, Field)}}]}
end}
);
{_, [{j_type, Param_type_name, none, []}]} ->
case erlang:element(2, Ftype) of
<<"Option"/utf8>> ->
Param_type_encoder@1 = unparameterized_type_encode_expr(
Param_type_name,
Birl_time_kind,
none
),
{call,
{field_access,
{variable, <<"json"/utf8>>},
<<"nullable"/utf8>>},
[{unlabelled_field,
{field_access,
{variable, <<"value"/utf8>>},
erlang:element(2, Field)}},
{unlabelled_field, Param_type_encoder@1}]};
<<"List"/utf8>> ->
Param_type_encoder@2 = unparameterized_type_encode_expr(
Param_type_name,
Birl_time_kind,
none
),
{call,
{field_access,
{variable, <<"json"/utf8>>},
<<"preprocessed_array"/utf8>>},
[{unlabelled_field,
{call,
{field_access,
{variable, <<"list"/utf8>>},
<<"map"/utf8>>},
[{unlabelled_field,
{field_access,
{variable, <<"value"/utf8>>},
erlang:element(2, Field)}},
{unlabelled_field, Param_type_encoder@2}]}}]};
_ ->
gleam@io:debug(Ftype),
erlang:error(#{gleam_error => panic,
message => <<"Not yet implemented for type printed above"/utf8>>,
module => <<"deriv/json"/utf8>>,
function => <<"encode_field"/utf8>>,
line => 324})
end;
{_,
[{j_type, Key_param_type_name, none, []},
{j_type, Val_param_type_name, none, []}]} ->
case {erlang:element(2, Ftype), Key_param_type_name} of
{<<"Dict"/utf8>>, <<"String"/utf8>>} ->
Val_param_type_encoder = unparameterized_type_encode_expr(
Val_param_type_name,
Birl_time_kind,
none
),
{call,
{field_access,
{variable, <<"json"/utf8>>},
<<"dict"/utf8>>},
[{unlabelled_field,
{field_access,
{variable, <<"value"/utf8>>},
erlang:element(2, Field)}},
{unlabelled_field,
{fn,
[{fn_parameter,
{named, <<"str"/utf8>>},
none}],
none,
[{expression, {variable, <<"str"/utf8>>}}]}},
{unlabelled_field, Val_param_type_encoder}]};
{_, _} ->
gleam@io:debug(Ftype),
erlang:error(#{gleam_error => panic,
message => <<"Not yet implemented for type printed above"/utf8>>,
module => <<"deriv/json"/utf8>>,
function => <<"encode_field"/utf8>>,
line => 347})
end;
{_, _} ->
gleam@io:debug(Ftype),
erlang:error(#{gleam_error => panic,
message => <<"Not yet implemented for type printed above"/utf8>>,
module => <<"deriv/json"/utf8>>,
function => <<"encode_field"/utf8>>,
line => 353})
end.
-file("src/deriv/json.gleam", 358).
-spec encode_variant_json_object_expr(
glance:custom_type(),
glance:variant(),
gleam@dict:dict(deriv@types:deriv_field(), list(deriv@types:deriv_field_opt()))
) -> glance:expression().
encode_variant_json_object_expr(Type_, Variant, All_field_opts) ->
Encode_lines = begin
_pipe = erlang:element(3, Variant),
gleam@list:map(
_pipe,
fun(Field) ->
Field@1 = variant_field(Field),
Json_field_name = begin
_pipe@1 = All_field_opts,
_pipe@2 = deriv@util:get_field_opts(
_pipe@1,
Type_,
Variant,
erlang:element(2, Field@1)
),
json_field_name(Field@1, _pipe@2)
end,
Encode_expr = encode_field(
Type_,
Variant,
Field@1,
All_field_opts
),
case gleam@string:split(Json_field_name, <<"."/utf8>>) of
[] ->
erlang:error(#{gleam_error => panic,
message => <<"`panic` expression evaluated."/utf8>>,
module => <<"deriv/json"/utf8>>,
function => <<"encode_variant_json_object_expr"/utf8>>,
line => 376});
[_] ->
{tuple, [{string, Json_field_name}, Encode_expr]};
[Top_level_field_name | Rest] ->
_pipe@3 = Rest,
_pipe@4 = lists:reverse(_pipe@3),
(fun(Fs) -> case Fs of
[Last_field_name | Other_field_names] ->
Terminal_expr = {call,
{field_access,
{variable, <<"json"/utf8>>},
<<"object"/utf8>>},
[{unlabelled_field,
{list,
[{tuple,
[{string,
Last_field_name},
Encode_expr]}],
none}}]},
Expr = gleam@list:fold(
Other_field_names,
Terminal_expr,
fun(Acc_expr, Json_field_name@1) ->
{call,
{field_access,
{variable, <<"json"/utf8>>},
<<"object"/utf8>>},
[{unlabelled_field,
{list,
[{tuple,
[{string,
Json_field_name@1},
Acc_expr]}],
none}}]}
end
),
{tuple,
[{string, Top_level_field_name}, Expr]};
_ ->
erlang:error(#{gleam_error => panic,
message => <<"`panic` expression evaluated."/utf8>>,
module => <<"deriv/json"/utf8>>,
function => <<"encode_variant_json_object_expr"/utf8>>,
line => 416})
end end)(_pipe@4)
end
end
)
end,
{call,
{field_access, {variable, <<"json"/utf8>>}, <<"object"/utf8>>},
[{unlabelled_field, {list, Encode_lines, none}}]}.
-file("src/deriv/json.gleam", 429).
-spec encode_type_func(
glance:custom_type(),
gleam@dict:dict(deriv@types:deriv_field(), list(deriv@types:deriv_field_opt()))
) -> glance:definition(glance:function_()).
encode_type_func(Type_, All_field_opts) ->
Name = <<"encode_"/utf8,
(deriv@util:snake_case(erlang:element(2, Type_)))/binary>>,
Parameters = [{function_parameter,
none,
{named, <<"value"/utf8>>},
{some, {named_type, erlang:element(2, Type_), none, []}}}],
Return = {some, {named_type, <<"Json"/utf8>>, none, []}},
Encode_variant_clause_exprs = begin
_pipe = erlang:element(6, Type_),
gleam@list:map(
_pipe,
fun(Variant) ->
Encode_json_object_expr = encode_variant_json_object_expr(
Type_,
Variant,
All_field_opts
),
{clause,
[[{pattern_assignment,
{pattern_constructor,
none,
erlang:element(2, Variant),
[],
true},
<<"value"/utf8>>}]],
none,
Encode_json_object_expr}
end
)
end,
Body = begin
_pipe@1 = {expression,
{'case',
[{variable, <<"value"/utf8>>}],
Encode_variant_clause_exprs}},
(fun(Expr) -> [Expr] end)(_pipe@1)
end,
{definition,
[],
{function, Name, public, Parameters, Return, Body, dummy_location()}}.
-file("src/deriv/json.gleam", 177).
-spec gen_json_encoders(
glance:custom_type(),
gleam@dict:dict(deriv@types:deriv_field(), list(deriv@types:deriv_field_opt())),
deriv@types:file()
) -> list(glance:definition(glance:function_())).
gen_json_encoders(Type_, All_field_opts, _) ->
_pipe = encode_type_func(Type_, All_field_opts),
(fun(Func) -> [Func] end)(_pipe).
-file("src/deriv/json.gleam", 11).
-spec gen(
glance:custom_type(),
deriv@types:derivation(),
gleam@dict:dict(deriv@types:deriv_field(), list(deriv@types:deriv_field_opt())),
deriv@types:file(),
fun((binary()) -> {ok, glance:module_()} |
{error, deriv@types:module_reader_err()})
) -> deriv@types:gen().
gen(Type_, Deriv, Field_opts, File, _) ->
Opts = erlang:element(3, Deriv),
Gen_funcs_for_opts = begin
_pipe = [{<<"decode"/utf8>>, fun gen_json_decoders/3},
{<<"encode"/utf8>>, fun gen_json_encoders/3}],
maps:from_list(_pipe)
end,
Imports = gen_imports(Opts, Type_),
Funcs = begin
_pipe@1 = Opts,
_pipe@2 = gleam@list:map(
_pipe@1,
fun(_capture) ->
gleam_stdlib:map_get(Gen_funcs_for_opts, _capture)
end
),
_pipe@3 = gleam@result:values(_pipe@2),
gleam@list:flat_map(_pipe@3, fun(F) -> F(Type_, Field_opts, File) end)
end,
Src = begin
_pipe@4 = Funcs,
_pipe@5 = gleam@list:map(_pipe@4, fun deriv@util:func_str/1),
gleam@string:join(_pipe@5, <<"\n\n"/utf8>>)
end,
{gen, File, Deriv, Imports, Funcs, Src, maps:new()}.