Skip to content

Commit cb8d47e

Browse files
committed
Auto-quote all values in modulemd-defaults-v1 documents
This patch enhances serialization of modulemd-defaults-v1 with respect to number-like values. Now these fields are quoted when necessary: module name, profile name, intent name. That means all fileds of modulemd-defaults-v1 are guarded if necessary. Default stream name was left always quoted not to disturb previous serializations. We can turn it into autoquoting when requested.
1 parent 3002371 commit cb8d47e

File tree

3 files changed

+91
-10
lines changed

3 files changed

+91
-10
lines changed

modulemd/modulemd-defaults-v1.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,10 +1015,9 @@ modulemd_defaults_v1_emit_yaml (ModulemdDefaultsV1 *self,
10151015
return FALSE;
10161016
}
10171017

1018-
if (!mmd_emitter_scalar (
1018+
if (!mmd_emitter_scalar_string (
10191019
emitter,
10201020
modulemd_defaults_get_module_name (MODULEMD_DEFAULTS (self)),
1021-
YAML_PLAIN_SCALAR_STYLE,
10221021
error))
10231022
{
10241023
return FALSE;
@@ -1092,7 +1091,7 @@ modulemd_defaults_v1_emit_profiles (GHashTable *profile_table,
10921091
GError **error)
10931092
{
10941093
g_autoptr (GPtrArray) stream_names = NULL;
1095-
g_auto (GStrv) streams = NULL;
1094+
g_auto (GStrv) profiles = NULL;
10961095
gchar *stream_name = NULL;
10971096
GHashTable *profile_set = NULL;
10981097

@@ -1129,13 +1128,13 @@ modulemd_defaults_v1_emit_profiles (GHashTable *profile_table,
11291128
return FALSE;
11301129
}
11311130

1132-
streams = modulemd_ordered_str_keys_as_strv (profile_set);
1131+
profiles = modulemd_ordered_str_keys_as_strv (profile_set);
11331132
if (!mmd_emitter_strv (
1134-
emitter, YAML_FLOW_SEQUENCE_STYLE, streams, error))
1133+
emitter, YAML_FLOW_SEQUENCE_STYLE, profiles, error))
11351134
{
11361135
return FALSE;
11371136
}
1138-
g_clear_pointer (&streams, g_strfreev);
1137+
g_clear_pointer (&profiles, g_strfreev);
11391138
}
11401139

11411140
/* End the mapping for "profiles:" */
@@ -1200,8 +1199,7 @@ modulemd_defaults_v1_emit_intents (ModulemdDefaultsV1 *self,
12001199
intent = g_ptr_array_index (intents, i);
12011200

12021201
/* Emit the intent name */
1203-
if (!mmd_emitter_scalar (
1204-
emitter, intent, YAML_PLAIN_SCALAR_STYLE, error))
1202+
if (!mmd_emitter_scalar_string (emitter, intent, error))
12051203
{
12061204
return FALSE;
12071205
}

modulemd/modulemd-yaml-util.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,12 @@ mmd_emitter_strv (yaml_emitter_t *emitter,
369369

370370
for (int i = 0; i < numentries; i++)
371371
{
372-
ret = mmd_emitter_scalar (
373-
emitter, list[i], YAML_PLAIN_SCALAR_STYLE, &nested_error);
372+
ret = mmd_emitter_scalar (emitter,
373+
list[i],
374+
string_is_empty_or_a_number (list[i]) ?
375+
YAML_DOUBLE_QUOTED_SCALAR_STYLE :
376+
YAML_PLAIN_SCALAR_STYLE,
377+
&nested_error);
374378
if (!ret)
375379
{
376380
g_propagate_prefixed_error (

modulemd/tests/test-modulemd-defaults-v1.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,82 @@ defaults_test_emit_yaml (void)
733733
}
734734

735735

736+
static void
737+
defaults_test_quoting (void)
738+
{
739+
MMD_INIT_YAML_EMITTER (emitter);
740+
MMD_INIT_YAML_EVENT (event);
741+
MMD_INIT_YAML_STRING (&emitter, yaml_string);
742+
g_autoptr (GError) error = NULL;
743+
g_autoptr (ModulemdDefaultsV1) defaults = NULL;
744+
745+
/* Module name which does not need quoting. */
746+
defaults = modulemd_defaults_v1_new ("unquoted");
747+
modulemd_defaults_v1_set_default_stream (defaults, "alwaysquoted", NULL);
748+
749+
g_assert_true (mmd_emitter_start_stream (&emitter, &error));
750+
g_assert_true (modulemd_defaults_v1_emit_yaml (defaults, &emitter, &error));
751+
g_assert_true (mmd_emitter_end_stream (&emitter, &error));
752+
g_assert_nonnull (yaml_string->str);
753+
g_assert_cmpstr (yaml_string->str,
754+
==,
755+
"---\n"
756+
"document: modulemd-defaults\n"
757+
"version: 1\n"
758+
"data:\n"
759+
" module: unquoted\n"
760+
" stream: \"alwaysquoted\"\n"
761+
"...\n");
762+
763+
g_clear_object (&defaults);
764+
765+
/* A module name which needs quoting. */
766+
defaults = modulemd_defaults_v1_new ("1.0");
767+
modulemd_defaults_v1_set_default_stream (defaults, "alwaysquoted", NULL);
768+
modulemd_defaults_v1_set_empty_default_profiles_for_stream (
769+
defaults, "unquoted", NULL);
770+
modulemd_defaults_v1_add_default_profile_for_stream (
771+
defaults, "2.0", "unquoted", NULL);
772+
modulemd_defaults_v1_add_default_profile_for_stream (
773+
defaults, "2.0", "3.0", NULL);
774+
modulemd_defaults_v1_set_default_stream (
775+
defaults, "alwaysquoted", "unquoted");
776+
modulemd_defaults_v1_set_empty_default_profiles_for_stream (
777+
defaults, "unquoted", "unquoted");
778+
modulemd_defaults_v1_add_default_profile_for_stream (
779+
defaults, "2.0", "unquoted", "unquoted");
780+
modulemd_defaults_v1_add_default_profile_for_stream (
781+
defaults, "2.0", "3.0", "unquoted");
782+
modulemd_defaults_v1_set_default_stream (defaults, "alwaysquoted", "4.0");
783+
784+
MMD_REINIT_YAML_STRING (&emitter, yaml_string);
785+
g_assert_true (mmd_emitter_start_stream (&emitter, &error));
786+
g_assert_true (modulemd_defaults_v1_emit_yaml (defaults, &emitter, &error));
787+
g_assert_true (mmd_emitter_end_stream (&emitter, &error));
788+
g_assert_nonnull (yaml_string->str);
789+
g_assert_cmpstr (yaml_string->str,
790+
==,
791+
"---\n"
792+
"document: modulemd-defaults\n"
793+
"version: 1\n"
794+
"data:\n"
795+
" module: \"1.0\"\n"
796+
" stream: \"alwaysquoted\"\n"
797+
" profiles:\n"
798+
" \"2.0\": [\"3.0\", unquoted]\n"
799+
" unquoted: []\n"
800+
" intents:\n"
801+
" \"4.0\":\n"
802+
" stream: \"alwaysquoted\"\n"
803+
" unquoted:\n"
804+
" stream: \"alwaysquoted\"\n"
805+
" profiles:\n"
806+
" \"2.0\": [\"3.0\", unquoted]\n"
807+
" unquoted: []\n"
808+
"...\n");
809+
}
810+
811+
736812
int
737813
main (int argc, char *argv[])
738814
{
@@ -764,5 +840,8 @@ main (int argc, char *argv[])
764840
g_test_add_func ("/modulemd/v2/defaults/v1/yaml/emit",
765841
defaults_test_emit_yaml);
766842

843+
g_test_add_func ("/modulemd/v2/defaults/v1/yaml/quoting",
844+
defaults_test_quoting);
845+
767846
return g_test_run ();
768847
}

0 commit comments

Comments
 (0)