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

Modernise #9

Merged
merged 3 commits into from
Jan 11, 2024
Merged
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
33 changes: 17 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,31 @@ on:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
test:
name: OTP ${{matrix.otp}}
name: OTP ${{matrix.otp_vsn}}
strategy:
matrix:
otp: ['24.2', '23.3']
runs-on: 'ubuntu-20.04'
otp_vsn: ['26.2', '25.3', '24.3']
rebar_vsn: ['3.22.0']
runs-on: 'ubuntu-22.04'
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/checkout@v4
- uses: erlef/setup-beam@v1
with:
otp-version: ${{ matrix.otp }}
rebar3-version: '3.18.0'
otp-version: ${{ matrix.otp_vsn }}
rebar3-version: ${{ matrix.rebar_vsn }}
- run: rebar3 as test compile
- run: rebar3 ct
- run: rebar3 ex_doc
if: ${{ matrix.otp == '24.2' }}
if: ${{ matrix.otp == '26.2' }}
- run: rebar3 dialyzer
if: ${{ matrix.otp == '24.2' }}
- run: pip install --user codecov
if: ${{ matrix.otp == '24.2' }}
if: ${{ matrix.otp == '26.2' }}
- run: rebar3 as test codecov analyze
if: ${{ matrix.otp == '24.2' }}
if: ${{ matrix.otp == '26.2' }}
- run: gcov -o c_src mongoose_jid
if: ${{ matrix.otp == '24.2' }}
- run: /home/runner/.local/bin/codecov
if: ${{ matrix.otp == '24.2' }}
if: ${{ matrix.otp == '26.2' }}
- run: rebar3 as test codecov analyze
- uses: codecov/codecov-action@v3
with:
name: Upload coverage reports to Codecov
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
10 changes: 4 additions & 6 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

