1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017, Olivier MATZ <zer0@droids-corp.org>
10 #include <ecoli_log.h>
11 #include <ecoli_malloc.h>
12 #include <ecoli_test.h>
13 #include <ecoli_strvec.h>
14 #include <ecoli_keyval.h>
15 #include <ecoli_node.h>
16 #include <ecoli_parse.h>
17 #include <ecoli_complete.h>
18 #include <ecoli_string.h>
19 #include <ecoli_node_str.h>
20 #include <ecoli_node_many.h>
22 #include "ecoli_node_dynamic.h"
24 EC_LOG_TYPE_REGISTER(node_dynamic);
26 struct ec_node_dynamic {
28 ec_node_dynamic_build_t build;
33 ec_node_dynamic_parse(const struct ec_node *gen_node,
34 struct ec_parse *parse,
35 const struct ec_strvec *strvec)
37 struct ec_node_dynamic *node = (struct ec_node_dynamic *)gen_node;
38 struct ec_node *child = NULL;
39 void (*node_free)(struct ec_node *) = ec_node_free;
43 child = node->build(parse, node->opaque);
47 /* add the node pointer in the attributes, so it will be freed
48 * when parse is freed */
49 snprintf(key, sizeof(key), "_dyn_%p", child);
50 ret = ec_keyval_set(ec_parse_get_attrs(parse), key, child,
53 child = NULL; /* already freed */
57 return ec_node_parse_child(child, parse, strvec);
65 ec_node_dynamic_complete(const struct ec_node *gen_node,
67 const struct ec_strvec *strvec)
69 struct ec_node_dynamic *node = (struct ec_node_dynamic *)gen_node;
70 struct ec_parse *parse;
71 struct ec_node *child = NULL;
72 void (*node_free)(struct ec_node *) = ec_node_free;
76 parse = ec_comp_get_state(comp);
77 child = node->build(parse, node->opaque);
81 /* add the node pointer in the attributes, so it will be freed
82 * when parse is freed */
83 snprintf(key, sizeof(key), "_dyn_%p", child);
84 ret = ec_keyval_set(comp->attrs, key, child,
87 child = NULL; /* already freed */
91 return ec_node_complete_child(child, comp, strvec);
98 static struct ec_node_type ec_node_dynamic_type = {
100 .parse = ec_node_dynamic_parse,
101 .complete = ec_node_dynamic_complete,
102 .size = sizeof(struct ec_node_dynamic),
106 ec_node_dynamic(const char *id, ec_node_dynamic_build_t build, void *opaque)
108 struct ec_node *gen_node = NULL;
109 struct ec_node_dynamic *node;
116 gen_node = __ec_node(&ec_node_dynamic_type, id);
117 if (gen_node == NULL)
120 node = (struct ec_node_dynamic *)gen_node;
122 node->opaque = opaque;
127 ec_node_free(gen_node);
132 EC_NODE_TYPE_REGISTER(ec_node_dynamic_type);
134 static struct ec_node *
135 build_counter(struct ec_parse *parse, void *opaque)
137 const struct ec_node *node;
138 struct ec_parse *iter;
139 unsigned int count = 0;
143 for (iter = ec_parse_get_root(parse); iter != NULL;
144 iter = ec_parse_iter_next(iter)) {
145 node = ec_parse_get_node(iter);
146 if (node->id && !strcmp(node->id, "my-id"))
149 snprintf(buf, sizeof(buf), "count-%u", count);
151 return ec_node_str("my-id", buf);
154 /* LCOV_EXCL_START */
155 static int ec_node_dynamic_testcase(void)
157 struct ec_node *node;
160 node = ec_node_many(EC_NO_ID,
161 ec_node_dynamic(EC_NO_ID, build_counter, NULL),
164 EC_LOG(EC_LOG_ERR, "cannot create node\n");
167 testres |= EC_TEST_CHECK_PARSE(node, 1, "count-0");
168 testres |= EC_TEST_CHECK_PARSE(node, 3, "count-0", "count-1", "count-2");
169 testres |= EC_TEST_CHECK_PARSE(node, 1, "count-0", "count-0");
171 /* test completion */
173 testres |= EC_TEST_CHECK_COMPLETE(node,
174 "c", EC_NODE_ENDLIST,
175 "count-0", EC_NODE_ENDLIST);
176 testres |= EC_TEST_CHECK_COMPLETE(node,
177 "count-0", "", EC_NODE_ENDLIST,
178 "count-1", EC_NODE_ENDLIST,
186 static struct ec_test ec_node_dynamic_test = {
187 .name = "node_dynamic",
188 .test = ec_node_dynamic_testcase,
191 EC_TEST_REGISTER(ec_node_dynamic_test);