From: Olivier Matz Date: Fri, 22 Feb 2019 17:30:57 +0000 (+0100) Subject: yaml: set node attributes X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=ec731385f1ed38a35e4e0df83e5ae4e8b8151df6;p=protos%2Flibecoli.git yaml: set node attributes --- diff --git a/examples/parse-yaml/parse-yaml.sh b/examples/parse-yaml/parse-yaml.sh new file mode 100644 index 0000000..1b7d029 --- /dev/null +++ b/examples/parse-yaml/parse-yaml.sh @@ -0,0 +1,140 @@ +#!/bin/sh + +set -e + +# use a safer version of echo (no option) +echo() +{ + printf "%s\n" "$*" +} + +debug() +{ + echo "$@" >&2 +} + +# $1: node sequence number (ex: ec_node4) +ec_parse_get_first_child() +{ + local first_child=${1}_first_child + echo $(eval 'echo ${'$first_child'}') +} + +# $1: node sequence number (ex: ec_node4) +ec_parse_get_next() +{ + local next=${1}_next + echo $(eval 'echo ${'$next'}') +} + +# $1: node sequence number (ex: ec_node4) +ec_parse_iter_next() +{ + local seq=${1#ec_node} + seq=$((seq+1)) + local next=ec_node${seq} + if [ "$(ec_parse_get_id $next)" != "" ]; then + echo $next + fi +} + +# $1: node sequence number (ex: ec_node4) +ec_parse_get_id() +{ + local id=${1}_id + echo $(eval 'echo ${'$id'}') +} + +# $1: node sequence number (ex: ec_node4) +ec_parse_get_strvec_len() +{ + local strvec_len=${1}_strvec_len + echo $(eval 'echo ${'$strvec_len'}') +} + +# $1: node sequence number (ex: ec_node4) +# $2: index in strvec +ec_parse_get_str() +{ + if [ $# -ne 2 ]; then + return + fi + local str=${1}_str${2} + echo $(eval 'echo ${'$str'}') +} + +# $1: node sequence number (ex: ec_node4) +# $2: node id (string) +ec_parse_find_first() +{ + if [ $# -ne 2 ]; then + return + fi + local node_seq=$1 + while [ "$node_seq" != "" ]; do + local id=$(ec_parse_get_id $node_seq) + if [ "$id" = "$2" ]; then + echo $node_seq + return 0 + fi + node_seq=$(ec_parse_iter_next $node_seq) + done +} + +path=$(dirname $0) + +yaml=$(mktemp) +cat << EOF > $yaml +type: or +children: +- type: seq + id: hello + help: Say hello to someone + children: + - type: str + string: hello + - type: or + id: name + help: Name of the person to greet + children: + - type: str + string: john + - type: str + string: mike +- type: seq + id: goodbye + help: Say good bye to someone + children: + - type: str + string: good + - type: str + string: bye + - type: or + id: name + help: Name of the person to greet + children: + - type: str + string: mary + - type: str + string: jessica +EOF + +output=$(mktemp) +match=1 +$path/build/parse-yaml -i $yaml -o $output || match=0 +if [ "$match" = "1" ]; then + cat $output + . $output + name=$(ec_parse_get_str $(ec_parse_find_first ec_node1 name) 0) + hello=$(ec_parse_get_str $(ec_parse_find_first ec_node1 hello) 0) + + if [ "$hello" != "" ]; then + echo "$name says hello to you!" + else + echo "$name says good bye to you!" + fi +else + echo "no match" +fi +rm $output +rm $yaml diff --git a/parse-yaml.sh b/parse-yaml.sh deleted file mode 100644 index 1b7d029..0000000 --- a/parse-yaml.sh +++ /dev/null @@ -1,140 +0,0 @@ -#!/bin/sh - -set -e - -# use a safer version of echo (no option) -echo() -{ - printf "%s\n" "$*" -} - -debug() -{ - echo "$@" >&2 -} - -# $1: node sequence number (ex: ec_node4) -ec_parse_get_first_child() -{ - local first_child=${1}_first_child - echo $(eval 'echo ${'$first_child'}') -} - -# $1: node sequence number (ex: ec_node4) -ec_parse_get_next() -{ - local next=${1}_next - echo $(eval 'echo ${'$next'}') -} - -# $1: node sequence number (ex: ec_node4) -ec_parse_iter_next() -{ - local seq=${1#ec_node} - seq=$((seq+1)) - local next=ec_node${seq} - if [ "$(ec_parse_get_id $next)" != "" ]; then - echo $next - fi -} - -# $1: node sequence number (ex: ec_node4) -ec_parse_get_id() -{ - local id=${1}_id - echo $(eval 'echo ${'$id'}') -} - -# $1: node sequence number (ex: ec_node4) -ec_parse_get_strvec_len() -{ - local strvec_len=${1}_strvec_len - echo $(eval 'echo ${'$strvec_len'}') -} - -# $1: node sequence number (ex: ec_node4) -# $2: index in strvec -ec_parse_get_str() -{ - if [ $# -ne 2 ]; then - return - fi - local str=${1}_str${2} - echo $(eval 'echo ${'$str'}') -} - -# $1: node sequence number (ex: ec_node4) -# $2: node id (string) -ec_parse_find_first() -{ - if [ $# -ne 2 ]; then - return - fi - local node_seq=$1 - while [ "$node_seq" != "" ]; do - local id=$(ec_parse_get_id $node_seq) - if [ "$id" = "$2" ]; then - echo $node_seq - return 0 - fi - node_seq=$(ec_parse_iter_next $node_seq) - done -} - -path=$(dirname $0) - -yaml=$(mktemp) -cat << EOF > $yaml -type: or -children: -- type: seq - id: hello - help: Say hello to someone - children: - - type: str - string: hello - - type: or - id: name - help: Name of the person to greet - children: - - type: str - string: john - - type: str - string: mike -- type: seq - id: goodbye - help: Say good bye to someone - children: - - type: str - string: good - - type: str - string: bye - - type: or - id: name - help: Name of the person to greet - children: - - type: str - string: mary - - type: str - string: jessica -EOF - -output=$(mktemp) -match=1 -$path/build/parse-yaml -i $yaml -o $output || match=0 -if [ "$match" = "1" ]; then - cat $output - . $output - name=$(ec_parse_get_str $(ec_parse_find_first ec_node1 name) 0) - hello=$(ec_parse_get_str $(ec_parse_find_first ec_node1 hello) 0) - - if [ "$hello" != "" ]; then - echo "$name says hello to you!" - else - echo "$name says good bye to you!" - fi -else - echo "no match" -fi -rm $output -rm $yaml diff --git a/src/ecoli_yaml.c b/src/ecoli_yaml.c index 84007d5..8ac47eb 100644 --- a/src/ecoli_yaml.c +++ b/src/ecoli_yaml.c @@ -360,6 +360,7 @@ parse_ec_node(struct enode_table *table, const yaml_node_pair_t *pair; const char *key_str, *value_str; struct ec_node *enode = NULL; + char *value_dup = NULL; if (ynode->type != YAML_MAPPING_NODE) { fprintf(stderr, "Ecoli node should be a yaml mapping node\n"); @@ -468,7 +469,24 @@ parse_ec_node(struct enode_table *table, } /* add attributes (all as string) */ - //XXX + if (attrs != NULL) { + for (pair = attrs->data.mapping.pairs.start; + pair < attrs->data.mapping.pairs.top; pair++) { + key = document->nodes.start + pair->key - 1; + value = document->nodes.start + pair->value - 1; + key_str = (const char *)key->data.scalar.value; + value_str = (const char *)value->data.scalar.value; + value_dup = ec_strdup(value_str); + if (value_dup == NULL) + goto fail; + if (ec_keyval_set(ec_node_attrs(enode), key_str, + value_dup, ec_free_func) < 0) { + value_dup = NULL; + goto fail; + } + value_dup = NULL; + } + } return enode; @@ -476,6 +494,7 @@ fail: ec_node_free(enode); ec_config_free(config); ec_free(help); + ec_free(value_dup); return NULL; }