Skip to content

Commit

Permalink
Merge pull request #69 from esl/modernise
Browse files Browse the repository at this point in the history
Modernise
  • Loading branch information
arcusfelis authored Jan 12, 2024
2 parents 74263a9 + 3df87ca commit deafb32
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 138 deletions.
43 changes: 24 additions & 19 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,33 @@ on:

jobs:
test:
name: OTP ${{matrix.otp}}
name: OTP ${{matrix.otp_vsn}}
strategy:
matrix:
otp: ['25.0', '24.3', '23.3', '22.3', '21.3']
runs-on: 'ubuntu-20.04'
otp_vsn: ['26.2', '25.3', '24.3']
rebar_vsn: ['3.22.0']
runs-on: 'ubuntu-22.04'
env:
OTPVER: ${{ matrix.otp }}
steps:
- uses: actions/checkout@v2
- uses: ErlGang/setup-erlang@v1.0.0
- uses: actions/checkout@v4
- uses: erlef/setup-beam@v1
with:
otp-version: ${{ matrix.otp }}
- run: make rebar3
- run: make deps
- run: make test
- run: make dialyzer
if: ${{ matrix.otp == '25.0' }}
- run: make codecov
if: ${{ matrix.otp == '25.0' }}
- run: make gcov
if: ${{ matrix.otp == '25.0' }}
- run: pip install --user codecov
if: ${{ matrix.otp == '25.0' }}
- run: /home/runner/.local/bin/codecov
if: ${{ matrix.otp == '25.0' }}
otp-version: ${{ matrix.otp_vsn }}
rebar3-version: ${{ matrix.rebar_vsn }}
- uses: actions/cache@v3
name: Cache
with:
path: _build
key: ${{ runner.os }}-build-${{ matrix.otp_vsn }}-${{ hashFiles(format('rebar.lock')) }}-1
restore-keys: ${{ runner.os }}-build-${{ matrix.otp_vsn }}-1-
- run: rebar3 as test compile -d
- run: rebar3 eunit
- run: rebar3 dialyzer
- run: rebar3 as test codecov analyze
- run: gcov -o c_src exml
- uses: codecov/codecov-action@v3
with:
name: Upload coverage reports to Codecov
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ _build/
*.beam
*.lock
/rebar3
doc/
45 changes: 0 additions & 45 deletions Makefile

This file was deleted.

2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{proper, "1.4.0"}
]},
{plugins, [
{rebar3_codecov, "0.3.0"}
{rebar3_codecov, "0.6.0"}
]},
{port_env, [
{"CXXFLAGS", "$CXXFLAGS -O3 -std=c++11 -g -Wall -Wextra -fPIC --coverage"},
Expand Down
2 changes: 1 addition & 1 deletion src/exml.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{modules, []},
{maintainers, ["ESL"]},
{pkg_name, "hexml"},
{licenses, ["Apache 2.0", "Boost Software License 1.0 (C++ code)", "GPL (tests)"]},
{licenses, ["Apache-2.0", "BSL-1.0", "GPL (tests)"]},
{links, [{"GitHub", "https://github.com/esl/exml/"}]},
{exclude_files, ["c_src/exml.d"]}
]}.
52 changes: 28 additions & 24 deletions src/exml.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%%%-------------------------------------------------------------------
%%% @copyright (C) 2011-2021, Erlang Solutions Ltd.
%%% @copyright (C) 2011-2024, Erlang Solutions Ltd.
%%% @doc
%%% @end
%%% Created : 12 Jul 2011 by Michal Ptaszek <[email protected]>
Expand Down Expand Up @@ -31,6 +31,7 @@
-type item() :: element() | attr() | cdata() | exml_stream:start() | exml_stream:stop().
-type prettify() :: pretty | not_pretty.

%% @doc Calculate the length of the original XML payload
-spec xml_size(item() | [item()]) -> non_neg_integer().
xml_size([]) ->
0;
Expand All @@ -54,14 +55,15 @@ xml_size({Key, Value}) ->
+ 4 % ="" and whitespace before
+ byte_size(Value).

