From 658ed10c829bab3a47c74b7a1371f688b3166667 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Mon, 9 Nov 2020 16:20:56 +0000
Subject: [PATCH] json-utils: Move JSON utility code to its own translation
 unit

utils.c is getting larger and more miscellaneous than I'd prefer.

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 steam-runtime-tools/json-utils-internal.h |  44 ++++++++
 steam-runtime-tools/json-utils.c          | 126 ++++++++++++++++++++++
 steam-runtime-tools/library.c             |   1 +
 steam-runtime-tools/locale.c              |   1 +
 steam-runtime-tools/meson.build           |   2 +
 steam-runtime-tools/runtime.c             |   1 +
 steam-runtime-tools/system-info.c         |   1 +
 steam-runtime-tools/utils-internal.h      |  11 --
 steam-runtime-tools/utils.c               |  97 -----------------
 steam-runtime-tools/xdg-portal.c          |   1 +
 10 files changed, 177 insertions(+), 108 deletions(-)
 create mode 100644 steam-runtime-tools/json-utils-internal.h
 create mode 100644 steam-runtime-tools/json-utils.c

diff --git a/steam-runtime-tools/json-utils-internal.h b/steam-runtime-tools/json-utils-internal.h
new file mode 100644
index 000000000..af01e3ef8
--- /dev/null
+++ b/steam-runtime-tools/json-utils-internal.h
@@ -0,0 +1,44 @@
+/*<private_header>*/
+/*
+ * Copyright © 2019-2020 Collabora Ltd.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#pragma once
+
+#include <stdio.h>
+
+#include <glib.h>
+
+#include <json-glib/json-glib.h>
+
+#include <steam-runtime-tools/macros.h>
+
+_SRT_PRIVATE_EXPORT
+guint srt_get_flags_from_json_array (GType flags_type,
+                                     JsonObject *json_obj,
+                                     const gchar *array_member,
+                                     guint flag_if_unknown);
+
+gchar ** _srt_json_array_to_strv (JsonObject *json_obj,
+                                  const gchar *array_member);
diff --git a/steam-runtime-tools/json-utils.c b/steam-runtime-tools/json-utils.c
new file mode 100644
index 000000000..a752f1267
--- /dev/null
+++ b/steam-runtime-tools/json-utils.c
@@ -0,0 +1,126 @@
+/*
+ * Copyright © 2019-2020 Collabora Ltd.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include "steam-runtime-tools/json-utils-internal.h"
+
+#include "steam-runtime-tools/glib-backports-internal.h"
+#include "steam-runtime-tools/utils-internal.h"
+
+/**
+ * srt_get_flags_from_json_array:
+ * @flags_type: The type of the flag
+ * @json_obj: (not nullable): A JSON Object used to search for
+ *  @array_member property
+ * @array_member: (not nullable): The JSON member to look up
+ * @flag_if_unknown: flag to use in case of parsing error
+ *
+ * Get the flags from a given JSON object array member.
+ * If @json_obj doesn't have the provided @member, or it is malformed, the
+ * @flag_if_unknown will be returned.
+ * If the parsed JSON array_member has some elements that we can't parse,
+ * @flag_if_unknown will be added to the returned flags.
+ *
+ * Returns: the found flags from the provided @json_obj
+ */
+guint
+srt_get_flags_from_json_array (GType flags_type,
+                               JsonObject *json_obj,
+                               const gchar *array_member,
+                               guint flag_if_unknown)
+{
+  JsonArray *array;
+  guint ret = flag_if_unknown;
+
+  g_return_val_if_fail (G_TYPE_IS_FLAGS (flags_type), 0);
+  g_return_val_if_fail (json_obj != NULL, 0);
+  g_return_val_if_fail (array_member != NULL, 0);
+
+  if (json_object_has_member (json_obj, array_member))
+    {
+      array = json_object_get_array_member (json_obj, array_member);
+
+      if (array == NULL)
+        goto out;
+
+      /* We reset the value out because we found the member we were looking for */
+      ret = 0;
+
+      for (guint j = 0; j < json_array_get_length (array); j++)
+        {
+          const gchar *issue_string = json_array_get_string_element (array, j);
+          if (!srt_add_flag_from_nick (flags_type, issue_string, &ret, NULL))
+            ret |= flag_if_unknown;
+        }
+    }
+
+out:
+  return ret;
+}
+
+/**
+ * _srt_json_array_to_strv:
+ * @json_obj: (not nullable): A JSON Object used to search for
+ *  @array_member property
+ * @array_member: (not nullable): The JSON member to look up
+ *
+ * Every %NULL or non string members of the array are replaced with the value
+ * "<invalid>".
+ *
+ * Returns: (transfer full) (array zero-terminated=1) (element-type utf8) (nullable):
+ *  A string array from the given @json_obj, or %NULL if it doesn't have a
+ *  property @array_member
+ */
+gchar **
+_srt_json_array_to_strv (JsonObject *json_obj,
+                         const gchar *array_member)
+{
+  JsonArray *array;
+  guint length;
+  const gchar *element;
+  gchar **ret = NULL;
+
+  g_return_val_if_fail (json_obj != NULL, NULL);
+  g_return_val_if_fail (array_member != NULL, NULL);
+
+  if (json_object_has_member (json_obj, array_member))
+    {
+      array = json_object_get_array_member (json_obj, array_member);
+      if (array == NULL)
+        return ret;
+
+      length = json_array_get_length (array);
+      ret = g_new0 (gchar *, length + 1);
+      for (guint i = 0; i < length; i++)
+        {
+          element = json_array_get_string_element (array, i);
+          if (element == NULL)
+            element = "<invalid>";
+          ret[i] = g_strdup (element);
+        }
+      ret[length] = NULL;
+    }
+
+  return ret;
+}
diff --git a/steam-runtime-tools/library.c b/steam-runtime-tools/library.c
index a09c18149..e77694aaf 100644
--- a/steam-runtime-tools/library.c
+++ b/steam-runtime-tools/library.c
@@ -29,6 +29,7 @@
 #include "steam-runtime-tools/enums.h"
 #include "steam-runtime-tools/glib-backports-internal.h"
 #include "steam-runtime-tools/json-glib-backports-internal.h"
