Skip to content

Commit

Permalink
Merge pull request #7 from esl/reduce_jid_struct
Browse files Browse the repository at this point in the history
Simplify the jid data structure
  • Loading branch information
chrzaszcz authored Apr 26, 2022
2 parents 3f23ca6 + 648f1c9 commit a70d876
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 60 deletions.
5 changes: 1 addition & 4 deletions include/jid.hrl
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-record(jid, {user = <<>> :: jid:user(),
server = <<>> :: jid:server(),
resource = <<>> :: jid:resource(),
luser = <<>> :: jid:luser(),
-record(jid, {luser = <<>> :: jid:luser(),
lserver = <<>> :: jid:lserver(),
lresource = <<>> :: jid:lresource()
}).
Expand Down
67 changes: 28 additions & 39 deletions src/jid.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,13 @@
-module(jid).
-on_load(load/0).

-export([make/3]).
-export([make/1]).
-export([make_bare/2]).
-export([make_noprep/3]).
-export([make_noprep/1]).
-export([are_equal/2]).
-export([are_bare_equal/2]).
-export([from_binary/1]).
-export([from_binary_noprep/1]).
-export([to_binary/1]).
-export([is_nodename/1]).
-export([nodeprep/1]).
-export([nameprep/1]).
-export([resourceprep/1]).
-export([to_lower/1]).
-export([to_lus/1]).
-export([to_bare/1]).
-export([replace_resource/2]).
-export([replace_resource_noprep/2]).
-export([binary_to_bare/1]).
-export([make/3, make/1, make_bare/2, make_noprep/3, make_noprep/1]).
-export([from_binary/1, from_binary_noprep/1]).
-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([to_lower/1, to_lus/1, to_bare/1]).
-export([replace_resource/2, replace_resource_noprep/2]).
-export([str_tolower/1]).

-include("jid.hrl").
Expand Down Expand Up @@ -75,10 +62,7 @@ make(User, Server, Res) ->
{_, error, _} -> error;
{_, _, error} -> error;
{LUser, LServer, LRes} ->
#jid{user = User,
server = Server,
resource = Res,
luser = LUser,
#jid{luser = LUser,
lserver = LServer,
lresource = LRes}
end.
Expand All @@ -95,10 +79,7 @@ make_bare(User, Server) ->
{error, _} -> error;
{_, error} -> error;
{LUser, LServer} ->
#jid{user = User,
server = Server,
resource = <<>>,
luser = LUser,
#jid{luser = LUser,
lserver = LServer,
lresource = <<>>}
end.
Expand All @@ -108,10 +89,7 @@ make_bare(User, Server) ->
Server :: lserver(),
Resource :: lresource()) -> jid().
make_noprep(LUser, LServer, LResource) ->
#jid{user = LUser,
server = LServer,
resource = LResource,
luser = LUser,
#jid{luser = LUser,
lserver = LServer,
lresource = LResource}.

