Skip to content

Commit cb03d94

Browse files
author
Denys Gonchar
authored
Merge pull request #193 from esl/fix_description_encoding
Fix #190: Allow description to be binary in required_variable attributes
2 parents 193bb1a + 6c0e243 commit cb03d94

6 files changed

Lines changed: 37 additions & 48 deletions

File tree

elvis.config

Lines changed: 0 additions & 33 deletions
This file was deleted.

rebar.config

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
{project_plugins, [
4444
{rebar3_hex, "~> 7.0"},
4545
{rebar3_ex_doc, "~> 0.2"},
46-
{rebar3_lint, "~> 4.1"},
46+
{rebar3_lint, "~> 4.2"},
4747
{rebar3_codecov, "~> 0.7"}
4848
]}.
4949

@@ -76,7 +76,8 @@
7676
rules => [
7777
{elvis_text_style, line_length, #{skip_comments => whole_line}},
7878
{elvis_style, export_used_types, disable},
79-
{elvis_style, invalid_dynamic_call, #{ignore => [{amoc_code_server, get_md5}]}},
79+
{elvis_style, private_data_types, disable},
80+
{elvis_style, no_invalid_dynamic_calls, #{ignore => [{amoc_code_server, get_md5}]}},
8081
{elvis_style, no_block_expressions, #{ignore => [amoc_cluster]}},
8182
{elvis_style, no_throw, #{ignore => [amoc_config]}}
8283
]

src/config/amoc_config.hrl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
-record(module_parameter, {name :: name(),
3838
mod :: module(),
3939
value :: value(),
40-
description :: string(),
40+
description :: unicode:chardata(),
4141
verification_fn :: maybe_verification_fun(),
4242
update_fn = read_only :: maybe_update_fun() | read_only}).
4343

@@ -51,7 +51,7 @@
5151
-type update_method() :: read_only | none | mfa(2) | update_fun().
5252

5353
-type module_attribute() :: #{ name := name(),
54-
description := string(),
54+
description := unicode:chardata(),
5555
default_value => value(),
5656
verification => verification_method(),
5757
update => update_method()}.

src/config/amoc_config_attributes.erl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,11 @@ process_var_attr(Module, Attr) ->
6868
end.
6969

7070
-spec check_mandatory_fields(maybe_module_attribute()) ->
71-
{ok, #{name := name(), description := string(), any() => any()}} | {error, reason()}.
72-
check_mandatory_fields(#{description := List, name := Atom} = Attr) when is_atom(Atom),
73-
is_list(List) ->
74-
case io_lib:char_list(List) of
75-
true -> {ok, Attr};
76-
false -> {error, invalid_attribute}
71+
{ok, #{name := name(), description := unicode:chardata(), any() => any()}} | {error, reason()}.
72+
check_mandatory_fields(#{description := Description, name := Atom} = Attr) when is_atom(Atom) ->
73+
case unicode:characters_to_binary(Description) of
74+
Bin when is_binary(Bin) -> {ok, Attr#{description := Bin}};
75+
_ -> {error, invalid_attribute}
7776
end;
7877
check_mandatory_fields(_Attr) ->
7978
{error, invalid_attribute}.
@@ -111,7 +110,7 @@ check_update_method(Attr) ->
111110
end.
112111

113112
-spec make_module_parameter(#{name := name(),
114-
description := string(),
113+
description := unicode:chardata(),
115114
default_value := value(),
116115
verification := maybe_verification_fun(),
117116
update := maybe_update_fun(),

src/coordinator/amoc_coordinator.erl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,14 @@ normalize_coordination_item({{Min, Max}, Actions}) when ?IS_POS_INT(Min), ?IS_PO
170170
[assert_action({Min, Max}, A) || A <- Actions],
171171
{{Min, Max}, Actions}.
172172

173-
assert_action(all, Action) when is_function(Action, 1);
174-
is_function(Action, 2) ->
173+
assert_action(all, Action) when is_function(Action, 1) orelse is_function(Action, 2) ->
175174
ok;
176-
assert_action(N, Action) when is_integer(N),
175+
assert_action(N, Action) when is_integer(N) andalso
177176
(is_function(Action, 1) orelse
178177
is_function(Action, 2) orelse
179178
is_function(Action, 3)) ->
180179
ok;
181-
assert_action({Min, Max}, Action) when is_integer(Min), is_integer(Max),
180+
assert_action({Min, Max}, Action) when is_integer(Min) andalso is_integer(Max) andalso
182181
(is_function(Action, 1) orelse
183182
is_function(Action, 2) orelse
184183
is_function(Action, 3)) ->

test/amoc_config_attributes_SUITE.erl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
-export([all/0]).
88
-export([get_module_attributes/1,
99
get_module_configuration/1,
10+
description_binary_and_list/1,
1011
errors_reporting/1,
1112
one_of_function/1]).
1213

@@ -52,6 +53,7 @@ update_value(_, _) -> ok.
5253
all() ->
5354
[get_module_attributes,
5455
get_module_configuration,
56+
description_binary_and_list,
5557
errors_reporting,
5658
one_of_function].
5759

@@ -109,6 +111,27 @@ get_module_configuration(_) ->
109111
verification_fn = VerificationNone, update_fn = UpdateValueFN}],
110112
Config).
111113

114+
description_binary_and_list(_) ->
115+
%% Description as list (string) is accepted and stored as binary
116+
ListAttr = #{name => desc_list_var, description => "list description",
117+
default_value => def, verification => none, update => read_only},
118+
{ok, [#module_parameter{description = <<"list description">>}]} =
119+
amoc_config_attributes:process_module_attributes(?MODULE, [ListAttr]),
120+
%% Description as binary is accepted and stored as binary
121+
BinAttr = #{name => desc_bin_var, description => <<"binary description">>,
122+
default_value => def, verification => none, update => read_only},
123+
{ok, [#module_parameter{description = <<"binary description">>}]} =
124+
amoc_config_attributes:process_module_attributes(?MODULE, [BinAttr]),
125+
%% Unicode in both forms is preserved
126+
UnicodeList = #{name => unicode_list_var, description => "café",
127+
default_value => def, verification => none, update => read_only},
128+
{ok, [#module_parameter{description = <<"café"/utf8>>}]} =
129+
amoc_config_attributes:process_module_attributes(?MODULE, [UnicodeList]),
130+
UnicodeBin = #{name => unicode_bin_var, description => <<"café"/utf8>>,
131+
default_value => def, verification => none, update => read_only},
132+
{ok, [#module_parameter{description = <<"café"/utf8>>}]} =
133+
amoc_config_attributes:process_module_attributes(?MODULE, [UnicodeBin]).
134+
112135
errors_reporting(_) ->
113136
InvalidParam0 = #{name => "invalid_var0", description => "config_attrs_var0"},
114137
InvalidParam1 = #{name => invalid_var1, description => [$a, -2]},

0 commit comments

Comments
 (0)