+#include "steam-runtime-tools/json-utils-internal.h"
 #include "steam-runtime-tools/library-internal.h"
 #include "steam-runtime-tools/utils.h"
 #include "steam-runtime-tools/utils-internal.h"
diff --git a/steam-runtime-tools/locale.c b/steam-runtime-tools/locale.c
index 8e46d9eb6..4bc7249d0 100644
--- a/steam-runtime-tools/locale.c
+++ b/steam-runtime-tools/locale.c
@@ -29,6 +29,7 @@
 #include "steam-runtime-tools/enums.h"
 #include "steam-runtime-tools/glib-backports-internal.h"
 #include "steam-runtime-tools/json-glib-backports-internal.h"
+#include "steam-runtime-tools/json-utils-internal.h"
 #include "steam-runtime-tools/locale-internal.h"
 #include "steam-runtime-tools/utils-internal.h"
 
diff --git a/steam-runtime-tools/meson.build b/steam-runtime-tools/meson.build
index 964a56fce..6c0acce1c 100644
--- a/steam-runtime-tools/meson.build
+++ b/steam-runtime-tools/meson.build
@@ -34,6 +34,8 @@ libsteamrt_sources = [
     'graphics.c',
     'json-glib-backports.c',
     'json-glib-backports-internal.h',
+    'json-utils.c',
+    'json-utils-internal.h',
     'library-internal.h',
     'library.c',
     'locale-internal.h',
diff --git a/steam-runtime-tools/runtime.c b/steam-runtime-tools/runtime.c
index be908f2e3..0cdd35405 100644
--- a/steam-runtime-tools/runtime.c
+++ b/steam-runtime-tools/runtime.c
@@ -37,6 +37,7 @@
 #include <glib/gstdio.h>
 
 #include "steam-runtime-tools/glib-backports-internal.h"
+#include "steam-runtime-tools/json-utils-internal.h"
 #include "steam-runtime-tools/utils.h"
 
 /**
diff --git a/steam-runtime-tools/system-info.c b/steam-runtime-tools/system-info.c
index fef92aba2..899a542c4 100644
--- a/steam-runtime-tools/system-info.c
+++ b/steam-runtime-tools/system-info.c
@@ -36,6 +36,7 @@
 #include "steam-runtime-tools/desktop-entry-internal.h"
 #include "steam-runtime-tools/graphics.h"
 #include "steam-runtime-tools/graphics-internal.h"
+#include "steam-runtime-tools/json-utils-internal.h"
 #include "steam-runtime-tools/library-internal.h"
 #include "steam-runtime-tools/locale-internal.h"
 #include "steam-runtime-tools/os-internal.h"
diff --git a/steam-runtime-tools/utils-internal.h b/steam-runtime-tools/utils-internal.h
index 3609b4b88..3a7f0fba5 100644
--- a/steam-runtime-tools/utils-internal.h
+++ b/steam-runtime-tools/utils-internal.h
@@ -30,8 +30,6 @@
 
 #include <glib.h>
 
-#include <json-glib/json-glib.h>
-
 #include <steam-runtime-tools/macros.h>
 
 typedef enum
@@ -73,12 +71,6 @@ gboolean srt_add_flag_from_nick (GType flags_type,
                                  guint *value_out,
                                  GError **error);
 
-_SRT_PRIVATE_EXPORT
-guint srt_get_flags_from_json_array (GType flags_type,
-                                     JsonObject *json_obj,
-                                     const gchar *array_member,
-                                     guint flag_if_unknown);
-
 G_GNUC_INTERNAL void _srt_child_setup_unblock_signals (gpointer ignored);
 
 _SRT_PRIVATE_EXPORT
@@ -87,9 +79,6 @@ void _srt_unblock_signals (void);
 G_GNUC_INTERNAL int _srt_indirect_strcmp0 (gconstpointer left,
                                            gconstpointer right);
 
-gchar ** _srt_json_array_to_strv (JsonObject *json_obj,
-                                  const gchar *array_member);
-
 _SRT_PRIVATE_EXPORT
 gboolean _srt_rm_rf (const char *directory);
 
diff --git a/steam-runtime-tools/utils.c b/steam-runtime-tools/utils.c
index a52f07947..074a8ad3b 100644
--- a/steam-runtime-tools/utils.c
+++ b/steam-runtime-tools/utils.c
@@ -573,57 +573,6 @@ srt_add_flag_from_nick (GType flags_type,
   return result;
 }
 
-/**
- * srt_get_flags_from_json_array:
- * @flags_type: The type of the flag
- * @json_obj: (not nullable): A JSON Object used to search for
- *  @array_member property
- * @array_member: (not nullable): The JSON member to look up
- * @flag_if_unknown: flag to use in case of parsing error
- *
- * Get the flags from a given JSON object array member.
- * If @json_obj doesn't have the provided @member, or it is malformed, the
- * @flag_if_unknown will be returned.
- * If the parsed JSON array_member has some elements that we can't parse,
- * @flag_if_unknown will be added to the returned flags.
- *
- * Returns: the found flags from the provided @json_obj
- */
-guint
-srt_get_flags_from_json_array (GType flags_type,
-                               JsonObject *json_obj,
-                               const gchar *array_member,
-                               guint flag_if_unknown)
-{
-  JsonArray *array;
-  guint ret = flag_if_unknown;
-
-  g_return_val_if_fail (G_TYPE_IS_FLAGS (flags_type), 0);
-  g_return_val_if_fail (json_obj != NULL, 0);
-  g_return_val_if_fail (array_member != NULL, 0);
-
-  if (json_object_has_member (json_obj, array_member))
-    {
-      array = json_object_get_array_member (json_obj, array_member);
-
-      if (array == NULL)
-        goto out;
-
-      /* We reset the value out because we found the member we were looking for */
-      ret = 0;
-
-      for (guint j = 0; j < json_array_get_length (array); j++)
-        {
-          const gchar *issue_string = json_array_get_string_element (array, j);
-          if (!srt_add_flag_from_nick (flags_type, issue_string, &ret, NULL))
-            ret |= flag_if_unknown;
-        }
-    }
-
-out:
-  return ret;
-}
-
 static void _srt_constructor (void) __attribute__((__constructor__));
 static void
 _srt_constructor (void)
@@ -787,52 +736,6 @@ _srt_rm_rf (const char *directory)
   return TRUE;
 }
 
