schema helpers REFACTOR inserting to hash table
diff --git a/src/tree_schema_helpers.c b/src/tree_schema_helpers.c
index 4eea7f0..deefa80 100644
--- a/src/tree_schema_helpers.c
+++ b/src/tree_schema_helpers.c
@@ -312,6 +312,41 @@
}
/**
+ * @brief Insert @p name to hash table and if @p name has already
+ * been added, then log an error.
+ *
+ * This function is used to detect duplicate names.
+ *
+ * @param[in,out] ctx Context to log the error.
+ * @param[in,out] ht Hash table with top-level names.
+ * @param[in] name Inserted top-level identifier.
+ * @param[in] statement The name of the statement type from which
+ * @p name originated (eg typedef, feature, ...).
+ * @param[in] err_detail Optional error specification.
+ * @return LY_ERR, but LY_EEXIST is mapped to LY_EVALID.
+ */
+static LY_ERR
+lysp_check_dup_ht_insert(struct lys_parser_ctx *ctx, struct hash_table *ht,
+ const char *name, const char *statement, const char *err_detail)
+{
+ LY_ERR ret;
+ uint32_t hash;
+
+ hash = dict_hash(name, strlen(name));
+ ret = lyht_insert(ht, &name, hash, NULL);
+ if (ret == LY_EEXIST) {
+ if (err_detail) {
+ LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT2, name, statement, err_detail);
+ } else {
+ LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, name, statement);
+ }
+ ret = LY_EVALID;
+ }
+
+ return ret;
+}
+
+/**
* @brief Check name of a new type to avoid name collisions.
*
* @param[in] ctx Parser context, module where the type is being defined is taken from here.
@@ -450,39 +485,22 @@
LY_ARRAY_COUNT_TYPE u;
struct hash_table *ht;
struct lysp_feature *f;
- uint32_t hash;
- LY_ERR ret = LY_SUCCESS, r;
+ LY_ERR ret = LY_SUCCESS;
ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
LY_CHECK_RET(!ht, LY_EMEM);
/* add all module features into a hash table */
LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
- hash = dict_hash(f->name, strlen(f->name));
- r = lyht_insert(ht, &f->name, hash, NULL);
- if (r == LY_EEXIST) {
- LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
- ret = LY_EVALID;
- goto cleanup;
- } else if (r) {
- ret = r;
- goto cleanup;
- }
+ ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature", NULL);
+ LY_CHECK_GOTO(ret, cleanup);
}
/* add all submodule features into a hash table */
LY_ARRAY_FOR(mod->includes, u) {
LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
- hash = dict_hash(f->name, strlen(f->name));
- r = lyht_insert(ht, &f->name, hash, NULL);
- if (r == LY_EEXIST) {
- LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
- ret = LY_EVALID;
- goto cleanup;
- } else if (r) {
- ret = r;
- goto cleanup;
- }
+ ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature", NULL);
+ LY_CHECK_GOTO(ret, cleanup);
}
}
@@ -497,39 +515,22 @@
LY_ARRAY_COUNT_TYPE u;
struct hash_table *ht;
struct lysp_ident *i;
- uint32_t hash;
- LY_ERR ret = LY_SUCCESS, r;
+ LY_ERR ret = LY_SUCCESS;
ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
LY_CHECK_RET(!ht, LY_EMEM);
/* add all module identities into a hash table */
LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
- hash = dict_hash(i->name, strlen(i->name));
- r = lyht_insert(ht, &i->name, hash, NULL);
- if (r == LY_EEXIST) {
- LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
- ret = LY_EVALID;
- goto cleanup;
- } else if (r) {
- ret = r;
- goto cleanup;
- }
+ ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity", NULL);
+ LY_CHECK_GOTO(ret, cleanup);
}
/* add all submodule identities into a hash table */
LY_ARRAY_FOR(mod->includes, u) {
LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
- hash = dict_hash(i->name, strlen(i->name));
- r = lyht_insert(ht, &i->name, hash, NULL);
- if (r == LY_EEXIST) {
- LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
- ret = LY_EVALID;
- goto cleanup;
- } else if (r) {
- ret = r;
- goto cleanup;
- }
+ ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity", NULL);
+ LY_CHECK_GOTO(ret, cleanup);
}
}