Allow YAML input to be just a list or scalar as well as a map.

This allows getdns_yaml2list(), getdns_yaml2bindata() and getdns_yaml2int() to work as expected.

Update the YAML test to check these.
This commit is contained in:
Jim Hague 2017-09-13 17:29:41 +01:00
parent 019ce4afe5
commit 8139201f12
3 changed files with 79 additions and 13 deletions

View File

@ -32,29 +32,80 @@ int main(int ac, char *av[])
buf[bytes_read] = '\0'; buf[bytes_read] = '\0';
getdns_dict *dict = NULL; getdns_dict *dict = NULL;
getdns_list *list = NULL;
getdns_bindata *bindata = NULL;
getdns_return_t r; getdns_return_t r;
if (!(dict = getdns_dict_create())) { if (!(dict = getdns_dict_create())) {
fprintf(stderr, "Could not create dict"); fprintf(stderr, "Could not create dict");
exit(EXIT_FAILURE); goto fail;
} }
r = getdns_yaml2dict(buf, &dict); r = getdns_yaml2dict(buf, &dict);
if (r) { if (r) {
fprintf(stderr, "Error setting dict data: %s", getdns_get_errorstr_by_id(r)); fprintf(stderr, "Error setting dict data: %s", getdns_get_errorstr_by_id(r));
getdns_dict_destroy(dict); goto fail;
exit(EXIT_FAILURE); }
/*
* Now add a list, bindata and int to the dict by hand to check
* the other yaml2* functions work.
*/
if (!(list = getdns_list_create())) {
fprintf(stderr, "Could not create list");
goto fail;
}
r = getdns_yaml2list("[\"One\", \"two\", \"three\"]", &list);
if (r) {
fprintf(stderr, "Error setting list data: %s", getdns_get_errorstr_by_id(r));
goto fail;
}
r = getdns_dict_set_list(dict, "List entry", list);
if (r) {
fprintf(stderr, "Error adding list to dict: %s", getdns_get_errorstr_by_id(r));
goto fail;
}
r = getdns_yaml2bindata("2001:7fd::1", &bindata);
if (r) {
fprintf(stderr, "Error setting bindata: %s", getdns_get_errorstr_by_id(r));
goto fail;
}
r = getdns_dict_set_bindata(dict, "Bindata entry", bindata);
if (r) {
fprintf(stderr, "Error adding list to dict: %s", getdns_get_errorstr_by_id(r));
goto fail;
}
uint32_t intval;
r = getdns_yaml2int("32767", &intval);
if (r) {
fprintf(stderr, "Error setting int: %s", getdns_get_errorstr_by_id(r));
goto fail;
}
if (intval != 32767) {
fprintf(stderr, "Error reading int: wrong value");
goto fail;
} }
char *dict_str = getdns_pretty_print_dict(dict); char *dict_str = getdns_pretty_print_dict(dict);
if (!dict_str) { if (!dict_str) {
fprintf(stderr, "Could not convert dict to string"); fprintf(stderr, "Could not convert dict to string");
getdns_dict_destroy(dict); goto fail;
exit(EXIT_FAILURE);
} }
printf("%s\n", dict_str); printf("%s\n", dict_str);
free(dict_str); free(dict_str);
getdns_dict_destroy(dict); getdns_dict_destroy(dict);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
fail:
if (dict)
getdns_dict_destroy(dict);
if (list)
getdns_list_destroy(list);
exit(EXIT_FAILURE);
} }

View File

@ -1,4 +1,11 @@
{ {
"Bindata entry": <bindata of 0x200107fd000000000000000000000001>,
"List entry":
[
<bindata of "One">,
<bindata of "two">,
<bindata of "three">
],
"dnssec_return_status": 1000, "dnssec_return_status": 1000,
"upstream_recursive_servers": "upstream_recursive_servers":
[ [

View File

@ -206,23 +206,31 @@ process_yaml_document(yaml_parser_t *parser, yaml_event_t *event, gldns_buffer *
break; break;
case YAML_MAPPING_START_EVENT: case YAML_MAPPING_START_EVENT:
/*
* getdns config data is a dictionary (ie yaml mapping)
* so the document must start with a mapping; scalar
* or sequence would be wrong.
*/
if (process_yaml_mapping(parser, event, buf) != 0) { if (process_yaml_mapping(parser, event, buf) != 0) {
return -1; return -1;
} }
break; break;
case YAML_SEQUENCE_START_EVENT:
if (process_yaml_sequence(parser, event, buf) != 0) {
return -1;
}
break;
case YAML_SCALAR_EVENT:
if (output_scalar(event, buf) != 0) {
fprintf(stderr, "Value error: Error outputting scalar\n");
return -1;
}
break;
case YAML_STREAM_START_EVENT: case YAML_STREAM_START_EVENT:
case YAML_STREAM_END_EVENT: case YAML_STREAM_END_EVENT:
case YAML_DOCUMENT_START_EVENT: case YAML_DOCUMENT_START_EVENT:
case YAML_ALIAS_EVENT: case YAML_ALIAS_EVENT:
case YAML_SCALAR_EVENT:
case YAML_SEQUENCE_START_EVENT:
case YAML_SEQUENCE_END_EVENT: case YAML_SEQUENCE_END_EVENT:
case YAML_MAPPING_END_EVENT: case YAML_MAPPING_END_EVENT: