lib: Add _get_{int,string}() APIs

- add integer and string version of the APIs,
- Also Refine the APIs, return value to indicate the setting existance
  and a new param to return the value of the setting.
- add their lua bindings as well, in lua binding the return value nil
  indicates that the setting is not defnied.
- Add corresponding C and lua tests as well.
- Add a few handy debug msgs.
This commit is contained in:
Ashok Sidipotu
2022-04-29 11:23:45 +05:30
committed by Julian Bouzas
parent a4f16a98f0
commit 7d304096e5
8 changed files with 245 additions and 19 deletions

View File

@@ -54,6 +54,7 @@ load_module (WpCore * core, const gchar * module_name,
GModule *gmodule; GModule *gmodule;
gpointer module_init; gpointer module_init;
wp_debug_object(core, "loading module(%s)", module_name);
module_path = g_module_build_path (wp_get_module_dir (), module_name); module_path = g_module_build_path (wp_get_module_dir (), module_name);
gmodule = g_module_open (module_path, G_MODULE_BIND_LOCAL); gmodule = g_module_open (module_path, G_MODULE_BIND_LOCAL);
if (!gmodule) { if (!gmodule) {

View File

@@ -70,14 +70,16 @@ wp_settings_init (WpSettings * self)
/*! /*!
* \brief gets the value of a setting. * \brief gets the boolean value of a setting.
* *
* \ingroup wpsetting * \ingroup wpsetting
* \param self the handle * \param self the handle
* \param setting name of the setting * \param setting name of the setting
* \returns: (transfer none) boolean value of the string. * \param value(out): the boolean value of the setting
* \returns: TRUE if the setting is defined, FALSE otherwise
*/ */
gboolean wp_settings_get_boolean (WpSettings *self, const gchar *setting) gboolean wp_settings_get_boolean (WpSettings *self, const gchar *setting,
gboolean *value)
{ {
g_return_val_if_fail (self, false); g_return_val_if_fail (self, false);
g_return_val_if_fail (setting, false); g_return_val_if_fail (setting, false);
@@ -86,7 +88,66 @@ gboolean wp_settings_get_boolean (WpSettings *self, const gchar *setting)
WP_OBJECT_FEATURES_ALL)) WP_OBJECT_FEATURES_ALL))
return false; return false;
return spa_atob (wp_properties_get (self->settings, setting)); if (!wp_properties_get (self->settings, setting))
return false;
*value = spa_atob (wp_properties_get (self->settings, setting));
return true;
}
/*!
* \brief gets the string value of a setting.
*
* \ingroup wpsetting
* \param self the handle
* \param setting name of the setting
* \param value(out): the string value of the setting
* \returns: TRUE if the setting is defined, FALSE otherwise
*/
gboolean wp_settings_get_string (WpSettings *self, const gchar *setting,
const char **value)
{
g_return_val_if_fail (self, false);
g_return_val_if_fail (setting, false);
if (!(wp_object_get_active_features (WP_OBJECT (self)) &
WP_OBJECT_FEATURES_ALL))
return false;
if (!wp_properties_get (self->settings, setting))
return false;
*value = wp_properties_get (self->settings, setting);
return true;
}
/*!
* \brief gets the integer(signed) value of a setting.
*
* \ingroup wpsetting
* \param self the handle
* \param value(out): the integer value of the setting
* \returns: TRUE if the setting is defined, FALSE otherwise
*/
gboolean wp_settings_get_int (WpSettings *self, const gchar *setting,
gint64 *val)
{
g_return_val_if_fail (self, false);
g_return_val_if_fail (setting, false);
if (!(wp_object_get_active_features (WP_OBJECT (self)) &
WP_OBJECT_FEATURES_ALL))
return false;
if (!wp_properties_get (self->settings, setting))
return false;
*val = 0; /* ground the value */
spa_atoi64 (wp_properties_get (self->settings, setting), val, 0);
return true;
} }
/*! /*!

View File

@@ -36,7 +36,15 @@ WpSettings * wp_settings_get_instance (WpCore * core,
const gchar *metadata_name); const gchar *metadata_name);
WP_API WP_API
gboolean wp_settings_get_boolean (WpSettings *self, const gchar *setting); gboolean wp_settings_get_boolean (WpSettings *self, const gchar *setting,
gboolean *value);
WP_API
gboolean wp_settings_get_string (WpSettings *self, const gchar *setting,
const char **value);
WP_API
gboolean wp_settings_get_int (WpSettings *self, const gchar *setting,
gint64 *val);
WP_API WP_API
gboolean wp_settings_apply_rule (WpSettings *self, const gchar *rule, gboolean wp_settings_apply_rule (WpSettings *self, const gchar *rule,

View File

@@ -1485,8 +1485,59 @@ get_boolean (lua_State *L)
if (s) if (s)
{ {
gboolean value = wp_settings_get_boolean (s, setting); gboolean value = 0;
lua_pushboolean (L, value); if (wp_settings_get_boolean (s, setting, &value))
lua_pushboolean (L, value);
else
lua_pushnil (L);
}
else
lua_pushnil (L);
return 1;
}
static gboolean
get_string (lua_State *L)
{
const char *setting = luaL_checkstring (L, 1);
const char *m = NULL;
if (lua_type (L, 2) == LUA_TSTRING)
m = luaL_checkstring (L, 2);
g_autoptr (WpSettings) s = wp_settings_get_instance (get_wp_core (L), m);
if (s)
{
const gchar *value = NULL;
if (wp_settings_get_string (s, setting, &value))
lua_pushstring (L, value);
else
lua_pushnil (L);
}
else
lua_pushnil (L);
return 1;
}
static gboolean
get_int (lua_State *L)
{
const char *setting = luaL_checkstring (L, 1);
const char *m = NULL;
if (lua_type (L, 2) == LUA_TSTRING)
m = luaL_checkstring (L, 2);
g_autoptr (WpSettings) s = wp_settings_get_instance (get_wp_core (L), m);
if (s)
{
gint64 value = 0;
if (wp_settings_get_int (s, setting, &value))
lua_pushinteger (L, value);
else
lua_pushnil (L);
} }
else else
lua_pushnil (L); lua_pushnil (L);
@@ -1521,6 +1572,8 @@ apply_rule (lua_State *L)
static const luaL_Reg settings_methods[] = { static const luaL_Reg settings_methods[] = {
{ "get_boolean", get_boolean }, { "get_boolean", get_boolean },
{ "apply_rule", apply_rule }, { "apply_rule", apply_rule },
{ "get_string", get_string },
{ "get_int", get_int },
{ NULL, NULL } { NULL, NULL }
}; };

View File

@@ -178,13 +178,16 @@ do_load_components(void *data, const char *location, const char *section,
} }
if (wp_spa_json_object_get (o, "deps", "s", &deps, NULL) && deps) { if (wp_spa_json_object_get (o, "deps", "s", &deps, NULL) && deps) {
if (!wp_settings_get_boolean (settings, deps)) {; gboolean value = 0;
if (!wp_settings_get_boolean (settings, deps, &value) && value) {
wp_info ("deps(%s) not met for component(%s), skip loading it", wp_info ("deps(%s) not met for component(%s), skip loading it",
deps, name); deps, name);
continue; continue;
} }
} }
wp_debug ("load component(%s) type(%s) deps(%s)", name, type,
(deps ? deps : "nill"));
if (!wp_core_load_component (core, name, type, NULL, &error)) { if (!wp_core_load_component (core, name, type, NULL, &error)) {
wp_transition_return_error (transition, error); wp_transition_return_error (transition, error);
return -EINVAL; return -EINVAL;

View File

@@ -111,8 +111,8 @@ test_parsing_setup (TestSettingsFixture *self, gconstpointer user_data)
self->settings = g_steal_pointer (&settings); self->settings = g_steal_pointer (&settings);
/* total no.of properties in the conf file */ /* total no.of settings in the conf file */
g_assert_cmpint (data.count, ==, 5); g_assert_cmpint (data.count, ==, 11);
} }
} }
@@ -128,8 +128,8 @@ test_parsing_teardown (TestSettingsFixture *self, gconstpointer user_data)
static void static void
test_parsing (TestSettingsFixture *self, gconstpointer data) test_parsing (TestSettingsFixture *self, gconstpointer data)
{ {
/* total no.of properties in the conf file */ /* total no.of settings in the conf file */
g_assert_cmpint (wp_properties_get_count(self->settings), ==, 5); g_assert_cmpint (wp_properties_get_count(self->settings), ==, 11);
} }
static void static void
@@ -248,10 +248,64 @@ static void
test_wpsettings (TestSettingsFixture *self, gconstpointer data) test_wpsettings (TestSettingsFixture *self, gconstpointer data)
{ {
WpSettings *s = self->s; WpSettings *s = self->s;
g_assert_false (wp_settings_get_boolean (s, "test-property1"));
g_assert_true (wp_settings_get_boolean (s, "test-property2"));
/* test the wp_settings_get_instance() API */ {
gboolean value = 0;
/* test settings _get_boolean */
g_assert_false (wp_settings_get_boolean (s, "test-property-undefined",
&value));
g_assert_true (wp_settings_get_boolean (s, "test-property1", &value));
g_assert_false (value);
g_assert_true (wp_settings_get_boolean (s, "test-property2", &value));
g_assert_true (value);
}
{
gint64 value = 0;
/* _get_int () */
g_assert_false (wp_settings_get_int (s, "test-property-undefined",
&value));
g_assert_true (wp_settings_get_int (s, "test-property3-int", &value));
g_assert_cmpint (value, ==, -20);
g_assert_true (wp_settings_get_int (s, "test-property4-max-int", &value));
g_assert_cmpint (value, ==, G_MAXINT64);
g_assert_true (wp_settings_get_int (s, "test-property4-min-int", &value));
g_assert_cmpint (value, ==, G_MININT64);
g_assert_true (wp_settings_get_int (s, "test-property4-max-int-one-more",
&value));
g_assert_cmpint (value, ==, 0);
g_assert_true (wp_settings_get_int (s, "test-property4-min-int-one-less",
&value));
g_assert_cmpint (value, ==, 0);
}
{
const gchar *value = NULL;
/* _get_string () */
g_assert_false (wp_settings_get_string (s, "test-property-undefined",
&value));
g_assert_true (wp_settings_get_string (s, "test-property4-string",
&value));
g_assert_cmpstr (value, ==, "blahblah");
g_assert_true (wp_settings_get_string (s, "test-property2",
&value));
g_assert_cmpstr (value, ==, "true");
g_assert_true (wp_settings_get_string (s, "test-property3-int",
&value));
g_assert_cmpstr (value, ==, "-20");
}
/* test the wp_settings_get_instance () API */
{ {
g_autoptr (WpSettings) s1 = g_autoptr (WpSettings) s1 =
wp_settings_get_instance (self->base.core, "test-settings"); wp_settings_get_instance (self->base.core, "test-settings");

View File

@@ -66,6 +66,12 @@ context.modules = [
wireplumber.settings = { wireplumber.settings = {
test-property1 = "false" test-property1 = "false"
test-property2 = "true" test-property2 = "true"
test-property3-int = "-20"
test-property4-max-int = "9223372036854775807"
test-property4-max-int-one-more = "9223372036854775808"
test-property4-min-int = -9223372036854775808
test-property4-min-int-one-less = -9223372036854775809
test-property4-string = "blahblah"
rule_one = [ rule_one = [
{ {
matches = [ matches = [

View File

@@ -2,11 +2,51 @@
-- tests the lua API of WpSettings, this file tests the settings present in -- tests the lua API of WpSettings, this file tests the settings present in
-- .conf file that is loaded. -- .conf file that is loaded.
-- test settings -- test settings _get_boolean ()
assert (Settings.get_boolean ("test-property1", "test-settings") == false) local value = Settings.get_boolean ("test-property1", "test-settings")
assert (Settings.get_boolean ("test-property2", "test-settings") == true) assert (value == false)
assert (Settings.get_boolean ("test-property1") == false) value = Settings.get_boolean ("test-property2", "test-settings")
assert (value == true)
value = Settings.get_boolean ("test-property1")
assert (value == nil)
value = Settings.get_boolean ("test-property-undefined",
"test-settings")
assert (value == nil)
-- test settings _get_int ()
value = Settings.get_int ("test-property-undefined", "test-settings")
assert (value == nil)
value = Settings.get_int ("test-property3-int", "test-settings")
assert (value == -20)
value = Settings.get_int ("test-property4-max-int", "test-settings")
assert (value == 9223372036854775807)
value = Settings.get_int ("test-property4-min-int", "test-settings")
assert (value == -9223372036854775808)
value = Settings.get_int ("test-property4-max-int-one-more",
"test-settings")
assert (value == 0)
value = Settings.get_int ("test-property4-min-int-one-less",
"test-settings")
assert (value == 0)
-- test settings _get_string ()
value = Settings.get_string ("test-property-undefined", "test-settings")
assert (value == nil)
value = Settings.get_string ("test-property4-string", "test-settings")
assert (value == "blahblah")
value = Settings.get_string ("test-property3-int", "test-settings")
assert (value == "-20")
-- test rules -- test rules
-- test #1 -- test #1