[jsonrpc-glib: 1/2] message: add the possibility to inject messages




commit 8e0dced97352a73676541abcdfb931b82b9ac97f
Author: Günther Wagner <info gunibert de>
Date:   Tue Aug 10 20:06:23 2021 +0200

    message: add the possibility to inject messages
    
    With this change it is possible to create messages and use these as
    object in other messages.

 meson.build                  |  2 +-
 src/jsonrpc-message.c        | 15 ++++++++++++++-
 src/jsonrpc-message.h        | 10 ++++++++++
 src/jsonrpc-version-macros.h | 15 +++++++++++++++
 tests/test-message.c         |  8 ++++++++
 5 files changed, 48 insertions(+), 2 deletions(-)
---
diff --git a/meson.build b/meson.build
index fc22277..7482d41 100644
--- a/meson.build
+++ b/meson.build
@@ -1,5 +1,5 @@
 project('jsonrpc-glib', 'c',
-          version: '3.38.0',
+          version: '3.40.0',
           license: 'LGPLv2.1+',
     meson_version: '>= 0.49.2',
   default_options: [ 'warning_level=1', 'buildtype=debugoptimized', 'c_std=gnu11' ],
diff --git a/src/jsonrpc-message.c b/src/jsonrpc-message.c
index a4f6522..9c236a3 100644
--- a/src/jsonrpc-message.c
+++ b/src/jsonrpc-message.c
@@ -45,6 +45,7 @@
 #define IS_PUT_INT64(_any)   COMPARE_MAGIC(_any, PUT_INT64)
 #define IS_PUT_BOOLEAN(_any) COMPARE_MAGIC(_any, PUT_BOOLEAN)
 #define IS_PUT_DOUBLE(_any)  COMPARE_MAGIC(_any, PUT_DOUBLE)
+#define IS_PUT_VARIANT(_any) COMPARE_MAGIC(_any, PUT_VARIANT)
 
 #define IS_GET_STRING(_any)  COMPARE_MAGIC(_any, GET_STRING)
 #define IS_GET_STRV(_any)    COMPARE_MAGIC(_any, GET_STRV)
@@ -89,6 +90,12 @@ jsonrpc_message_build_object (GVariantBuilder *builder,
   if (!keyptr || keyptr->magic.bytes[0] == '}')
     EXIT;
 
+  if (IS_PUT_VARIANT (keyptr))
+    {
+      g_variant_builder_add (builder, "v", ((JsonrpcMessagePutVariant *)keyptr)->val);
+      EXIT;
+    }
+
   g_variant_builder_open (builder, G_VARIANT_TYPE ("{sv}"));
 
   /*
@@ -112,8 +119,14 @@ jsonrpc_message_build_object (GVariantBuilder *builder,
   switch (valptr->magic.bytes[0])
     {
     case '{':
-      g_variant_builder_open (builder, G_VARIANT_TYPE ("a{sv}"));
       param = va_arg (*args, gconstpointer);
+      /*
+       * Peek ahead if a possible GVariant will be injected
+       */
+      if (IS_PUT_VARIANT ((JsonrpcMessageAny *)param))
+        g_variant_builder_open (builder, G_VARIANT_TYPE ("v"));
+      else
+        g_variant_builder_open (builder, G_VARIANT_TYPE ("a{sv}"));
       jsonrpc_message_build_object (builder, param, args);
       g_variant_builder_close (builder);
       break;
diff --git a/src/jsonrpc-message.h b/src/jsonrpc-message.h
index 5bc4109..bf6b2ab 100644
--- a/src/jsonrpc-message.h
+++ b/src/jsonrpc-message.h
@@ -119,6 +119,12 @@ typedef struct
   GVariantDict **dictptr;
 } JsonrpcMessageGetDict __attribute__((aligned (8)));
 
+typedef struct
+{
+  JsonrpcMessageMagic magic;
+  GVariant *val;
+} JsonrpcMessagePutVariant __attribute__((aligned (8)));
+
 typedef struct
 {
   JsonrpcMessageMagic magic;
@@ -142,6 +148,7 @@ typedef struct
 #define _JSONRPC_MESSAGE_GET_DOUBLE_MAGIC  _JSONRPC_MAGIC("GETD")
 #define _JSONRPC_MESSAGE_GET_ITER_MAGIC    _JSONRPC_MAGIC("GETT")
 #define _JSONRPC_MESSAGE_GET_DICT_MAGIC    _JSONRPC_MAGIC("GETC")
+#define _JSONRPC_MESSAGE_PUT_VARIANT_MAGIC _JSONRPC_MAGIC("PUTV")
 #define _JSONRPC_MESSAGE_GET_VARIANT_MAGIC _JSONRPC_MAGIC("GETV")
 
 #define _JSONRPC_MESSAGE_PUT_STRING_MAGIC_C  _JSONRPC_MAGIC_C('P','U','T','S')
@@ -158,6 +165,7 @@ typedef struct
 #define _JSONRPC_MESSAGE_GET_DOUBLE_MAGIC_C  _JSONRPC_MAGIC_C('G','E','T','D')
 #define _JSONRPC_MESSAGE_GET_ITER_MAGIC_C    _JSONRPC_MAGIC_C('G','E','T','T')
 #define _JSONRPC_MESSAGE_GET_DICT_MAGIC_C    _JSONRPC_MAGIC_C('G','E','T','C')
+#define _JSONRPC_MESSAGE_PUT_VARIANT_MAGIC_C _JSONRPC_MAGIC_C('P','U','T','V')
 #define _JSONRPC_MESSAGE_GET_VARIANT_MAGIC_C _JSONRPC_MAGIC_C('G','E','T','V')
 
 #define JSONRPC_MESSAGE_NEW(first_, ...) \
@@ -205,6 +213,8 @@ typedef struct
 #define JSONRPC_MESSAGE_GET_DICT(_valptr) \
   (&((JsonrpcMessageGetDict) { .magic = {_JSONRPC_MESSAGE_GET_DICT_MAGIC_C}, .dictptr = _valptr }))
 
+#define JSONRPC_MESSAGE_PUT_VARIANT(_val) \
+  (&((JsonrpcMessagePutVariant) { .magic = {_JSONRPC_MESSAGE_PUT_VARIANT_MAGIC_C}, .val = _val }))
 #define JSONRPC_MESSAGE_GET_VARIANT(_valptr) \
   (&((JsonrpcMessageGetVariant) { .magic = {_JSONRPC_MESSAGE_GET_VARIANT_MAGIC_C}, .variantptr = _valptr }))
 
diff --git a/src/jsonrpc-version-macros.h b/src/jsonrpc-version-macros.h
index 9eed3ec..cf2e5c1 100644
--- a/src/jsonrpc-version-macros.h
+++ b/src/jsonrpc-version-macros.h
@@ -43,6 +43,7 @@
 #define JSONRPC_VERSION_3_26 (G_ENCODE_VERSION (3, 26))
 #define JSONRPC_VERSION_3_28 (G_ENCODE_VERSION (3, 28))
 #define JSONRPC_VERSION_3_30 (G_ENCODE_VERSION (3, 30))
+#define JSONRPC_VERSION_3_40 (G_ENCODE_VERSION (3, 40))
 
 #if (JSONRPC_MINOR_VERSION == 99)
 # define JSONRPC_VERSION_CUR_STABLE (G_ENCODE_VERSION (JSONRPC_MAJOR_VERSION + 1, 0))
@@ -155,4 +156,18 @@
 # define JSONRPC_AVAILABLE_IN_3_30                 _JSONRPC_EXTERN
 #endif
 
+#if JSONRPC_VERSION_MIN_REQUIRED >= JSONRPC_VERSION_3_40
+# define JSONRPC_DEPRECATED_IN_3_40                JSONRPC_DEPRECATED
+# define JSONRPC_DEPRECATED_IN_3_40_FOR(f)         JSONRPC_DEPRECATED_FOR(f)
+#else
+# define JSONRPC_DEPRECATED_IN_3_40                _JSONRPC_EXTERN
+# define JSONRPC_DEPRECATED_IN_3_40_FOR(f)         _JSONRPC_EXTERN
+#endif
+
+#if JSONRPC_VERSION_MAX_ALLOWED < JSONRPC_VERSION_3_40
+# define JSONRPC_AVAILABLE_IN_3_40                 JSONRPC_UNAVAILABLE(3, 40)
+#else
+# define JSONRPC_AVAILABLE_IN_3_40                 _JSONRPC_EXTERN
+#endif
+
 #endif /* JSONRPC_VERSION_MACROS_H */
diff --git a/tests/test-message.c b/tests/test-message.c
index d7f3faf..7497ee2 100644
--- a/tests/test-message.c
+++ b/tests/test-message.c
@@ -115,6 +115,7 @@ static void
 test_extract_node (void)
 {
   g_autoptr(GVariant) node = NULL;
+  g_autoptr(GVariant) node2 = NULL;
   g_autoptr(GVariant) ar = NULL;
   gboolean r;
 
@@ -124,6 +125,13 @@ test_extract_node (void)
   r = JSONRPC_MESSAGE_PARSE (node, "foo", "{", "bar", JSONRPC_MESSAGE_GET_VARIANT (&ar), "}");
   g_assert (ar != NULL);
   g_assert_cmpint (r, ==, TRUE);
+
+  node = jsonrpc_message_new ("foo", "{", "bar", "[", JSONRPC_MESSAGE_PUT_INT32 (1), "]", "}", NULL);
+  g_assert (node != NULL);
+
+  node2 = JSONRPC_MESSAGE_NEW ("bar", JSONRPC_MESSAGE_PUT_INT32 (1));
+  node = JSONRPC_MESSAGE_NEW ("foo", "{", JSONRPC_MESSAGE_PUT_VARIANT (g_steal_pointer (&node2)), "}");
+  g_assert (node != NULL);
 }
 
 static void


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