Skip to content

Commit

Permalink
Add modulemd-validator for v2 API
Browse files Browse the repository at this point in the history
Relegate the v1 implementation to the name modulemd-validator-v1

Signed-off-by: Stephen Gallagher <[email protected]>
  • Loading branch information
sgallagher committed Dec 13, 2018
1 parent 37c907d commit 3a7a000
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 16 deletions.
3 changes: 2 additions & 1 deletion libmodulemd.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ ln -s libmodulemd.so.%{libmodulemd_v1_version} \
%files
%license COPYING
%doc README.md
%{_bindir}/modulemd-validator
%{_libdir}/%{name}.so.2*
%dir %{_libdir}/girepository-1.0
%{_libdir}/girepository-1.0/Modulemd-2.0.typelib
Expand All @@ -154,7 +155,7 @@ ln -s libmodulemd.so.%{libmodulemd_v1_version} \
%files -n libmodulemd1
%license COPYING
%doc README.md
%{_bindir}/modulemd-validator
%{_bindir}/modulemd-validator-v1
%{_libdir}/%{name}.so.1*
%dir %{_libdir}/girepository-1.0
%{_libdir}/girepository-1.0/Modulemd-1.0.typelib
Expand Down
5 changes: 5 additions & 0 deletions modulemd/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ modulemd_v2_srcs = files(
'v2/modulemd-yaml-util.c',
)

modulemd_v2_validator_srcs = files (
'v2/modulemd-validator.c',
)

