1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
10 #include <ecoli_malloc.h>
11 #include <ecoli_log.h>
12 #include <ecoli_test.h>
13 #include <ecoli_strvec.h>
14 #include <ecoli_node.h>
15 #include <ecoli_parse.h>
16 #include <ecoli_complete.h>
17 #include <ecoli_keyval.h>
18 #include <ecoli_config.h>
19 #include <ecoli_node_any.h>
21 EC_LOG_TYPE_REGISTER(node_any);
28 static int ec_node_any_parse(const struct ec_node *gen_node,
29 struct ec_parse *state,
30 const struct ec_strvec *strvec)
32 struct ec_node_any *node = (struct ec_node_any *)gen_node;
33 const struct ec_keyval *attrs;
37 if (ec_strvec_len(strvec) == 0)
38 return EC_PARSE_NOMATCH;
39 if (node->attr_name != NULL) {
40 attrs = ec_strvec_get_attrs(strvec, 0);
41 if (attrs == NULL || !ec_keyval_has_key(attrs, node->attr_name))
42 return EC_PARSE_NOMATCH;
48 static void ec_node_any_free_priv(struct ec_node *gen_node)
50 struct ec_node_any *node = (struct ec_node_any *)gen_node;
52 ec_free(node->attr_name);
55 static const struct ec_config_schema ec_node_any_schema[] = {
58 .desc = "The optional attribute name to attach.",
59 .type = EC_CONFIG_TYPE_STRING,
62 .type = EC_CONFIG_TYPE_NONE,
66 static int ec_node_any_set_config(struct ec_node *gen_node,
67 const struct ec_config *config)
69 struct ec_node_any *node = (struct ec_node_any *)gen_node;
70 const struct ec_config *value = NULL;
73 value = ec_config_dict_get(config, "attr");
75 s = ec_strdup(value->string);
80 ec_free(node->attr_name);
90 static struct ec_node_type ec_node_any_type = {
92 .schema = ec_node_any_schema,
93 .set_config = ec_node_any_set_config,
94 .parse = ec_node_any_parse,
95 .complete = ec_node_complete_unknown,
96 .size = sizeof(struct ec_node_any),
97 .free_priv = ec_node_any_free_priv,
100 EC_NODE_TYPE_REGISTER(ec_node_any_type);
103 ec_node_any(const char *id, const char *attr)
105 struct ec_config *config = NULL;
106 struct ec_node *gen_node = NULL;
109 gen_node = ec_node_from_type(&ec_node_any_type, id);
110 if (gen_node == NULL)
113 config = ec_config_dict();
117 ret = ec_config_dict_set(config, "attr", ec_config_string(attr));
121 ret = ec_node_set_config(gen_node, config);
129 ec_config_free(config);
130 ec_node_free(gen_node);
134 /* LCOV_EXCL_START */
135 static int ec_node_any_testcase(void)
137 struct ec_node *node;
140 node = ec_node("any", EC_NO_ID);
142 EC_LOG(EC_LOG_ERR, "cannot create node\n");
145 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
146 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
147 testres |= EC_TEST_CHECK_PARSE(node, -1);
150 /* never completes */
151 node = ec_node("any", EC_NO_ID);
153 EC_LOG(EC_LOG_ERR, "cannot create node\n");
156 testres |= EC_TEST_CHECK_COMPLETE(node,
159 testres |= EC_TEST_CHECK_COMPLETE(node,
160 "foo", EC_NODE_ENDLIST,
168 static struct ec_test ec_node_any_test = {
170 .test = ec_node_any_testcase,
173 EC_TEST_REGISTER(ec_node_any_test);