{dialyzer,
[{warnings,
[race_conditions,
unknown,
[unknown,
unmatched_returns,
error_handling,
underspecs
]}]}.

{deps, [
{stringprep, "1.0.27"}
{stringprep, "1.0.29"}
]}.

{project_plugins, [
Expand Down Expand Up @@ -44,7 +43,7 @@
{cover_enabled, true},
{cover_export_enabled, true},
{deps, [{proper, "1.4.0"}]},
{plugins, [{rebar3_codecov, "0.3.0"}]},
{plugins, [{rebar3_codecov, "0.6.0"}]},
{port_env,
[{"CFLAGS", "$CFLAGS -std=c99 -O2 -g -Wall -Wextra -fPIC --coverage"},
{"LDFLAGS", "$LDFLAGS --coverage"}
Expand All @@ -53,8 +52,7 @@
{prod, [
{erl_opts, [deterministic]},
{port_env,
[{"CFLAGS", "$CFLAGS -std=c99 -O3 -Wall -Wextra -fPIC"}
]}
[{"CFLAGS", "$CFLAGS -std=c99 -O3 -Wall -Wextra -fPIC"}]}
]}
]}.

Expand Down
48 changes: 29 additions & 19 deletions src/jid.erl
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
%%==============================================================================
%% Copyright 2022 Erlang Solutions Ltd.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%==============================================================================
-module(jid).
-on_load(load/0).

Expand All @@ -21,6 +6,7 @@
-export([binary_to_bare/1, to_bare_binary/1, to_binary/1]).
-export([are_equal/2, are_bare_equal/2, is_nodename/1]).
-export([nodeprep/1, nameprep/1, resourceprep/1]).
-export([luser/1, lserver/1, lresource/1]).
-export([to_lower/1, to_lus/1, to_bare/1]).
-export([replace_resource/2, replace_resource_noprep/2]).
-export([str_tolower/1]).
Expand Down Expand Up @@ -167,7 +153,7 @@ to_binary(#jid{luser = LUser, lserver = LServer, lresource = LResource}) ->
to_binary(Jid) when is_binary(Jid) ->
Jid.

-spec to_bare_binary(simple_jid() | simple_bare_jid() | jid() | literal_jid()) -> binary() | error.
-spec to_bare_binary(ljid() | simple_bare_jid() | jid() | literal_jid()) -> binary() | error.
to_bare_binary({<<>>, Server}) ->
<<Server/binary>>;
to_bare_binary({User, Server}) ->
Expand Down Expand Up @@ -202,6 +188,30 @@ nodeprep(S) when is_binary(S), byte_size(S) < ?SANE_LIMIT ->
nodeprep(_) ->
error.

-spec luser(jid() | ljid() | simple_bare_jid()) -> luser().
luser(#jid{luser = LUser}) ->
LUser;
luser({LUser, _, _}) ->
LUser;
luser({LUser, _}) ->
LUser.

-spec lserver(jid() | ljid() | simple_bare_jid()) -> lserver().
lserver(#jid{lserver = LServer}) ->
LServer;
lserver({_, LServer, _}) ->
LServer;
lserver({_, LServer}) ->
LServer.

-spec lresource(jid() | ljid() | simple_bare_jid()) -> lresource().
lresource(#jid{lresource = LResource}) ->
LResource;
lresource({_, _, LResource}) ->
LResource;
lresource({_, _}) ->
<<>>.

%% @doc Prepares the server part of a jid
-spec nameprep(server()) -> lserver() | error.
nameprep(S) when is_binary(S), byte_size(S) < ?SANE_LIMIT ->
Expand All @@ -219,7 +229,7 @@ resourceprep(_) ->
error.

%% @doc Returns a jid that contains only prepared strings
-spec to_lower(simple_jid() | jid()) -> error | simple_jid().
-spec to_lower(simple_jid() | jid()) -> error | ljid().
to_lower(#jid{luser = U, lserver = S, lresource = R}) ->
{U, S, R};
to_lower({U, S, R}) ->
Expand All @@ -241,8 +251,8 @@ to_lus(error) ->
error.

%% @doc Takes a jid and returns the same jid without its resourcepart
-spec to_bare(simple_jid()) -> simple_jid();
(jid()) -> jid();
-spec to_bare(jid()) -> jid();
(ljid()) -> ljid();
(error) -> error.
to_bare(#jid{} = JID) ->
JID#jid{lresource = <<>>};
Expand Down
17 changes: 16 additions & 1 deletion test/jid_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ groups() ->
to_bare_binary_equals_to_bare_then_to_binary,
to_lower_to_bare_equals_to_bare_to_lower,
make_to_lus_equals_to_lower_to_lus,
make_bare_like_make_with_empty_resource
make_bare_like_make_with_empty_resource,
getters_equal_struct_lookup
]},
{old_comparison, [parallel], [
with_nif_from_binary,
Expand Down Expand Up @@ -263,6 +264,20 @@ make_bare_like_make_with_empty_resource(_) ->
jid:make(U, S, <<>>))),
run_property(Prop, 200, 1, 100).

getters_equal_struct_lookup(_) ->
Jid = jid:from_binary(<<"alice@localhost/res1">>),
?assertEqual(jid:luser(Jid), Jid#jid.luser),
?assertEqual(jid:lserver(Jid), Jid#jid.lserver),
?assertEqual(jid:lresource(Jid), Jid#jid.lresource),
LJid = jid:to_lower(Jid),
?assertEqual(jid:luser(LJid), Jid#jid.luser),
?assertEqual(jid:lserver(LJid), Jid#jid.lserver),
?assertEqual(jid:lresource(LJid), Jid#jid.lresource),
US = jid:to_lus(Jid),
?assertEqual(jid:luser(US), Jid#jid.luser),
?assertEqual(jid:lserver(US), Jid#jid.lserver),
?assertEqual(jid:lresource(US), <<>>).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Original code kept for documentation purposes
Expand Down