[getdns-api] Getting from dicts by subscript operator

Willem Toorop willem at nlnetlabs.nl
Fri Sep 18 09:13:29 UTC 2015


Dear getdns API designers,

Getting into the specific parts of getdns data structures (notably the
response dict) can be cumbersome in C.  Take for example this
(relatively short) example on how to get to the AD bit of the first
reply of a response:

getdns_list *replies_tree;
getdns_dict *reply;
getdns_dict *header;
uint32_t     ad;

if ((r = getdns_dict_get_list(resp, "replies_tree", &replies_tree)))
	return r;
else if ((r = getdns_list_get_dict(replies_tree, 0, &reply)))
	return r;
else if ((r = getdns_dict_get_dict(reply, "header", &header)))
	return r;
else if ((r = getdns_dict_get_int(header, "ad", &ad)))
	return r;


Also, getting data members by text strings like getdns does, is
unconventional in C libraries, but makes getdns very flexible and easy
to extend.  It also maps very well to modern scripting languages
(python, javascript, perl, ruby etc.).

Why don't we take this approach one step further, and get to the data
members by allowing the text strings to contain subscript operators into
the sub-dicts and sub-lists?

To do the same as the example above with subscript operator:

uint32_t ad;

if ((r = getdns_dict_get_int(resp, "replies_tree[0][header][ad]", &ad)))
	return r;

The conciseness and thereby clarity and readability is increasingly more
prominent with more involved examples.


What do you think?

-- Willem



More information about the Users mailing list