Skip to content

Commit

Permalink
filterx/func-flatten: reinsert flattened keys closer to the original …
Browse files Browse the repository at this point in the history
…order

Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
  • Loading branch information
alltilla committed Nov 6, 2024
1 parent ba3e7db commit 7018eb3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/filterx/func-flatten.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ _collect_modifications_from_elem(FilterXObject *key, FilterXObject *value, gpoin
if (filterx_object_is_type(dict, &FILTERX_TYPE_NAME(dict)))
{
if (is_top_level)
*top_level_dict_keys = g_list_prepend(*top_level_dict_keys, filterx_object_ref(key));
*top_level_dict_keys = g_list_append(*top_level_dict_keys, filterx_object_ref(key));

gssize orig_len = key_buffer->len;
if (!filterx_object_repr_append(key, key_buffer))
Expand Down Expand Up @@ -108,7 +108,7 @@ _collect_modifications_from_elem(FilterXObject *key, FilterXObject *value, gpoin
}

FilterXObject *flat_key = filterx_string_new(key_buffer->str, (gssize) MIN(key_buffer->len, G_MAXSSIZE));
*flattened_kvs = g_list_prepend(*flattened_kvs, _kv_new(flat_key, value));
*flattened_kvs = g_list_append(*flattened_kvs, _kv_new(flat_key, value));
filterx_object_unref(flat_key);

g_string_truncate(key_buffer, orig_len);
Expand Down
4 changes: 2 additions & 2 deletions lib/filterx/tests/test_func_flatten.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Test(filterx_func_flatten, default_separator)
GList *args = g_list_append(NULL, filterx_function_arg_new(NULL,
filterx_literal_new(filterx_json_new_from_repr(input, -1))));
_assert_flatten(args,
"{\"top_level_field\":42,\"top_level_dict.inner_dict.inner_inner_field\":1,\"top_level_dict.inner_field\":1337}");
"{\"top_level_field\":42,\"top_level_dict.inner_field\":1337,\"top_level_dict.inner_dict.inner_inner_field\":1}");
}

Test(filterx_func_flatten, custom_separator)
Expand All @@ -122,7 +122,7 @@ Test(filterx_func_flatten, custom_separator)
filterx_literal_new(filterx_json_new_from_repr(input, -1))));
args = g_list_append(args, filterx_function_arg_new("separator", filterx_literal_new(filterx_string_new("->", -1))));
_assert_flatten(args,
"{\"top_level_field\":42,\"top_level_dict->inner_dict->inner_inner_field\":1,\"top_level_dict->inner_field\":1337}");
"{\"top_level_field\":42,\"top_level_dict->inner_field\":1337,\"top_level_dict->inner_dict->inner_inner_field\":1}");
}

static void
Expand Down
4 changes: 2 additions & 2 deletions tests/light/functional_tests/filterx/test_filterx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1956,8 +1956,8 @@ def test_flatten(config, syslog_ng):
assert file_true.get_stats()["processed"] == 1
assert "processed" not in file_false.get_stats()
assert file_true.read_log() == '[' \
'{"top_level_field":42,"top_level_dict.inner_dict.inner_inner_field":1,"top_level_dict.inner_field":1337},' \
'{"top_level_field":42,"top_level_dict->inner_dict->inner_inner_field":1,"top_level_dict->inner_field":1337}' \
'{"top_level_field":42,"top_level_dict.inner_field":1337,"top_level_dict.inner_dict.inner_inner_field":1},' \
'{"top_level_field":42,"top_level_dict->inner_field":1337,"top_level_dict->inner_dict->inner_inner_field":1}' \
']\n'


Expand Down

0 comments on commit 7018eb3

Please sign in to comment.