Expand Down Expand Up @@ -157,8 +135,7 @@ from_binary(_) ->
from_binary_noprep(J) when is_binary(J), byte_size(J) < ?XMPP_JID_SIZE_LIMIT ->
case from_binary_nif(J) of
{U, S, R} ->
#jid{user = U, server = S, resource = R,
luser = U, lserver = S, lresource = R};
#jid{luser = U, lserver = S, lresource = R};
error -> error
end;
from_binary_noprep(_) ->
Expand All @@ -185,11 +162,23 @@ to_binary({<<>>, Server}) ->
<<Server/binary>>;
to_binary({Node, Server}) ->
<<Node/binary, "@", Server/binary>>;
to_binary(#jid{user = User, server = Server, resource = Resource}) ->
to_binary({User, Server, Resource});
to_binary(#jid{luser = LUser, lserver = LServer, lresource = LResource}) ->
to_binary({LUser, LServer, LResource});
to_binary(Jid) when is_binary(Jid) ->
Jid.

-spec to_bare_binary(simple_jid() | simple_bare_jid() | jid() | literal_jid()) -> binary() | error.
to_bare_binary({<<>>, Server}) ->
<<Server/binary>>;
to_bare_binary({User, Server}) ->
<<User/binary, "@", Server/binary>>;
to_bare_binary({User, Server, _}) ->
to_bare_binary({User, Server});
to_bare_binary(#jid{luser = LUser, lserver = LServer}) ->
to_bare_binary({LUser, LServer});
to_bare_binary(Jid) when is_binary(Jid) ->
binary_to_bare(Jid).

%% @doc Returns true if the input is a valid user part
-spec is_nodename(<<>> | binary()) -> boolean().
is_nodename(<<>>) ->
Expand Down Expand Up @@ -256,7 +245,7 @@ to_lus(error) ->
(jid()) -> jid();
(error) -> error.
to_bare(#jid{} = JID) ->
JID#jid{resource = <<>>, lresource = <<>>};
JID#jid{lresource = <<>>};
to_bare({U, S, _R}) ->
{U, S, <<>>};
to_bare(error) ->
Expand All @@ -268,13 +257,13 @@ replace_resource(#jid{} = JID, Resource) ->
case resourceprep(Resource) of
error -> error;
LResource ->
JID#jid{resource = Resource, lresource = LResource}
JID#jid{lresource = LResource}
end.

%% @doc Replaces the resource part of a jid with a new resource, but without normalisation
-spec replace_resource_noprep(jid(), resource()) -> jid().
replace_resource_noprep(#jid{} = JID, LResource) ->
JID#jid{resource = LResource, lresource = LResource}.
JID#jid{lresource = LResource}.

%% @equiv jid:to_bare(jid:from_binary(BinaryJid))
-spec binary_to_bare(binary()) -> jid() | error.
Expand Down
21 changes: 7 additions & 14 deletions test/jid_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ groups() ->
binary_noprep_to_jid_fails_with_empty_binary,
make_jid_fails_on_binaries_that_are_too_long,
make_is_independent_of_the_input_format,
make_noprep_and_make_have_equal_raw_jid,
make_noprep_is_independent_of_the_input_format,
jid_to_lower_fails_if_any_binary_is_invalid,
jid_replace_resource_failes_for_invalid_resource,
Expand All @@ -40,6 +39,7 @@ groups() ->
compare_bare_jids_doesnt_depend_on_the_order,
compare_bare_with_jids_structs_and_bare_jids,
binary_to_bare_equals_binary_and_then_bare,
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
Expand Down Expand Up @@ -98,18 +98,6 @@ make_is_independent_of_the_input_format(_) ->
jid:make(U,S,R) == jid:make({U,S,R})),
run_property(Prop, 100, 1, 500).

make_noprep_and_make_have_equal_raw_jid(_) ->
Prop = ?FORALL({U, S, R},
{jid_gen:username(), jid_gen:domain(), jid_gen:resource()},
begin
#jid{user = U, server = S, resource = R} = jid:make(U, S, R),
#jid{user = U, server = S, resource = R} = jid:make_noprep(U, S, R),
true
end
),
run_property(Prop, 10, 1, 500).


make_noprep_is_independent_of_the_input_format(_) ->
Prop = ?FORALL({U, S, R},
{jid_gen:username(), jid_gen:domain(), jid_gen:resource()},
Expand Down Expand Up @@ -246,6 +234,11 @@ binary_to_bare_equals_binary_and_then_bare(_) ->
equals(jid:to_bare(jid:from_binary(A)), jid:binary_to_bare(A))),
run_property(Prop, 200, 1, 100).

to_bare_binary_equals_to_bare_then_to_binary(_) ->
Prop = ?FORALL(A, jid_gen:jid_struct(),
equals(jid:to_binary(jid:to_bare(A)), jid:to_bare_binary(A))),
run_property(Prop, 200, 1, 100).

to_lower_to_bare_equals_to_bare_to_lower(_) ->
Prop = ?FORALL(JID, oneof([jid_gen:jid_struct(),
{jid_gen:maybe_valid_username(),
Expand Down Expand Up @@ -330,7 +323,7 @@ binary_to_jid3(<<>>, N, S, R) ->
to_binary(Jid) when is_binary(Jid) ->
% sometimes it is used to format error messages
Jid;
to_binary(#jid{user = User, server = Server, resource = Resource}) ->
to_binary(#jid{luser = User, lserver = Server, lresource = Resource}) ->
to_binary({User, Server, Resource});
to_binary({User, Server}) ->
to_binary({User, Server, <<>>});
Expand Down
5 changes: 2 additions & 3 deletions test/jid_gen.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@

jid_struct() ->
?LET({U, S, R}, {username(), domain(), resource()},
{jid, U, S, R, U, S, R}).
{jid, U, S, R}).

from_jid() ->
oneof([
{jid_gen:username(), jid_gen:domain(), jid_gen:resource()},
{<<>>, jid_gen:domain(), jid_gen:resource()},
{jid_gen:username(), jid_gen:domain()},
{jid, jid_gen:username(), jid_gen:domain(), jid_gen:resource(),
jid_gen:username(), jid_gen:domain(), jid_gen:resource()}
{jid, jid_gen:username(), jid_gen:domain(), jid_gen:resource()}
]).

jid() ->
Expand Down

0 comments on commit a70d876

Please sign in to comment.