Skip to content

Commit

Permalink
Room config unify from and to binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Nov 22, 2023
1 parent f2533fc commit 29d8186
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/muc_light/mod_muc_light_db_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ create_room_transaction(HostType, {RoomU, RoomS}, Config, AffUsers, Version) ->
insert_room(HostType, RoomU, RoomS, Version),
RoomID = mongoose_rdbms:selected_to_integer(select_room_id(HostType, RoomU, RoomS)),
Schema = mod_muc_light:config_schema(RoomS),
ConfigFields = mod_muc_light_room_config:to_binary_kv(Config, Schema),
{ok, ConfigFields} = mod_muc_light_room_config:to_binary_kv(Config, Schema),
[insert_aff_tuple(HostType, RoomID, AffUser) || AffUser <- AffUsers],
[insert_config_kv(HostType, RoomID, KV) || KV <- ConfigFields],
ok.
Expand Down
4 changes: 2 additions & 2 deletions src/muc_light/mod_muc_light_room.erl
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ process_request({get, #config{} = ConfigReq},
{_, RoomS} = RoomUS,
HostType = mongoose_acc:host_type(Acc),
{ok, Config, RoomVersion} = mod_muc_light_db_backend:get_config(HostType, RoomUS),
RawConfig = mod_muc_light_room_config:to_binary_kv(Config, mod_muc_light:config_schema(RoomS)),
{ok, RawConfig} = mod_muc_light_room_config:to_binary_kv(Config, mod_muc_light:config_schema(RoomS)),
{get, ConfigReq#config{ version = RoomVersion,
raw_config = RawConfig }};
process_request({get, #affiliations{} = AffReq},
Expand All @@ -121,7 +121,7 @@ process_request({get, #info{} = InfoReq},
_From, {_, RoomS} = RoomUS, _Auth, _AffUsers, Acc) ->
HostType = mongoose_acc:host_type(Acc),
{ok, Config, AffUsers, RoomVersion} = mod_muc_light_db_backend:get_info(HostType, RoomUS),
RawConfig = mod_muc_light_room_config:to_binary_kv(Config, mod_muc_light:config_schema(RoomS)),
{ok, RawConfig} = mod_muc_light_room_config:to_binary_kv(Config, mod_muc_light:config_schema(RoomS)),
{get, InfoReq#info{ version = RoomVersion, aff_users = AffUsers,
raw_config = RawConfig }};
process_request({set, #config{} = ConfigReq},
Expand Down
84 changes: 30 additions & 54 deletions src/muc_light/mod_muc_light_room_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
-type binary_kv() :: [{Key :: binary(), Value :: binary()}].

%% User definition processing
-type schema_item() :: {FieldName :: binary(), DefaultValue :: value(),
key(), value_type()}.
-type schema_item() :: {FieldName :: binary(), DefaultValue :: value(), key(), value_type()}.
-type schema() :: [schema_item()]. % has to be sorted

%%====================================================================
Expand All @@ -53,36 +52,40 @@
-spec from_binary_kv_diff(RawConfig :: binary_kv(), ConfigSchema :: schema()) ->
{ok, kv()} | validation_error().
from_binary_kv_diff(RawConfig, ConfigSchema) ->
from_binary_kv_diff(lists:ukeysort(1, RawConfig), ConfigSchema, []).

from_binary_kv_diff([], [], Config) ->
{ok, Config};
from_binary_kv_diff(RawConfig, ConfigSchema, Config) ->
case take_next_kv(RawConfig, ConfigSchema) of
{error, Reason} ->
{error, Reason};
{value, RRawConfig, RConfigSchema, KV} ->
from_binary_kv_diff(RRawConfig, RConfigSchema, [KV | Config]);
{default, RRawConfig, RConfigSchema, _} ->
% do not populate the diff with default values
from_binary_kv_diff(RRawConfig, RConfigSchema, Config)
end.
take_next(lists:ukeysort(1, RawConfig), ConfigSchema, true, fun take_next_kv/2, []).

-spec from_binary_kv(RawConfig :: binary_kv(), ConfigSchema :: schema()) ->
{ok, kv()} | validation_error().
from_binary_kv(RawConfig, ConfigSchema) ->
from_binary_kv(lists:ukeysort(1, RawConfig), ConfigSchema, []).
take_next(lists:ukeysort(1, RawConfig), ConfigSchema, false, fun take_next_kv/2, []).

-spec to_binary_kv_diff(RawConfig :: kv(), ConfigSchema :: schema()) ->
{ok, binary_kv()} | validation_error().
to_binary_kv_diff(RawConfig, ConfigSchema) ->
take_next(lists:ukeysort(1, RawConfig), ConfigSchema, true, fun take_next_binary_kv/2, []).

from_binary_kv([], [], Config) ->
-spec to_binary_kv(Config :: kv(), ConfigSchema :: schema()) ->
{ok, binary_kv()} | validation_error().
to_binary_kv(RawConfig, ConfigSchema) ->
take_next(lists:ukeysort(1, RawConfig), ConfigSchema, false, fun take_next_binary_kv/2, []).

take_next([], [], _, _, Config) ->
{ok, Config};
from_binary_kv(RawConfig, ConfigSchema, Config) ->
case take_next_kv(RawConfig, ConfigSchema) of
{error, Reason} ->
{error, Reason};
{_, RRawConfig, RConfigSchema, KV} ->
from_binary_kv(RRawConfig, RConfigSchema, [KV | Config])
take_next(RawConfig, ConfigSchema, DropDefaults, TakeNext, Config) ->
case {DropDefaults, TakeNext(RawConfig, ConfigSchema)} of
{true, {default, RRawConfig, RConfigSchema, _}} ->
% do not populate the diff with default values
take_next(RRawConfig, RConfigSchema, DropDefaults, TakeNext, Config);
{_, {_, RRawConfig, RConfigSchema, KV}} ->
take_next(RRawConfig, RConfigSchema, DropDefaults, TakeNext, [KV | Config]);
{_, {error, Reason}} ->
{error, Reason}
end.

%%====================================================================
%% Internal functions
%%====================================================================

take_next_kv([{KeyBin, ValBin} | RRawConfig], [{KeyBin, _Default, Key, Type} | RSchema]) ->
try {value, RRawConfig, RSchema, {Key, b2value(ValBin, Type)}}
catch _:_ -> {error, {KeyBin, type_error}}
Expand All @@ -92,42 +95,15 @@ take_next_kv(RawConfig, [{_KeyBin, Default, Key, _Type} | RSchema]) ->
take_next_kv([{KeyBin, _} | _], _) ->
{error, {KeyBin, not_found}}.

-spec to_binary_kv_diff(RawConfig :: kv(), ConfigSchema :: schema()) ->
{ok, binary_kv()} | validation_error().
to_binary_kv_diff(RawConfig, ConfigSchema) ->
to_binary_kv_diff(lists:ukeysort(1, RawConfig), ConfigSchema, []).

to_binary_kv_diff([], [], Config) ->
{ok, Config};
to_binary_kv_diff(RawConfig, ConfigSchema, Config) ->
case take_next_binary_kv(RawConfig, ConfigSchema) of
{error, Reason} ->
{error, Reason};
{value, RRawConfig, RConfigSchema, KV} ->
to_binary_kv_diff(RRawConfig, RConfigSchema, [KV | Config]);
{default, RRawConfig, RConfigSchema, _} ->
% do not populate the diff with default values
to_binary_kv_diff(RRawConfig, RConfigSchema, Config)
end.

take_next_binary_kv([{Key, ValBin} | RRawConfig], [{KeyBin, _Default, Key, Type} | RSchema]) ->
try {value, RRawConfig, RSchema, {Key, value2b(ValBin, Type)}}
try {value, RRawConfig, RSchema, {KeyBin, value2b(ValBin, Type)}}
catch _:_ -> {error, {KeyBin, type_error}}

Check warning on line 100 in src/muc_light/mod_muc_light_room_config.erl

View check run for this annotation

Codecov / codecov/patch

src/muc_light/mod_muc_light_room_config.erl#L100

Added line #L100 was not covered by tests
end;
take_next_binary_kv(RawConfig, [{_KeyBin, Default, Key, _Type} | RSchema]) ->
{default, RawConfig, RSchema, {Key, Default}};
take_next_binary_kv(RawConfig, [{KeyBin, Default, _Key, _Type} | RSchema]) ->
{default, RawConfig, RSchema, {KeyBin, Default}};
take_next_binary_kv([{KeyBin, _} | _], _) ->
{error, {KeyBin, not_found}}.

Check warning on line 105 in src/muc_light/mod_muc_light_room_config.erl

View check run for this annotation

Codecov / codecov/patch

src/muc_light/mod_muc_light_room_config.erl#L105

Added line #L105 was not covered by tests

-spec to_binary_kv(Config :: kv(), ConfigSchema :: schema()) -> binary_kv().
to_binary_kv(Config, ConfigSchema) ->
ConfigWithSchema = lists:zip(lists:sort(Config), lists:keysort(3, ConfigSchema)),
[{KeyBin, value2b(Val, Type)} || {{Key, Val}, {KeyBin, _Default, Key, Type}} <- ConfigWithSchema].

%%====================================================================
%% Internal functions
%%====================================================================

-spec b2value(ValBin :: binary(), Type :: value_type()) -> Converted :: value().
b2value(ValBin, binary) when is_binary(ValBin) -> ValBin;
b2value(ValBin, integer) -> binary_to_integer(ValBin);
Expand Down

0 comments on commit 29d8186

Please sign in to comment.