Refactor config schema initialization

Now the configuration module seems to be fit for inclusion in liberty.
This commit is contained in:
Přemysl Eric Janouch 2015-12-11 02:14:15 +01:00
parent de942e40ac
commit 649ea0baf7
1 changed files with 46 additions and 30 deletions

View File

@ -1935,61 +1935,77 @@ config_item_clone (struct config_item *self)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void static struct config_item *
config_schema_initialize_item (struct config_schema *schema, config_schema_initialize_item (struct config_schema *schema,
struct config_item *parent, void *user_data) struct config_item *parent, struct error **warning, struct error **e)
{ {
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);
bool replace = true; struct error *error = NULL;
if (item) if (item && config_item_validate_by_schema (item, schema, &error))
goto keep_current;
if (error)
{ {
// FIXME: either do this silently or tell about it via a callback error_set (warning, "resetting configuration item "
// or just store it in an output vector; don't print it directly "`%s' to default: %s", schema->name, error->message);
struct error *e = NULL; error_free (error);
replace = !config_item_validate_by_schema (item, schema, &e); error = NULL;
if (e)
{
print_error ("resetting configuration item "
"`%s' to default: %s", schema->name, e->message);
error_free (e);
}
} }
if (replace) if (schema->default_)
item = config_item_parse
(schema->default_, strlen (schema->default_), true, &error);
else
item = config_item_null ();
if (error || !config_item_validate_by_schema (item, schema, &error))
{ {
struct error *e = NULL; error_set (e, "invalid default for configuration item `%s': %s",
if (schema->default_) schema->name, error->message);
item = config_item_parse error_free (error);
(schema->default_, strlen (schema->default_), true, &e);
else
item = config_item_null ();
if (e || !config_item_validate_by_schema (item, schema, &e)) config_item_destroy (item);
exit_fatal ("invalid default for `%s': %s", return NULL;
schema->name, e->message);
// This will free the old item if there was any
str_map_set (&parent->value.object, schema->name, item);
} }
// This will free the old item if there was any
str_map_set (&parent->value.object, schema->name, item);
keep_current:
// Make sure the string subtype fits the schema // Make sure the string subtype fits the schema
if (config_item_type_is_string (item->type) if (config_item_type_is_string (item->type)
&& config_item_type_is_string (schema->type)) && config_item_type_is_string (schema->type))
item->type = schema->type; item->type = schema->type;
item->schema = schema; item->schema = schema;
item->user_data = user_data; return item;
} }
/// Assign schemas and user_data to multiple items at once;
/// feel free to copy over and modify to suit your particular needs
static void static void
config_schema_apply_to_object (struct config_schema *schema_array, config_schema_apply_to_object (struct config_schema *schema_array,
struct config_item *object, void *user_data) struct config_item *object, void *user_data)
{ {
hard_assert (object->type == CONFIG_ITEM_OBJECT);
while (schema_array->name) while (schema_array->name)
config_schema_initialize_item (schema_array++, object, user_data); {
struct error *warning = NULL, *e = NULL;
struct config_item *item = config_schema_initialize_item
(schema_array++, object, &warning, &e);
if (warning)
{
print_warning ("%s", warning->message);
error_free (warning);
}
if (e)
print_fatal ("%s", e->message);
item->user_data = user_data;
}
} }
static void static void