[getdns-api] Getting from dicts by subscript operator

Goyal, Neel ngoyal at verisign.com
Mon Sep 21 19:54:00 CEST 2015


I like it - however I¹d suggest tweaking the syntax a bit:

"replies_tree[0][header][ad]² would become "replies_tree[0].header.ad²


So the bracket syntax indicates that it¹s an array index and then the dot
specifier indicates it¹s a field within a dict.


On 9/21/15, 12:26 PM, "spec on behalf of Wessels, Duane"
<spec-bounces at getdnsapi.net on behalf of dwessels at verisign.com> wrote:

>Willem,
>
>I think this idea is really great!
>
>
>> On Sep 18, 2015, at 2:13 AM, Willem Toorop <Willem at nlnetlabs.nl> wrote:
>> 
>> 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
>> _______________________________________________
>> spec mailing list
>> spec at getdnsapi.net
>




More information about the spec mailing list