[jsonrpc-glib] message: add JSONRPC_MESSAGE_NEW_ARRAY()
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [jsonrpc-glib] message: add JSONRPC_MESSAGE_NEW_ARRAY()
- Date: Sun, 24 Dec 2017 21:03:11 +0000 (UTC)
commit 2a011dede060626397c1ba36bb17c0dccfd6011e
Author: Christian Hergert <chergert redhat com>
Date: Sun Dec 24 13:01:24 2017 -0800
message: add JSONRPC_MESSAGE_NEW_ARRAY()
This is a helper to allow creating array variants easier. You
can use it similar to JSONRPC_MESSAGE_NEW().
g_autoptr(GVariant) v = JSONRPC_MESSAGE_NEW_ARRAY(
"foo", "bar", JSONRPC_MESSAGE_PUT_STRING (vstr),
JSONRPC_MESSAGE_PUT_INT32 (v32));
src/jsonrpc-message.c | 36 +++++++++++++++++++++++++++++++++++-
src/jsonrpc-message.h | 5 +++++
tests/test-message.c | 34 ++++++++++++++++++++++++++++++++++
3 files changed, 74 insertions(+), 1 deletion(-)
---
diff --git a/src/jsonrpc-message.c b/src/jsonrpc-message.c
index f225b8a..74246e4 100644
--- a/src/jsonrpc-message.c
+++ b/src/jsonrpc-message.c
@@ -209,7 +209,8 @@ jsonrpc_message_build_array (GVariantBuilder *builder,
g_variant_builder_close (builder);
param = va_arg (*args, gpointer);
- jsonrpc_message_build_array (builder, param, args);
+ if (param != NULL)
+ jsonrpc_message_build_array (builder, param, args);
EXIT;
}
@@ -247,6 +248,39 @@ jsonrpc_message_new (gpointer first_param,
return ret;
}
+static GVariant *
+jsonrpc_message_new_array_valist (gpointer first_param,
+ va_list *args)
+{
+ GVariantBuilder builder;
+
+ g_variant_builder_init (&builder, G_VARIANT_TYPE ("av"));
+
+ if (first_param != NULL)
+ jsonrpc_message_build_array (&builder, first_param, args);
+
+ return g_variant_builder_end (&builder);
+}
+
+GVariant *
+jsonrpc_message_new_array (gpointer first_param,
+ ...)
+{
+ GVariant *ret;
+ va_list args;
+
+ g_return_val_if_fail (first_param != NULL, NULL);
+
+ va_start (args, first_param);
+ ret = jsonrpc_message_new_array_valist (first_param, &args);
+ va_end (args);
+
+ if (g_variant_is_floating (ret))
+ g_variant_take_ref (ret);
+
+ return ret;
+}
+
static gboolean
jsonrpc_message_parse_object (GVariantDict *dict,
gpointer param,
diff --git a/src/jsonrpc-message.h b/src/jsonrpc-message.h
index 85b3716..8fddb46 100644
--- a/src/jsonrpc-message.h
+++ b/src/jsonrpc-message.h
@@ -146,6 +146,8 @@ typedef struct
#define JSONRPC_MESSAGE_NEW(first_, ...) \
jsonrpc_message_new(first_, __VA_ARGS__, NULL)
+#define JSONRPC_MESSAGE_NEW_ARRAY(first_, ...) \
+ jsonrpc_message_new_array(first_, __VA_ARGS__, NULL)
#define JSONRPC_MESSAGE_PARSE(message, ...) \
jsonrpc_message_parse(message, __VA_ARGS__, NULL)
#define JSONRPC_MESSAGE_PARSE_ARRAY(iter, ...) \
@@ -189,6 +191,9 @@ JSONRPC_AVAILABLE_IN_3_26
GVariant *jsonrpc_message_new (gpointer first_param, ...) G_GNUC_NULL_TERMINATED;
JSONRPC_AVAILABLE_IN_3_26
+GVariant *jsonrpc_message_new_array (gpointer first_param, ...) G_GNUC_NULL_TERMINATED;
+
+JSONRPC_AVAILABLE_IN_3_26
gboolean jsonrpc_message_parse (GVariant *message, ...) G_GNUC_NULL_TERMINATED;
JSONRPC_AVAILABLE_IN_3_26
diff --git a/tests/test-message.c b/tests/test-message.c
index f6b7219..1979685 100644
--- a/tests/test-message.c
+++ b/tests/test-message.c
@@ -163,6 +163,39 @@ test_array_toplevel (void)
g_assert_cmpstr (a, ==, "a");
}
+static void
+test_new_array (void)
+{
+ g_autoptr(GVariant) node = NULL;
+ GVariantIter iter;
+ const gchar *a = NULL;
+ const gchar *b = NULL;
+ const gchar *c = NULL;
+ const gchar *d = NULL;
+ const gchar *e = NULL;
+ gboolean r;
+
+ node = JSONRPC_MESSAGE_NEW_ARRAY ("a", "b", "c", "d", "e");
+ g_assert (node != NULL);
+ g_assert (g_variant_is_of_type (node, G_VARIANT_TYPE ("av")));
+
+ r = g_variant_iter_init (&iter, node);
+ g_assert_true (r);
+
+ r = JSONRPC_MESSAGE_PARSE_ARRAY (&iter,
+ JSONRPC_MESSAGE_GET_STRING (&a),
+ JSONRPC_MESSAGE_GET_STRING (&b),
+ JSONRPC_MESSAGE_GET_STRING (&c),
+ JSONRPC_MESSAGE_GET_STRING (&d),
+ JSONRPC_MESSAGE_GET_STRING (&e));
+ g_assert_cmpint (r, ==, TRUE);
+ g_assert_cmpstr (a, ==, "a");
+ g_assert_cmpstr (b, ==, "b");
+ g_assert_cmpstr (c, ==, "c");
+ g_assert_cmpstr (d, ==, "d");
+ g_assert_cmpstr (e, ==, "e");
+}
+
gint
main (gint argc,
gchar *argv[])
@@ -175,5 +208,6 @@ main (gint argc,
g_test_add_func ("/Jsonrpc/Message/extract_node", test_extract_node);
g_test_add_func ("/Jsonrpc/Message/paren", test_paren);
g_test_add_func ("/Jsonrpc/Message/array_toplevel", test_array_toplevel);
+ g_test_add_func ("/Jsonrpc/Message/new_array", test_new_array);
return g_test_run ();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]