diff options
author | Přemysl Janouch <p.janouch@gmail.com> | 2015-05-02 20:43:01 +0200 |
---|---|---|
committer | Přemysl Janouch <p.janouch@gmail.com> | 2015-05-02 20:43:01 +0200 |
commit | 5a6b12245b10e5ca3acb4add42198ccecb5a492c (patch) | |
tree | 17bc93df892e58aea800d8ece092318f93211920 | |
parent | 7e42399ad0b048f53f84f362fdecb82b24eaa344 (diff) | |
download | xK-5a6b12245b10e5ca3acb4add42198ccecb5a492c.tar.gz xK-5a6b12245b10e5ca3acb4add42198ccecb5a492c.tar.xz xK-5a6b12245b10e5ca3acb4add42198ccecb5a492c.zip |
config: implement config_schema_apply_to_object()
-rw-r--r-- | common.c | 88 |
1 files changed, 64 insertions, 24 deletions
@@ -702,6 +702,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)" : ""); - return false; - } - - if (schema->validate && !schema->validate (self, source)) - { - // XXX: perhaps "schema->validate" could provide a message for us? - error_set (e, "invalid value"); + if (!config_item_validate_by_schema (self, source, schema, e)) 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) |