From 17537a36c833684c2a80afa6e6f95553dac264c6 Mon Sep 17 00:00:00 2001 From: Mark Walters Date: Thu, 12 Jul 2012 10:21:51 +0100 Subject: [PATCH] Re: [PATCH v4 1/3] Add support for structured output formatters. --- f4/763c2bb8f7b6e41c83bd14e23e957ae7a064fc | 219 ++++++++++++++++++++++ 1 file changed, 219 insertions(+) create mode 100644 f4/763c2bb8f7b6e41c83bd14e23e957ae7a064fc diff --git a/f4/763c2bb8f7b6e41c83bd14e23e957ae7a064fc b/f4/763c2bb8f7b6e41c83bd14e23e957ae7a064fc new file mode 100644 index 000000000..3db31b798 --- /dev/null +++ b/f4/763c2bb8f7b6e41c83bd14e23e957ae7a064fc @@ -0,0 +1,219 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id B57D0431FC7 + for ; Thu, 12 Jul 2012 02:21:56 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -1.098 +X-Spam-Level: +X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5 + tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001, + NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id jFHSvwIOdpCj for ; + Thu, 12 Jul 2012 02:21:56 -0700 (PDT) +Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6]) + (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id C60C9431FAE + for ; Thu, 12 Jul 2012 02:21:55 -0700 (PDT) +Received: from smtp.qmul.ac.uk ([138.37.6.40]) + by mail2.qmul.ac.uk with esmtp (Exim 4.71) + (envelope-from ) + id 1SpFar-0003s2-SS; Thu, 12 Jul 2012 10:21:54 +0100 +Received: from 94-192-233-223.zone6.bethere.co.uk ([94.192.233.223] + helo=localhost) + by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.69) + (envelope-from ) + id 1SpFar-0001iA-F5; Thu, 12 Jul 2012 10:21:53 +0100 +From: Mark Walters +To: craven@gmx.net, notmuch@notmuchmail.org +Subject: Re: [PATCH v4 1/3] Add support for structured output formatters. +In-Reply-To: <1342079004-5300-2-git-send-email-craven@gmx.net> +References: <87d34hsdx8.fsf@awakening.csail.mit.edu> + <1342079004-5300-1-git-send-email-craven@gmx.net> + <1342079004-5300-2-git-send-email-craven@gmx.net> +User-Agent: Notmuch/0.13.2+61~gf708609 (http://notmuchmail.org) Emacs/23.4.1 + (x86_64-pc-linux-gnu) +Date: Thu, 12 Jul 2012 10:21:51 +0100 +Message-ID: <87pq81tjv4.fsf@qmul.ac.uk> +MIME-Version: 1.0 +Content-Type: text/plain; charset=us-ascii +X-Sender-Host-Address: 94.192.233.223 +X-QM-SPAM-Info: Sender has good ham record. :) +X-QM-Body-MD5: 3f501e9c07fc91483d22fb48a286358c (of first 20000 bytes) +X-SpamAssassin-Score: -1.8 +X-SpamAssassin-SpamBar: - +X-SpamAssassin-Report: The QM spam filters have analysed this message to + determine if it is + spam. We require at least 5.0 points to mark a message as spam. + This message scored -1.8 points. + Summary of the scoring: + * -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/, + * medium trust + * [138.37.6.40 listed in list.dnswl.org] + * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail + provider * (markwalters1009[at]gmail.com) + * -0.0 T_RP_MATCHES_RCVD Envelope sender domain matches handover relay + * domain + * 0.5 AWL AWL: From: address is in the auto white-list +X-QM-Scan-Virus: ClamAV says the message is clean +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Thu, 12 Jul 2012 09:21:56 -0000 + + +A couple more things: + +On Thu, 12 Jul 2012, craven@gmx.net wrote: +> This patch adds a new type sprinter_t, which is used for structured +> formatting, e.g. JSON or S-Expressions. The structure printer is the +> code from Austin Clements (id:87d34hsdx8.fsf@awakening.csail.mit.edu). +> +> The structure printer contains the following function pointers: +> +> /* start a new map/dictionary structure. +> */ +> void (*begin_map) (struct sprinter *); +> +> /* start a new list/array structure +> */ +> void (*begin_list) (struct sprinter *); +> +> /* end the last opened list or map structure +> */ +> void (*end) (struct sprinter *); + +what is the advantage of having this as one function rather than end_map +and end_list? Indeed, my choice (but I think most other people would +disagree) would be to have both functions but still keep state as this +currently does and then throw an error if the code closes the wrong +thing. + +A second question: do you have an implementation in this style for +s-expressions. I find it hard to tell whether the interface is right +with just a single example. Even a completely hacky not ready for review +example would be helpful. + +Best wishes + +Mark + +> /* print one string/integer/boolean/null element (possibly inside a +> * list or map +> */ +> void (*string) (struct sprinter *, const char *); +> void (*integer) (struct sprinter *, int); +> void (*boolean) (struct sprinter *, notmuch_bool_t); +> void (*null) (struct sprinter *); +> +> /* print the key of a map's key/value pair. +> */ +> void (*map_key) (struct sprinter *, const char *); +> +> /* print a frame delimiter (such as a newline or extra whitespace) +> */ +> void (*frame) (struct sprinter *); +> +> The printer can (and should) use internal state to insert delimiters and +> syntax at the correct places. +> +> Example: +> +> format->begin_map(format); +> format->map_key(format, "foo"); +> format->begin_list(format); +> format->integer(format, 1); +> format->integer(format, 2); +> format->integer(format, 3); +> format->end(format); +> format->map_key(format, "bar"); +> format->begin_map(format); +> format->map_key(format, "baaz"); +> format->string(format, "hello world"); +> format->end(format); +> format->end(format); +> +> would output JSON as follows: +> +> {"foo": [1, 2, 3], "bar": { "baaz": "hello world"}} +> --- +> sprinter.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ +> 1 file changed, 49 insertions(+) +> create mode 100644 sprinter.h +> +> diff --git a/sprinter.h b/sprinter.h +> new file mode 100644 +> index 0000000..1dad9a0 +> --- /dev/null +> +++ b/sprinter.h +> @@ -0,0 +1,49 @@ +> +#ifndef NOTMUCH_SPRINTER_H +> +#define NOTMUCH_SPRINTER_H +> + +> +/* for notmuch_bool_t */ +> +#include "notmuch-client.h" +> + +> +/* Structure printer interface */ +> +typedef struct sprinter +> +{ +> + /* start a new map/dictionary structure. +> + */ +> + void (*begin_map) (struct sprinter *); +> + +> + /* start a new list/array structure +> + */ +> + void (*begin_list) (struct sprinter *); +> + +> + /* end the last opened list or map structure +> + */ +> + void (*end) (struct sprinter *); +> + +> + /* print one string/integer/boolean/null element (possibly inside a +> + * list or map +> + */ +> + void (*string) (struct sprinter *, const char *); +> + void (*integer) (struct sprinter *, int); +> + void (*boolean) (struct sprinter *, notmuch_bool_t); +> + void (*null) (struct sprinter *); +> + +> + /* print the key of a map's key/value pair. +> + */ +> + void (*map_key) (struct sprinter *, const char *); +> + +> + /* print a frame delimiter (such as a newline or extra whitespace) +> + */ +> + void (*frame) (struct sprinter *); +> +} sprinter_t; +> + +> +/* Create a new structure printer that emits JSON */ +> +struct sprinter * +> +sprinter_json_new(const void *ctx, FILE *stream); +> + +> +/* A dummy structure printer that signifies that standard text output is +> + * to be used instead of any structured format. +> + */ +> +struct sprinter * +> +sprinter_text; +> + +> +#endif // NOTMUCH_SPRINTER_H +> -- +> 1.7.11.1 +> +> _______________________________________________ +> notmuch mailing list +> notmuch@notmuchmail.org +> http://notmuchmail.org/mailman/listinfo/notmuch -- 2.26.2