-
Notifications
You must be signed in to change notification settings - Fork 188
/
riakc_utils.erl
74 lines (68 loc) · 2.64 KB
/
riakc_utils.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
%% -------------------------------------------------------------------
%%
%% riakc_utils: erlang client utils
%%
%% Copyright (c) 2016 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------
-module(riakc_utils).
-export([wait_for_list/1,
characters_to_unicode_binary/1,
get_allow_listing/0,
get_allow_listing/1]).
-spec wait_for_list(non_neg_integer()) -> {ok, list()} | {error, any()}.
%% @doc Wait for the results of a listing operation
wait_for_list(ReqId) ->
wait_for_list(ReqId, []).
wait_for_list(ReqId, Acc) ->
receive
{ReqId, done} -> {ok, lists:flatten(Acc)};
{ReqId, {error, Reason}} -> {error, Reason};
{ReqId, {_, Res}} -> wait_for_list(ReqId, [Res|Acc])
end.
-spec characters_to_unicode_binary(string()|binary()) -> binary().
%% @doc Convert to unicode binary with informative errors
%% @throws {unicode_error, ErrMsg}
characters_to_unicode_binary(String) ->
case unicode:characters_to_binary(String) of
{incomplete, Encoded, Rest} ->
ErrMsg = lists:flatten(io_lib:format("Incomplete unicode data provided. Encoded: ~p Rest: ~p", [Encoded, Rest])),
throw({unicode_error, ErrMsg});
{error, Encoded, Rest} ->
ErrMsg = lists:flatten(io_lib:format("Unicode encoding error. Encoded: ~p Rest: ~p", [Encoded, Rest])),
throw({unicode_error, ErrMsg});
Binary ->
Binary
end.
%% @doc Return the value of allow_listing, which, if set to 'true`
%% will allow listing keys and buckets.
-spec get_allow_listing() -> boolean().
get_allow_listing() ->
case application:get_env(riakc, allow_listing) of
{ok, true} -> true;
_ -> false
end.
-spec get_allow_listing(proplists:proplist()) -> boolean().
get_allow_listing(Options) ->
case application:get_env(riakc, allow_listing) of
{ok, true} -> true;
_ ->
case proplists:get_value(allow_listing, Options) of
true -> true;
_ -> false
end
end.