modulemd_v2_hdrs = files(
'v2/include/modulemd-2.0/modulemd.h',
'v2/include/modulemd-2.0/modulemd-buildopts.h',
Expand Down Expand Up @@ -239,6 +243,7 @@ test('clang_format', clang_format,
modulemd_v2_srcs +
modulemd_v2_hdrs +
modulemd_v2_priv_hdrs +
modulemd_v2_validator_srcs +
test_v2_srcs +
test_v2_priv_hdrs)

Expand Down
4 changes: 2 additions & 2 deletions modulemd/v1/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ else
endif


modulemd_validator = executable(
'modulemd-validator',
modulemd_validator_v1 = executable(
'modulemd-validator-v1',
'modulemd-validator.c',
dependencies : [
modulemd_v1_dep,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#pragma once

#include <glib-object.h>
#include <yaml.h>
#include "modulemd-module-index.h"

G_BEGIN_DECLS
Expand All @@ -27,6 +28,40 @@ G_BEGIN_DECLS
*/


/**
* modulemd_module_index_update_from_parser:
* @self: (in): This #ModulemdModuleIndex object
* @parser: (inout): An initialized YAML parser that has not yet processed any
* events.
* @strict: (in): Whether the parser should return failure if it encounters an
* unknown mapping key or if it should ignore it.
* @autogen_module_name: (in): When parsing a module stream that contains no
* module name or stream name, whether to autogenerate one or not. This option
* should be used only for validation tools such as modulemd-validator. Normal
* public routines should always set this to FALSE.
* @failures: (out) (element-type ModulemdSubdocumentInfo) (transfer container):
* An array containing any subdocuments from the YAML file that failed to parse.
* See #ModulemdSubdocumentInfo for more details. If the array is NULL, it will
* be allocated by this function. If it is non-NULL, this function will append
* to it.
* @error: (out): A GError containing additional information if this function
* fails in a way that prevents program continuation.
*
* Returns: TRUE if the update was successful. Returns FALSE and sets failures
* approriately if any of the YAML subdocuments were invalid or sets @error if
* there was a fatal parse error.
*
* Since: 2.0
*/
gboolean
modulemd_module_index_update_from_parser (ModulemdModuleIndex *self,
yaml_parser_t *parser,
gboolean strict,
gboolean autogen_module_name,
GPtrArray **failures,
GError **error);


/**
* modulemd_module_index_merge:
* @from: (in) (transfer none): The #ModulemdModuleIndex whose contents are
Expand Down
12 changes: 12 additions & 0 deletions modulemd/v2/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ else
)
endif

modulemd_v2_validator = executable(
'modulemd-validator',
sources : modulemd_v2_validator_srcs,
include_directories : v2_include_dirs,
dependencies : [
gobject,
yaml,
modulemd_v2_dep
],
install : true
)

v2_header_path = 'modulemd-2.0'

install_headers(
Expand Down
52 changes: 39 additions & 13 deletions modulemd/v2/modulemd-module-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "private/modulemd-defaults-v1-private.h"
#include "private/modulemd-subdocument-info-private.h"
#include "private/modulemd-module-index-private.h"
#include "private/modulemd-module-stream-private.h"
#include "private/modulemd-module-stream-v1-private.h"
#include "private/modulemd-module-stream-v2-private.h"
#include "private/modulemd-translation-private.h"
Expand Down Expand Up @@ -121,11 +122,13 @@ static gboolean
add_subdoc (ModulemdModuleIndex *self,
ModulemdSubdocumentInfo *subdoc,
gboolean strict,
gboolean autogen_module_name,
GError **error)
{
g_autoptr (ModulemdModuleStream) stream = NULL;
g_autoptr (ModulemdTranslation) translation = NULL;
g_autoptr (ModulemdDefaults) defaults = NULL;
g_autofree gchar *name = NULL;

switch (modulemd_subdocument_info_get_doctype (subdoc))
{
Expand All @@ -135,20 +138,12 @@ add_subdoc (ModulemdModuleIndex *self,
case MD_MODULESTREAM_VERSION_ONE:
stream = MODULEMD_MODULE_STREAM (
modulemd_module_stream_v1_parse_yaml (subdoc, strict, error));
if (stream == NULL)
return FALSE;
if (!modulemd_module_index_add_module_stream (self, stream, error))
return FALSE;
break;

case MD_MODULESTREAM_VERSION_TWO:
stream =
(ModulemdModuleStream *)modulemd_module_stream_v2_parse_yaml (
subdoc, strict, error);
if (stream == NULL)
return FALSE;
if (!modulemd_module_index_add_module_stream (self, stream, error))
return FALSE;
break;

default:
Expand All @@ -158,6 +153,32 @@ add_subdoc (ModulemdModuleIndex *self,
"Invalid mdversion for a stream object");
return FALSE;
}

if (stream == NULL)
return FALSE;

if (autogen_module_name &&
!modulemd_module_stream_get_module_name (stream))
{
name = g_strdup_printf ("__unnamed_module_%d",
g_hash_table_size (self->modules) + 1);
modulemd_module_stream_set_module_name (stream, name);
g_clear_pointer (&name, g_free);
}

if (autogen_module_name &&
!modulemd_module_stream_get_stream_name (stream))
{
name = g_strdup_printf ("__unnamed_stream_%d",
g_hash_table_size (self->modules) + 1);
modulemd_module_stream_set_stream_name (stream, name);
g_clear_pointer (&name, g_free);
}


if (!modulemd_module_index_add_module_stream (self, stream, error))
return FALSE;

break;

case MODULEMD_YAML_DOC_DEFAULTS:
Expand Down Expand Up @@ -201,10 +222,11 @@ add_subdoc (ModulemdModuleIndex *self,
}


static gboolean
gboolean
modulemd_module_index_update_from_parser (ModulemdModuleIndex *self,
yaml_parser_t *parser,
gboolean strict,
gboolean autogen_module_name,
GPtrArray **failures,
GError **error)
{
Expand All @@ -213,6 +235,9 @@ modulemd_module_index_update_from_parser (ModulemdModuleIndex *self,
g_autoptr (ModulemdSubdocumentInfo) subdoc = NULL;
MMD_INIT_YAML_EVENT (event);

if (*failures == NULL)
*failures = g_ptr_array_new_with_free_func (g_object_unref);

YAML_PARSER_PARSE_WITH_EXIT_BOOL (parser, &event, error);
if (event.type != YAML_STREAM_START_EVENT)
MMD_YAML_ERROR_EVENT_EXIT_BOOL (
Expand All @@ -236,16 +261,17 @@ modulemd_module_index_update_from_parser (ModulemdModuleIndex *self,
else
{
/* Initial parsing worked, parse further */
if (!add_subdoc (self, subdoc, strict, error))
if (!add_subdoc (
self, subdoc, strict, autogen_module_name, error))
{
modulemd_subdocument_info_set_gerror (subdoc, *error);
g_clear_pointer (error, g_error_free);
/* Add to failures and ignore */
g_ptr_array_add (*failures, g_steal_pointer (&subdoc));
all_passed = FALSE;
}
g_clear_pointer (&subdoc, g_object_unref);
}
g_clear_pointer (&subdoc, g_object_unref);
break;

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

return modulemd_module_index_update_from_parser (
self, &parser, strict, failures, error);
self, &parser, strict, FALSE, failures, error);
}


Expand Down Expand Up @@ -507,7 +533,7 @@ modulemd_module_index_update_from_stream (ModulemdModuleIndex *self,
yaml_parser_set_input_file (&parser, yaml_stream);

return modulemd_module_index_update_from_parser (
self, &parser, strict, failures, error);
self, &parser, strict, FALSE, failures, error);
}


Expand Down
Loading

0 comments on commit 3a7a000

Please sign in to comment.