1
0
mirror of https://github.com/haiwen/libsearpc.git synced 2025-05-08 07:06:20 +00:00

Support json param type.

This commit is contained in:
Shuai Lin 2016-06-28 12:00:44 +08:00
parent 5fd3bb7176
commit 53a1663d0d
5 changed files with 58 additions and 4 deletions

View File

@ -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;

View File

@ -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"),
}

View File

@ -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 ());
}
}

View File

@ -8,4 +8,5 @@ func_table = [
[ "object", ["string"] ],
[ "objlist", ["string", "int"] ],
[ "json", ["string", "int"] ],
[ "json", ["json"]],
]

View File

@ -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();
}