Skip to content
This repository
branch: adt-poolboy-up…
Fetching contributors…

Cannot retrieve contributors at this time

file 120 lines (101 sloc) 4.063 kb
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
%% -------------------------------------------------------------------
%%
%% riak_core: Core Riak Application
%%
%% Copyright (c) 2007-2010 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(app_helper).

-export([get_env/1,
         get_env/2,
         get_env/3,
         get_prop_or_env/3,
         get_prop_or_env/4]).

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.

%% ===================================================================
%% Public API
%% ===================================================================

%% @spec get_env(App :: atom()) -> [{Key :: atom(), Value :: term()}]
%% @doc Retrieve all Key/Value pairs in the env for the specified app.
get_env(App) ->
    application:get_all_env(App).

%% @spec get_env(App :: atom(), Key :: atom()) -> term()
%% @doc The official way to get a value from the app's env.
%% Will return the 'undefined' atom if that key is unset.
get_env(App, Key) ->
    get_env(App, Key, undefined).

%% @spec get_env(App :: atom(), Key :: atom(), Default :: term()) -> term()
%% @doc The official way to get a value from this application's env.
%% Will return Default if that key is unset.
get_env(App, Key, Default) ->
    case application:get_env(App, Key) of
{ok, Value} ->
            Value;
        _ ->
            Default
    end.

%% @doc Retrieve value for Key from Properties if it exists, otherwise
%% return from the application's env.
-spec get_prop_or_env(atom(), [{atom(), term()}], atom()) -> term().
get_prop_or_env(Key, Properties, App) ->
    get_prop_or_env(Key, Properties, App, undefined).

%% @doc Return the value for Key in Properties if it exists, otherwise return
%% the value from the application's env, or Default.
-spec get_prop_or_env(atom(), [{atom(), term()}], atom(), term()) -> term().
get_prop_or_env(Key, Properties, App, Default) ->
    case proplists:get_value(Key, Properties) of
        undefined ->
            get_env(App, Key, Default);
        Value ->
            Value
    end.

%% ===================================================================
%% EUnit tests
%% ===================================================================
-ifdef(TEST).

app_helper_test_() ->
    { setup,
      fun setup/0,
      fun cleanup/1,
      [
       fun get_prop_or_env_default_value_test_case/0,
       fun get_prop_or_env_undefined_value_test_case/0,
       fun get_prop_or_env_from_env_test_case/0,
       fun get_prop_or_env_from_prop_test_case/0,
       fun get_prop_or_env_from_prop_with_default_test_case/0
      ]
    }.

setup() ->
    application:set_env(bogus_app, envkeyone, value),
    application:set_env(bogus_app, envkeytwo, valuetwo).

cleanup(_Ctx) ->
    ok.

get_prop_or_env_default_value_test_case() ->
    ?assertEqual(default, get_prop_or_env(key, [], bogus, default)).

get_prop_or_env_undefined_value_test_case() ->
    ?assertEqual(undefined, get_prop_or_env(key, [], bogus)).

get_prop_or_env_from_env_test_case() ->
    ?assertEqual(value, get_prop_or_env(envkeyone, [], bogus_app)).

get_prop_or_env_from_prop_test_case() ->
    Properties = [{envkeyone, propvalue}],
    ?assertEqual(propvalue, get_prop_or_env(envkeyone, Properties, bogus_app)).

get_prop_or_env_from_prop_with_default_test_case() ->
    Properties = [{envkeyone, propvalue}],
    ?assertEqual(propvalue, get_prop_or_env(envkeyone, Properties, bogus_app, default)).

-endif.
Something went wrong with that request. Please try again.