Current section

Files

Jump to
sprocket src sprocket@renderers@json.erl
Raw

src/sprocket@renderers@json.erl

-module(sprocket@renderers@json).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([json_renderer/0]).
-spec text(binary()) -> gleam@json:json().
text(T) ->
gleam@json:string(T).
-spec maybe_append_string(
list({binary(), gleam@json:json()}),
binary(),
gleam@option:option(binary())
) -> list({binary(), gleam@json:json()}).
maybe_append_string(Json_object_builder, Key, Value) ->
case Value of
{some, V} ->
gleam@list:append(
Json_object_builder,
[{Key, gleam@json:string(V)}]
);
none ->
Json_object_builder
end.
-spec element(
binary(),
gleam@option:option(binary()),
list(sprocket@internal@reconcile:reconciled_attribute()),
list(sprocket@internal@reconcile:reconciled_element())
) -> gleam@json:json().
element(Tag, Key, Attrs, Children) ->
{Attrs@2, Events@1, Hooks@1} = begin
_pipe = Attrs,
gleam@list:fold(
_pipe,
{[], [], []},
fun(Acc, Attr) ->
{Attrs@1, Events, Hooks} = Acc,
case Attr of
{reconciled_attribute, Name, Value} ->
{[{Name, gleam@json:string(Value)} | Attrs@1],
Events,
Hooks};
{reconciled_event_handler, Kind, Id} ->
{Attrs@1,
[begin
_pipe@1 = [{<<"kind"/utf8>>,
gleam@json:string(Kind)},
{<<"id"/utf8>>, gleam@json:string(Id)}],
gleam@json:object(_pipe@1)
end |
Events],
Hooks};
{reconciled_client_hook, Name@1, Id@1} ->
{Attrs@1,
Events,
[begin
_pipe@2 = [{<<"name"/utf8>>,
gleam@json:string(Name@1)},
{<<"id"/utf8>>, gleam@json:string(Id@1)}],
gleam@json:object(_pipe@2)
end |
Hooks]}
end
end
)
end,
Children@1 = begin
_pipe@3 = Children,
gleam@list:index_map(
_pipe@3,
fun(Child, I) -> {gleam@int:to_string(I), render(Child)} end
)
end,
_pipe@4 = [{<<"type"/utf8>>, gleam@json:string(<<"element"/utf8>>)},
{<<"tag"/utf8>>, gleam@json:string(Tag)},
{<<"attrs"/utf8>>, gleam@json:object(Attrs@2)},
{<<"events"/utf8>>, gleam@json:preprocessed_array(Events@1)},
{<<"hooks"/utf8>>, gleam@json:preprocessed_array(Hooks@1)}],
_pipe@5 = maybe_append_string(_pipe@4, <<"key"/utf8>>, Key),
_pipe@6 = gleam@list:append(_pipe@5, Children@1),
gleam@json:object(_pipe@6).
-spec render(sprocket@internal@reconcile:reconciled_element()) -> gleam@json:json().
render(El) ->
case El of
{reconciled_element, Tag, Key, Attrs, Children} ->
element(Tag, Key, Attrs, Children);
{reconciled_component, _, Key@1, _, _, El@1} ->
component(Key@1, El@1);
{reconciled_fragment, Key@2, Children@1} ->
fragment(Key@2, Children@1);
{reconciled_ignore_update, El@2} ->
render(El@2);
{reconciled_text, T} ->
text(T)
end.
-spec json_renderer() -> sprocket@render:renderer(gleam@json:json()).
json_renderer() ->
{renderer, fun(El) -> render(El) end}.
-spec component(
gleam@option:option(binary()),
sprocket@internal@reconcile:reconciled_element()
) -> gleam@json:json().
component(Key, El) ->
_pipe = [{<<"type"/utf8>>, gleam@json:string(<<"component"/utf8>>)}],
_pipe@1 = maybe_append_string(_pipe, <<"key"/utf8>>, Key),
_pipe@2 = gleam@list:append(_pipe@1, [{<<"0"/utf8>>, render(El)}]),
gleam@json:object(_pipe@2).
-spec fragment(
gleam@option:option(binary()),
list(sprocket@internal@reconcile:reconciled_element())
) -> gleam@json:json().
fragment(Key, Children) ->
Children@1 = begin
_pipe = Children,
gleam@list:index_map(
_pipe,
fun(Child, I) -> {gleam@int:to_string(I), render(Child)} end
)
end,
_pipe@1 = [{<<"type"/utf8>>, gleam@json:string(<<"fragment"/utf8>>)}],
_pipe@2 = maybe_append_string(_pipe@1, <<"key"/utf8>>, Key),
_pipe@3 = gleam@list:append(_pipe@2, Children@1),
gleam@json:object(_pipe@3).