Skip to content

Commit

Permalink
Add override_name for components
Browse files Browse the repository at this point in the history
Related to #96

Signed-off-by: Stephen Gallagher <[email protected]>
  • Loading branch information
sgallagher committed Mar 14, 2019
1 parent 0d77106 commit e219b93
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 51 deletions.
41 changes: 38 additions & 3 deletions modulemd/v2/include/modulemd-2.0/modulemd-component.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ struct _ModulemdComponentClass
GObjectClass parent_class;

ModulemdComponent *(*copy) (ModulemdComponent *self, const gchar *name);
void (*set_name) (ModulemdComponent *self, const gchar *name);
const gchar *(*get_name) (ModulemdComponent *self);

/* Padding to allow adding up to 10 new virtual functions without
/* Padding to allow adding up to 8 new virtual functions without
* breaking ABI. */
gpointer padding[10];
gpointer padding[8];
};

/**
Expand Down Expand Up @@ -75,18 +77,51 @@ gint64
modulemd_component_get_buildorder (ModulemdComponent *self);


/**
* modulemd_component_set_name:
* @self: This #ModulemdComponent object.
* @name: (nullable): The name of this component. Note that this is different
* from the key used to save this component to a #ModulemdModuleStream. If this
* value is set, it adds a "name:" attribute to this component. This is used in
* bootstrapping cases where the key is a different name used to differentiate
* multiple ordered builds of the same component name. This function is
* currently only implemented for #ModulemdComponentRpm and has no effect on
* other #ModulemdComponent types.
*
* Since: 2.2
*/
void
modulemd_component_set_name (ModulemdComponent *self, const gchar *name);


/**
* modulemd_component_get_name:
* @self: This #ModulemdComponent object
*
* Returns: (transfer none): The name of the component.
* Returns: (transfer none): The name of the component. Note that this may be
* different from the key used to save this component to a
* #ModulemdModuleStream. If you specifically need the key, use
* modulemd_component_get_key() instead.
*
* Since: 2.0
*/
const gchar *
modulemd_component_get_name (ModulemdComponent *self);


/**
* modulemd_component_get_key:
* @self: This #ModulemdComponent object
*
* Returns: (transfer none): The name of the key used to attach this component
* to a #ModulemdModuleStream.
*
* Since: 2.2
*/
const gchar *
modulemd_component_get_key (ModulemdComponent *self);


/**
* modulemd_component_set_rationale:
* @self: This #ModulemdComponent object
Expand Down
114 changes: 80 additions & 34 deletions modulemd/v2/modulemd-component-rpm.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct _ModulemdComponentRpm
{
GObject parent_instance;

gchar *override_name;
gchar *ref;
gchar *repository;
gchar *cache;
Expand Down Expand Up @@ -61,6 +62,7 @@ modulemd_component_rpm_finalize (GObject *object)
{
ModulemdComponentRpm *self = (ModulemdComponentRpm *)object;

g_clear_pointer (&self->override_name, g_free);
g_clear_pointer (&self->ref, g_free);
g_clear_pointer (&self->repository, g_free);
g_clear_pointer (&self->cache, g_free);
Expand Down Expand Up @@ -92,6 +94,11 @@ hash_table_str_set_copy (GHashTable *orig)
}


static void
modulemd_component_rpm_set_name (ModulemdComponent *self, const gchar *name);
static const gchar *
modulemd_component_rpm_get_name (ModulemdComponent *self);

static ModulemdComponent *
modulemd_component_rpm_copy (ModulemdComponent *self, const gchar *name)
{
Expand All @@ -106,6 +113,8 @@ modulemd_component_rpm_copy (ModulemdComponent *self, const gchar *name)

modulemd_component_rpm_set_ref (copy,
modulemd_component_rpm_get_ref (rpm_self));
modulemd_component_rpm_set_name (MODULEMD_COMPONENT (copy),
rpm_self->override_name);
modulemd_component_rpm_set_repository (
copy, modulemd_component_rpm_get_repository (rpm_self));
modulemd_component_rpm_set_cache (
Expand All @@ -120,6 +129,56 @@ modulemd_component_rpm_copy (ModulemdComponent *self, const gchar *name)
}


static void
modulemd_component_rpm_set_name (ModulemdComponent *self, const gchar *name)
{
const gchar *key = NULL;
ModulemdComponentRpm *rpm_self = NULL;

g_return_if_fail (MODULEMD_IS_COMPONENT_RPM (self));
rpm_self = MODULEMD_COMPONENT_RPM (self);

if (g_strcmp0 (rpm_self->override_name, name) == 0)
{
/* No change, so just return.
*/
return;
}