%% @doc Sort a (list of) `xmlel()'.
%% @doc Sort in ascending order a list of xml `t:item()'.
%%
%% Sorting is defined as calling `lists:sort/1' at:
%% * all the `xmlel's provided (if there is a list of them) AND
%% * all the `xmlel' elements' attributes recursively (the root and descendants) AND
%% * all the `xmlel' children recursively (the root and descendants).
%% The order is ascending.
%%
%% <ul>
%% <li>all the `xmlel's provided (if there is a list of them) AND</li>
%% <li>all the `xmlel' elements' attributes recursively (the root and descendants) AND</li>
%% <li>all the `xmlel' children recursively (the root and descendants).</li>
%% </ul>
%% @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()].
Expand Down Expand Up @@ -99,31 +101,19 @@ to_iolist(Element) ->
to_pretty_iolist(Element) ->
to_iolist(Element, pretty).

%% @doc Parses a binary or a list of binaries into an XML `t:element()'.
-spec parse(binary() | [binary()]) -> {ok, exml:element()} | {error, any()}.
parse(XML) ->
exml_nif:parse(XML).

%% @doc Turn a –list of– exml element into iodata for IO interactions.
%% @doc Turn a –list of– exml elements into iodata for IO interactions.
%%
%% The `Pretty' argument indicates if the generated XML should have new lines and indentation,
%% which is useful for the debugging eye, or should rather be a minified version,
%% which is better for IO.
-spec to_iolist(exml_stream:element() | [exml_stream:element()], prettify()) -> iodata().
%% which is better for IO performance.
-spec to_iolist(cdata() | exml_stream:element() | [exml_stream:element()], prettify()) -> iodata().
to_iolist(#xmlel{} = Element, Pretty) ->
to_binary_nif(Element, Pretty);
to_iolist([Element], Pretty) ->
to_iolist(Element, Pretty);
to_iolist([Head | _] = Elements, Pretty) ->
[Last | RevChildren] = lists:reverse(tl(Elements)),
case {Head, Last} of
{#xmlstreamstart{name = Name, attrs = Attrs},
#xmlstreamend{name = Name}} ->
Element = #xmlel{name = Name, attrs = Attrs,
children = lists:reverse(RevChildren)},
to_binary_nif(Element, Pretty);
_ ->
[to_iolist(El, Pretty) || El <- Elements]
end;
to_iolist(#xmlstreamstart{name = Name, attrs = Attrs}, _Pretty) ->
Result = to_binary_nif(#xmlel{name = Name, attrs = Attrs}, not_pretty),
FrontSize = byte_size(Result) - 2,
Expand All @@ -132,7 +122,21 @@ to_iolist(#xmlstreamstart{name = Name, attrs = Attrs}, _Pretty) ->
to_iolist(#xmlstreamend{name = Name}, _Pretty) ->
[<<"</">>, Name, <<">">>];
to_iolist(#xmlcdata{content = Content}, _Pretty) ->
exml_nif:escape_cdata(Content).
exml_nif:escape_cdata(Content);
to_iolist([Element], Pretty) ->
to_iolist(Element, Pretty);
to_iolist([#xmlstreamstart{name = Name, attrs = Attrs} | Tail] = Elements, Pretty) ->
[Last | RevChildren] = lists:reverse(Tail),
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)},
to_binary_nif(Element, Pretty);
_ ->
[to_iolist(El, Pretty) || El <- Elements]
end;
to_iolist(Elements, Pretty) when is_list(Elements) ->
[to_iolist(El, Pretty) || El <- Elements].

-spec to_binary_nif(element(), prettify()) -> binary().
to_binary_nif(#xmlel{} = Element, Pretty) ->
Expand Down
2 changes: 1 addition & 1 deletion src/exml_nif.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%%%-------------------------------------------------------------------
%%% @copyright (C) 2018-2022, Erlang Solutions Ltd.
%%% @copyright (C) 2018-2024, Erlang Solutions Ltd.
%%% @private
%%%-------------------------------------------------------------------

Expand Down
Loading

0 comments on commit deafb32

Please sign in to comment.