conf: allow a SPA-JSON container to be loaded as a named WpConf section

A "plain" JSON object may be passed to WpConf constructors. Previously, this
would cause an error as open_and_load_sections expects the first parsed token
to be a string. Use WpProperties to denote that the file being parsed is an
object/container, and specify a section name for it.

Signed-off-by: James Calligeros <jcalligeros99@gmail.com>
This commit is contained in:
James Calligeros
2024-03-30 16:26:29 +10:00
parent 34040d8e44
commit 59d29f37ac

View File

@@ -201,6 +201,13 @@ detect_old_conf_format (WpConf * self, GMappedFile *file)
static gboolean static gboolean
open_and_load_sections (WpConf * self, const gchar *path, GError ** error) open_and_load_sections (WpConf * self, const gchar *path, GError ** error)
{ {
const gchar *as_section = NULL;
if (self->properties) {
as_section = wp_properties_get (self->properties, "as-section");
wp_info_object (self, "Opening as section %s", as_section);
}
g_autoptr (GMappedFile) file = g_mapped_file_new (path, FALSE, error); g_autoptr (GMappedFile) file = g_mapped_file_new (path, FALSE, error);
if (!file) if (!file)
return FALSE; return FALSE;
@@ -229,7 +236,7 @@ open_and_load_sections (WpConf * self, const gchar *path, GError ** error)
if (!tmp) if (!tmp)
break; break;
if (wp_spa_json_is_container (tmp) || if ((wp_spa_json_is_container (tmp) && !as_section) ||
wp_spa_json_is_int (tmp) || wp_spa_json_is_int (tmp) ||
wp_spa_json_is_float (tmp) || wp_spa_json_is_float (tmp) ||
wp_spa_json_is_boolean (tmp) || wp_spa_json_is_boolean (tmp) ||
@@ -241,9 +248,12 @@ open_and_load_sections (WpConf * self, const gchar *path, GError ** error)
return FALSE; return FALSE;
} }
if (as_section) {
wp_info_object (self, "Parsing object file as section %s", as_section);
section.name = g_strdup (as_section);
} else {
section.name = wp_spa_json_parse_string (tmp); section.name = wp_spa_json_parse_string (tmp);
g_clear_pointer (&tmp, wp_spa_json_unref); g_clear_pointer (&tmp, wp_spa_json_unref);
/* parse the section contents */ /* parse the section contents */
tmp = wp_spa_json_parser_get_json (parser); tmp = wp_spa_json_parser_get_json (parser);
if (!tmp) { if (!tmp) {
@@ -251,6 +261,7 @@ open_and_load_sections (WpConf * self, const gchar *path, GError ** error)
"section '%s' has no value", section.name); "section '%s' has no value", section.name);
return FALSE; return FALSE;
} }
}
section.value = g_steal_pointer (&tmp); section.value = g_steal_pointer (&tmp);
section.location = g_strdup (path); section.location = g_strdup (path);