/* We're changing the value, so clear the existing version */
g_clear_pointer (&rpm_self->override_name, g_free);

key = MODULEMD_COMPONENT_CLASS (modulemd_component_rpm_parent_class)
->get_name (self);

/* If we were passed non-NULL and the passed name differs from the hash table
* key, save it. Otherwise, just leave it set NULL.
*/
if (name && g_strcmp0 (key, name) != 0)
{
rpm_self->override_name = g_strdup (name);
}
}


static const gchar *
modulemd_component_rpm_get_name (ModulemdComponent *self)
{
ModulemdComponentRpm *rpm_self = NULL;

g_return_val_if_fail (MODULEMD_IS_COMPONENT_RPM (self), NULL);
rpm_self = MODULEMD_COMPONENT_RPM (self);

/* If an override name was set, return it */
if (rpm_self->override_name)
return rpm_self->override_name;

/* Otherwise, return the hash table key as the name */
return MODULEMD_COMPONENT_CLASS (modulemd_component_rpm_parent_class)
->get_name (self);
}


void
modulemd_component_rpm_set_ref (ModulemdComponentRpm *self, const gchar *ref)
{
Expand Down Expand Up @@ -303,6 +362,8 @@ modulemd_component_rpm_class_init (ModulemdComponentRpmClass *klass)
object_class->set_property = modulemd_component_rpm_set_property;

component_class->copy = modulemd_component_rpm_copy;
component_class->set_name = modulemd_component_rpm_set_name;
component_class->get_name = modulemd_component_rpm_get_name;

properties[PROP_CACHE] =
g_param_spec_string ("cache",
Expand Down Expand Up @@ -348,43 +409,13 @@ modulemd_component_rpm_emit_yaml (ModulemdComponentRpm *self,
MODULEMD_COMPONENT (self), emitter, error))
return FALSE;

if (modulemd_component_rpm_get_repository (self) != NULL)
{
if (!mmd_emitter_scalar (
emitter, "repository", YAML_PLAIN_SCALAR_STYLE, error))
return FALSE;

if (!mmd_emitter_scalar (emitter,
modulemd_component_rpm_get_repository (self),
YAML_PLAIN_SCALAR_STYLE,
error))
return FALSE;
}

if (modulemd_component_rpm_get_cache (self) != NULL)
{
if (!mmd_emitter_scalar (
emitter, "cache", YAML_PLAIN_SCALAR_STYLE, error))
return FALSE;
EMIT_KEY_VALUE_IF_SET (emitter, error, "name", self->override_name);

if (!mmd_emitter_scalar (emitter,
modulemd_component_rpm_get_cache (self),
YAML_PLAIN_SCALAR_STYLE,
error))
return FALSE;
}
EMIT_KEY_VALUE_IF_SET (emitter, error, "repository", self->repository);

if (modulemd_component_rpm_get_ref (self) != NULL)
{
if (!mmd_emitter_scalar (emitter, "ref", YAML_PLAIN_SCALAR_STYLE, error))
return FALSE;
EMIT_KEY_VALUE_IF_SET (emitter, error, "cache", self->cache);

if (!mmd_emitter_scalar (emitter,
modulemd_component_rpm_get_ref (self),
YAML_PLAIN_SCALAR_STYLE,
error))
return FALSE;
}
EMIT_KEY_VALUE_IF_SET (emitter, error, "ref", self->ref);

if (!modulemd_component_emit_yaml_buildorder (
MODULEMD_COMPONENT (self), emitter, error))
Expand Down Expand Up @@ -480,6 +511,21 @@ modulemd_component_rpm_parse_yaml (yaml_parser_t *parser,
modulemd_component_set_rationale (MODULEMD_COMPONENT (r), value);
g_clear_pointer (&value, g_free);
}
else if (g_str_equal ((const gchar *)event.data.scalar.value,
"name"))
{
value = modulemd_yaml_parse_string (parser, &nested_error);
if (!value)
MMD_YAML_ERROR_EVENT_EXIT (
error,
event,
"Failed to parse override name in component: %s",
nested_error->message);

modulemd_component_set_name (MODULEMD_COMPONENT (r), value);
g_clear_pointer (&value, g_free);
}

else if (g_str_equal ((const gchar *)event.data.scalar.value,
"repository"))
{
Expand Down
46 changes: 38 additions & 8 deletions modulemd/v2/modulemd-component.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ modulemd_component_copy (ModulemdComponent *self, const gchar *name)
static ModulemdComponent *
modulemd_component_copy_component (ModulemdComponent *self, const gchar *name)
{
ModulemdComponentPrivate *priv =
modulemd_component_get_instance_private (self);
g_autoptr (ModulemdComponent) m = NULL;
if (name == NULL)
name = modulemd_component_get_name (self);
name = priv->name;

m = g_object_new (G_OBJECT_TYPE (self), "name", name, NULL);

Expand Down Expand Up @@ -118,7 +120,7 @@ modulemd_component_get_buildorder (ModulemdComponent *self)


static void
modulemd_component_set_name (ModulemdComponent *self, const gchar *name)
modulemd_component_set_key (ModulemdComponent *self, const gchar *name)
{
g_return_if_fail (MODULEMD_IS_COMPONENT (self));
g_return_if_fail (name);
Expand All @@ -132,9 +134,22 @@ modulemd_component_set_name (ModulemdComponent *self, const gchar *name)
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_NAME]);
}

void
modulemd_component_set_name (ModulemdComponent *self, const gchar *name)
{
ModulemdComponentClass *klass;

klass = MODULEMD_COMPONENT_GET_CLASS (self);

/* Do nothing if the child class has not implemented this */
if (!klass->set_name)
return;

klass->set_name (self, name);
}

const gchar *
modulemd_component_get_name (ModulemdComponent *self)
modulemd_component_get_key (ModulemdComponent *self)
{
g_return_val_if_fail (MODULEMD_IS_COMPONENT (self), NULL);

Expand All @@ -145,6 +160,18 @@ modulemd_component_get_name (ModulemdComponent *self)
}


const gchar *
modulemd_component_get_name (ModulemdComponent *self)
{
ModulemdComponentClass *klass;

klass = MODULEMD_COMPONENT_GET_CLASS (self);
g_return_val_if_fail (klass->get_name, NULL);

return klass->get_name (self);
}


void
modulemd_component_set_rationale (ModulemdComponent *self,
const gchar *rationale)
Expand Down Expand Up @@ -214,7 +241,7 @@ modulemd_component_set_property (GObject *object,
break;

case PROP_NAME:
modulemd_component_set_name (self, g_value_get_string (value));
modulemd_component_set_key (self, g_value_get_string (value));
break;

case PROP_RATIONALE:
Expand All @@ -236,6 +263,8 @@ modulemd_component_class_init (ModulemdComponentClass *klass)
object_class->set_property = modulemd_component_set_property;

klass->copy = modulemd_component_copy_component;
klass->set_name = NULL;
klass->get_name = modulemd_component_get_key;

properties[PROP_BUILDORDER] = g_param_spec_int64 (
"buildorder",
Expand Down Expand Up @@ -274,12 +303,13 @@ modulemd_component_emit_yaml_start (ModulemdComponent *self,
yaml_emitter_t *emitter,
GError **error)
{
ModulemdComponentPrivate *priv =
modulemd_component_get_instance_private (self);

MODULEMD_INIT_TRACE ();

if (!mmd_emitter_scalar (emitter,
modulemd_component_get_name (self),
YAML_PLAIN_SCALAR_STYLE,
error))
if (!mmd_emitter_scalar (
emitter, priv->name, YAML_PLAIN_SCALAR_STYLE, error))
return FALSE;

if (!mmd_emitter_start_mapping (emitter, YAML_BLOCK_MAPPING_STYLE, error))
Expand Down
2 changes: 1 addition & 1 deletion modulemd/v2/modulemd-module-stream-v1.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ modulemd_module_stream_v1_add_component (ModulemdModuleStreamV1 *self,
* with the same name
*/
g_hash_table_replace (table,
g_strdup (modulemd_component_get_name (component)),
g_strdup (modulemd_component_get_key (component)),
modulemd_component_copy (component, NULL));
}

Expand Down
2 changes: 1 addition & 1 deletion modulemd/v2/modulemd-module-stream-v2.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ modulemd_module_stream_v2_add_component (ModulemdModuleStreamV2 *self,
* with the same name
*/
g_hash_table_replace (table,
g_strdup (modulemd_component_get_name (component)),
g_strdup (modulemd_component_get_key (component)),
modulemd_component_copy (component, NULL));
}

Expand Down
Loading

0 comments on commit e219b93

Please sign in to comment.