WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 3043af9

Browse files
Merge pull request #15109 from rabbitmq/mergify/bp/v4.2.x/pr-15105
Introduce rabbit_data_coercion:to_boolean/1 (backport #15105)
2 parents 7004d8f + 5d5b964 commit 3043af9

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

deps/rabbit_common/src/rabbit_data_coercion.erl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
-module(rabbit_data_coercion).
99

10-
-export([to_binary/1, to_list/1, to_atom/1, to_integer/1, to_proplist/1, to_map/1, to_map_recursive/1]).
10+
-export([to_binary/1, to_list/1, to_atom/1, to_integer/1, to_boolean/1, to_proplist/1, to_map/1, to_map_recursive/1]).
1111
-export([to_atom/2, atomize_keys/1, to_list_of_binaries/1]).
1212
-export([to_utf8_binary/1, to_unicode_charlist/1]).
1313
-export([as_list/1]).
@@ -56,6 +56,11 @@ to_integer(Val) when is_integer(Val) -> Val;
5656
to_integer(Val) when is_list(Val) -> list_to_integer(Val);
5757
to_integer(Val) when is_binary(Val) -> binary_to_integer(Val).
5858

59+
-spec to_boolean(Val :: boolean() | list() | binary()) -> boolean().
60+
to_boolean(Val) when is_boolean(Val) -> Val;
61+
to_boolean(Val) when is_list(Val) -> list_to_boolean(string:lowercase(Val));
62+
to_boolean(Val) when is_binary(Val) -> list_to_boolean(string:lowercase(binary_to_list(Val))).
63+
5964
-spec to_proplist(Val :: map() | list()) -> list().
6065
to_proplist(Val) when is_list(Val) -> Val;
6166
to_proplist(Val) when is_map(Val) -> maps:to_list(Val).
@@ -160,3 +165,6 @@ is_proplist_element(Atom) when is_atom(Atom) ->
160165
true;
161166
is_proplist_element(_) ->
162167
false.
168+
169+
list_to_boolean("true") -> true;
170+
list_to_boolean("false") -> false.

deps/rabbit_common/test/unit_SUITE.erl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ groups() ->
5050
data_coercion_to_map_recursive_property,
5151
data_coercion_atomize_keys_proplist,
5252
data_coercion_atomize_keys_map,
53+
data_coercion_to_boolean,
5354
pget,
5455
deep_pget,
5556
encrypt_decrypt,
@@ -287,6 +288,25 @@ data_coercion_atomize_keys_proplist(_Config) ->
287288
B = rabbit_data_coercion:atomize_keys([{a, 1}, {"b", 2}, {<<"c">>, 3}]),
288289
?assertEqual(lists:usort(A), lists:usort(B)).
289290

291+
data_coercion_to_boolean(_Config) ->
292+
%% For booleans, this is an identity function
293+
?assertEqual(true, rabbit_data_coercion:to_boolean(true)),
294+
?assertEqual(false, rabbit_data_coercion:to_boolean(false)),
295+
%% Strings (lists) are converted regardless of their case
296+
?assertEqual(true, rabbit_data_coercion:to_boolean("true")),
297+
?assertEqual(true, rabbit_data_coercion:to_boolean("TRUE")),
298+
?assertEqual(true, rabbit_data_coercion:to_boolean("True")),
299+
?assertEqual(false, rabbit_data_coercion:to_boolean("false")),
300+
?assertEqual(false, rabbit_data_coercion:to_boolean("FALSE")),
301+
?assertEqual(false, rabbit_data_coercion:to_boolean("False")),
302+
%% Binaries are also converted regardless of their case
303+
?assertEqual(true, rabbit_data_coercion:to_boolean(<<"true">>)),
304+
?assertEqual(true, rabbit_data_coercion:to_boolean(<<"TRUE">>)),
305+
?assertEqual(true, rabbit_data_coercion:to_boolean(<<"True">>)),
306+
?assertEqual(false, rabbit_data_coercion:to_boolean(<<"false">>)),
307+
?assertEqual(false, rabbit_data_coercion:to_boolean(<<"FALSE">>)),
308+
?assertEqual(false, rabbit_data_coercion:to_boolean(<<"False">>)).
309+
290310
data_coercion_to_list(_Config) ->
291311
?assertEqual([{a, 1}], rabbit_data_coercion:to_list([{a, 1}])),
292312
?assertEqual([{a, 1}], rabbit_data_coercion:to_list(#{a => 1})).

0 commit comments

Comments
 (0)