Commit cc18a5db authored by Alexey Botchkov's avatar Alexey Botchkov

MDEV-5313 Improving audit API.

json_locate_key() implemented.
get rid of 'key_len' argument in functions.
parent dd03cb37
...@@ -425,6 +425,11 @@ int json_path_compare(const json_path_t *a, const json_path_t *b, ...@@ -425,6 +425,11 @@ int json_path_compare(const json_path_t *a, const json_path_t *b,
int json_valid(const char *js, size_t js_len, CHARSET_INFO *cs); int json_valid(const char *js, size_t js_len, CHARSET_INFO *cs);
int json_locate_key(const char *js, const char *js_end,
const char *kname,
const char **key_start, const char **key_end,
int *comma_pos);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
...@@ -393,7 +393,7 @@ extern struct json_service_st { ...@@ -393,7 +393,7 @@ extern struct json_service_st {
int n_item, int n_item,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types (*json_get_object_key)(const char *js, const char *js_end, enum json_types (*json_get_object_key)(const char *js, const char *js_end,
const char *key, const char *key_end, const char *key,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types (*json_get_object_nkey)(const char *js,const char *js_end, enum json_types (*json_get_object_nkey)(const char *js,const char *js_end,
int nkey, int nkey,
...@@ -410,7 +410,7 @@ enum json_types json_get_array_item(const char *js, const char *js_end, ...@@ -410,7 +410,7 @@ enum json_types json_get_array_item(const char *js, const char *js_end,
int n_item, int n_item,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types json_get_object_key(const char *js, const char *js_end, enum json_types json_get_object_key(const char *js, const char *js_end,
const char *key, const char *key_end, const char *key,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types json_get_object_nkey(const char *js,const char *js_end, int nkey, enum json_types json_get_object_nkey(const char *js,const char *js_end, int nkey,
const char **keyname, const char **keyname_end, const char **keyname, const char **keyname_end,
......
...@@ -393,7 +393,7 @@ extern struct json_service_st { ...@@ -393,7 +393,7 @@ extern struct json_service_st {
int n_item, int n_item,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types (*json_get_object_key)(const char *js, const char *js_end, enum json_types (*json_get_object_key)(const char *js, const char *js_end,
const char *key, const char *key_end, const char *key,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types (*json_get_object_nkey)(const char *js,const char *js_end, enum json_types (*json_get_object_nkey)(const char *js,const char *js_end,
int nkey, int nkey,
...@@ -410,7 +410,7 @@ enum json_types json_get_array_item(const char *js, const char *js_end, ...@@ -410,7 +410,7 @@ enum json_types json_get_array_item(const char *js, const char *js_end,
int n_item, int n_item,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types json_get_object_key(const char *js, const char *js_end, enum json_types json_get_object_key(const char *js, const char *js_end,
const char *key, const char *key_end, const char *key,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types json_get_object_nkey(const char *js,const char *js_end, int nkey, enum json_types json_get_object_nkey(const char *js,const char *js_end, int nkey,
const char **keyname, const char **keyname_end, const char **keyname, const char **keyname_end,
......
...@@ -393,7 +393,7 @@ extern struct json_service_st { ...@@ -393,7 +393,7 @@ extern struct json_service_st {
int n_item, int n_item,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types (*json_get_object_key)(const char *js, const char *js_end, enum json_types (*json_get_object_key)(const char *js, const char *js_end,
const char *key, const char *key_end, const char *key,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types (*json_get_object_nkey)(const char *js,const char *js_end, enum json_types (*json_get_object_nkey)(const char *js,const char *js_end,
int nkey, int nkey,
...@@ -410,7 +410,7 @@ enum json_types json_get_array_item(const char *js, const char *js_end, ...@@ -410,7 +410,7 @@ enum json_types json_get_array_item(const char *js, const char *js_end,
int n_item, int n_item,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types json_get_object_key(const char *js, const char *js_end, enum json_types json_get_object_key(const char *js, const char *js_end,
const char *key, const char *key_end, const char *key,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types json_get_object_nkey(const char *js,const char *js_end, int nkey, enum json_types json_get_object_nkey(const char *js,const char *js_end, int nkey,
const char **keyname, const char **keyname_end, const char **keyname, const char **keyname_end,
......
...@@ -393,7 +393,7 @@ extern struct json_service_st { ...@@ -393,7 +393,7 @@ extern struct json_service_st {
int n_item, int n_item,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types (*json_get_object_key)(const char *js, const char *js_end, enum json_types (*json_get_object_key)(const char *js, const char *js_end,
const char *key, const char *key_end, const char *key,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types (*json_get_object_nkey)(const char *js,const char *js_end, enum json_types (*json_get_object_nkey)(const char *js,const char *js_end,
int nkey, int nkey,
...@@ -410,7 +410,7 @@ enum json_types json_get_array_item(const char *js, const char *js_end, ...@@ -410,7 +410,7 @@ enum json_types json_get_array_item(const char *js, const char *js_end,
int n_item, int n_item,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types json_get_object_key(const char *js, const char *js_end, enum json_types json_get_object_key(const char *js, const char *js_end,
const char *key, const char *key_end, const char *key,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types json_get_object_nkey(const char *js,const char *js_end, int nkey, enum json_types json_get_object_nkey(const char *js,const char *js_end, int nkey,
const char **keyname, const char **keyname_end, const char **keyname, const char **keyname_end,
......
...@@ -393,7 +393,7 @@ extern struct json_service_st { ...@@ -393,7 +393,7 @@ extern struct json_service_st {
int n_item, int n_item,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types (*json_get_object_key)(const char *js, const char *js_end, enum json_types (*json_get_object_key)(const char *js, const char *js_end,
const char *key, const char *key_end, const char *key,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types (*json_get_object_nkey)(const char *js,const char *js_end, enum json_types (*json_get_object_nkey)(const char *js,const char *js_end,
int nkey, int nkey,
...@@ -410,7 +410,7 @@ enum json_types json_get_array_item(const char *js, const char *js_end, ...@@ -410,7 +410,7 @@ enum json_types json_get_array_item(const char *js, const char *js_end,
int n_item, int n_item,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types json_get_object_key(const char *js, const char *js_end, enum json_types json_get_object_key(const char *js, const char *js_end,
const char *key, const char *key_end, const char *key,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types json_get_object_nkey(const char *js,const char *js_end, int nkey, enum json_types json_get_object_nkey(const char *js,const char *js_end, int nkey,
const char **keyname, const char **keyname_end, const char **keyname, const char **keyname_end,
......
...@@ -66,7 +66,7 @@ extern struct json_service_st { ...@@ -66,7 +66,7 @@ extern struct json_service_st {
int n_item, int n_item,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types (*json_get_object_key)(const char *js, const char *js_end, enum json_types (*json_get_object_key)(const char *js, const char *js_end,
const char *key, const char *key_end, const char *key,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types (*json_get_object_nkey)(const char *js,const char *js_end, enum json_types (*json_get_object_nkey)(const char *js,const char *js_end,
int nkey, int nkey,
...@@ -95,7 +95,7 @@ enum json_types json_get_array_item(const char *js, const char *js_end, ...@@ -95,7 +95,7 @@ enum json_types json_get_array_item(const char *js, const char *js_end,
int n_item, int n_item,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types json_get_object_key(const char *js, const char *js_end, enum json_types json_get_object_key(const char *js, const char *js_end,
const char *key, const char *key_end, const char *key,
const char **value, int *value_len); const char **value, int *value_len);
enum json_types json_get_object_nkey(const char *js,const char *js_end, int nkey, enum json_types json_get_object_nkey(const char *js,const char *js_end, int nkey,
const char **keyname, const char **keyname_end, const char **keyname, const char **keyname_end,
......
This diff is collapsed.
...@@ -1943,9 +1943,10 @@ enum json_types json_get_array_item(const char *js, const char *js_end, ...@@ -1943,9 +1943,10 @@ enum json_types json_get_array_item(const char *js, const char *js_end,
@retval JSV_NOTHING - no such key found. @retval JSV_NOTHING - no such key found.
*/ */
enum json_types json_get_object_key(const char *js, const char *js_end, enum json_types json_get_object_key(const char *js, const char *js_end,
const char *key, const char *key_end, const char *key,
const char **value, int *value_len) const char **value, int *value_len)
{ {
const char *key_end= key + strlen(key);
json_engine_t je; json_engine_t je;
json_string_t key_name; json_string_t key_name;
int n_keys= 0; int n_keys= 0;
...@@ -1999,10 +2000,99 @@ enum json_types json_get_object_nkey(const char *js,const char *js_end, int nkey ...@@ -1999,10 +2000,99 @@ enum json_types json_get_object_nkey(const char *js,const char *js_end, int nkey
@retval 0 - success, json is well-formed @retval 0 - success, json is well-formed
@retval 1 - error, json is invalid @retval 1 - error, json is invalid
*/int json_valid(const char *js, size_t js_len, CHARSET_INFO *cs) */
int json_valid(const char *js, size_t js_len, CHARSET_INFO *cs)
{ {
json_engine_t je; json_engine_t je;
json_scan_start(&je, cs, (const uchar *) js, (const uchar *) js + js_len); json_scan_start(&je, cs, (const uchar *) js, (const uchar *) js + js_len);
while (json_scan_next(&je) == 0) /* no-op */ ; while (json_scan_next(&je) == 0) /* no-op */ ;
return je.s.error == 0; return je.s.error == 0;
} }
/*
Expects the JSON object as an js argument, and the key name.
Looks for this key in the object and returns
the location of all the text related to it.
The text includes the comma, separating this key.
comma_pos - the hint where the comma is. It is important
if you plan to replace the key rather than just cut.
1 - comma is on the left
2 - comma is on the right.
0 - no comma at all (the object has just this single key)
if no such key found *key_start is set to NULL.
*/
int json_locate_key(const char *js, const char *js_end,
const char *kname,
const char **key_start, const char **key_end,
int *comma_pos)
{
const char *kname_end= kname + strlen(kname);
json_engine_t je;
json_string_t key_name;
int t_next, c_len, match_result;
json_string_set_cs(&key_name, &my_charset_utf8mb4_bin);
json_scan_start(&je, &my_charset_utf8mb4_bin,(const uchar *) js,
(const uchar *) js_end);
if (json_read_value(&je) ||
je.value_type != JSON_VALUE_OBJECT)
goto err_return;
*key_start= (const char *) je.s.c_str;
*comma_pos= 0;
while (!json_scan_next(&je))
{
switch (je.state)
{
case JST_KEY:
json_string_set_str(&key_name, (const uchar *) kname,
(const uchar *) kname_end);
match_result= json_key_matches(&je, &key_name);
if (json_skip_key(&je))
goto err_return;
get_first_nonspace(&je.s, &t_next, &c_len);
je.s.c_str-= c_len;
if (match_result)
{
*key_end= (const char *) je.s.c_str;
if (*comma_pos == 1)
return 0;
DBUG_ASSERT(*comma_pos == 0);
if (t_next == C_COMMA)
{
*key_end+= c_len;
*comma_pos= 2;
}
else if (t_next == C_RCURB)
*comma_pos= 0;
else
goto err_return;
return 0;
}
*key_start= (const char *) je.s.c_str;
*comma_pos= 1;
break;
case JST_OBJ_END:
*key_start= NULL;
return 0;
}
}
err_return:
return 1;
}
...@@ -17,21 +17,29 @@ ...@@ -17,21 +17,29 @@
#include <my_sys.h> #include <my_sys.h>
#include <json_lib.h> #include <json_lib.h>
int json_locate_key(const char *js, const char *js_end, const char *kname,
const char **key_start, const char **key_end,
int *comma_pos);
int main() int main()
{ {
const char *json="{\"int\":1, \"str\":\"foo bar\", " const char *json="{\"int\":1, \"str\":\"foo bar\", "
"\"array\":[10,20,{\"c\":\"d\"}],\"bool\":false}"; "\"array\":[10,20,{\"c\":\"d\"}],\"bool\":false}";
const char *json_ar="[1, \"foo bar\", " "[10,20,{\"c\":\"d\"}], false]"; const char *json_ar="[1, \"foo bar\", " "[10,20,{\"c\":\"d\"}], false]";
const char *json_w="{\"int\" : 1 , \"str\" : \"foo bar\" , "
"\"array\" : [10,20,{\"c\":\"d\"}] , \"bool\" : false }";
const char *json_1="{ \"str\" : \"foo bar\" }";
enum json_types value_type; enum json_types value_type;
const char *value_start; const char *value_start;
int value_len; int value_len;
const char *key_start, *key_end;
int result, comma_pos;
plan(10); plan(15);
#define do_json(V) \ #define do_json(V) \
do { \ do { \
value_type= json_get_object_key(json, json+strlen(json), \ value_type= json_get_object_key(json, json+strlen(json), \
V, V + (sizeof(V) - 1),&value_start, &value_len); \ V, &value_start, &value_len); \
ok(value_type != JSV_BAD_JSON, V); \ ok(value_type != JSV_BAD_JSON, V); \
diag("type=%d, value=\"%.*s\"", value_type, (int)value_len, value_start); \ diag("type=%d, value=\"%.*s\"", value_type, (int)value_len, value_start); \
} while(0) } while(0)
...@@ -42,6 +50,16 @@ int main() ...@@ -42,6 +50,16 @@ int main()
ok(value_type != JSV_BAD_JSON, #N); \ ok(value_type != JSV_BAD_JSON, #N); \
diag("type=%d, value=\"%.*s\"", value_type, (int)value_len, value_start); \ diag("type=%d, value=\"%.*s\"", value_type, (int)value_len, value_start); \
} while(0) } while(0)
#define do_json_locate(J, V) \
do { \
result= json_locate_key(J, J+strlen(J), \
V, &key_start, &key_end, &comma_pos); \
ok(result == 0, V); \
if (key_start) \
diag("key_str=\"%.*s\" comma_pos= %d", (int)(key_end - key_start), key_start, comma_pos); \
else \
diag("no key found"); \
} while(0)
do_json("int"); do_json("int");
do_json("str"); do_json("str");
...@@ -54,5 +72,11 @@ int main() ...@@ -54,5 +72,11 @@ int main()
do_json_ar(2); do_json_ar(2);
do_json_ar(3); do_json_ar(3);
do_json_ar(4); do_json_ar(4);
do_json_locate(json_w, "bool");
do_json_locate(json_w, "int");
do_json_locate(json_w, "array");
do_json_locate(json_1, "str");
do_json_locate(json_w, "c");
return exit_status(); return exit_status();
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment