[gnome-builder] serialization: add variant serializers for diagnostics



commit 157b5998cd99af363179c4e81e22db37c973e97d
Author: Christian Hergert <chergert redhat com>
Date:   Fri Apr 20 15:33:56 2018 -0700

    serialization: add variant serializers for diagnostics
    
    We'll need to add deserializers too, but this lets us move forward on the
    serialization work for now.

 src/libide/diagnostics/ide-diagnostic.c      | 68 ++++++++++++++++++++++++++++
 src/libide/diagnostics/ide-diagnostic.h      |  3 +-
 src/libide/diagnostics/ide-fixit.c           | 28 ++++++++++++
 src/libide/diagnostics/ide-fixit.h           | 16 ++++---
 src/libide/diagnostics/ide-source-location.c | 35 ++++++++++++++
 src/libide/diagnostics/ide-source-location.h |  2 +
 src/libide/diagnostics/ide-source-range.c    | 38 ++++++++++++++++
 src/libide/diagnostics/ide-source-range.h    | 14 +++---
 8 files changed, 190 insertions(+), 14 deletions(-)
---
diff --git a/src/libide/diagnostics/ide-diagnostic.c b/src/libide/diagnostics/ide-diagnostic.c
index 312184746..0c26ec3de 100644
--- a/src/libide/diagnostics/ide-diagnostic.c
+++ b/src/libide/diagnostics/ide-diagnostic.c
@@ -394,3 +394,71 @@ ide_diagnostic_get_file (IdeDiagnostic *self)
 
   return NULL;
 }
+
+/**
+ * ide_diagnostic_to_variant:
+ * @self: a #IdeDiagnostic
+ *
+ * Creates a #GVariant to represent the diagnostic. This can be useful when
+ * working in subprocesses to serialize the diagnostic.
+ *
+ * This function will never return a floating variant.
+ *
+ * Returns: (transfer full): a #GVariant
+ *
+ * Since: 3.30
+ */
+GVariant *
+ide_diagnostic_to_variant (const IdeDiagnostic *self)
+{
+  GVariantDict dict;
+
+  g_return_val_if_fail (self != NULL, NULL);
+
+  g_variant_dict_init (&dict, NULL);
+
+  g_variant_dict_insert (&dict, "text", "s", self->text ?: "");
+  g_variant_dict_insert (&dict, "severity", "s", ide_diagnostic_severity_to_string (self->severity));
+
+  if (self->location != NULL)
+    {
+      g_autoptr(GVariant) vloc = ide_source_location_to_variant (self->location);
+      g_variant_dict_insert_value (&dict, "location", vloc);
+    }
+
+  if (self->ranges != NULL && self->ranges->len > 0)
+    {
+      GVariantBuilder builder;
+
+      g_variant_builder_init (&builder, G_VARIANT_TYPE ("aa{sv}"));
+
+      for (guint i = 0; i < self->ranges->len; i++)
+        {
+          const IdeSourceRange *range = g_ptr_array_index (self->ranges, i);
+          g_autoptr(GVariant) vrange = ide_source_range_to_variant (range);
+
+          g_variant_builder_add_value (&builder, vrange);
+        }
+
+      g_variant_dict_insert_value (&dict, "ranges", g_variant_builder_end (&builder));
+    }
+
+  if (self->fixits != NULL && self->fixits->len > 0)
+    {
+      GVariantBuilder builder;
+
+      g_variant_builder_init (&builder, G_VARIANT_TYPE ("aa{sv}"));
+
+      for (guint i = 0; i < self->ranges->len; i++)
+        {
+          const IdeFixit *fixit = g_ptr_array_index (self->fixits, i);
+          g_autoptr(GVariant) vfixit = ide_fixit_to_variant (fixit);
+
+          g_variant_builder_add_value (&builder, vfixit);
+        }
+
+      g_variant_dict_insert_value (&dict, "fixits", g_variant_builder_end (&builder));
+    }
+
+  return g_variant_ref_sink (g_variant_dict_end (&dict));
+}
diff --git a/src/libide/diagnostics/ide-diagnostic.h b/src/libide/diagnostics/ide-diagnostic.h
index 2121d9ae0..7000fffc5 100644
--- a/src/libide/diagnostics/ide-diagnostic.h
+++ b/src/libide/diagnostics/ide-diagnostic.h
@@ -83,7 +83,8 @@ gint                   ide_diagnostic_compare              (const IdeDiagnostic
                                                             const IdeDiagnostic   *b);
 IDE_AVAILABLE_IN_ALL
 guint                  ide_diagnostic_hash                 (IdeDiagnostic         *self);
-
+IDE_AVAILABLE_IN_3_30
+GVariant              *ide_diagnostic_to_variant           (const IdeDiagnostic   *self);
 
 IDE_AVAILABLE_IN_ALL
 const gchar           *ide_diagnostic_severity_to_string   (IdeDiagnosticSeverity severity);
diff --git a/src/libide/diagnostics/ide-fixit.c b/src/libide/diagnostics/ide-fixit.c
index 7c1e93d30..954ddd706 100644
--- a/src/libide/diagnostics/ide-fixit.c
+++ b/src/libide/diagnostics/ide-fixit.c
@@ -123,3 +123,31 @@ ide_fixit_get_range (IdeFixit *self)
 
   return self->range;
 }
+
+/**
+ * ide_fixit_to_variant:
+ * @self: a #IdeFixit
+ *
+ * Creates a #GVariant to represent a fixit.
+ *
+ * This function will never return a floating variant.
+ *
+ * Returns: (transfer full): a #GVariant
+ */
+GVariant *
+ide_fixit_to_variant (const IdeFixit *self)
+{
+  GVariantDict dict;
+  g_autoptr(GVariant) vrange = NULL;
+
+  g_return_val_if_fail (self != NULL, NULL);
+
+  g_variant_dict_init (&dict, NULL);
+
+  vrange = ide_source_range_to_variant (self->range);
+
+  g_variant_dict_insert (&dict, "text", "s", self->text ?: "");
+  g_variant_dict_insert_value (&dict, "range", vrange);
+
+  return g_variant_ref_sink (g_variant_dict_end (&dict));
+}
diff --git a/src/libide/diagnostics/ide-fixit.h b/src/libide/diagnostics/ide-fixit.h
index a01b53c28..ea0de12b3 100644
--- a/src/libide/diagnostics/ide-fixit.h
+++ b/src/libide/diagnostics/ide-fixit.h
@@ -27,19 +27,21 @@ G_BEGIN_DECLS
 
 IDE_AVAILABLE_IN_ALL
 IdeFixit       *ide_fixit_new       (IdeSourceRange *source_range,
-                                     const gchar    *replacement_text);
+                                      const gchar    *replacement_text);
 IDE_AVAILABLE_IN_ALL
-GType           ide_fixit_get_type  (void);
+GType           ide_fixit_get_type   (void);
 IDE_AVAILABLE_IN_ALL
-IdeFixit       *ide_fixit_ref       (IdeFixit       *self);
+IdeFixit       *ide_fixit_ref        (IdeFixit       *self);
 IDE_AVAILABLE_IN_ALL
-void            ide_fixit_unref     (IdeFixit       *self);
+void            ide_fixit_unref      (IdeFixit       *self);
 IDE_AVAILABLE_IN_ALL
-void            ide_fixit_apply     (IdeFixit       *self);
+void            ide_fixit_apply      (IdeFixit       *self);
 IDE_AVAILABLE_IN_ALL
-const gchar    *ide_fixit_get_text  (IdeFixit       *self);
+const gchar    *ide_fixit_get_text   (IdeFixit       *self);
 IDE_AVAILABLE_IN_ALL
-IdeSourceRange *ide_fixit_get_range (IdeFixit       *self);
+IdeSourceRange *ide_fixit_get_range  (IdeFixit       *self);
+IDE_AVAILABLE_IN_3_30
+GVariant       *ide_fixit_to_variant (const IdeFixit *self);
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (IdeFixit, ide_fixit_unref)
 
diff --git a/src/libide/diagnostics/ide-source-location.c b/src/libide/diagnostics/ide-source-location.c
index c2d0b9714..dfaf4ab56 100644
--- a/src/libide/diagnostics/ide-source-location.c
+++ b/src/libide/diagnostics/ide-source-location.c
@@ -240,3 +240,38 @@ ide_source_location_new_for_path (IdeContext  *context,
 
   return ide_source_location_new (ifile, line, line_offset, 0);
 }
