netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ana Rey <anarey@gmail.com>
To: netfilter-devel@vger.kernel.org
Cc: Ana Rey <anarey@gmail.com>
Subject: [libnftnl PATCH 4/5] expr: queue: Do not print unset values in json
Date: Fri, 13 Jun 2014 11:31:52 +0200	[thread overview]
Message-ID: <1402651913-15065-5-git-send-email-anarey@gmail.com> (raw)
In-Reply-To: <1402651913-15065-1-git-send-email-anarey@gmail.com>

It changes the parse and the snprint functions to omit unset values.

This json file is gotten for a queue:

{"expr":[{"type":"queue","num":0,"total":1,"flags":0}]}

Signed-off-by: Ana Rey <anarey@gmail.com>
---
 src/expr/queue.c | 46 +++++++++++++++++++++++++++++-----------------
 1 file changed, 29 insertions(+), 17 deletions(-)

diff --git a/src/expr/queue.c b/src/expr/queue.c
index 5235027..55d2ab0 100644
--- a/src/expr/queue.c
+++ b/src/expr/queue.c
@@ -137,20 +137,14 @@ nft_rule_expr_queue_json_parse(struct nft_rule_expr *e, json_t *root,
 	uint32_t type;
 	uint16_t code;
 
-	if (nft_jansson_parse_val(root, "num", NFT_TYPE_U16, &type, err) < 0)
-		return -1;
-
-	nft_rule_expr_set_u32(e, NFT_EXPR_QUEUE_NUM, type);
-
-	if (nft_jansson_parse_val(root, "total", NFT_TYPE_U16, &code, err) < 0)
-		return -1;
-
-	nft_rule_expr_set_u16(e, NFT_EXPR_QUEUE_TOTAL, code);
+	if (nft_jansson_parse_val(root, "num", NFT_TYPE_U16, &type, err) == 0)
+		nft_rule_expr_set_u32(e, NFT_EXPR_QUEUE_NUM, type);
 
-	if (nft_jansson_parse_val(root, "flags", NFT_TYPE_U16, &code, err) < 0)
-		return -1;
+	if (nft_jansson_parse_val(root, "total", NFT_TYPE_U16, &code, err) == 0)
+		nft_rule_expr_set_u16(e, NFT_EXPR_QUEUE_TOTAL, code);
 
-	nft_rule_expr_set_u16(e, NFT_EXPR_QUEUE_FLAGS, code);
+	if (nft_jansson_parse_val(root, "flags", NFT_TYPE_U16, &code, err) == 0)
+		nft_rule_expr_set_u16(e, NFT_EXPR_QUEUE_FLAGS, code);
 
 	return 0;
 #else
@@ -241,13 +235,31 @@ static int nft_rule_expr_queue_snprintf_xml(char *buf, size_t len,
 static int nft_rule_expr_queue_snprintf_json(char *buf, size_t len,
 					     struct nft_rule_expr *e)
 {
+	int ret, size = len, offset = 0;
 	struct nft_expr_queue *queue = nft_expr_data(e);
 
-	return snprintf(buf, len, "\"num\":%u,"
-				  "\"total\":%u,"
-				  "\"flags\":%u,",
-			queue->queuenum, queue->queues_total,
-			queue->flags);
+	if (e->flags & (1 << NFT_EXPR_QUEUE_NUM)) {
+		ret = snprintf(buf + offset, len, "\"num\":%u,",
+			       queue->queuenum);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+
+	if (e->flags & (1 << NFT_EXPR_QUEUE_TOTAL)) {
+		ret = snprintf(buf + offset, len, "\"total\":%u,",
+			       queue->queues_total);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+	if (e->flags & (1 << NFT_EXPR_QUEUE_FLAGS)) {
+		ret = snprintf(buf + offset, len, "\"flags\":%u,",
+			       queue->flags);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+
+	/* Remove the last comma characther */
+	if (offset > 0)
+		offset--;
+
+	return offset;
 }
 
 static int
-- 
2.0.0


  parent reply	other threads:[~2014-06-13  9:32 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-13  9:31 [libnftnl PATCH 0/5] queue: Do not print unset value Ana Rey
2014-06-13  9:31 ` [libnftnl PATCH 1/5] expr: queue: Add nft_rule_expr_queue_snprinf_* functions Ana Rey
2014-06-13 10:16   ` Arturo Borrero Gonzalez
2014-06-13  9:31 ` [libnftnl PATCH 2/5] expr: queue: Use nft_rule_expr_* in the xml Ana Rey
2014-06-13  9:31 ` [libnftnl PATCH 3/5] expr: queue: Do not print unset values in xml Ana Rey
2014-06-13  9:31 ` Ana Rey [this message]
2014-06-13  9:31 ` [libnftnl PATCH 5/5] expr: queue: Use the correct data type Ana Rey
2014-06-13 10:51 ` [libnftnl PATCH 0/5] queue: Do not print unset value Arturo Borrero Gonzalez
2014-06-16 10:11 ` Pablo Neira Ayuso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1402651913-15065-5-git-send-email-anarey@gmail.com \
    --to=anarey@gmail.com \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).