Skip to content

Commit 3a7a000

Browse files
committed
Add modulemd-validator for v2 API
Relegate the v1 implementation to the name modulemd-validator-v1 Signed-off-by: Stephen Gallagher <[email protected]>
1 parent 37c907d commit 3a7a000

File tree

7 files changed

+308
-16
lines changed

7 files changed

+308
-16
lines changed

libmodulemd.spec.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ ln -s libmodulemd.so.%{libmodulemd_v1_version} \
128128
%files
129129
%license COPYING
130130
%doc README.md
131+
%{_bindir}/modulemd-validator
131132
%{_libdir}/%{name}.so.2*
132133
%dir %{_libdir}/girepository-1.0
133134
%{_libdir}/girepository-1.0/Modulemd-2.0.typelib
@@ -154,7 +155,7 @@ ln -s libmodulemd.so.%{libmodulemd_v1_version} \
154155
%files -n libmodulemd1
155156
%license COPYING
156157
%doc README.md
157-
%{_bindir}/modulemd-validator
158+
%{_bindir}/modulemd-validator-v1
158159
%{_libdir}/%{name}.so.1*
159160
%dir %{_libdir}/girepository-1.0
160161
%{_libdir}/girepository-1.0/Modulemd-1.0.typelib

modulemd/meson.build

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ modulemd_v2_srcs = files(
140140
'v2/modulemd-yaml-util.c',
141141
)
142142

