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_dict.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 {
27 ec_node_dynamic_build_t build;
32 ec_node_dynamic_parse(const struct ec_node *node,
33 struct ec_pnode *parse,
34 const struct ec_strvec *strvec)
36 struct ec_node_dynamic *priv = ec_node_priv(node);
37 struct ec_node *child = NULL;
38 void (*node_free)(struct ec_node *) = ec_node_free;
42 child = priv->build(parse, priv->opaque);
46 /* add the node pointer in the attributes, so it will be freed
47 * when parse is freed */
48 snprintf(key, sizeof(key), "_dyn_%p", child);
49 ret = ec_dict_set(ec_pnode_get_attrs(parse), key, child,
52 child = NULL; /* already freed */
56 return ec_parse_child(child, parse, strvec);
64 ec_node_dynamic_complete(const struct ec_node *node,
66 const struct ec_strvec *strvec)
68 struct ec_node_dynamic *priv = ec_node_priv(node);
69 struct ec_pnode *parse;
70 struct ec_node *child = NULL;
71 void (*node_free)(struct ec_node *) = ec_node_free;
75 parse = ec_comp_get_cur_pstate(comp);
76 child = priv->build(parse, priv->opaque);
80 /* add the node pointer in the attributes, so it will be freed
81 * when parse is freed */
82 snprintf(key, sizeof(key), "_dyn_%p", child);
83 ret = ec_dict_set(ec_comp_get_attrs(comp), key, child,
86 child = NULL; /* already freed */
90 return ec_complete_child(child, comp, strvec);
97 static struct ec_node_type ec_node_dynamic_type = {
99 .parse = ec_node_dynamic_parse,
100 .complete = ec_node_dynamic_complete,
101 .size = sizeof(struct ec_node_dynamic),
105 ec_node_dynamic(const char *id, ec_node_dynamic_build_t build, void *opaque)
107 struct ec_node *node = NULL;
108 struct ec_node_dynamic *priv;
115 node = ec_node_from_type(&ec_node_dynamic_type, id);
119 priv = ec_node_priv(node);
121 priv->opaque = opaque;
131 EC_NODE_TYPE_REGISTER(ec_node_dynamic_type);
133 static struct ec_node *
134 build_counter(struct ec_pnode *parse, void *opaque)
136 const struct ec_node *node;
137 struct ec_pnode *root, *iter;
138 unsigned int count = 0;
142 root = ec_pnode_get_root(parse);
143 for (iter = root; iter != NULL;
144 iter = EC_PNODE_ITER_NEXT(root, iter, 1)) {
145 node = ec_pnode_get_node(iter);
146 if (!strcmp(ec_node_id(node), "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,
175 "count-0", EC_VA_END);
176 testres |= EC_TEST_CHECK_COMPLETE(node,
177 "count-0", "", EC_VA_END,
178 "count-1", EC_VA_END,
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);