conf: drop all the _get_value() functions and remove the fallback from _get_section()
We do not use these APIs, so there's no point in keeping them. Realistically, every component that needs a section just does its own parsing on it, so the _get_value() functions are not needed. The fallback in _get_section() is also not needed, as we always pass NULL and then test for it. In Lua, however, it seems we are using the fallback to return an empty object, so that getting a section does not expand to multiple lines of code. For that reason, I have kept the syntax there and implemented it in the bindings layer.
This commit is contained in:
222
lib/wp/conf.c
222
lib/wp/conf.c
@@ -425,227 +425,21 @@ ensure_merged_section (WpConf * self, const gchar *section)
|
||||
* This method will get the JSON value of a specific section from the
|
||||
* configuration. If the same section is defined in multiple locations, the
|
||||
* sections with the same name will be either merged in case of arrays and
|
||||
* objects, or overridden in case of boolean, int, double and strings. The
|
||||
* passed fallback value will be returned if the section does not exist.
|
||||
* objects, or overridden in case of boolean, int, double and strings.
|
||||
*
|
||||
* \ingroup wpconf
|
||||
* \param self the configuration
|
||||
* \param section the section name
|
||||
* \param fallback (transfer full)(nullable): the fallback value
|
||||
* \returns (transfer full): the JSON value of the section
|
||||
* \returns (transfer full) (nullable): the JSON value of the section or NULL
|
||||
* if the section does not exist
|
||||
*/
|
||||
WpSpaJson *
|
||||
wp_conf_get_section (WpConf *self, const gchar *section, WpSpaJson *fallback)
|
||||
wp_conf_get_section (WpConf *self, const gchar *section)
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = NULL;
|
||||
g_autoptr (WpSpaJson) fb = fallback;
|
||||
|
||||
g_return_val_if_fail (WP_IS_CONF (self), NULL);
|
||||
|
||||
s = ensure_merged_section (self, section);
|
||||
if (!s)
|
||||
return fb ? g_steal_pointer (&fb) : NULL;
|
||||
|
||||
return g_steal_pointer (&s);
|
||||
}
|
||||
|
||||
/*!
|
||||
* This is a convenient function to access a JSON value from an object
|
||||
* section in the configuration. If the section is an array, or the key does
|
||||
* not exist in the object section, it will return the passed fallback value.
|
||||
*
|
||||
* \ingroup wpconf
|
||||
* \param self the configuration
|
||||
* \param section the section name
|
||||
* \param key the key name
|
||||
* \param fallback (transfer full)(nullable): the fallback value
|
||||
* \returns (transfer full): the JSON value of the section's key if it exists,
|
||||
* or the passed fallback value otherwise
|
||||
*/
|
||||
WpSpaJson *
|
||||
wp_conf_get_value (WpConf *self, const gchar *section, const gchar *key,
|
||||
WpSpaJson *fallback)
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = NULL;
|
||||
g_autoptr (WpSpaJson) fb = fallback;
|
||||
WpSpaJson *v;
|
||||
|
||||
g_return_val_if_fail (WP_IS_CONF (self), NULL);
|
||||
g_return_val_if_fail (section, NULL);
|
||||
g_return_val_if_fail (key, NULL);
|
||||
|
||||
s = wp_conf_get_section (self, section, NULL);
|
||||
if (!s)
|
||||
goto return_fallback;
|
||||
|
||||
if (!wp_spa_json_is_object (s)) {
|
||||
wp_warning_object (self,
|
||||
"Cannot get JSON key %s from %s as section is not an JSON object",
|
||||
key, section);
|
||||
goto return_fallback;
|
||||
}
|
||||
|
||||
if (wp_spa_json_object_get (s, key, "J", &v, NULL))
|
||||
return v;
|
||||
|
||||
return_fallback:
|
||||
return fb ? g_steal_pointer (&fb) : NULL;
|
||||
}
|
||||
|
||||
/*!
|
||||
* This is a convenient function to access a boolean value from an object
|
||||
* section in the configuration. If the section is an array, or the key does
|
||||
* not exist in the object section, it will return the passed fallback value.
|
||||
*
|
||||
* \ingroup wpconf
|
||||
* \param self the configuration
|
||||
* \param section the section name
|
||||
* \param key the key name
|
||||
* \param fallback the fallback value
|
||||
* \returns the boolean value of the section's key if it exists and could be
|
||||
* parsed, or the passed fallback value otherwise
|
||||
*/
|
||||
gboolean
|
||||
wp_conf_get_value_boolean (WpConf *self, const gchar *section,
|
||||
const gchar *key, gboolean fallback)
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = NULL;
|
||||
gboolean v;
|
||||
|
||||
g_return_val_if_fail (WP_IS_CONF (self), FALSE);
|
||||
g_return_val_if_fail (section, FALSE);
|
||||
g_return_val_if_fail (key, FALSE);
|
||||
|
||||
s = wp_conf_get_section (self, section, NULL);
|
||||
if (!s)
|
||||
return fallback;
|
||||
|
||||
if (!wp_spa_json_is_object (s)) {
|
||||
wp_warning_object (self,
|
||||
"Cannot get boolean key %s from %s as section is not an JSON object",
|
||||
key, section);
|
||||
return fallback;
|
||||
}
|
||||
|
||||
return wp_spa_json_object_get (s, key, "b", &v, NULL) ? v : fallback;
|
||||
}
|
||||
|
||||
/*!
|
||||
* This is a convenient function to access a int value from an object
|
||||
* section in the configuration. If the section is an array, or the key does
|
||||
* not exist in the object section, it will return the passed fallback value.
|
||||
*
|
||||
* \ingroup wpconf
|
||||
* \param self the configuration
|
||||
* \param section the section name
|
||||
* \param key the key name
|
||||
* \param fallback the fallback value
|
||||
* \returns the int value of the section's key if it exists and could be
|
||||
* parsed, or the passed fallback value otherwise
|
||||
*/
|
||||
gint
|
||||
wp_conf_get_value_int (WpConf *self, const gchar *section,
|
||||
const gchar *key, gint fallback)
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = NULL;
|
||||
gint v;
|
||||
|
||||
g_return_val_if_fail (WP_IS_CONF (self), 0);
|
||||
g_return_val_if_fail (section, 0);
|
||||
g_return_val_if_fail (key, 0);
|
||||
|
||||
s = wp_conf_get_section (self, section, NULL);
|
||||
if (!s)
|
||||
return fallback;
|
||||
|
||||
if (!wp_spa_json_is_object (s)) {
|
||||
wp_warning_object (self,
|
||||
"Cannot get int key %s from %s as section is not an JSON object",
|
||||
key, section);
|
||||
return fallback;
|
||||
}
|
||||
|
||||
return wp_spa_json_object_get (s, key, "i", &v, NULL) ? v : fallback;
|
||||
}
|
||||
|
||||
/*!
|
||||
* This is a convenient function to access a float value from an object
|
||||
* section in the configuration. If the section is an array, or the key does
|
||||
* not exist in the object section, it will return the passed fallback value.
|
||||
*
|
||||
* \ingroup wpconf
|
||||
* \param self the configuration
|
||||
* \param section the section name
|
||||
* \param key the key name
|
||||
* \param fallback the fallback value
|
||||
* \returns the float value of the section's key if it exists and could be
|
||||
* parsed, or the passed fallback value otherwise
|
||||
*/
|
||||
float
|
||||
wp_conf_get_value_float (WpConf *self, const gchar *section,
|
||||
const gchar *key, float fallback)
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = NULL;
|
||||
float v;
|
||||
|
||||
g_return_val_if_fail (WP_IS_CONF (self), 0);
|
||||
g_return_val_if_fail (section, 0);
|
||||
g_return_val_if_fail (key, 0);
|
||||
|
||||
s = wp_conf_get_section (self, section, NULL);
|
||||
if (!s)
|
||||
return fallback;
|
||||
|
||||
if (!wp_spa_json_is_object (s)) {
|
||||
wp_warning_object (self,
|
||||
"Cannot get float key %s from %s as section is not an JSON object",
|
||||
key, section);
|
||||
return fallback;
|
||||
}
|
||||
|
||||
return wp_spa_json_object_get (s, key, "f", &v, NULL) ? v : fallback;
|
||||
}
|
||||
|
||||
/*!
|
||||
* This is a convenient function to access a string value from an object
|
||||
* section in the configuration. If the section is an array, or the key does
|
||||
* not exist in the object section, it will return the passed fallback value.
|
||||
*
|
||||
* \ingroup wpconf
|
||||
* \param self the configuration
|
||||
* \param section the section name
|
||||
* \param key the key name
|
||||
* \param fallback (nullable): the fallback value
|
||||
* \returns (transfer full): the string value of the section's key if it exists
|
||||
* and could be parsed, or the passed fallback value otherwise
|
||||
*/
|
||||
gchar *
|
||||
wp_conf_get_value_string (WpConf *self, const gchar *section,
|
||||
const gchar *key, const gchar *fallback)
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = NULL;
|
||||
gchar *v;
|
||||
|
||||
g_return_val_if_fail (WP_IS_CONF (self), NULL);
|
||||
g_return_val_if_fail (section, NULL);
|
||||
g_return_val_if_fail (key, NULL);
|
||||
|
||||
s = wp_conf_get_section (self, section, NULL);
|
||||
if (!s)
|
||||
goto return_fallback;
|
||||
|
||||
if (!wp_spa_json_is_object (s)) {
|
||||
wp_warning_object (self,
|
||||
"Cannot get string key %s from %s as section is not an JSON object",
|
||||
key, section);
|
||||
goto return_fallback;
|
||||
}
|
||||
|
||||
if (wp_spa_json_object_get (s, key, "s", &v, NULL))
|
||||
return v;
|
||||
|
||||
return_fallback:
|
||||
return fallback ? g_strdup (fallback) : NULL;
|
||||
return ensure_merged_section (self, section);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -668,7 +462,7 @@ wp_conf_section_update_props (WpConf *self, const gchar *section,
|
||||
g_return_val_if_fail (section, -1);
|
||||
g_return_val_if_fail (props, -1);
|
||||
|
||||
json = wp_conf_get_section (self, section, NULL);
|
||||
json = wp_conf_get_section (self, section);
|
||||
if (!json)
|
||||
return 0;
|
||||
return wp_properties_update_from_json (props, json);
|
||||
@@ -696,14 +490,14 @@ wp_conf_parse_pw_context_sections (WpConf * self, struct pw_context * context)
|
||||
/* convert needed sections into a pipewire-style conf dictionary */
|
||||
conf_wp = wp_properties_new ("config.path", "wpconf", NULL);
|
||||
{
|
||||
g_autoptr (WpSpaJson) j = wp_conf_get_section (self, "context.spa-libs", NULL);
|
||||
g_autoptr (WpSpaJson) j = wp_conf_get_section (self, "context.spa-libs");
|
||||
if (j) {
|
||||
g_autofree gchar *js = wp_spa_json_parse_string (j);
|
||||
wp_properties_set (conf_wp, "context.spa-libs", js);
|
||||
}
|
||||
}
|
||||
{
|
||||
g_autoptr (WpSpaJson) j = wp_conf_get_section (self, "context.modules", NULL);
|
||||
g_autoptr (WpSpaJson) j = wp_conf_get_section (self, "context.modules");
|
||||
if (j) {
|
||||
g_autofree gchar *js = wp_spa_json_parse_string (j);
|
||||
wp_properties_set (conf_wp, "context.modules", js);
|
||||
|
@@ -45,28 +45,7 @@ WP_API
|
||||
const gchar * wp_conf_get_name (WpConf * self);
|
||||
|
||||
WP_API
|
||||
WpSpaJson * wp_conf_get_section (WpConf *self, const gchar *section,
|
||||
WpSpaJson *fallback);
|
||||
|
||||
WP_API
|
||||
WpSpaJson *wp_conf_get_value (WpConf *self,
|
||||
const gchar *section, const gchar *key, WpSpaJson *fallback);
|
||||
|
||||
WP_API
|
||||
gboolean wp_conf_get_value_boolean (WpConf *self,
|
||||
const gchar *section, const gchar *key, gboolean fallback);
|
||||
|
||||
WP_API
|
||||
gint wp_conf_get_value_int (WpConf *self,
|
||||
const gchar *section, const gchar *key, gint fallback);
|
||||
|
||||
WP_API
|
||||
float wp_conf_get_value_float (WpConf *self,
|
||||
const gchar *section, const gchar *key, float fallback);
|
||||
|
||||
WP_API
|
||||
gchar *wp_conf_get_value_string (WpConf *self,
|
||||
const gchar *section, const gchar *key, const gchar *fallback);
|
||||
WpSpaJson * wp_conf_get_section (WpConf *self, const gchar *section);
|
||||
|
||||
WP_API
|
||||
gint wp_conf_section_update_props (WpConf * self, const gchar * section,
|
||||
|
@@ -752,24 +752,27 @@ wp_internal_comp_loader_load (WpComponentLoader * self, WpCore * core,
|
||||
/* component name is the profile name;
|
||||
component list and profile features are loaded from config */
|
||||
g_autoptr (WpConf) conf = wp_core_get_conf (core);
|
||||
g_autoptr (WpSpaJson) profile_json = NULL;
|
||||
g_autoptr (WpSpaJson) all_profiles_j = NULL;
|
||||
g_autoptr (WpSpaJson) profile_j = NULL;
|
||||
const gchar *profile_name = component;
|
||||
|
||||
profile_json =
|
||||
wp_conf_get_value (conf, "wireplumber.profiles", component, NULL);
|
||||
if (!profile_json) {
|
||||
all_profiles_j = wp_conf_get_section (conf, "wireplumber.profiles");
|
||||
if (all_profiles_j)
|
||||
wp_spa_json_object_get (all_profiles_j, profile_name, "J", &profile_j, NULL);
|
||||
|
||||
if (!profile_j) {
|
||||
g_autoptr (GTask) task = g_task_new (self, cancellable, callback, data);
|
||||
g_task_set_source_tag (task, wp_internal_comp_loader_load);
|
||||
g_task_return_new_error (G_TASK (task), WP_DOMAIN_LIBRARY,
|
||||
WP_LIBRARY_ERROR_INVALID_ARGUMENT,
|
||||
"profile '%s' not found in configuration", component);
|
||||
"profile '%s' not found in configuration", profile_name);
|
||||
return;
|
||||
}
|
||||
|
||||
wp_properties_update_from_json (profile, profile_json);
|
||||
wp_properties_update_from_json (profile, profile_j);
|
||||
|
||||
components = wp_conf_get_section (conf, "wireplumber.components", NULL);
|
||||
|
||||
rules = wp_conf_get_section (conf, "wireplumber.components.rules", NULL);
|
||||
components = wp_conf_get_section (conf, "wireplumber.components");
|
||||
rules = wp_conf_get_section (conf, "wireplumber.components.rules");
|
||||
}
|
||||
else {
|
||||
/* component list is retrieved from args; profile features are empty */
|
||||
|
@@ -1638,137 +1638,18 @@ conf_get_section (lua_State *L)
|
||||
fb = wp_spa_json_ref (v);
|
||||
}
|
||||
|
||||
s = wp_conf_get_section (conf, section, g_steal_pointer (&fb));
|
||||
s = wp_conf_get_section (conf, section);
|
||||
if (s)
|
||||
wplua_pushboxed (L, WP_TYPE_SPA_JSON, g_steal_pointer (&s));
|
||||
else if (fb)
|
||||
wplua_pushboxed (L, WP_TYPE_SPA_JSON, g_steal_pointer (&fb));
|
||||
else
|
||||
lua_pushnil (L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
conf_get_value (lua_State *L)
|
||||
{
|
||||
g_autoptr (WpConf) conf = wp_core_get_conf (get_wp_core (L));
|
||||
const char *section;
|
||||
const char *key;
|
||||
g_autoptr (WpSpaJson) fb = NULL;
|
||||
g_autoptr (WpSpaJson) v = NULL;
|
||||
|
||||
if (!conf) {
|
||||
lua_pushnil (L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
section = luaL_checkstring (L, 1);
|
||||
key = luaL_checkstring (L, 2);
|
||||
if (lua_isuserdata (L, 3)) {
|
||||
WpSpaJson *v = wplua_checkboxed (L, 3, WP_TYPE_SPA_JSON);
|
||||
if (v)
|
||||
fb = wp_spa_json_ref (v);
|
||||
}
|
||||
|
||||
v = wp_conf_get_value (conf, section, key, g_steal_pointer (&fb));
|
||||
if (v)
|
||||
wplua_pushboxed (L, WP_TYPE_SPA_JSON, g_steal_pointer (&v));
|
||||
else
|
||||
lua_pushnil (L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
conf_get_value_boolean (lua_State *L)
|
||||
{
|
||||
g_autoptr (WpConf) conf = wp_core_get_conf (get_wp_core (L));
|
||||
const char *section;
|
||||
const char *key;
|
||||
gboolean fb;
|
||||
|
||||
if (!conf) {
|
||||
lua_pushnil (L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
section = luaL_checkstring (L, 1);
|
||||
key = luaL_checkstring (L, 2);
|
||||
fb = lua_toboolean (L, 3) ? TRUE : FALSE;
|
||||
|
||||
lua_pushboolean (L, wp_conf_get_value_boolean (conf, section, key, fb));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
conf_get_value_int (lua_State *L)
|
||||
{
|
||||
g_autoptr (WpConf) conf = wp_core_get_conf (get_wp_core (L));
|
||||
const char *section;
|
||||
const char *key;
|
||||
gint fb;
|
||||
|
||||
if (!conf) {
|
||||
lua_pushnil (L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
section = luaL_checkstring (L, 1);
|
||||
key = luaL_checkstring (L, 2);
|
||||
fb = luaL_checkinteger (L, 3);
|
||||
|
||||
lua_pushinteger (L, wp_conf_get_value_int (conf, section, key, fb));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
conf_get_value_float (lua_State *L)
|
||||
{
|
||||
g_autoptr (WpConf) conf = wp_core_get_conf (get_wp_core (L));
|
||||
const char *section;
|
||||
const char *key;
|
||||
float fb;
|
||||
|
||||
if (!conf) {
|
||||
lua_pushnil (L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
section = luaL_checkstring (L, 1);
|
||||
key = luaL_checkstring (L, 2);
|
||||
fb = lua_tonumber (L, 3);
|
||||
|
||||
lua_pushnumber (L, wp_conf_get_value_float (conf, section, key, fb));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
conf_get_value_string (lua_State *L)
|
||||
{
|
||||
g_autoptr (WpConf) conf = wp_core_get_conf (get_wp_core (L));
|
||||
const char *section;
|
||||
const char *key;
|
||||
const char *fb;
|
||||
g_autofree gchar *str = NULL;
|
||||
|
||||
if (!conf) {
|
||||
lua_pushnil (L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
section = luaL_checkstring (L, 1);
|
||||
key = luaL_checkstring (L, 2);
|
||||
fb = luaL_checkstring (L, 3);
|
||||
|
||||
str = wp_conf_get_value_string (conf, section, key, fb);
|
||||
lua_pushstring (L, str);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const luaL_Reg conf_methods[] = {
|
||||
{ "get_section", conf_get_section },
|
||||
{ "get_value", conf_get_value },
|
||||
{ "get_value_boolean", conf_get_value_boolean },
|
||||
{ "get_value_int", conf_get_value_int },
|
||||
{ "get_value_float", conf_get_value_float },
|
||||
{ "get_value_string", conf_get_value_string },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
|
@@ -89,7 +89,7 @@ load_configuration_settings (WpSettingsPlugin *self)
|
||||
conf = wp_core_get_conf (core);
|
||||
g_return_val_if_fail (conf, NULL);
|
||||
|
||||
json = wp_conf_get_section (conf, "wireplumber.settings", NULL);
|
||||
json = wp_conf_get_section (conf, "wireplumber.settings");
|
||||
if (!json)
|
||||
return g_steal_pointer (&res);
|
||||
|
||||
@@ -266,7 +266,7 @@ on_schema_metadata_activated (WpMetadata * m, GAsyncResult * res,
|
||||
}
|
||||
|
||||
/* Load the schema into metadata if any */
|
||||
schema_json = wp_conf_get_section (conf, "wireplumber.settings.schema", NULL);
|
||||
schema_json = wp_conf_get_section (conf, "wireplumber.settings.schema");
|
||||
if (schema_json) {
|
||||
g_autoptr (WpIterator) it = NULL;
|
||||
g_auto (GValue) item = G_VALUE_INIT;
|
||||
|
173
tests/wp/conf.c
173
tests/wp/conf.c
@@ -37,7 +37,7 @@ test_conf_basic (TestConfFixture *f, gconstpointer data)
|
||||
/* Boolean Array */
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
|
||||
"wireplumber.section.array.boolean", NULL);
|
||||
"wireplumber.section.array.boolean");
|
||||
g_assert_nonnull (s);
|
||||
g_assert_true (wp_spa_json_is_array (s));
|
||||
gboolean v1 = FALSE, v2 = TRUE;
|
||||
@@ -49,7 +49,7 @@ test_conf_basic (TestConfFixture *f, gconstpointer data)
|
||||
/* Int Array */
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
|
||||
"wireplumber.section.array.int", NULL);
|
||||
"wireplumber.section.array.int");
|
||||
g_assert_nonnull (s);
|
||||
g_assert_true (wp_spa_json_is_array (s));
|
||||
gint v1 = 0, v2 = 0, v3 = 0;
|
||||
@@ -63,7 +63,7 @@ test_conf_basic (TestConfFixture *f, gconstpointer data)
|
||||
/* Float Array */
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
|
||||
"wireplumber.section.array.float", NULL);
|
||||
"wireplumber.section.array.float");
|
||||
g_assert_nonnull (s);
|
||||
g_assert_true (wp_spa_json_is_array (s));
|
||||
float v1 = 0.0, v2 = 0.0, v3 = 0.0;
|
||||
@@ -77,7 +77,7 @@ test_conf_basic (TestConfFixture *f, gconstpointer data)
|
||||
/* String Array */
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
|
||||
"wireplumber.section.array.string", NULL);
|
||||
"wireplumber.section.array.string");
|
||||
g_assert_nonnull (s);
|
||||
g_assert_true (wp_spa_json_is_array (s));
|
||||
g_autofree gchar *v1 = NULL, *v2 = NULL;
|
||||
@@ -91,7 +91,7 @@ test_conf_basic (TestConfFixture *f, gconstpointer data)
|
||||
/* Array Array */
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
|
||||
"wireplumber.section.array.array", NULL);
|
||||
"wireplumber.section.array.array");
|
||||
g_assert_nonnull (s);
|
||||
g_assert_true (wp_spa_json_is_array (s));
|
||||
g_autoptr (WpSpaJson) v1 = NULL;
|
||||
@@ -111,7 +111,7 @@ test_conf_basic (TestConfFixture *f, gconstpointer data)
|
||||
/* Object Array */
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
|
||||
"wireplumber.section.array.object", NULL);
|
||||
"wireplumber.section.array.object");
|
||||
g_assert_nonnull (s);
|
||||
g_assert_true (wp_spa_json_is_array (s));
|
||||
g_autoptr (WpSpaJson) v1 = NULL;
|
||||
@@ -132,7 +132,7 @@ test_conf_basic (TestConfFixture *f, gconstpointer data)
|
||||
/* Object */
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
|
||||
"wireplumber.section.object", NULL);
|
||||
"wireplumber.section.object");
|
||||
g_assert_nonnull (s);
|
||||
g_assert_true (wp_spa_json_is_object (s));
|
||||
gboolean v1 = FALSE;
|
||||
@@ -165,54 +165,6 @@ test_conf_basic (TestConfFixture *f, gconstpointer data)
|
||||
NULL));
|
||||
g_assert_false (v9);
|
||||
}
|
||||
|
||||
/* Fallback */
|
||||
{
|
||||
g_autoptr (WpSpaJson) fallback = wp_spa_json_new_wrap_string ("{key1 = 3");
|
||||
g_assert_nonnull (fallback);
|
||||
|
||||
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
|
||||
"wireplumber.section.object", wp_spa_json_ref (fallback));
|
||||
g_assert_nonnull (s);
|
||||
g_assert_true (wp_spa_json_is_object (s));
|
||||
gboolean v1 = FALSE;
|
||||
gint v2 = 0;
|
||||
float v3 = 0.0;
|
||||
g_autofree gchar *v4 = NULL;
|
||||
g_autoptr (WpSpaJson) v5 = NULL;
|
||||
g_autoptr (WpSpaJson) v6 = NULL;
|
||||
g_assert_true (wp_spa_json_object_get (s,
|
||||
"key.boolean", "b", &v1,
|
||||
"key.int", "i", &v2,
|
||||
"key.float", "f", &v3,
|
||||
"key.string", "s", &v4,
|
||||
"key.array", "J", &v5,
|
||||
"key.object", "J", &v6,
|
||||
NULL));
|
||||
g_assert_true (v1);
|
||||
g_assert_cmpint (v2, ==, -1);
|
||||
g_assert_cmpfloat_with_epsilon (v3, 3.14, 0.001);
|
||||
g_assert_cmpstr (v4, ==, "wireplumber");
|
||||
g_assert_true (wp_spa_json_is_array (v5));
|
||||
g_autofree gchar *v7 = NULL, *v8 = NULL;
|
||||
g_assert_true (wp_spa_json_parse_array (v5, "s", &v7, "s", &v8, NULL));
|
||||
g_assert_cmpstr (v7, ==, "an");
|
||||
g_assert_cmpstr (v8, ==, "array");
|
||||
g_assert_true (wp_spa_json_is_object (v6));
|
||||
gboolean v9 = TRUE;
|
||||
g_assert_true (wp_spa_json_object_get (v6,
|
||||
"key.nested.boolean", "b", &v9,
|
||||
NULL));
|
||||
g_assert_false (v9);
|
||||
|
||||
g_autoptr (WpSpaJson) s2 = wp_conf_get_section (f->conf,
|
||||
"invalid-section", wp_spa_json_ref (fallback));
|
||||
g_assert_nonnull (s2);
|
||||
g_assert_true (wp_spa_json_is_object (s2));
|
||||
gint v = 0;
|
||||
g_assert_true (wp_spa_json_object_get (s2, "key1", "i", &v, NULL));
|
||||
g_assert_cmpint (v, ==, 3);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -223,7 +175,7 @@ test_conf_merge (TestConfFixture *f, gconstpointer data)
|
||||
/* Boolean Array */
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
|
||||
"wireplumber.section-merged.array.boolean", NULL);
|
||||
"wireplumber.section-merged.array.boolean");
|
||||
g_assert_nonnull (s);
|
||||
g_assert_true (wp_spa_json_is_array (s));
|
||||
gboolean v1 = TRUE, v2 = FALSE;
|
||||
@@ -235,7 +187,7 @@ test_conf_merge (TestConfFixture *f, gconstpointer data)
|
||||
/* Int Array */
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
|
||||
"wireplumber.section-merged.array.int", NULL);
|
||||
"wireplumber.section-merged.array.int");
|
||||
g_assert_nonnull (s);
|
||||
g_assert_true (wp_spa_json_is_array (s));
|
||||
gint v1 = 0, v2 = 0;
|
||||
@@ -247,7 +199,7 @@ test_conf_merge (TestConfFixture *f, gconstpointer data)
|
||||
/* Float Array */
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
|
||||
"wireplumber.section-merged.array.float", NULL);
|
||||
"wireplumber.section-merged.array.float");
|
||||
g_assert_nonnull (s);
|
||||
g_assert_true (wp_spa_json_is_array (s));
|
||||
float v1 = 0.0, v2 = 0.0;
|
||||
@@ -259,7 +211,7 @@ test_conf_merge (TestConfFixture *f, gconstpointer data)
|
||||
/* String Array */
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
|
||||
"wireplumber.section-merged.array.string", NULL);
|
||||
"wireplumber.section-merged.array.string");
|
||||
g_assert_nonnull (s);
|
||||
g_assert_true (wp_spa_json_is_array (s));
|
||||
g_autofree gchar *v1 = NULL, *v2 = NULL;
|
||||
@@ -273,7 +225,7 @@ test_conf_merge (TestConfFixture *f, gconstpointer data)
|
||||
/* Array Array */
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
|
||||
"wireplumber.section-merged.array.array", NULL);
|
||||
"wireplumber.section-merged.array.array");
|
||||
g_assert_nonnull (s);
|
||||
g_assert_true (wp_spa_json_is_array (s));
|
||||
g_autoptr (WpSpaJson) v1 = NULL;
|
||||
@@ -293,7 +245,7 @@ test_conf_merge (TestConfFixture *f, gconstpointer data)
|
||||
/* Object Array */
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
|
||||
"wireplumber.section-merged.array.object", NULL);
|
||||
"wireplumber.section-merged.array.object");
|
||||
g_assert_nonnull (s);
|
||||
g_assert_true (wp_spa_json_is_array (s));
|
||||
g_autoptr (WpSpaJson) v1 = NULL;
|
||||
@@ -314,7 +266,7 @@ test_conf_merge (TestConfFixture *f, gconstpointer data)
|
||||
/* Object */
|
||||
{
|
||||
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
|
||||
"wireplumber.section-merged.object", NULL);
|
||||
"wireplumber.section-merged.object");
|
||||
g_assert_nonnull (s);
|
||||
g_assert_true (wp_spa_json_is_object (s));
|
||||
gboolean v1 = FALSE;
|
||||
@@ -355,7 +307,7 @@ test_conf_merge_nested (TestConfFixture *f, gconstpointer data)
|
||||
g_assert_nonnull (f->conf);
|
||||
|
||||
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
|
||||
"wireplumber.section-nested-merged", NULL);
|
||||
"wireplumber.section-nested-merged");
|
||||
g_assert_nonnull (s);
|
||||
g_assert_true (wp_spa_json_is_object (s));
|
||||
|
||||
@@ -394,7 +346,7 @@ test_conf_override (TestConfFixture *f, gconstpointer data)
|
||||
g_assert_nonnull (f->conf);
|
||||
|
||||
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
|
||||
"wireplumber.section-override", NULL);
|
||||
"wireplumber.section-override");
|
||||
g_assert_nonnull (s);
|
||||
g_assert_true (wp_spa_json_is_object (s));
|
||||
|
||||
@@ -414,7 +366,7 @@ test_conf_override_nested (TestConfFixture *f, gconstpointer data)
|
||||
g_assert_nonnull (f->conf);
|
||||
|
||||
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
|
||||
"wireplumber.section-nested-override", NULL);
|
||||
"wireplumber.section-nested-override");
|
||||
g_assert_nonnull (s);
|
||||
g_assert_true (wp_spa_json_is_object (s));
|
||||
|
||||
@@ -433,95 +385,6 @@ test_conf_override_nested (TestConfFixture *f, gconstpointer data)
|
||||
g_assert_cmpint (v3, ==, 3);
|
||||
}
|
||||
|
||||
static void
|
||||
test_conf_get_value (TestConfFixture *f, gconstpointer data)
|
||||
{
|
||||
g_assert_nonnull (f->conf);
|
||||
|
||||
/* Value */
|
||||
{
|
||||
g_autoptr (WpSpaJson) fallback = wp_spa_json_new_int (8);
|
||||
g_assert_nonnull (fallback);
|
||||
|
||||
g_autoptr (WpSpaJson) v1 = wp_conf_get_value (f->conf,
|
||||
"wireplumber.section.object", "key.int", wp_spa_json_ref (fallback));
|
||||
g_assert_nonnull (v1);
|
||||
gint v1_int = 0;
|
||||
g_assert_true (wp_spa_json_parse_int (v1, &v1_int));
|
||||
g_assert_cmpint (v1_int, ==, -1);
|
||||
|
||||
g_autoptr (WpSpaJson) v2 = wp_conf_get_value (f->conf,
|
||||
"wireplumber.section.object", "unavailable", wp_spa_json_ref (fallback));
|
||||
g_assert_nonnull (v2);
|
||||
gint v2_int = 0;
|
||||
g_assert_true (wp_spa_json_parse_int (v2, &v2_int));
|
||||
g_assert_cmpint (v2_int, ==, 8);
|
||||
|
||||
g_autoptr (WpSpaJson) v3 = wp_conf_get_value (f->conf,
|
||||
"wireplumber.section.object", "key.int", NULL);
|
||||
g_assert_nonnull (v3);
|
||||
gint v3_int = 0;
|
||||
g_assert_true (wp_spa_json_parse_int (v3, &v3_int));
|
||||
g_assert_cmpint (v3_int, ==, -1);
|
||||
|
||||
g_autoptr (WpSpaJson) v4 = wp_conf_get_value (f->conf,
|
||||
"wireplumber.section.object", "unavailable", NULL);
|
||||
g_assert_null (v4);
|
||||
}
|
||||
|
||||
/* Boolean */
|
||||
{
|
||||
gboolean v1 = wp_conf_get_value_boolean (f->conf,
|
||||
"wireplumber.section.object", "key.boolean", FALSE);
|
||||
g_assert_true (v1);
|
||||
|
||||
gboolean v2 = wp_conf_get_value_boolean (f->conf,
|
||||
"wireplumber.section.object", "unavailable", TRUE);
|
||||
g_assert_true (v2);
|
||||
}
|
||||
|
||||
/* Int */
|
||||
{
|
||||
gint v1 = wp_conf_get_value_int (f->conf,
|
||||
"wireplumber.section.object", "key.int", 4);
|
||||
g_assert_cmpint (v1, ==, -1);
|
||||
|
||||
gint v2 = wp_conf_get_value_int (f->conf,
|
||||
"wireplumber.section.object", "unavailable", 4);
|
||||
g_assert_cmpint (v2, ==, 4);
|
||||
}
|
||||
|
||||
/* Float */
|
||||
{
|
||||
float v1 = wp_conf_get_value_float (f->conf,
|
||||
"wireplumber.section.object", "key.float", 9.99);
|
||||
g_assert_cmpfloat_with_epsilon (v1, 3.14, 0.001);
|
||||
|
||||
float v2 = wp_conf_get_value_float (f->conf,
|
||||
"wireplumber.section.object", "unavailable", 9.99);
|
||||
g_assert_cmpfloat_with_epsilon (v2, 9.99, 0.001);
|
||||
}
|
||||
|
||||
/* String */
|
||||
{
|
||||
g_autofree gchar *v1 = wp_conf_get_value_string (f->conf,
|
||||
"wireplumber.section.object", "key.string", "fallback");
|
||||
g_assert_cmpstr (v1, ==, "wireplumber");
|
||||
|
||||
g_autofree gchar *v2 = wp_conf_get_value_string (f->conf,
|
||||
"wireplumber.section.object", "unavailable", "fallback");
|
||||
g_assert_cmpstr (v2, ==, "fallback");
|
||||
|
||||
g_autofree gchar *v3 = wp_conf_get_value_string (f->conf,
|
||||
"wireplumber.section.object", "key.string", NULL);
|
||||
g_assert_cmpstr (v3, ==, "wireplumber");
|
||||
|
||||
g_autofree gchar *v4 = wp_conf_get_value_string (f->conf,
|
||||
"wireplumber.section.object", "unavailable", NULL);
|
||||
g_assert_null (v4);
|
||||
}
|
||||
}
|
||||
|
||||
gint
|
||||
main (gint argc, gchar *argv[])
|
||||
{
|
||||
@@ -538,8 +401,6 @@ main (gint argc, gchar *argv[])
|
||||
test_conf_setup, test_conf_override, test_conf_teardown);
|
||||
g_test_add ("/wp/conf/override_nested", TestConfFixture, NULL,
|
||||
test_conf_setup, test_conf_override_nested, test_conf_teardown);
|
||||
g_test_add ("/wp/conf/get_value", TestConfFixture, NULL,
|
||||
test_conf_setup, test_conf_get_value, test_conf_teardown);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
||||
|
@@ -78,7 +78,7 @@ test_parsing_setup (TestSettingsFixture *self, gconstpointer user_data)
|
||||
|
||||
{
|
||||
g_autoptr (WpSpaJson) json = wp_conf_get_section (conf,
|
||||
"wireplumber.settings", NULL);
|
||||
"wireplumber.settings");
|
||||
g_assert_nonnull (json);
|
||||
self->loaded_settings = do_parse_section (json);
|
||||
g_assert_nonnull (self->loaded_settings);
|
||||
@@ -86,7 +86,7 @@ test_parsing_setup (TestSettingsFixture *self, gconstpointer user_data)
|
||||
|
||||
{
|
||||
g_autoptr (WpSpaJson) json = wp_conf_get_section (conf,
|
||||
"wireplumber.settings.schema", NULL);
|
||||
"wireplumber.settings.schema");
|
||||
self->loaded_schema = do_parse_section (json);
|
||||
g_assert_nonnull (self->loaded_schema);
|
||||
}
|
||||
|
Reference in New Issue
Block a user