mirror of
https://github.com/falk-werner/webfuse
synced 2025-06-13 12:54:15 +00:00
introduces convenience function for json type checking (wf_impl_json_is_<type>)
This commit is contained in:
parent
6c522b3072
commit
4b544ef35d
@ -125,7 +125,7 @@ wf_impl_client_protocol_on_add_filesystem_finished(
|
||||
if (NULL == protocol->filesystem)
|
||||
{
|
||||
struct wf_json const * id = wf_impl_json_object_get(result, "id");
|
||||
if (WF_JSON_TYPE_STRING == wf_impl_json_type(id))
|
||||
if (wf_impl_json_is_string(id))
|
||||
{
|
||||
char const * name = wf_impl_json_string_get(id);
|
||||
struct wf_mountpoint * mountpoint = wf_impl_mountpoint_create(context->local_path);
|
||||
|
@ -23,6 +23,55 @@ wf_impl_json_bool_get(
|
||||
return (WF_JSON_TYPE_BOOL == json->type) ? json->value.b : false;
|
||||
}
|
||||
|
||||
bool
|
||||
wf_impl_json_is_undefined(
|
||||
struct wf_json const * json)
|
||||
{
|
||||
return (WF_JSON_TYPE_UNDEFINED == json->type);
|
||||
}
|
||||
|
||||
bool
|
||||
wf_impl_json_is_null(
|
||||
struct wf_json const * json)
|
||||
{
|
||||
return (WF_JSON_TYPE_NULL == json->type);
|
||||
}
|
||||
|
||||
bool
|
||||
wf_impl_json_is_bool(
|
||||
struct wf_json const * json)
|
||||
{
|
||||
return (WF_JSON_TYPE_BOOL == json->type);
|
||||
}
|
||||
|
||||
bool
|
||||
wf_impl_json_is_int(
|
||||
struct wf_json const * json)
|
||||
{
|
||||
return (WF_JSON_TYPE_INT == json->type);
|
||||
}
|
||||
|
||||
bool
|
||||
wf_impl_json_is_string(
|
||||
struct wf_json const * json)
|
||||
{
|
||||
return (WF_JSON_TYPE_STRING == json->type);
|
||||
}
|
||||
|
||||
bool
|
||||
wf_impl_json_is_array(
|
||||
struct wf_json const * json)
|
||||
{
|
||||
return (WF_JSON_TYPE_ARRAY == json->type);
|
||||
}
|
||||
|
||||
bool
|
||||
wf_impl_json_is_object(
|
||||
struct wf_json const * json)
|
||||
{
|
||||
return (WF_JSON_TYPE_OBJECT == json->type);
|
||||
}
|
||||
|
||||
int
|
||||
wf_impl_json_int_get(
|
||||
struct wf_json const * json)
|
||||
|
@ -30,6 +30,34 @@ extern enum wf_json_type
|
||||
wf_impl_json_type(
|
||||
struct wf_json const * json);
|
||||
|
||||
extern bool
|
||||
wf_impl_json_is_undefined(
|
||||
struct wf_json const * json);
|
||||
|
||||
extern bool
|
||||
wf_impl_json_is_null(
|
||||
struct wf_json const * json);
|
||||
|
||||
extern bool
|
||||
wf_impl_json_is_bool(
|
||||
struct wf_json const * json);
|
||||
|
||||
extern bool
|
||||
wf_impl_json_is_int(
|
||||
struct wf_json const * json);
|
||||
|
||||
extern bool
|
||||
wf_impl_json_is_string(
|
||||
struct wf_json const * json);
|
||||
|
||||
extern bool
|
||||
wf_impl_json_is_array(
|
||||
struct wf_json const * json);
|
||||
|
||||
extern bool
|
||||
wf_impl_json_is_object(
|
||||
struct wf_json const * json);
|
||||
|
||||
extern bool
|
||||
wf_impl_json_bool_get(
|
||||
struct wf_json const * json);
|
||||
|
@ -24,8 +24,8 @@ wf_impl_jsonrpc_is_request(
|
||||
struct wf_json const * method = wf_impl_json_object_get(message, "method");
|
||||
struct wf_json const * params = wf_impl_json_object_get(message, "params");
|
||||
|
||||
return ( (WF_JSON_TYPE_INT == wf_impl_json_type(id)) && (WF_JSON_TYPE_STRING == wf_impl_json_type(method)) &&
|
||||
( (WF_JSON_TYPE_ARRAY == wf_impl_json_type(params)) || (WF_JSON_TYPE_OBJECT == wf_impl_json_type(params)) ));
|
||||
return ( (wf_impl_json_is_int(id)) && (wf_impl_json_is_string(method)) &&
|
||||
( (wf_impl_json_is_array(params)) || (wf_impl_json_is_object(params)) ));
|
||||
}
|
||||
|
||||
|
||||
|
@ -11,8 +11,8 @@ wf_impl_jsonrpc_is_response(
|
||||
struct wf_json const * err = wf_impl_json_object_get(message, "error");
|
||||
struct wf_json const * result = wf_impl_json_object_get(message, "result");
|
||||
|
||||
return ((WF_JSON_TYPE_INT == wf_impl_json_type(id)) &&
|
||||
((WF_JSON_TYPE_OBJECT == wf_impl_json_type(err)) || (WF_JSON_TYPE_UNDEFINED != wf_impl_json_type(result))));
|
||||
return ((wf_impl_json_is_int(id)) &&
|
||||
((wf_impl_json_is_object(err)) || (!wf_impl_json_is_undefined(result))));
|
||||
}
|
||||
|
||||
|
||||
@ -26,7 +26,7 @@ wf_impl_jsonrpc_response_init(
|
||||
result->error = NULL;
|
||||
|
||||
struct wf_json const * id_holder = wf_impl_json_object_get(response, "id");
|
||||
if (WF_JSON_TYPE_INT != wf_impl_json_type(id_holder))
|
||||
if (wf_impl_json_is_int(id_holder))
|
||||
{
|
||||
result->error = wf_impl_jsonrpc_error(WF_BAD_FORMAT, "invalid format: missing id");
|
||||
return;
|
||||
@ -34,14 +34,14 @@ wf_impl_jsonrpc_response_init(
|
||||
|
||||
result->id = wf_impl_json_int_get(id_holder);
|
||||
result->result = wf_impl_json_object_get(response, "result");
|
||||
if (WF_JSON_TYPE_UNDEFINED == wf_impl_json_type(result->result))
|
||||
if (wf_impl_json_is_undefined(result->result))
|
||||
{
|
||||
int code = WF_BAD_FORMAT;
|
||||
char const * message = "invalid format: invalid error object";
|
||||
|
||||
struct wf_json const * error = wf_impl_json_object_get(response, "error");
|
||||
struct wf_json const * code_holder = wf_impl_json_object_get(error, "code");
|
||||
if (WF_JSON_TYPE_INT == wf_impl_json_type(code_holder))
|
||||
if (wf_impl_json_is_int(code_holder))
|
||||
{
|
||||
code = wf_impl_json_int_get(code_holder);
|
||||
struct wf_json const * message_holder = wf_impl_json_object_get(error, "message");
|
||||
|
@ -114,9 +114,9 @@ void wf_impl_jsonrpc_server_process(
|
||||
struct wf_json const * params = wf_impl_json_object_get(request_data, "params");
|
||||
struct wf_json const * id_holder = wf_impl_json_object_get(request_data, "id");
|
||||
|
||||
if ((WF_JSON_TYPE_STRING == wf_impl_json_type(method_holder)) &&
|
||||
((WF_JSON_TYPE_ARRAY == wf_impl_json_type(params)) || (WF_JSON_TYPE_OBJECT == wf_impl_json_type(params))) &&
|
||||
(WF_JSON_TYPE_INT == wf_impl_json_type(id_holder)))
|
||||
if ((wf_impl_json_is_string(method_holder)) &&
|
||||
((wf_impl_json_is_array(params)) || (wf_impl_json_is_object(params))) &&
|
||||
(wf_impl_json_is_int(id_holder)))
|
||||
{
|
||||
char const * method_name = wf_impl_json_string_get(method_holder);
|
||||
int id = wf_impl_json_int_get(id_holder);
|
||||
|
@ -27,7 +27,7 @@ void wf_impl_operation_getattr_finished(
|
||||
{
|
||||
struct wf_json const * mode_holder = wf_impl_json_object_get(result, "mode");
|
||||
struct wf_json const * type_holder = wf_impl_json_object_get(result, "type");
|
||||
if ((WF_JSON_TYPE_INT == wf_impl_json_type(mode_holder)) && (WF_JSON_TYPE_STRING == wf_impl_json_type(type_holder)))
|
||||
if ((wf_impl_json_is_int(mode_holder)) && (wf_impl_json_is_string(type_holder)))
|
||||
{
|
||||
memset(&buffer, 0, sizeof(struct stat));
|
||||
|
||||
|
@ -31,9 +31,9 @@ void wf_impl_operation_lookup_finished(
|
||||
struct wf_json const * inode_holder = wf_impl_json_object_get(result, "inode");
|
||||
struct wf_json const * mode_holder = wf_impl_json_object_get(result, "mode");
|
||||
struct wf_json const * type_holder = wf_impl_json_object_get(result, "type");
|
||||
if ((WF_JSON_TYPE_INT == wf_impl_json_type(inode_holder)) &&
|
||||
(WF_JSON_TYPE_INT == wf_impl_json_type(mode_holder)) &&
|
||||
(WF_JSON_TYPE_INT == wf_impl_json_type(type_holder)))
|
||||
if ((wf_impl_json_is_int(inode_holder)) &&
|
||||
(wf_impl_json_is_int(mode_holder)) &&
|
||||
(wf_impl_json_is_int(type_holder)))
|
||||
{
|
||||
memset(&buffer, 0, sizeof(struct stat));
|
||||
|
||||
|
@ -23,7 +23,7 @@ void wf_impl_operation_open_finished(
|
||||
if (NULL != result)
|
||||
{
|
||||
struct wf_json const * handle_holder = wf_impl_json_object_get(result, "handle");
|
||||
if (WF_JSON_TYPE_INT == wf_impl_json_type(handle_holder))
|
||||
if (wf_impl_json_is_int(handle_holder))
|
||||
{
|
||||
file_info.fh = wf_impl_json_int_get(handle_holder);
|
||||
}
|
||||
|
@ -76,9 +76,9 @@ void wf_impl_operation_read_finished(
|
||||
struct wf_json const * format_holder = wf_impl_json_object_get(result, "format");
|
||||
struct wf_json const * count_holder = wf_impl_json_object_get(result, "count");
|
||||
|
||||
if ((WF_JSON_TYPE_STRING == wf_impl_json_type(data_holder)) &&
|
||||
(WF_JSON_TYPE_STRING == wf_impl_json_type(format_holder)) &&
|
||||
(WF_JSON_TYPE_INT == wf_impl_json_type(count_holder)))
|
||||
if ((wf_impl_json_is_string(data_holder)) &&
|
||||
(wf_impl_json_is_string(format_holder)) &&
|
||||
(wf_impl_json_is_int(count_holder)))
|
||||
{
|
||||
char const * const data = wf_impl_json_string_get(data_holder);
|
||||
size_t const data_size = wf_impl_json_string_size(data_holder);
|
||||
|
@ -78,18 +78,18 @@ void wf_impl_operation_readdir_finished(
|
||||
struct wf_impl_dirbuffer buffer;
|
||||
wf_impl_dirbuffer_init(&buffer);
|
||||
|
||||
if (WF_JSON_TYPE_ARRAY == wf_impl_json_type(result))
|
||||
if (wf_impl_json_is_array(result))
|
||||
{
|
||||
size_t const count = wf_impl_json_array_size(result);
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
struct wf_json const * entry = wf_impl_json_array_get(result, i);
|
||||
if (WF_JSON_TYPE_OBJECT == wf_impl_json_type(entry))
|
||||
if (wf_impl_json_is_object(entry))
|
||||
{
|
||||
struct wf_json const * name_holder = wf_impl_json_object_get(entry, "name");
|
||||
struct wf_json const * inode_holder = wf_impl_json_object_get(entry, "inode");
|
||||
|
||||
if ((WF_JSON_TYPE_STRING == wf_impl_json_type(name_holder)) && (WF_JSON_TYPE_INT == wf_impl_json_type(inode_holder)))
|
||||
if ((wf_impl_json_is_string(name_holder)) && (wf_impl_json_is_int(inode_holder)))
|
||||
{
|
||||
char const * name = wf_impl_json_string_get(name_holder);
|
||||
fuse_ino_t entry_inode = (fuse_ino_t) wf_impl_json_int_get(inode_holder);
|
||||
|
@ -123,7 +123,7 @@ static void wf_impl_server_protocol_authenticate(
|
||||
struct wf_json const * type_holder = wf_impl_json_array_get(params, 0);
|
||||
struct wf_json const * creds_holder = wf_impl_json_array_get(params, 1);
|
||||
|
||||
if ((WF_JSON_TYPE_STRING == wf_impl_json_type(type_holder)) && (WF_JSON_TYPE_OBJECT == wf_impl_json_type(creds_holder)))
|
||||
if ((wf_impl_json_is_string(type_holder)) && (wf_impl_json_is_object(creds_holder)))
|
||||
{
|
||||
char const * type = wf_impl_json_string_get(type_holder);
|
||||
struct wf_credentials creds;
|
||||
@ -174,7 +174,7 @@ static void wf_impl_server_protocol_add_filesystem(
|
||||
if (WF_GOOD == status)
|
||||
{
|
||||
struct wf_json const * name_holder = wf_impl_json_array_get(params, 0);
|
||||
if (WF_JSON_TYPE_STRING == wf_impl_json_type(name_holder))
|
||||
if (wf_impl_json_is_string(name_holder))
|
||||
{
|
||||
name = wf_impl_json_string_get(name_holder);
|
||||
if (wf_impl_server_protocol_check_name(name))
|
||||
|
@ -11,7 +11,7 @@ wf_impl_json_get_int(
|
||||
int result = default_value;
|
||||
|
||||
struct wf_json const * holder = wf_impl_json_object_get(object, key);
|
||||
if (WF_JSON_TYPE_INT == wf_impl_json_type(holder))
|
||||
if (wf_impl_json_is_int(holder))
|
||||
{
|
||||
result = wf_impl_json_int_get(holder);
|
||||
}
|
||||
|
@ -9,12 +9,14 @@ TEST(json_node, null)
|
||||
{
|
||||
JsonDoc doc("null");
|
||||
ASSERT_EQ(WF_JSON_TYPE_NULL, wf_impl_json_type(doc.root()));
|
||||
ASSERT_TRUE(wf_impl_json_is_null(doc.root()));
|
||||
}
|
||||
|
||||
TEST(json_node, true)
|
||||
{
|
||||
JsonDoc doc("true");
|
||||
ASSERT_EQ(WF_JSON_TYPE_BOOL, wf_impl_json_type(doc.root()));
|
||||
ASSERT_TRUE(wf_impl_json_is_bool(doc.root()));
|
||||
ASSERT_TRUE(wf_impl_json_bool_get(doc.root()));
|
||||
}
|
||||
|
||||
@ -22,6 +24,7 @@ TEST(json_node, false)
|
||||
{
|
||||
JsonDoc doc("false");
|
||||
ASSERT_EQ(WF_JSON_TYPE_BOOL, wf_impl_json_type(doc.root()));
|
||||
ASSERT_TRUE(wf_impl_json_is_bool(doc.root()));
|
||||
ASSERT_FALSE(wf_impl_json_bool_get(doc.root()));
|
||||
}
|
||||
|
||||
@ -29,6 +32,7 @@ TEST(json_node, int)
|
||||
{
|
||||
JsonDoc doc("42");
|
||||
ASSERT_EQ(WF_JSON_TYPE_INT, wf_impl_json_type(doc.root()));
|
||||
ASSERT_TRUE(wf_impl_json_is_int(doc.root()));
|
||||
ASSERT_EQ(42, wf_impl_json_int_get(doc.root()));
|
||||
}
|
||||
|
||||
@ -36,6 +40,7 @@ TEST(json_node, string)
|
||||
{
|
||||
JsonDoc doc("\"brummni\"");
|
||||
ASSERT_EQ(WF_JSON_TYPE_STRING, wf_impl_json_type(doc.root()));
|
||||
ASSERT_TRUE(wf_impl_json_is_string(doc.root()));
|
||||
ASSERT_STREQ("brummni", wf_impl_json_string_get(doc.root()));
|
||||
}
|
||||
|
||||
@ -43,6 +48,7 @@ TEST(json_node, array)
|
||||
{
|
||||
JsonDoc doc("[1,2,3]");
|
||||
ASSERT_EQ(WF_JSON_TYPE_ARRAY, wf_impl_json_type(doc.root()));
|
||||
ASSERT_TRUE(wf_impl_json_is_array(doc.root()));
|
||||
ASSERT_EQ(3, wf_impl_json_array_size(doc.root()));
|
||||
ASSERT_EQ(WF_JSON_TYPE_INT, wf_impl_json_type(wf_impl_json_array_get(doc.root(), 0)));
|
||||
ASSERT_EQ(WF_JSON_TYPE_UNDEFINED, wf_impl_json_type(wf_impl_json_array_get(doc.root(), 4)));
|
||||
@ -52,6 +58,7 @@ TEST(json_node, object)
|
||||
{
|
||||
JsonDoc doc("{\"answer\": 42}");
|
||||
ASSERT_EQ(WF_JSON_TYPE_OBJECT, wf_impl_json_type(doc.root()));
|
||||
ASSERT_TRUE(wf_impl_json_is_object(doc.root()));
|
||||
ASSERT_EQ(1, wf_impl_json_object_size(doc.root()));
|
||||
|
||||
ASSERT_EQ(WF_JSON_TYPE_INT, wf_impl_json_type(wf_impl_json_object_get(doc.root(), "answer")));
|
||||
|
Loading…
Reference in New Issue
Block a user