-/**
- * _srt_json_array_to_strv:
- * @json_obj: (not nullable): A JSON Object used to search for
- *  @array_member property
- * @array_member: (not nullable): The JSON member to look up
- *
- * Every %NULL or non string members of the array are replaced with the value
- * "<invalid>".
- *
- * Returns: (transfer full) (array zero-terminated=1) (element-type utf8) (nullable):
- *  A string array from the given @json_obj, or %NULL if it doesn't have a
- *  property @array_member
- */
-gchar **
-_srt_json_array_to_strv (JsonObject *json_obj,
-                         const gchar *array_member)
-{
-  JsonArray *array;
-  guint length;
-  const gchar *element;
-  gchar **ret = NULL;
-
-  g_return_val_if_fail (json_obj != NULL, NULL);
-  g_return_val_if_fail (array_member != NULL, NULL);
-
-  if (json_object_has_member (json_obj, array_member))
-    {
-      array = json_object_get_array_member (json_obj, array_member);
-      if (array == NULL)
-        return ret;
-
-      length = json_array_get_length (array);
-      ret = g_new0 (gchar *, length + 1);
-      for (guint i = 0; i < length; i++)
-        {
-          element = json_array_get_string_element (array, i);
-          if (element == NULL)
-            element = "<invalid>";
-          ret[i] = g_strdup (element);
-        }
-      ret[length] = NULL;
-    }
-
-  return ret;
-}
-
 /*
  * _srt_divert_stdout_to_stderr:
  * @error: Used to raise an error on failure
diff --git a/steam-runtime-tools/xdg-portal.c b/steam-runtime-tools/xdg-portal.c
index 5c32df417..fb3c56a95 100644
--- a/steam-runtime-tools/xdg-portal.c
+++ b/steam-runtime-tools/xdg-portal.c
@@ -28,6 +28,7 @@
 #include "steam-runtime-tools/enums.h"
 #include "steam-runtime-tools/glib-backports-internal.h"
 #include "steam-runtime-tools/json-glib-backports-internal.h"
+#include "steam-runtime-tools/json-utils-internal.h"
 #include "steam-runtime-tools/xdg-portal-internal.h"
 #include "steam-runtime-tools/utils.h"
 #include "steam-runtime-tools/utils-internal.h"
-- 
GitLab