X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=src%2Fecoli_node_any.c;fp=src%2Fecoli_node_any.c;h=9166dbbd5fa0799794c25f31d74ea72799b2bfec;hb=18d03456d96f7a086a2ccc82ce97fcf056848d90;hp=0000000000000000000000000000000000000000;hpb=a1571d413d2acac5d4a4fbdf2e50b2d1a6da3aa6;p=protos%2Flibecoli.git diff --git a/src/ecoli_node_any.c b/src/ecoli_node_any.c new file mode 100644 index 0000000..9166dbb --- /dev/null +++ b/src/ecoli_node_any.c @@ -0,0 +1,141 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright 2016, Olivier MATZ + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +EC_LOG_TYPE_REGISTER(node_any); + +struct ec_node_any { + struct ec_node gen; + char *attr_name; +}; + +static int ec_node_any_parse(const struct ec_node *gen_node, + struct ec_parse *state, + const struct ec_strvec *strvec) +{ + struct ec_node_any *node = (struct ec_node_any *)gen_node; + const struct ec_keyval *attrs; + + (void)state; + + if (ec_strvec_len(strvec) == 0) + return EC_PARSE_NOMATCH; + if (node->attr_name != NULL) { + attrs = ec_strvec_get_attrs(strvec, 0); + if (attrs == NULL || !ec_keyval_has_key(attrs, node->attr_name)) + return EC_PARSE_NOMATCH; + } + + return 1; +} + +static void ec_node_any_free_priv(struct ec_node *gen_node) +{ + struct ec_node_any *node = (struct ec_node_any *)gen_node; + + ec_free(node->attr_name); +} + +static const struct ec_config_schema ec_node_any_schema[] = { + { + .key = "attr", + .desc = "The optional attribute name to attach.", + .type = EC_CONFIG_TYPE_STRING, + }, + { + .type = EC_CONFIG_TYPE_NONE, + }, +}; + +static int ec_node_any_set_config(struct ec_node *gen_node, + const struct ec_config *config) +{ + struct ec_node_any *node = (struct ec_node_any *)gen_node; + const struct ec_config *value = NULL; + char *s = NULL; + + value = ec_config_dict_get(config, "attr"); + if (value != NULL) { + s = ec_strdup(value->string); + if (s == NULL) + goto fail; + } + + ec_free(node->attr_name); + node->attr_name = s; + + return 0; + +fail: + ec_free(s); + return -1; +} + +static struct ec_node_type ec_node_any_type = { + .name = "any", + .schema = ec_node_any_schema, + .set_config = ec_node_any_set_config, + .parse = ec_node_any_parse, + .complete = ec_node_complete_unknown, + .size = sizeof(struct ec_node_any), + .free_priv = ec_node_any_free_priv, +}; + +EC_NODE_TYPE_REGISTER(ec_node_any_type); + +/* LCOV_EXCL_START */ +static int ec_node_any_testcase(void) +{ + struct ec_node *node; + int testres = 0; + + node = ec_node("any", EC_NO_ID); + if (node == NULL) { + EC_LOG(EC_LOG_ERR, "cannot create node\n"); + return -1; + } + testres |= EC_TEST_CHECK_PARSE(node, 1, "foo"); + testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar"); + testres |= EC_TEST_CHECK_PARSE(node, -1); + ec_node_free(node); + + /* never completes */ + node = ec_node("any", EC_NO_ID); + if (node == NULL) { + EC_LOG(EC_LOG_ERR, "cannot create node\n"); + return -1; + } + testres |= EC_TEST_CHECK_COMPLETE(node, + "", EC_NODE_ENDLIST, + EC_NODE_ENDLIST); + testres |= EC_TEST_CHECK_COMPLETE(node, + "foo", EC_NODE_ENDLIST, + EC_NODE_ENDLIST); + ec_node_free(node); + + return testres; +} +/* LCOV_EXCL_STOP */ + +static struct ec_test ec_node_any_test = { + .name = "node_any", + .test = ec_node_any_testcase, +}; + +EC_TEST_REGISTER(ec_node_any_test);