Make config_schema_initialize_item more useful

By also allowing it to set the user_data member.
This commit is contained in:
Přemysl Eric Janouch 2015-12-28 04:01:58 +01:00
parent a4313ee4b9
commit 2d8a8e0b1b

View File

@ -4707,32 +4707,38 @@ config_item_clone (struct config_item *self)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/// "user_data" is passed so that it be immediately used by validation callbacks
static struct config_item * static struct config_item *
config_schema_initialize_item (struct config_schema *schema, config_schema_initialize_item (struct config_schema *schema,
struct config_item *parent, struct error **warning, struct error **e) struct config_item *parent, void *user_data, struct error **warning,
struct error **e)
{ {
hard_assert (parent->type == CONFIG_ITEM_OBJECT); hard_assert (parent->type == CONFIG_ITEM_OBJECT);
struct config_item *item = struct config_item *item =
str_map_find (&parent->value.object, schema->name); str_map_find (&parent->value.object, schema->name);
if (item)
{
struct error *error = NULL; struct error *error = NULL;
if (item && config_item_validate_by_schema (item, schema, &error)) item->user_data = user_data;
if (config_item_validate_by_schema (item, schema, &error))
goto keep_current; goto keep_current;
if (error)
{
error_set (warning, "resetting configuration item " error_set (warning, "resetting configuration item "
"`%s' to default: %s", schema->name, error->message); "`%s' to default: %s", schema->name, error->message);
error_free (error); error_free (error);
error = NULL;
} }
struct error *error = NULL;
if (schema->default_) if (schema->default_)
item = config_item_parse item = config_item_parse
(schema->default_, strlen (schema->default_), true, &error); (schema->default_, strlen (schema->default_), true, &error);
else else
item = config_item_null (); item = config_item_null ();
if (item)
item->user_data = user_data;
if (error || !config_item_validate_by_schema (item, schema, &error)) if (error || !config_item_validate_by_schema (item, schema, &error))
{ {
error_set (e, "invalid default for configuration item `%s': %s", error_set (e, "invalid default for configuration item `%s': %s",
@ -4765,8 +4771,8 @@ config_schema_apply_to_object (struct config_schema *schema_array,
while (schema_array->name) while (schema_array->name)
{ {
struct error *warning = NULL, *e = NULL; struct error *warning = NULL, *e = NULL;
struct config_item *item = config_schema_initialize_item config_schema_initialize_item
(schema_array++, object, &warning, &e); (schema_array++, object, user_data, &warning, &e);
if (warning) if (warning)
{ {
@ -4775,8 +4781,6 @@ config_schema_apply_to_object (struct config_schema *schema_array,
} }
if (e) if (e)
print_fatal ("%s", e->message); print_fatal ("%s", e->message);
item->user_data = user_data;
} }
} }