mirror of
https://github.com/haiwen/libsearpc.git
synced 2025-06-25 05:01:34 +00:00
Support json param type.
This commit is contained in:
parent
5fd3bb7176
commit
53a1663d0d
@ -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)));
|
json_array_append_new (array, json_integer (*((gint64 *)value)));
|
||||||
else if (strcmp(type, "string") == 0)
|
else if (strcmp(type, "string") == 0)
|
||||||
json_array_add_string_or_null_element (array, (char *)value);
|
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 {
|
else {
|
||||||
g_warning ("unrecognized parameter type %s\n", type);
|
g_warning ("unrecognized parameter type %s\n", type);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -47,9 +47,9 @@ type_table = {
|
|||||||
"NULL"),
|
"NULL"),
|
||||||
"json": ("const json_t*",
|
"json": ("const json_t*",
|
||||||
"json_t*",
|
"json_t*",
|
||||||
"",
|
"json_array_get_json_or_null_element",
|
||||||
"searpc_set_json_to_ret_object",
|
"searpc_set_json_to_ret_object",
|
||||||
"",
|
"json_array_add_json_or_null_element",
|
||||||
"NULL"),
|
"NULL"),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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));
|
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 ());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -8,4 +8,5 @@ func_table = [
|
|||||||
[ "object", ["string"] ],
|
[ "object", ["string"] ],
|
||||||
[ "objlist", ["string", "int"] ],
|
[ "objlist", ["string", "int"] ],
|
||||||
[ "json", ["string", "int"] ],
|
[ "json", ["string", "int"] ],
|
||||||
|
[ "json", ["json"]],
|
||||||
]
|
]
|
||||||
|
@ -319,7 +319,7 @@ get_maman_bar_json (const char *name, int num, GError **error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
test_searpc__json_call (void)
|
test_searpc__json_return_type (void)
|
||||||
{
|
{
|
||||||
json_t *result;
|
json_t *result;
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
@ -331,6 +331,40 @@ test_searpc__json_call (void)
|
|||||||
cl_assert (error == NULL);
|
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)
|
void simple_callback (void *result, void *user_data, GError *error)
|
||||||
{
|
{
|
||||||
@ -488,6 +522,8 @@ test_searpc__initialize (void)
|
|||||||
searpc_signature_objlist__string_int());
|
searpc_signature_objlist__string_int());
|
||||||
searpc_server_register_function ("test", get_maman_bar_json, "get_maman_bar_json",
|
searpc_server_register_function ("test", get_maman_bar_json, "get_maman_bar_json",
|
||||||
searpc_signature_json__string_int());
|
searpc_signature_json__string_int());
|
||||||
|
searpc_server_register_function ("test", count_json_kvs, "count_json_kvs",
|
||||||
|
searpc_signature_json__json());
|
||||||
|
|
||||||
/* sample client */
|
/* sample client */
|
||||||
client = searpc_client_new();
|
client = searpc_client_new();
|
||||||
@ -513,5 +549,5 @@ test_searpc__cleanup (void)
|
|||||||
searpc_free_client_with_pipe_transport(client_with_pipe_transport);
|
searpc_free_client_with_pipe_transport(client_with_pipe_transport);
|
||||||
|
|
||||||
/* free memory for memory debug with valgrind */
|
/* free memory for memory debug with valgrind */
|
||||||
// searpc_server_final();
|
searpc_server_final();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user