[PATCH for-5.1 2/5] qobject: Factor out helper json_pretty_newline()

Markus Armbruster posted 5 patches 5 years, 9 months ago
Maintainers: Markus Armbruster <armbru@redhat.com>, Michael Roth <mdroth@linux.vnet.ibm.com>
[PATCH for-5.1 2/5] qobject: Factor out helper json_pretty_newline()
Posted by Markus Armbruster 5 years, 9 months ago
Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 qobject/qjson.c | 40 ++++++++++++++++------------------------
 1 file changed, 16 insertions(+), 24 deletions(-)

diff --git a/qobject/qjson.c b/qobject/qjson.c
index db36101f3b..f3c62711b9 100644
--- a/qobject/qjson.c
+++ b/qobject/qjson.c
@@ -159,21 +159,28 @@ typedef struct ToJsonIterState
 
 static void to_json(const QObject *obj, QString *str, int pretty, int indent);
 
+static void json_pretty_newline(QString *str, bool pretty, int indent)
+{
+    int i;
+
+    if (pretty) {
+        qstring_append(str, "\n");
+        for (i = 0 ; i < indent ; i++) {
+            qstring_append(str, "    ");
+        }
+    }
+}
+
 static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
 {
     ToJsonIterState *s = opaque;
     QString *qkey;
-    int j;
 
     if (s->count) {
         qstring_append(s->str, s->pretty ? "," : ", ");
     }
 
-    if (s->pretty) {
-        qstring_append(s->str, "\n");
-        for (j = 0 ; j < s->indent ; j++)
-            qstring_append(s->str, "    ");
-    }
+    json_pretty_newline(s->str, s->pretty, s->indent);
 
     qkey = qstring_from_str(key);
     to_json(QOBJECT(qkey), s->str, s->pretty, s->indent);
@@ -187,17 +194,12 @@ static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
 static void to_json_list_iter(QObject *obj, void *opaque)
 {
     ToJsonIterState *s = opaque;
-    int j;
 
     if (s->count) {
         qstring_append(s->str, s->pretty ? "," : ", ");
     }
 
-    if (s->pretty) {
-        qstring_append(s->str, "\n");
-        for (j = 0 ; j < s->indent ; j++)
-            qstring_append(s->str, "    ");
-    }
+    json_pretty_newline(s->str, s->pretty, s->indent);
 
     to_json(obj, s->str, s->pretty, s->indent);
     s->count++;
@@ -282,12 +284,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)
         s.pretty = pretty;
         qstring_append(str, "{");
         qdict_iter(val, to_json_dict_iter, &s);
-        if (pretty) {
-            int j;
-            qstring_append(str, "\n");
-            for (j = 0 ; j < indent ; j++)
-                qstring_append(str, "    ");
-        }
+        json_pretty_newline(str, pretty, indent);
         qstring_append(str, "}");
         break;
     }
@@ -301,12 +298,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)
         s.pretty = pretty;
         qstring_append(str, "[");
         qlist_iter(val, (void *)to_json_list_iter, &s);
-        if (pretty) {
-            int j;
-            qstring_append(str, "\n");
-            for (j = 0 ; j < indent ; j++)
-                qstring_append(str, "    ");
-        }
+        json_pretty_newline(str, pretty, indent);
         qstring_append(str, "]");
         break;
     }
-- 
2.21.1


Re: [PATCH for-5.1 2/5] qobject: Factor out helper json_pretty_newline()
Posted by Eric Blake 5 years, 9 months ago
On 4/15/20 3:30 AM, Markus Armbruster wrote:
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>   qobject/qjson.c | 40 ++++++++++++++++------------------------
>   1 file changed, 16 insertions(+), 24 deletions(-)
> 
> diff --git a/qobject/qjson.c b/qobject/qjson.c
> index db36101f3b..f3c62711b9 100644
> --- a/qobject/qjson.c
> +++ b/qobject/qjson.c
> @@ -159,21 +159,28 @@ typedef struct ToJsonIterState
>   
>   static void to_json(const QObject *obj, QString *str, int pretty, int indent);
>   
> +static void json_pretty_newline(QString *str, bool pretty, int indent)
> +{
> +    int i;
> +
> +    if (pretty) {
> +        qstring_append(str, "\n");
> +        for (i = 0 ; i < indent ; i++) {

Why are you keeping the spaces before ; ?  Yes, I know they were 
copied-and-pasted from the old code, but as long as you are refactoring, 
fixing the style is worthwhile.

Otherwise,
Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org


Re: [PATCH for-5.1 2/5] qobject: Factor out helper json_pretty_newline()
Posted by Markus Armbruster 5 years, 9 months ago
Eric Blake <eblake@redhat.com> writes:

> On 4/15/20 3:30 AM, Markus Armbruster wrote:
>> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>> ---
>>   qobject/qjson.c | 40 ++++++++++++++++------------------------
>>   1 file changed, 16 insertions(+), 24 deletions(-)
>>
>> diff --git a/qobject/qjson.c b/qobject/qjson.c
>> index db36101f3b..f3c62711b9 100644
>> --- a/qobject/qjson.c
>> +++ b/qobject/qjson.c
>> @@ -159,21 +159,28 @@ typedef struct ToJsonIterState
>>     static void to_json(const QObject *obj, QString *str, int
>> pretty, int indent);
>>   +static void json_pretty_newline(QString *str, bool pretty, int
>> indent)
>> +{
>> +    int i;
>> +
>> +    if (pretty) {
>> +        qstring_append(str, "\n");
>> +        for (i = 0 ; i < indent ; i++) {
>
> Why are you keeping the spaces before ; ?  Yes, I know they were
> copied-and-pasted from the old code, but as long as you are
> refactoring, fixing the style is worthwhile.

Makes sense.

> Otherwise,
> Reviewed-by: Eric Blake <eblake@redhat.com>

Thanks!