From d1d834820ddaba4fa31e6ab6d07a96ac7cc6d999 Mon Sep 17 00:00:00 2001 From: serge Date: Thu, 10 Jan 2013 09:07:38 -0500 Subject: Add application:get_key/3 The new function provides a default value for a configuration parameter. --- lib/kernel/doc/src/application.xml | 4 +++- lib/kernel/src/application.erl | 16 +++++++++++++++- lib/kernel/test/application_SUITE.erl | 13 +++++++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) (limited to 'lib/kernel') diff --git a/lib/kernel/doc/src/application.xml b/lib/kernel/doc/src/application.xml index 51a3311ec2..3513e2b592 100644 --- a/lib/kernel/doc/src/application.xml +++ b/lib/kernel/doc/src/application.xml @@ -108,12 +108,14 @@ + Get the value of a configuration parameter

Returns the value of the configuration parameter Par for Application. If the application argument is omitted, it defaults to the application of the calling - process.

+ process. If Def value is provided, it is returned + when no configuration parameter Par is defined.

If the specified application is not loaded, or the configuration parameter does not exist, or if the process executing the call does not belong to any application, diff --git a/lib/kernel/src/application.erl b/lib/kernel/src/application.erl index 9b7c4aa7b8..4e65883be7 100644 --- a/lib/kernel/src/application.erl +++ b/lib/kernel/src/application.erl @@ -23,7 +23,7 @@ which_applications/0, which_applications/1, loaded_applications/0, permit/2]). -export([set_env/3, set_env/4, unset_env/2, unset_env/3]). --export([get_env/1, get_env/2, get_all_env/0, get_all_env/1]). +-export([get_env/1, get_env/2, get_env/3, get_all_env/0, get_all_env/1]). -export([get_key/1, get_key/2, get_all_key/0, get_all_key/1]). -export([get_application/0, get_application/1, info/0]). -export([start_type/0]). @@ -264,6 +264,20 @@ get_env(Key) -> get_env(Application, Key) -> application_controller:get_env(Application, Key). +-spec get_env(Application, Par, Def) -> Val when + Application :: atom(), + Par :: atom(), + Def :: term(), + Val :: term(). + +get_env(Application, Key, Def) -> + case get_env(Application, Key) of + {ok, Val} -> + Val; + undefined -> + Def + end. + -spec get_all_env() -> Env when Env :: [{Par :: atom(), Val :: term()}]. diff --git a/lib/kernel/test/application_SUITE.erl b/lib/kernel/test/application_SUITE.erl index f469a0af98..412a969b13 100644 --- a/lib/kernel/test/application_SUITE.erl +++ b/lib/kernel/test/application_SUITE.erl @@ -27,7 +27,7 @@ otp_1586/1, otp_2078/1, otp_2012/1, otp_2718/1, otp_2973/1, otp_3002/1, otp_3184/1, otp_4066/1, otp_4227/1, otp_5363/1, otp_5606/1, - start_phases/1, get_key/1, + start_phases/1, get_key/1, get_env/1, permit_false_start_local/1, permit_false_start_dist/1, script_start/1, nodedown_start/1, init2973/0, loop2973/0, loop5606/1]). @@ -49,7 +49,7 @@ all() -> [failover, failover_comp, permissions, load, load_use_cache, {group, reported_bugs}, start_phases, script_start, nodedown_start, permit_false_start_local, - permit_false_start_dist, get_key, + permit_false_start_dist, get_key, get_env, {group, distr_changed}, config_change, shutdown_func, shutdown_timeout]. groups() -> @@ -1503,6 +1503,15 @@ loop5606(Pid) -> Pid ! {self(), Res} end. +get_env(suite) -> []; +get_env(doc) -> + ["Tests get_env/* functions"]; +get_env(Conf) when is_list(Conf) -> + ?line {ok, _} = application:get_env(kernel, error_logger), + ?line undefined = application:get_env(undefined_app, a), + ?line undefined = application:get_env(kernel, error_logger_xyz), + ?line default = application:get_env(kernel, error_logger_xyz, default), + ok. %%----------------------------------------------------------------- %% Should be started in a CC view with: -- cgit v1.2.3 From 719e25ebda24db268cf94121c29258283e24b753 Mon Sep 17 00:00:00 2001 From: Serge Aleynikov Date: Mon, 14 Jan 2013 13:23:04 -0500 Subject: Removed obsolete ?line macro --- lib/kernel/test/application_SUITE.erl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/kernel') diff --git a/lib/kernel/test/application_SUITE.erl b/lib/kernel/test/application_SUITE.erl index 412a969b13..2ca8840e1f 100644 --- a/lib/kernel/test/application_SUITE.erl +++ b/lib/kernel/test/application_SUITE.erl @@ -1507,10 +1507,10 @@ get_env(suite) -> []; get_env(doc) -> ["Tests get_env/* functions"]; get_env(Conf) when is_list(Conf) -> - ?line {ok, _} = application:get_env(kernel, error_logger), - ?line undefined = application:get_env(undefined_app, a), - ?line undefined = application:get_env(kernel, error_logger_xyz), - ?line default = application:get_env(kernel, error_logger_xyz, default), + {ok, _} = application:get_env(kernel, error_logger), + undefined = application:get_env(undefined_app, a), + undefined = application:get_env(kernel, error_logger_xyz), + default = application:get_env(kernel, error_logger_xyz, default), ok. %%----------------------------------------------------------------- -- cgit v1.2.3 From 37654ca0993d24753d6596f7a5e4e784030092c4 Mon Sep 17 00:00:00 2001 From: Serge Aleynikov Date: Tue, 15 Jan 2013 10:24:14 -0500 Subject: Moved documentation in a separate entry --- lib/kernel/doc/src/application.xml | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'lib/kernel') diff --git a/lib/kernel/doc/src/application.xml b/lib/kernel/doc/src/application.xml index 3513e2b592..9f19efc793 100644 --- a/lib/kernel/doc/src/application.xml +++ b/lib/kernel/doc/src/application.xml @@ -108,20 +108,27 @@ - Get the value of a configuration parameter

Returns the value of the configuration parameter Par for Application. If the application argument is omitted, it defaults to the application of the calling - process. If Def value is provided, it is returned - when no configuration parameter Par is defined.

+ process.

If the specified application is not loaded, or the configuration parameter does not exist, or if the process executing the call does not belong to any application, the function returns undefined.

+ + + Get the value of a configuration parameter using a default + +

Works like get_env/2 but returns + Def value when configuration parameter + Par does not exist.

+
+
-- cgit v1.2.3