Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable eqwalizer #70

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{deps, []}.
{deps, [
{eqwalizer_support,
{git_subdir,
"https://github.com/whatsapp/eqwalizer.git",
{branch, "main"},
"eqwalizer_support"}}
]}.

{dialyzer,
[{warnings,
Expand All @@ -25,7 +31,15 @@
]}
]}.

{project_plugins, [rebar3_ex_doc]}.
{project_plugins,
[
rebar3_ex_doc,
{eqwalizer_rebar3,
{git_subdir,
"https://github.com/whatsapp/eqwalizer.git",
{branch, "main"},
"eqwalizer_rebar3"}}
]}.
{plugins, [pc, rebar3_hex]}.

% Interrupt compilation, if the artifact is not found
Expand Down
3 changes: 2 additions & 1 deletion src/exml.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
{registered, []},
{applications,
[kernel,
stdlib
stdlib,
eqwalizer_support
]},
{env, []},
{modules, []},
Expand Down
14 changes: 11 additions & 3 deletions src/exml.erl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ xml_size(#xmlstreamstart{ name = Name, attrs = Attrs }) ->
byte_size(Name) + 2 + xml_size(Attrs);
xml_size(#xmlstreamend{ name = Name }) ->
byte_size(Name) + 3;
xml_size({Key, Value}) ->
xml_size({Key, Value}) when is_binary(Key) ->
% Attributes
byte_size(Key)
+ 4 % ="" and whitespace before
+ byte_size(Value).
Expand All @@ -66,7 +67,11 @@ xml_size({Key, Value}) ->
%% @end
%% The implementation of this function is a subtle modification of
%% https://github.com/erszcz/rxml/commit/e8483408663f0bc2af7896e786c1cdea2e86e43d
-spec xml_sort(item() | [item()]) -> item() | [item()].
-spec xml_sort([item()]) -> [item()];
(element()) -> element();
(cdata()) -> cdata();
(exml_stream:start()) -> exml_stream:start();
(exml_stream:stop()) -> exml_stream:stop().
xml_sort(#xmlcdata{} = Cdata) ->
Cdata;
xml_sort(#xmlel{ attrs = Attrs, children = Children } = El) ->
Expand Down Expand Up @@ -130,7 +135,10 @@ to_iolist([#xmlstreamstart{name = Name, attrs = Attrs} | Tail] = Elements, Prett
case Last of
#xmlstreamend{name = Name} ->
%% Add extra nesting for streams so pretty-printing would be indented properly
Element = #xmlel{name = Name, attrs = Attrs, children = lists:reverse(RevChildren)},
Children = lists:reverse(RevChildren),
%% The cast is safe, since we eliminated #xmlstreamstart{} and #xmlstreamend{}.
%% The rest are element()s, which are valid children.
Element = #xmlel{name = Name, attrs = Attrs, children = eqwalizer:dynamic_cast(Children)},
to_binary_nif(Element, Pretty);
_ ->
[to_iolist(El, Pretty) || El <- Elements]
Expand Down
12 changes: 9 additions & 3 deletions src/exml_nif.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@
load() ->
PrivDir = case code:priv_dir(?MODULE) of
{error, _} ->
EbinDir = filename:dirname(code:which(?MODULE)),
AppPath = filename:dirname(EbinDir),
filename:join(AppPath, "priv");
case code:which(?MODULE) of
Path when is_list(Path) ->
EbinDir = filename:dirname(Path),
AppPath = filename:dirname(EbinDir),
filename:join(AppPath, "priv");
_ ->
%% cover_compiled | preloaded | non_existing
erlang:error({cannot_get_load_path, ?MODULE})
end;
Path ->
Path
end,
Expand Down
20 changes: 11 additions & 9 deletions src/exml_query.erl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ path(Element, Path) ->
%% '''
%% will return `<<"Message from bob to alice">>'
%% @end
-spec path(exml:element(), path(), Default) -> exml:element() | binary() | Default.
-spec path(exml:element() | undefined, path(), Default) -> exml:element() | binary() | Default.
path(#xmlel{} = Element, [], _) ->
Element;
path(#xmlel{} = Element, [{element, Name} | Rest], Default) ->
Expand Down Expand Up @@ -129,10 +129,10 @@ subelement(Element, Name) ->
-spec subelement(exml:element(), binary(), Default) -> exml:element() | Default.
subelement(#xmlel{children = Children}, Name, Default) ->
case lists:keyfind(Name, #xmlel.name, Children) of
#xmlel{} = Result ->
Result;
false ->
Default;
Result ->
Result
Default
end.

%% @equiv path(Element, [{element_with_ns, NS}])
Expand Down Expand Up @@ -161,7 +161,9 @@ child_with_ns([_ | Rest], NS, Default) ->
-spec subelement_with_attr(exml:element(), AttrName :: binary(), AttrValue :: binary()) ->
exml:element() | undefined.
subelement_with_attr(Element, AttrName, AttrValue) ->
subelement_with_attr(Element, AttrName, AttrValue, undefined).
%% eqwalizer can't tell that Default in subelement_with_attr/4
%% in this case WILL be just 'undefined', which is a valid return type from this function.
eqwalizer:dynamic_cast(subelement_with_attr(Element, AttrName, AttrValue, undefined)).

%% @equiv path(Element, [{element_with_attr, AttrName, AttrValue}], Default)
-spec subelement_with_attr(Element, AttrName, AttrValue, Default) -> SubElement | Default when
Expand Down Expand Up @@ -209,7 +211,7 @@ subelements(#xmlel{children = Children}, Name) ->
true;
(_) ->
false
end, Children).
end, eqwalizer:dynamic_cast(Children)).

%% @equiv paths(Element, [{element_with_ns, NS}])
-spec subelements_with_ns(exml:element(), binary()) -> [exml:element()].
Expand All @@ -218,7 +220,7 @@ subelements_with_ns(#xmlel{children = Children}, NS) ->
NS =:= attr(Child, <<"xmlns">>);
(_) ->
false
end, Children).
end, eqwalizer:dynamic_cast(Children)).

%% @equiv paths(Element, [{element_with_ns, Name, NS}])
-spec subelements_with_name_and_ns(exml:element(), binary(), binary()) -> [exml:element()].
Expand All @@ -228,7 +230,7 @@ subelements_with_name_and_ns(#xmlel{children = Children}, Name, NS) ->
NS =:= attr(Child, <<"xmlns">>);
(_) ->
false
end, Children).
end, eqwalizer:dynamic_cast(Children)).

%% @equiv paths(Element, [{element_with_attr, AttrName, AttrValue}])
-spec subelements_with_attr(exml:element(), binary(), binary()) -> [exml:element()].
Expand All @@ -237,7 +239,7 @@ subelements_with_attr(#xmlel{children = Children}, AttrName, Value) ->
Value =:= attr(Child, AttrName);
(_) ->
false
end, Children).
end, eqwalizer:dynamic_cast(Children)).

%% @equiv path(Element, [cdata])
-spec cdata(exml:element()) -> binary().
Expand Down
2 changes: 1 addition & 1 deletion test/exml_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
-compile(export_all).

application_test() ->
?assertEqual(ok, application:start(exml)),
?assertMatch({ok, _}, application:ensure_all_started(exml)),
?assertEqual(ok, application:stop(exml)).

size_of_normal_xml_test() ->
Expand Down
Loading