add yaml parser!
[protos/libecoli.git] / lib / ecoli_config.c
index 838438e..0698f07 100644 (file)
 
 EC_LOG_TYPE_REGISTER(config);
 
+const char *ec_config_reserved_keys[] = {
+       "id",
+       "attrs",
+       "help",
+       "type",
+};
+
 static int
 __ec_config_dump(FILE *out, const char *key, const struct ec_config *config,
        size_t indent);
@@ -25,6 +32,18 @@ static int
 ec_config_dict_validate(const struct ec_keyval *dict,
                        const struct ec_config_schema *schema);
 
+bool
+ec_config_key_is_reserved(const char *name)
+{
+       size_t i;
+
+       for (i = 0; i < EC_COUNT_OF(ec_config_reserved_keys); i++) {
+               if (!strcmp(name, ec_config_reserved_keys[i]))
+                       return true;
+       }
+       return false;
+}
+
 /* return ec_value type as a string */
 static const char *
 ec_config_type_str(enum ec_config_type type)
@@ -82,6 +101,13 @@ __ec_config_schema_validate(const struct ec_config_schema *schema,
        }
 
        for (i = 0; schema[i].type != EC_CONFIG_TYPE_NONE; i++) {
+               if (schema[i].key != NULL &&
+                               ec_config_key_is_reserved(schema[i].key)) {
+                       errno = EINVAL;
+                       EC_LOG(EC_LOG_ERR,
+                               "key name <%s> is reserved\n", schema[i].key);
+                       return -1;
+               }
                /* check for duplicate name if more than one element */
                for (j = i + 1; schema[j].type != EC_CONFIG_TYPE_NONE; j++) {
                        if (!strcmp(schema[i].key, schema[j].key)) {
@@ -334,15 +360,14 @@ ec_config_list(void)
        return value;
 }
 
-static const struct ec_config_schema *
+const struct ec_config_schema *
 ec_config_schema_lookup(const struct ec_config_schema *schema,
-                       const char *key, enum ec_config_type type)
+                       const char *key)
 {
        size_t i;
 
        for (i = 0; schema[i].type != EC_CONFIG_TYPE_NONE; i++) {
-               if (!strcmp(key, schema[i].key) &&
-                               type == schema[i].type)
+               if (!strcmp(key, schema[i].key))
                        return &schema[i];
        }
 
@@ -350,6 +375,18 @@ ec_config_schema_lookup(const struct ec_config_schema *schema,
        return NULL;
 }
 
+enum ec_config_type
+ec_config_schema_type(const struct ec_config_schema *schema_elt)
+{
+       return schema_elt->type;
+}
+
+const struct ec_config_schema *
+ec_config_schema_sub(const struct ec_config_schema *schema_elt)
+{
+       return schema_elt->subschema;
+}
+
 void
 ec_config_free(struct ec_config *value)
 {
@@ -510,12 +547,11 @@ ec_config_dict_validate(const struct ec_keyval *dict,
 
                key = ec_keyval_iter_get_key(iter);
                value = ec_keyval_iter_get_val(iter);
-               sch = ec_config_schema_lookup(schema, key, value->type);
-               if (sch == NULL) {
+               sch = ec_config_schema_lookup(schema, key);
+               if (sch == NULL || sch->type != value->type) {
                        errno = EBADMSG;
                        goto fail;
                }
-
                if (value->type == EC_CONFIG_TYPE_LIST) {
                        if (ec_config_list_validate(&value->list,
                                                        sch->subschema) < 0)
@@ -742,12 +778,15 @@ ec_config_dup(const struct ec_config *config)
 }
 
 static int
-ec_config_list_dump(FILE *out, const struct ec_config_list *list,
-               size_t indent)
+ec_config_list_dump(FILE *out, const char *key,
+               const struct ec_config_list *list, size_t indent)
 {
        const struct ec_config *v;
 
-       fprintf(out, "%*s" "type=list:\n", (int)indent * 4, "");
+       fprintf(out, "%*s" "%s%s%stype=list\n", (int)indent * 4, "",
+               key ? "key=": "",
+               key ? key: "",
+               key ? " ": "");
 
        TAILQ_FOREACH(v, list, next) {
                if (__ec_config_dump(out, NULL, v, indent + 1) < 0)
@@ -758,20 +797,24 @@ ec_config_list_dump(FILE *out, const struct ec_config_list *list,
 }
 
 static int
-ec_config_dict_dump(FILE *out, const struct ec_keyval *dict,
+ec_config_dict_dump(FILE *out, const char *key, const struct ec_keyval *dict,
                size_t indent)
 {
        const struct ec_config *value;
        struct ec_keyval_iter *iter;
-       const char *key;
+       const char *k;
+
+       fprintf(out, "%*s" "%s%s%stype=dict\n", (int)indent * 4, "",
+               key ? "key=": "",
+               key ? key: "",
+               key ? " ": "");
 
-       fprintf(out, "%*s" "type=dict:\n", (int)indent * 4, "");
        for (iter = ec_keyval_iter(dict);
             ec_keyval_iter_valid(iter);
             ec_keyval_iter_next(iter)) {
-               key = ec_keyval_iter_get_key(iter);
+               k = ec_keyval_iter_get_key(iter);
                value = ec_keyval_iter_get_val(iter);
-               if (__ec_config_dump(out, key, value, indent + 1) < 0)
+               if (__ec_config_dump(out, k, value, indent + 1) < 0)
                        goto fail;
        }
        ec_keyval_iter_free(iter);
@@ -808,9 +851,9 @@ __ec_config_dump(FILE *out, const char *key, const struct ec_config *value,
                ec_asprintf(&val_str, "%p", value->node);
                break;
        case EC_CONFIG_TYPE_LIST:
-               return ec_config_list_dump(out, &value->list, indent);
+               return ec_config_list_dump(out, key, &value->list, indent);
        case EC_CONFIG_TYPE_DICT:
-               return ec_config_dict_dump(out, value->dict, indent);
+               return ec_config_dict_dump(out, key, value->dict, indent);
        default:
                errno = EINVAL;
                break;
@@ -935,6 +978,11 @@ static int ec_config_testcase(void)
        int testres = 0;
        int ret;
 
+       testres |= EC_TEST_CHECK(ec_config_key_is_reserved("id"),
+               "'id' should be reserved");
+       testres |= EC_TEST_CHECK(!ec_config_key_is_reserved("foo"),
+               "'foo' should not be reserved");
+
        node = ec_node("empty", EC_NO_ID);
        if (node == NULL)
                goto fail;