143+
modulemd_v2_validator_srcs = files (
144+
'v2/modulemd-validator.c',
145+
)
146+
143147
modulemd_v2_hdrs = files(
144148
'v2/include/modulemd-2.0/modulemd.h',
145149
'v2/include/modulemd-2.0/modulemd-buildopts.h',
@@ -239,6 +243,7 @@ test('clang_format', clang_format,
239243
modulemd_v2_srcs +
240244
modulemd_v2_hdrs +
241245
modulemd_v2_priv_hdrs +
246+
modulemd_v2_validator_srcs +
242247
test_v2_srcs +
243248
test_v2_priv_hdrs)
244249

modulemd/v1/meson.build

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ else
5555
endif
5656

5757

58-
modulemd_validator = executable(
59-
'modulemd-validator',
58+
modulemd_validator_v1 = executable(
59+
'modulemd-validator-v1',
6060
'modulemd-validator.c',
6161
dependencies : [
6262
modulemd_v1_dep,

modulemd/v2/include/modulemd-2.0/private/modulemd-module-index-private.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#pragma once
1515

1616
#include <glib-object.h>
17+
#include <yaml.h>
1718
#include "modulemd-module-index.h"
1819

1920
G_BEGIN_DECLS
@@ -27,6 +28,40 @@ G_BEGIN_DECLS
2728
*/
2829

2930

31+
/**
32+
* modulemd_module_index_update_from_parser:
33+
* @self: (in): This #ModulemdModuleIndex object
34+
* @parser: (inout): An initialized YAML parser that has not yet processed any
35+
* events.
36+
* @strict: (in): Whether the parser should return failure if it encounters an
37+
* unknown mapping key or if it should ignore it.
38+
* @autogen_module_name: (in): When parsing a module stream that contains no
39+
* module name or stream name, whether to autogenerate one or not. This option
40+
* should be used only for validation tools such as modulemd-validator. Normal
41+
* public routines should always set this to FALSE.
42+
* @failures: (out) (element-type ModulemdSubdocumentInfo) (transfer container):
43+
* An array containing any subdocuments from the YAML file that failed to parse.
44+
* See #ModulemdSubdocumentInfo for more details. If the array is NULL, it will
45+
* be allocated by this function. If it is non-NULL, this function will append
46+
* to it.
47+
* @error: (out): A GError containing additional information if this function
48+
* fails in a way that prevents program continuation.
49+
*
50+
* Returns: TRUE if the update was successful. Returns FALSE and sets failures
51+
* approriately if any of the YAML subdocuments were invalid or sets @error if
52+
* there was a fatal parse error.
53+
*
54+
* Since: 2.0
55+
*/
56+
gboolean
57+
modulemd_module_index_update_from_parser (ModulemdModuleIndex *self,
58+
yaml_parser_t *parser,
59+
gboolean strict,
60+
gboolean autogen_module_name,
61+
GPtrArray **failures,
62+
GError **error);
63+
64+
3065
/**
3166
* modulemd_module_index_merge:
3267
* @from: (in) (transfer none): The #ModulemdModuleIndex whose contents are

modulemd/v2/meson.build

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ else
6666
)
6767
endif
6868

69+
modulemd_v2_validator = executable(
70+
'modulemd-validator',
71+
sources : modulemd_v2_validator_srcs,
72+
include_directories : v2_include_dirs,
73+
dependencies : [
74+
gobject,
75+
yaml,
76+
modulemd_v2_dep
77+
],
78+
install : true
79+
)
80+
6981
v2_header_path = 'modulemd-2.0'
7082

7183
install_headers(

modulemd/v2/modulemd-module-index.c

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "private/modulemd-defaults-v1-private.h"
2424
#include "private/modulemd-subdocument-info-private.h"
2525
#include "private/modulemd-module-index-private.h"
26+
#include "private/modulemd-module-stream-private.h"
2627
#include "private/modulemd-module-stream-v1-private.h"
2728
#include "private/modulemd-module-stream-v2-private.h"
2829
#include "private/modulemd-translation-private.h"
@@ -121,11 +122,13 @@ static gboolean
121122
add_subdoc (ModulemdModuleIndex *self,
122123
ModulemdSubdocumentInfo *subdoc,
123124
gboolean strict,
125+
gboolean autogen_module_name,
124126
GError **error)
125127
{
126128
g_autoptr (ModulemdModuleStream) stream = NULL;
127129
g_autoptr (ModulemdTranslation) translation = NULL;
128130
g_autoptr (ModulemdDefaults) defaults = NULL;
131+
g_autofree gchar *name = NULL;
129132

130133
switch (modulemd_subdocument_info_get_doctype (subdoc))
131134
{
@@ -135,20 +138,12 @@ add_subdoc (ModulemdModuleIndex *self,
135138
case MD_MODULESTREAM_VERSION_ONE:
136139
stream = MODULEMD_MODULE_STREAM (
137140
modulemd_module_stream_v1_parse_yaml (subdoc, strict, error));
138-
if (stream == NULL)
139-
return FALSE;
140-
if (!modulemd_module_index_add_module_stream (self, stream, error))
141-
return FALSE;
142141
break;
143142

144143
case MD_MODULESTREAM_VERSION_TWO:
145144
stream =
146145
(ModulemdModuleStream *)modulemd_module_stream_v2_parse_yaml (
147146
subdoc, strict, error);
148-
if (stream == NULL)
149-
return FALSE;
150-
if (!modulemd_module_index_add_module_stream (self, stream, error))
151-
return FALSE;
152147
break;
153148

154149
default:
@@ -158,6 +153,32 @@ add_subdoc (ModulemdModuleIndex *self,
158153
"Invalid mdversion for a stream object");
159154
return FALSE;
160155
}
156+
157+
if (stream == NULL)
158+
return FALSE;
159+
160+
if (autogen_module_name &&
161+
!modulemd_module_stream_get_module_name (stream))
162+
{
163+
name = g_strdup_printf ("__unnamed_module_%d",
164+
g_hash_table_size (self->modules) + 1);
165+
modulemd_module_stream_set_module_name (stream, name);
166+
g_clear_pointer (&name, g_free);
167+
}
168+
169+
if (autogen_module_name &&
170+
!modulemd_module_stream_get_stream_name (stream))
171+
{
172+
name = g_strdup_printf ("__unnamed_stream_%d",
173+
g_hash_table_size (self->modules) + 1);
174+
modulemd_module_stream_set_stream_name (stream, name);
175+
g_clear_pointer (&name, g_free);
176+
}
177+
178+
179+
if (!modulemd_module_index_add_module_stream (self, stream, error))
180+
return FALSE;
181+
161182
break;
162183

163184
case MODULEMD_YAML_DOC_DEFAULTS:
@@ -201,10 +222,11 @@ add_subdoc (ModulemdModuleIndex *self,
201222
}
202223

203224

204-
static gboolean
225+
gboolean
205226
modulemd_module_index_update_from_parser (ModulemdModuleIndex *self,
206227
yaml_parser_t *parser,
207228
gboolean strict,
229+
gboolean autogen_module_name,
208230
GPtrArray **failures,
209231
GError **error)
210232
{
@@ -213,6 +235,9 @@ modulemd_module_index_update_from_parser (ModulemdModuleIndex *self,
213235
g_autoptr (ModulemdSubdocumentInfo) subdoc = NULL;
214236
MMD_INIT_YAML_EVENT (event);
215237

238+
if (*failures == NULL)
239+
*failures = g_ptr_array_new_with_free_func (g_object_unref);
240+
216241
YAML_PARSER_PARSE_WITH_EXIT_BOOL (parser, &event, error);
217242
if (event.type != YAML_STREAM_START_EVENT)
218243
MMD_YAML_ERROR_EVENT_EXIT_BOOL (
@@ -236,16 +261,17 @@ modulemd_module_index_update_from_parser (ModulemdModuleIndex *self,
236261
else
237262
{
238263
/* Initial parsing worked, parse further */
239-
if (!add_subdoc (self, subdoc, strict, error))
264+
if (!add_subdoc (
265+
self, subdoc, strict, autogen_module_name, error))
240266
{
241267
modulemd_subdocument_info_set_gerror (subdoc, *error);
242268
g_clear_pointer (error, g_error_free);
243269
/* Add to failures and ignore */
244270
g_ptr_array_add (*failures, g_steal_pointer (&subdoc));
245271
all_passed = FALSE;
246272
}
247-
g_clear_pointer (&subdoc, g_object_unref);
248273
}
274+
g_clear_pointer (&subdoc, g_object_unref);
249275
break;
250276

251277
case YAML_STREAM_END_EVENT: done = TRUE; break;
@@ -479,7 +505,7 @@ modulemd_module_index_update_from_string (ModulemdModuleIndex *self,
479505
&parser, (const unsigned char *)yaml_string, strlen (yaml_string));
480506

481507
return modulemd_module_index_update_from_parser (
482-
self, &parser, strict, failures, error);
508+
self, &parser, strict, FALSE, failures, error);
483509
}
484510

485511

@@ -507,7 +533,7 @@ modulemd_module_index_update_from_stream (ModulemdModuleIndex *self,
507533
yaml_parser_set_input_file (&parser, yaml_stream);
508534

509535
return modulemd_module_index_update_from_parser (
510-
self, &parser, strict, failures, error);
536+
self, &parser, strict, FALSE, failures, error);
511537
}
512538

513539

0 commit comments

Comments
 (0)