+
+/**
+ * ide_source_location_to_variant:
+ * @self: a #IdeSourceLocation
+ *
+ * Serializes the location into a variant that can be used to transport
+ * across IPC boundaries.
+ *
+ * This function will never return a variant with a floating reference.
+ *
+ * Returns: (transfer full): a #GVariant
+ */
+GVariant *
+ide_source_location_to_variant (const IdeSourceLocation *self)
+{
+  GVariantDict dict;
+
+  g_return_val_if_fail (self != NULL, NULL);
+
+  g_variant_dict_init (&dict, NULL);
+
+  if (self->file != NULL)
+    {
+      GFile *gfile = ide_file_get_file (self->file);
+      g_autofree gchar *uri = g_file_get_uri (gfile);
+
+      g_variant_dict_insert (&dict, "uri", "s", uri);
+    }
+
+  g_variant_dict_insert (&dict, "line", "u", self->line);
+  g_variant_dict_insert (&dict, "line-offset", "u", self->line_offset);
+  g_variant_dict_insert (&dict, "offset", "u", self->offset);
+
+  return g_variant_ref_sink (g_variant_dict_end (&dict));
+}
diff --git a/src/libide/diagnostics/ide-source-location.h b/src/libide/diagnostics/ide-source-location.h
index 6d548b84f..bdb07c408 100644
--- a/src/libide/diagnostics/ide-source-location.h
+++ b/src/libide/diagnostics/ide-source-location.h
@@ -59,6 +59,8 @@ gint               ide_source_location_compare         (const IdeSourceLocation
                                                         const IdeSourceLocation *b);
 IDE_AVAILABLE_IN_ALL
 guint              ide_source_location_hash            (IdeSourceLocation       *self);
+IDE_AVAILABLE_IN_3_30
+GVariant          *ide_source_location_to_variant      (const IdeSourceLocation *self);
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (IdeSourceLocation, ide_source_location_unref)
 
diff --git a/src/libide/diagnostics/ide-source-range.c b/src/libide/diagnostics/ide-source-range.c
index 8eee78787..d55e01fef 100644
--- a/src/libide/diagnostics/ide-source-range.c
+++ b/src/libide/diagnostics/ide-source-range.c
@@ -130,3 +130,41 @@ ide_source_range_unref (IdeSourceRange *self)
       DZL_COUNTER_DEC (instances);
     }
 }
+
+/**
+ * ide_source_range_to_variant:
+ * @self: a #IdeSourceRange
+ *
+ * Creates a variant to represent the range.
+ *
+ * This function will never return a floating variant.
+ *
+ * Returns: (transfer full): a #GVariant
+ *
+ * Since: 3.30
+ */
+GVariant *
+ide_source_range_to_variant (const IdeSourceRange *self)
+{
+  GVariantDict dict;
+  g_autoptr(GVariant) begin = NULL;
+  g_autoptr(GVariant) end = NULL;
+
+  g_return_val_if_fail (self != NULL, NULL);
+
+  g_variant_dict_init (&dict, NULL);
+
+  if (self->begin)
+    {
+      begin = ide_source_location_to_variant (self->begin);
+      g_variant_dict_insert_value (&dict, "begin", begin);
+    }
+
+  if (self->end)
+    {
+      end = ide_source_location_to_variant (self->end);
+      g_variant_dict_insert_value (&dict, "end", end);
+    }
+
+  return g_variant_ref_sink (g_variant_dict_end (&dict));
+}
diff --git a/src/libide/diagnostics/ide-source-range.h b/src/libide/diagnostics/ide-source-range.h
index 784e9209a..7700fec1c 100644
--- a/src/libide/diagnostics/ide-source-range.h
+++ b/src/libide/diagnostics/ide-source-range.h
@@ -29,16 +29,18 @@ G_BEGIN_DECLS
 IDE_AVAILABLE_IN_ALL
 GType              ide_source_range_get_type  (void);
 IDE_AVAILABLE_IN_ALL
-IdeSourceRange    *ide_source_range_ref       (IdeSourceRange    *self);
+IdeSourceRange    *ide_source_range_ref        (IdeSourceRange       *self);
 IDE_AVAILABLE_IN_ALL
-void               ide_source_range_unref     (IdeSourceRange    *self);
+void               ide_source_range_unref      (IdeSourceRange       *self);
 IDE_AVAILABLE_IN_ALL
-IdeSourceLocation *ide_source_range_get_begin (IdeSourceRange    *self);
+IdeSourceLocation *ide_source_range_get_begin  (IdeSourceRange       *self);
 IDE_AVAILABLE_IN_ALL
-IdeSourceLocation *ide_source_range_get_end   (IdeSourceRange    *self);
+IdeSourceLocation *ide_source_range_get_end    (IdeSourceRange       *self);
 IDE_AVAILABLE_IN_ALL
-IdeSourceRange    *ide_source_range_new       (IdeSourceLocation *begin,
-                                               IdeSourceLocation *end);
+IdeSourceRange    *ide_source_range_new        (IdeSourceLocation    *begin,
+                                                IdeSourceLocation    *end);
+IDE_AVAILABLE_IN_3_30
+GVariant          *ide_source_range_to_variant (const IdeSourceRange *self);
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (IdeSourceRange, ide_source_range_unref)
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]