Skip to content

Commit

Permalink
Merge pull request #174 from alltilla/filterx-json-from-msg-value
Browse files Browse the repository at this point in the history
filterx: support json() from LM_VT_STRING message_value
  • Loading branch information
bshifter authored Jun 24, 2024
2 parents 0ef7af9 + 0897e09 commit 7abac0a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/filterx/object-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ filterx_json_new_from_args(FilterXExpr *s, GPtrArray *args)

if (filterx_object_is_type(arg, &FILTERX_TYPE_NAME(message_value)))
{
if (filterx_message_value_get_type(arg) == LM_VT_STRING)
{
gsize repr_len;
const gchar *repr = filterx_message_value_get_value(arg, &repr_len);
g_assert(repr);
return filterx_json_new_from_repr(repr, repr_len);
}

FilterXObject *unmarshalled = filterx_object_unmarshal(arg);
if (!filterx_object_is_type(unmarshalled, &FILTERX_TYPE_NAME(json_array)) &&
!filterx_object_is_type(unmarshalled, &FILTERX_TYPE_NAME(json_object)))
Expand Down
15 changes: 15 additions & 0 deletions lib/filterx/tests/test_object_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ Test(filterx_json, test_json_function)
assert_object_json_equals(fobj, "[\"foo\",\"bar\"]");
filterx_object_unref(fobj);

fobj = _exec_json_func(filterx_message_value_new("{\"foo\": 1}", -1, LM_VT_STRING));
cr_assert(filterx_object_is_type(fobj, &FILTERX_TYPE_NAME(json_object)));
assert_object_json_equals(fobj, "{\"foo\":1}");
filterx_object_unref(fobj);

fobj = _exec_json_func(filterx_message_value_new("[1, 2]", -1, LM_VT_STRING));
cr_assert(filterx_object_is_type(fobj, &FILTERX_TYPE_NAME(json_array)));
assert_object_json_equals(fobj, "[1,2]");
filterx_object_unref(fobj);

fobj = _exec_json_func(filterx_json_object_new_from_repr("{\"foo\": 1}", -1));
cr_assert(filterx_object_is_type(fobj, &FILTERX_TYPE_NAME(json_object)));
assert_object_json_equals(fobj, "{\"foo\":1}");
Expand Down Expand Up @@ -159,6 +169,11 @@ Test(filterx_json, test_json_array_function)
assert_object_json_equals(fobj, "[\"foo\",\"bar\"]");
filterx_object_unref(fobj);

fobj = _exec_json_func(filterx_message_value_new("[1, 2]", -1, LM_VT_STRING));
cr_assert(filterx_object_is_type(fobj, &FILTERX_TYPE_NAME(json_array)));
assert_object_json_equals(fobj, "[1,2]");
filterx_object_unref(fobj);

fobj = _exec_json_array_func(filterx_json_array_new_from_repr("[1, 2]", -1));
cr_assert(filterx_object_is_type(fobj, &FILTERX_TYPE_NAME(json_array)));
assert_object_json_equals(fobj, "[1,2]");
Expand Down

0 comments on commit 7abac0a

Please sign in to comment.