Current section
Files
Jump to
Current section
Files
src/xmljson@element.erl
-module(xmljson@element).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new/1, text/2, inherited_namespaces/2, to_xml_type/1, to_xml/1, from_xml/1, from_json/1, to_json/1]).
-export_type([element/0, decode_error/0]).
-type element() :: {element,
binary(),
list({binary(), binary()}),
list(binary()),
list(element()),
list({binary(), binary()}),
list({binary(), binary()})}.
-type decode_error() :: {decode_error, binary()}.
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 27).
-spec new(binary()) -> element().
new(Name) ->
{element, Name, [], [], [], [], []}.
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 31).
-spec text(element(), list(binary())) -> element().
text(Elem, Data) ->
erlang:setelement(4, Elem, Data).
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 35).
-spec inherited_namespaces(element(), list({binary(), binary()})) -> element().
inherited_namespaces(Elem, Data) ->
erlang:setelement(7, Elem, Data).
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 59).
-spec to_xml_type(element()) -> xmb:xml().
to_xml_type(Elem) ->
Children = begin
_pipe = erlang:element(4, Elem),
gleam@list:map(_pipe, fun(S) -> xmb:text(S) end)
end,
Children@1 = lists:append(
Children,
begin
_pipe@1 = erlang:element(5, Elem),
gleam@list:map(_pipe@1, fun(Elem@1) -> to_xml_type(Elem@1) end)
end
),
Nms = begin
_pipe@2 = erlang:element(6, Elem),
gleam@list:map(_pipe@2, fun(Pair) -> case Pair of
{<<"$"/utf8>>, Value} ->
{<<"xmlns"/utf8>>, Value};
{Key, Value@1} ->
{<<"xmlns:"/utf8, Key/binary>>, Value@1}
end end)
end,
xmb:x(
erlang:element(2, Elem),
lists:append(Nms, erlang:element(3, Elem)),
Children@1
).
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 53).
-spec to_xml(element()) -> binary().
to_xml(Elem) ->
_pipe = to_xml_type(Elem),
_pipe@1 = xmb_ffi:identity(_pipe),
unicode:characters_to_binary(_pipe@1).
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 161).
-spec text_from_json(gleam@dict:dict(binary(), list(gleam@dynamic:dynamic_()))) -> {ok,
list(binary())} |
{error, list(gleam@dynamic:decode_error())}.
text_from_json(Obj) ->
_pipe = gleam_stdlib:map_get(Obj, <<"$"/utf8>>),
_pipe@1 = gleam@result:unwrap(_pipe, []),
_pipe@2 = gleam@list:map(_pipe@1, fun(X) -> gleam@dynamic:string(X) end),
gleam@result:all(_pipe@2).
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 168).
-spec namespaces_from_json(list({binary(), list(gleam@dynamic:dynamic_())})) -> {ok,
list({binary(), binary()})} |
{error, list(gleam@dynamic:decode_error())}.
namespaces_from_json(Xs) ->
X = begin
_pipe = Xs,
gleam@list:filter(_pipe, fun(Pair) -> case erlang:element(1, Pair) of
<<"@xmlns"/utf8>> ->
true;
_ ->
false
end end)
end,
case X of
[] ->
{ok, []};
[{<<"@xmlns"/utf8>>, [Value]}] ->
_pipe@1 = Value,
_pipe@2 = (gleam@dynamic:dict(
fun gleam@dynamic:string/1,
fun gleam@dynamic:string/1
))(_pipe@1),
gleam@result:map(_pipe@2, fun(X@1) -> maps:to_list(X@1) end);
_ ->
{error,
[{decode_error,
<<"Expected only one namespaces dict"/utf8>>,
<<""/utf8>>,
[]}]}
end.
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 211).
-spec attributes_from_json(list({binary(), list(gleam@dynamic:dynamic_())})) -> {ok,
list({binary(), binary()})} |
{error, list(gleam@dynamic:decode_error())}.
attributes_from_json(Xs) ->
_pipe = Xs,
_pipe@1 = gleam@list:filter(
_pipe,
fun(Pair) -> case erlang:element(1, Pair) of
<<"@xmlns"/utf8>> ->
false;
<<"@"/utf8, _/binary>> ->
true;
_ ->
false
end end
),
_pipe@3 = gleam@list:map(_pipe@1, fun(Pair@1) -> case Pair@1 of
{<<"@"/utf8, Key/binary>>, [Value]} ->
_pipe@2 = gleam@dynamic:string(Value),
gleam@result:map(_pipe@2, fun(V) -> {Key, V} end);
_ ->
{error,
[{decode_error,
<<"One value per attribute key."/utf8>>,
<<""/utf8>>,
[]}]}
end end),
gleam@result:all(_pipe@3).
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 232).
-spec dynamic_to_list(gleam@dynamic:dynamic_()) -> list(gleam@dynamic:dynamic_()).
dynamic_to_list(Dyn) ->
case gleam@dynamic:shallow_list(Dyn) of
{ok, Value} ->
Value;
_ ->
[Dyn]
end.
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 107).
-spec unpack_json(gleam@dynamic:dynamic_()) -> {ok,
{binary(), gleam@dict:dict(binary(), list(gleam@dynamic:dynamic_()))}} |
{error, list(gleam@dynamic:decode_error())}.
unpack_json(Dyn) ->
_pipe = Dyn,
_pipe@2 = (gleam@dynamic:dict(
fun gleam@dynamic:string/1,
fun(A) -> _pipe@1 = A,
(gleam@dynamic:dict(
fun gleam@dynamic:string/1,
fun(A@1) -> {ok, dynamic_to_list(A@1)} end
))(_pipe@1) end
))(_pipe),
gleam@result:'try'(_pipe@2, fun(Dct) -> case maps:to_list(Dct) of
[X] ->
{ok, X};
Value ->
{error,
[{decode_error,
<<"Expected list of one element"/utf8>>,
<<"Got "/utf8,
(gleam@string:inspect(Value))/binary>>,
[]}]}
end end).
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 266).
-spec ordered_group(list(JWJ), fun((JWJ) -> JWX)) -> gleam@dict:dict(JWX, list(JWJ)).
ordered_group(Xs, Func) ->
_pipe = gleam@list:fold(
Xs,
maps:new(),
fun(Acc, X) -> gleam@dict:upsert(Acc, Func(X), fun(Ys) -> case Ys of
none ->
[X];
{some, Y} ->
[X | Y]
end end) end
),
gleam@dict:map_values(_pipe, fun(_, X@1) -> lists:reverse(X@1) end).
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 307).
-spec to_optional_list(binary(), list(gleam@json:json())) -> list({binary(),
gleam@json:json()}).
to_optional_list(Key, Array) ->
case Array of
[_, _ | _] ->
Jarr = gleam@json:array(Array, fun(A) -> A end),
[{Key, Jarr}];
_ ->
_pipe = Array,
gleam@list:map(_pipe, fun(Jsn) -> {Key, Jsn} end)
end.
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 278).
-spec attributes_to_json(list({binary(), binary()})) -> list({binary(),
gleam@json:json()}).
attributes_to_json(Attributes) ->
Dct = ordered_group(Attributes, fun(Pair) -> erlang:element(1, Pair) end),
_pipe = Dct,
_pipe@1 = maps:to_list(_pipe),
gleam@list:flat_map(
_pipe@1,
fun(Pair@1) ->
{Key, Value} = Pair@1,
_pipe@2 = Value,
_pipe@3 = gleam@list:map(
_pipe@2,
fun(Pair@2) -> gleam@json:string(erlang:element(2, Pair@2)) end
),
to_optional_list(<<"@"/utf8, Key/binary>>, _pipe@3)
end
).
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 397).
-spec parse_xml_attributes(list(xmlm:attribute())) -> {list({binary(), binary()}),
list({binary(), binary()})}.
parse_xml_attributes(Xs) ->
_pipe = Xs,
gleam@list:fold(
_pipe,
{[], []},
fun(Acc, X) ->
{attribute, {name, Nm, Key}, Value} = X,
case Nm of
<<""/utf8>> ->
{[{Key, Value} | erlang:element(1, Acc)],
erlang:element(2, Acc)};
_ ->
{erlang:element(1, Acc),
[{Key, Value} | erlang:element(2, Acc)]}
end
end
).
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 320).
-spec reduce_xml_input(element(), xmlm:input()) -> {ok,
{element(), xmlm:input()}} |
{error, xmlm:input_error()}.
reduce_xml_input(Elem, Input) ->
case xmlm:signal(Input) of
{ok, {{element_start, {tag, {name, Namespace, Name}, Attr}}, Input@1}} ->
{Attr@1, Nms} = begin
_pipe = Attr,
parse_xml_attributes(_pipe)
end,
Inms = lists:append(
erlang:element(6, Elem),
erlang:element(7, Elem)
),
Lookup_nms = begin
_pipe@1 = Inms,
_pipe@2 = gleam@list:map(
_pipe@1,
fun(Pair) ->
{erlang:element(2, Pair), erlang:element(1, Pair)}
end
),
maps:from_list(_pipe@2)
end,
Name@1 = case gleam_stdlib:map_get(Lookup_nms, Namespace) of
{ok, <<"$"/utf8>>} ->
Name;
{ok, Nm} ->
<<<<Nm/binary, ":"/utf8>>/binary, Name/binary>>;
_ ->
Name
end,
Inms_dct = maps:from_list(Inms),
Nms@1 = begin
_pipe@3 = Nms,
_pipe@4 = gleam@list:filter(
_pipe@3,
fun(Pair@1) ->
not gleam@dict:has_key(
Inms_dct,
erlang:element(1, Pair@1)
)
end
),
gleam@list:map(
_pipe@4,
fun(Pair@2) -> case erlang:element(1, Pair@2) of
<<"xmlns"/utf8>> ->
{<<"$"/utf8>>, erlang:element(2, Pair@2)};
_ ->
Pair@2
end end
)
end,
Child = reduce_xml_input(
erlang:setelement(
7,
erlang:setelement(
6,
erlang:setelement(
3,
erlang:setelement(2, new(<<"dummy"/utf8>>), Name@1),
Attr@1
),
Nms@1
),
Inms
),
Input@1
),
case Child of
{ok, {Child@1, Input@2}} ->
reduce_xml_input(
erlang:setelement(
5,
Elem,
lists:append(erlang:element(5, Elem), [Child@1])
),
Input@2
);
{error, Err} ->
{error, Err}
end;
{ok, {{data, Data}, Input@3}} ->
reduce_xml_input(
erlang:setelement(
4,
Elem,
lists:append(erlang:element(4, Elem), [Data])
),
Input@3
);
{ok, {element_end, Input@4}} ->
{ok, {Elem, Input@4}};
{error, Err@1} ->
case xmlm:eoi(Input) of
{ok, {true, Input@5}} ->
{ok, {Elem, Input@5}};
_ ->
{error, Err@1}
end;
_ ->
{ok, {Elem, Input}}
end.
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 79).
-spec from_xml(binary()) -> {ok, element()} | {error, decode_error()}.
from_xml(Xml_string) ->
Input = begin
_pipe = Xml_string,
_pipe@1 = xmlm:from_string(_pipe),
xmlm:with_stripping(_pipe@1, true)
end,
case xmlm:signal(Input) of
{ok, {{dtd, _}, Input@1}} ->
_pipe@2 = reduce_xml_input(new(<<"dummy"/utf8>>), Input@1),
_pipe@3 = gleam@result:map_error(
_pipe@2,
fun(_) -> {decode_error, <<"XML parse error"/utf8>>} end
),
_pipe@4 = gleam@result:'try'(
_pipe@3,
fun(Pair) -> case xmlm:eoi(erlang:element(2, Pair)) of
{ok, {true, _}} ->
{ok, erlang:element(1, Pair)};
_ ->
{error,
{decode_error,
<<"Did not fully parse the xml stream"/utf8>>}}
end end
),
gleam@result:'try'(
_pipe@4,
fun(Elem) -> case erlang:element(5, Elem) of
[Child] ->
{ok, Child};
_ ->
{error,
{decode_error,
<<"Expected one element in the root node"/utf8>>}}
end end
);
_ ->
{error, {decode_error, <<"XML parse error"/utf8>>}}
end.
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 189).
-spec children_from_json(
list({binary(), list(gleam@dynamic:dynamic_())}),
list({binary(), binary()})
) -> {ok, list(element())} | {error, list(gleam@dynamic:decode_error())}.
children_from_json(Xs, Inherited) ->
_pipe = Xs,
_pipe@1 = gleam@list:filter(
_pipe,
fun(Pair) -> case erlang:element(1, Pair) of
<<"@"/utf8, _/binary>> ->
false;
<<"$"/utf8>> ->
false;
_ ->
true
end end
),
_pipe@5 = gleam@list:flat_map(
_pipe@1,
fun(Pair@1) -> _pipe@2 = erlang:element(2, Pair@1),
_pipe@4 = gleam@list:map(_pipe@2, fun(X) -> _pipe@3 = X,
(gleam@dynamic:dict(
fun gleam@dynamic:string/1,
fun(A) -> {ok, dynamic_to_list(A)} end
))(_pipe@3) end),
gleam@list:map(
_pipe@4,
fun(X@1) ->
gleam@result:'try'(
X@1,
fun(X@2) ->
element_from_json(
erlang:element(1, Pair@1),
X@2,
Inherited
)
end
)
end
) end
),
gleam@result:all(_pipe@5).
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 128).
-spec element_from_json(
binary(),
gleam@dict:dict(binary(), list(gleam@dynamic:dynamic_())),
list({binary(), binary()})
) -> {ok, element()} | {error, list(gleam@dynamic:decode_error())}.
element_from_json(Name, Obj, Inherited) ->
Elem = begin
_pipe = new(Name),
inherited_namespaces(_pipe, Inherited)
end,
_pipe@1 = text_from_json(Obj),
_pipe@2 = gleam@result:map(
_pipe@1,
fun(Txt) -> erlang:setelement(4, Elem, Txt) end
),
_pipe@5 = gleam@result:'try'(
_pipe@2,
fun(Elem@1) ->
_pipe@4 = attributes_from_json(
begin
_pipe@3 = Obj,
maps:to_list(_pipe@3)
end
),
gleam@result:map(
_pipe@4,
fun(Attr) -> erlang:setelement(3, Elem@1, Attr) end
)
end
),
_pipe@9 = gleam@result:'try'(
_pipe@5,
fun(Elem@2) ->
_pipe@7 = namespaces_from_json(
begin
_pipe@6 = Obj,
maps:to_list(_pipe@6)
end
),
gleam@result:map(
_pipe@7,
fun(Nms) ->
Dct = maps:from_list(Inherited),
Nms@1 = begin
_pipe@8 = Nms,
gleam@list:filter(
_pipe@8,
fun(Nm) ->
not gleam@dict:has_key(
Dct,
erlang:element(1, Nm)
)
end
)
end,
erlang:setelement(6, Elem@2, Nms@1)
end
)
end
),
gleam@result:'try'(
_pipe@9,
fun(Elem@3) ->
_pipe@11 = children_from_json(
begin
_pipe@10 = Obj,
maps:to_list(_pipe@10)
end,
lists:append(
erlang:element(6, Elem@3),
erlang:element(7, Elem@3)
)
),
gleam@result:map(
_pipe@11,
fun(Attr@1) -> erlang:setelement(5, Elem@3, Attr@1) end
)
end
).
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 45).
-spec from_json(binary()) -> {ok, element()} |
{error, gleam@json:decode_error()}.
from_json(Json_string) ->
_pipe = gleam@json:decode(Json_string, fun unpack_json/1),
gleam@result:'try'(
_pipe,
fun(Pair) ->
_pipe@1 = element_from_json(
erlang:element(1, Pair),
erlang:element(2, Pair),
[]
),
gleam@result:map_error(
_pipe@1,
fun(Err) -> {unexpected_format, Err} end
)
end
).
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 290).
-spec children_to_json(list(element())) -> list({binary(), gleam@json:json()}).
children_to_json(Children) ->
Xs = begin
_pipe = Children,
gleam@list:map(
_pipe,
fun(Elem) -> {erlang:element(2, Elem), collect_key_values(Elem)} end
)
end,
Dct = begin
_pipe@1 = Xs,
ordered_group(_pipe@1, fun(Pair) -> erlang:element(1, Pair) end)
end,
_pipe@2 = Dct,
_pipe@3 = maps:to_list(_pipe@2),
gleam@list:flat_map(
_pipe@3,
fun(Pair@1) ->
{Key, Value} = Pair@1,
_pipe@4 = Value,
_pipe@5 = gleam@list:map(
_pipe@4,
fun(Pair@2) -> gleam@json:object(erlang:element(2, Pair@2)) end
),
to_optional_list(Key, _pipe@5)
end
).
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 239).
-spec collect_key_values(element()) -> list({binary(), gleam@json:json()}).
collect_key_values(Elem) ->
Text_json = begin
_pipe = erlang:element(4, Elem),
gleam@list:map(_pipe, fun(Txt) -> gleam@json:string(Txt) end)
end,
Text_obj = to_optional_list(<<"$"/utf8>>, Text_json),
Attr_obj = attributes_to_json(erlang:element(3, Elem)),
Children_obj = children_to_json(erlang:element(5, Elem)),
Nms = begin
_pipe@1 = lists:append(erlang:element(6, Elem), erlang:element(7, Elem)),
gleam@list:map(
_pipe@1,
fun(Pair) ->
{Key, Value} = Pair,
{Key, gleam@json:string(Value)}
end
)
end,
Obj = begin
_pipe@2 = Text_obj,
_pipe@3 = lists:append(_pipe@2, Attr_obj),
lists:append(_pipe@3, Children_obj)
end,
case Nms of
[] ->
Obj;
_ ->
_pipe@4 = Obj,
lists:append(_pipe@4, [{<<"@xmlns"/utf8>>, gleam@json:object(Nms)}])
end.
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/element.gleam", 39).
-spec to_json(element()) -> gleam@json:json().
to_json(Elem) ->
Key_values = collect_key_values(Elem),
_pipe = [{erlang:element(2, Elem), gleam@json:object(Key_values)}],
gleam@json:object(_pipe).