From 53a1663d0dcb86db9c99cf1b2d6085f37d77c95a Mon Sep 17 00:00:00 2001 From: Shuai Lin Date: Tue, 28 Jun 2016 12:00:44 +0800 Subject: [PATCH] Support json param type. --- lib/searpc-client.c | 2 ++ lib/searpc-codegen.py | 4 ++-- lib/searpc-utils.h | 15 +++++++++++++++ tests/rpc_table.py | 1 + tests/searpc.c | 40 ++++++++++++++++++++++++++++++++++++++-- 5 files changed, 58 insertions(+), 4 deletions(-) diff --git a/lib/searpc-client.c b/lib/searpc-client.c index 9a8e51d..9f94978 100644 --- a/lib/searpc-client.c +++ b/lib/searpc-client.c @@ -79,6 +79,8 @@ fcall_to_str (const char *fname, int n_params, va_list args, gsize *len) json_array_append_new (array, json_integer (*((gint64 *)value))); else if (strcmp(type, "string") == 0) json_array_add_string_or_null_element (array, (char *)value); + else if (strcmp(type, "json") == 0) + json_array_add_json_or_null_element (array, (const json_t *)value); else { g_warning ("unrecognized parameter type %s\n", type); return NULL; diff --git a/lib/searpc-codegen.py b/lib/searpc-codegen.py index 014119c..a357e90 100644 --- a/lib/searpc-codegen.py +++ b/lib/searpc-codegen.py @@ -47,9 +47,9 @@ type_table = { "NULL"), "json": ("const json_t*", "json_t*", - "", + "json_array_get_json_or_null_element", "searpc_set_json_to_ret_object", - "", + "json_array_add_json_or_null_element", "NULL"), } diff --git a/lib/searpc-utils.h b/lib/searpc-utils.h index ff41e1d..141aa68 100644 --- a/lib/searpc-utils.h +++ b/lib/searpc-utils.h @@ -57,3 +57,18 @@ inline static json_int_t json_array_get_int_element (json_t *array, size_t index { return json_integer_value (json_array_get (array, index)); } + +inline static const json_t *json_array_get_json_or_null_element (json_t *array, size_t index) +{ + return json_array_get (array, index); +} + +inline static void json_array_add_json_or_null_element (json_t *array, const json_t *value) +{ + if (value) { + json_t *obj = json_deep_copy(value); + json_array_append_new (array, obj); + } else { + json_array_append_new (array, json_null ()); + } +} diff --git a/tests/rpc_table.py b/tests/rpc_table.py index a9a79b5..81c03a1 100644 --- a/tests/rpc_table.py +++ b/tests/rpc_table.py @@ -8,4 +8,5 @@ func_table = [ [ "object", ["string"] ], [ "objlist", ["string", "int"] ], [ "json", ["string", "int"] ], + [ "json", ["json"]], ] diff --git a/tests/searpc.c b/tests/searpc.c index 7b316a9..ec97341 100644 --- a/tests/searpc.c +++ b/tests/searpc.c @@ -319,7 +319,7 @@ get_maman_bar_json (const char *name, int num, GError **error) } void -test_searpc__json_call (void) +test_searpc__json_return_type (void) { json_t *result; GError *error = NULL; @@ -331,6 +331,40 @@ test_searpc__json_call (void) cl_assert (error == NULL); } +json_t * +count_json_kvs (const json_t *obj, GError **error) +{ + int count = 0; + const char *key; + const json_t *value; + json_object_foreach(((json_t*)obj), key, value) { + count++; + } + + json_t * ret = json_object(); + json_object_set_new (ret, "number_of_kvs", json_integer (count)); + return ret; +} + +void +test_searpc__json_param_type (void) +{ + json_t *result; + GError *error = NULL; + + json_t *param = json_object(); + json_object_set_new (param, "a", json_integer (1)); + json_object_set_new (param, "b", json_integer (2)); + + result = searpc_client_call__json (client, "count_json_kvs", + &error, 1, + "json", param); + + cl_assert_ (error == NULL, error ? error->message : ""); + int count = json_integer_value(json_object_get((json_t*)result, "number_of_kvs")); + cl_assert_(count == 2, json_dumps(result, JSON_INDENT(2))); +} + void simple_callback (void *result, void *user_data, GError *error) { @@ -488,6 +522,8 @@ test_searpc__initialize (void) searpc_signature_objlist__string_int()); searpc_server_register_function ("test", get_maman_bar_json, "get_maman_bar_json", searpc_signature_json__string_int()); + searpc_server_register_function ("test", count_json_kvs, "count_json_kvs", + searpc_signature_json__json()); /* sample client */ client = searpc_client_new(); @@ -513,5 +549,5 @@ test_searpc__cleanup (void) searpc_free_client_with_pipe_transport(client_with_pipe_transport); /* free memory for memory debug with valgrind */ - // searpc_server_final(); + searpc_server_final(); }