diff mbox series

[v3,1/2] btrfs-progs: common: extend fmt_print_start_group handles unnamed group

Message ID 20201211164812.459012-1-realwakka@gmail.com (mailing list archive)
State New, archived
Headers show
Series [v3,1/2] btrfs-progs: common: extend fmt_print_start_group handles unnamed group | expand

Commit Message

Sidong Yang Dec. 11, 2020, 4:48 p.m. UTC
This patch extends fmt_print_start_group() that it can handle when name
argument is NULL. It is useful for printing unnamed array or map.

Signed-off-by: Sidong Yang <realwakka@gmail.com>
---
v3:
 - extend fmt_print_start_group rather than writing new function
---
 common/format-output.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

Comments

David Sterba Dec. 11, 2020, 5:31 p.m. UTC | #1
On Fri, Dec 11, 2020 at 04:48:11PM +0000, Sidong Yang wrote:
> This patch extends fmt_print_start_group() that it can handle when name
> argument is NULL. It is useful for printing unnamed array or map.
> 
> Signed-off-by: Sidong Yang <realwakka@gmail.com>
> ---
> v3:
>  - extend fmt_print_start_group rather than writing new function
> ---
>  common/format-output.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/common/format-output.c b/common/format-output.c
> index 8df93ecb..2f74595c 100644
> --- a/common/format-output.c
> +++ b/common/format-output.c
> @@ -181,17 +181,23 @@ void fmt_end_value(struct format_ctx *fctx, const struct rowspec *row)
>  void fmt_print_start_group(struct format_ctx *fctx, const char *name,
>  		enum json_type jtype)
>  {
> +	char bracket;
>  	if (bconf.output_format == CMD_FORMAT_JSON) {
>  		fmt_separator(fctx);
>  		fmt_inc_depth(fctx);
>  		fctx->jtype[fctx->depth] = jtype;
>  		fctx->memb[fctx->depth] = 0;

This can be simplified a bit, the name can be conditionally printed here

>  		if (jtype == JSON_TYPE_MAP)
> -			printf("\"%s\": {", name);
> +			bracket = '{';

and this just does the right putchar(). With this change now added to
devel, thanks.

>  		else if (jtype == JSON_TYPE_ARRAY)
> -			printf("\"%s\": [", name);
> +			bracket = '[';
>  		else
>  			fmt_error(fctx);
> +
> +		if (name)
> +			printf("\"%s\": %c", name, bracket);
> +		else
> +			putchar(bracket);
diff mbox series

Patch

diff --git a/common/format-output.c b/common/format-output.c
index 8df93ecb..2f74595c 100644
--- a/common/format-output.c
+++ b/common/format-output.c
@@ -181,17 +181,23 @@  void fmt_end_value(struct format_ctx *fctx, const struct rowspec *row)
 void fmt_print_start_group(struct format_ctx *fctx, const char *name,
 		enum json_type jtype)
 {
+	char bracket;
 	if (bconf.output_format == CMD_FORMAT_JSON) {
 		fmt_separator(fctx);
 		fmt_inc_depth(fctx);
 		fctx->jtype[fctx->depth] = jtype;
 		fctx->memb[fctx->depth] = 0;
 		if (jtype == JSON_TYPE_MAP)
-			printf("\"%s\": {", name);
+			bracket = '{';
 		else if (jtype == JSON_TYPE_ARRAY)
-			printf("\"%s\": [", name);
+			bracket = '[';
 		else
 			fmt_error(fctx);
+
+		if (name)
+			printf("\"%s\": %c", name, bracket);
+		else
+			putchar(bracket);
 	}
 }