conf: further improve how top-level objects are handled

With this change, it is possible to have a top-level object that does
not start at exactly the beginning of the file, allowing comments and
whitespace to exist.

Also add an empty conf file in the tests to verify that
it does not crash.
This commit is contained in:
George Kiagiadakis 2024-06-02 15:47:09 +03:00
parent 5235c025fe
commit be9259d952
3 changed files with 19 additions and 22 deletions

View file

@ -251,34 +251,26 @@ open_and_load_sections (WpConf * self, const gchar *path, GError ** error)
g_array_append_val (sections, section);
}
else {
/* if !is_object && !is_string, but we can't test it this way because
is_string expects quotes and we want to support strings without quotes */
if (wp_spa_json_is_array (json) ||
wp_spa_json_is_int (json) ||
wp_spa_json_is_float (json) ||
wp_spa_json_is_boolean (json) ||
wp_spa_json_is_null (json))
{
g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVALID_ARGUMENT,
"invalid config file start (expected an object or a section name): %.*s",
(int) wp_spa_json_get_size (json), wp_spa_json_get_data (json));
return FALSE;
g_auto (WpConfSection) section = { 0, };
g_autoptr (WpSpaJson) tmp = NULL, toplevel = NULL;
g_autoptr (WpSpaJsonParser) parser = wp_spa_json_parser_new_undefined (json);
/* get the very first token */
tmp = wp_spa_json_parser_get_json (parser);
/* if the top-level token is an object, parse that instead */
if (tmp && wp_spa_json_is_object (tmp)) {
g_clear_pointer (&parser, wp_spa_json_parser_unref);
toplevel = g_steal_pointer (&tmp);
parser = wp_spa_json_parser_new_object (toplevel);
tmp = wp_spa_json_parser_get_json (parser);
}
g_autoptr (WpSpaJsonParser) parser = wp_spa_json_is_object (json) ?
wp_spa_json_parser_new_object (json) :
wp_spa_json_parser_new_undefined (json);
while (TRUE) {
g_auto (WpConfSection) section = { 0, };
g_autoptr (WpSpaJson) tmp = NULL;
/* parse the section name */
tmp = wp_spa_json_parser_get_json (parser);
if (!tmp)
break;
/* if !is_string, but we want to supprt strings without quotes */
/* if !is_string, but we want to support strings without quotes */
if (wp_spa_json_is_container (tmp) ||
wp_spa_json_is_int (tmp) ||
wp_spa_json_is_float (tmp) ||
@ -306,6 +298,9 @@ open_and_load_sections (WpConf * self, const gchar *path, GError ** error)
section.location = g_strdup (path);
g_array_append_val (sections, section);
memset (&section, 0, sizeof (section));
/* parse the next section name */
tmp = wp_spa_json_parser_get_json (parser);
}
}

View file

@ -1,3 +1,5 @@
# some comment
{
"override.wireplumber.section-override": {
"key2": 5