-
Notifications
You must be signed in to change notification settings - Fork 426
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
Muclight config fix #4178
Merged
Merged
Muclight config fix #4178
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -23,7 +23,8 @@ | |||||
-module(mod_muc_light_room_config). | ||||||
|
||||||
%% API | ||||||
-export([from_binary_kv_diff/2, from_binary_kv/2, to_binary_kv/2]). | ||||||
-export([from_binary_kv_diff/2, from_binary_kv/2, | ||||||
to_binary_kv_diff/2, to_binary_kv/2]). | ||||||
|
||||||
-include("mod_muc_light.hrl"). | ||||||
|
||||||
|
@@ -40,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 | ||||||
|
||||||
%%==================================================================== | ||||||
|
@@ -52,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(RRawConfig, RConfigSchema, [KV | Config]); | ||||||
{default, _, _, _} -> | ||||||
% do not populate the diff with default values | ||||||
from_binary_kv(RawConfig, ConfigSchema, 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(). | ||||||
{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, []). | ||||||
|
||||||
from_binary_kv([], [], Config) -> | ||||||
-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, []). | ||||||
|
||||||
-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}} | ||||||
|
@@ -91,14 +95,14 @@ | |||||
take_next_kv([{KeyBin, _} | _], _) -> | ||||||
{error, {KeyBin, not_found}}. | ||||||
|
||||||
-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 | ||||||
%%==================================================================== | ||||||
take_next_binary_kv([{Key, ValBin} | RRawConfig], [{KeyBin, _Default, Key, Type} | RSchema]) -> | ||||||
try {value, RRawConfig, RSchema, {KeyBin, value2b(ValBin, Type)}} | ||||||
catch _:_ -> {error, {KeyBin, type_error}} | ||||||
end; | ||||||
take_next_binary_kv(RawConfig, [{KeyBin, Default, _Key, _Type} | RSchema]) -> | ||||||
{default, RawConfig, RSchema, {KeyBin, Default}}; | ||||||
take_next_binary_kv([{KeyBin, _} | _], _) -> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a minor naming inconsistency
Suggested change
|
||||||
{error, {KeyBin, not_found}}. | ||||||
|
||||||
-spec b2value(ValBin :: binary(), Type :: value_type()) -> Converted :: value(). | ||||||
b2value(ValBin, binary) when is_binary(ValBin) -> ValBin; | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to write "Why so complicated" and I after I see the old take_next_kv doing the same.
{error, _} is not covered by tests even :)
take_next logic is kinda hard to follow, in the perfect world it should be just simple maps:merge(NewConfig, maps:merge(OldConfig, Default))
or something.