diff --git a/common.c b/common.c index 57518d9..6d73130 100644 --- a/common.c +++ b/common.c @@ -701,6 +701,23 @@ config_schema_accepts_type } +static bool +config_item_validate_by_schema (struct config_item_ *self, + struct config_item_ *source, struct config_schema *schema, struct error **e) +{ + // Otherwise we check the type and validate the item + if (!config_schema_accepts_type (schema, source->type)) + error_set (e, "invalid type of value, expected: %s%s", + config_item_type_name (schema->type), + !schema->default_ ? " (or null)" : ""); + else if (schema->validate && !schema->validate (self, source)) + // XXX: perhaps "schema->validate" could provide a message for us? + error_set (e, "invalid value"); + else + return true; + return false; +} + static bool config_item_set_from (struct config_item_ *self, struct config_item_ *source, struct error **e) @@ -713,21 +730,8 @@ config_item_set_from (struct config_item_ *self, struct config_item_ *source, return true; } - // Otherwise we check the type and validate the item - if (!config_schema_accepts_type (schema, source->type)) - { - error_set (e, "invalid type of value, expected: %s%s", - config_item_type_name (schema->type), - !schema->default_ ? " (or null)" : ""); + if (!config_item_validate_by_schema (self, source, schema, e)) return false; - } - - if (schema->validate && !schema->validate (self, source)) - { - // XXX: perhaps "schema->validate" could provide a message for us? - error_set (e, "invalid value"); - return false; - } // Make sure the string subtype fits the schema if (config_item_type_is_string (self->type) @@ -883,16 +887,6 @@ config_item_write (struct config_item_ *value, // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -static void -config_schema_apply_to_object - (struct config_schema *schema_array, struct config_item_ *object) -{ - hard_assert (object->type == CONFIG_ITEM_OBJECT); - // TODO -} - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - enum config_token { CONFIG_T_ABORT, ///< EOF or error @@ -1421,6 +1415,52 @@ end: // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +static void +config_schema_apply_to_object + (struct config_schema *schema_array, struct config_item_ *object) +{ + hard_assert (object->type == CONFIG_ITEM_OBJECT); + while (schema_array->name) + { + struct config_schema *schema = schema_array++; + struct config_item_ *item = + str_map_find (&object->value.object, schema->name); + + bool make_new = true; + if (item) + { + struct error *e = NULL; + make_new = !config_item_validate_by_schema + (NULL, item, schema, &e); + if (e) + { + print_error ("resetting configuration item " + "`%s' to defaults: %s", schema->name, e->message); + error_free (e); + } + } + + if (make_new) + { + struct error *e = NULL; + struct config_item_ *default_ = config_item_parse + (schema->default_, strlen (schema->default_), true, &e); + if (e) + exit_fatal ("invalid default: %s", e->message); + + config_item_move (item, default_); + } + + item->type = schema->type; + item->schema = schema; + + if (schema->on_changed) + schema->on_changed (item); + } +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + // XXX: this doesn't necessarily have to be well designed at all typedef void (*config_module_load_fn)