Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filterx keys function #435

Merged
merged 5 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/filterx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ set(FILTERX_HEADERS
filterx/object-null.h
filterx/object-primitive.h
filterx/object-string.h
filterx/func-keys.h
PARENT_SCOPE
)

Expand Down Expand Up @@ -127,6 +128,7 @@ set(FILTERX_SOURCES
filterx/object-null.c
filterx/object-primitive.c
filterx/object-string.c
filterx/func-keys.c
PARENT_SCOPE
)

Expand Down
2 changes: 2 additions & 0 deletions lib/filterx/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ filterxinclude_HEADERS = \
lib/filterx/func-flags.h \
lib/filterx/func-flatten.h \
lib/filterx/func-istype.h \
lib/filterx/func-keys.h \
lib/filterx/func-len.h \
lib/filterx/func-sdata.h \
lib/filterx/func-set-fields.h \
Expand Down Expand Up @@ -111,6 +112,7 @@ filterx_sources = \
lib/filterx/filterx-weakrefs.c \
lib/filterx/func-flatten.c \
lib/filterx/func-istype.c \
lib/filterx/func-keys.c \
lib/filterx/func-len.c \
lib/filterx/func-sdata.c \
lib/filterx/func-set-fields.c \
Expand Down
2 changes: 2 additions & 0 deletions lib/filterx/filterx-globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "filterx/expr-regexp.h"
#include "filterx/expr-unset.h"
#include "filterx/filterx-eval.h"
#include "filterx/func-keys.h"

static GHashTable *filterx_builtin_simple_functions = NULL;
static GHashTable *filterx_builtin_function_ctors = NULL;
Expand Down Expand Up @@ -150,6 +151,7 @@ _ctors_init(void)
g_assert(filterx_builtin_function_ctor_register("endswith", filterx_function_endswith_new));
g_assert(filterx_builtin_function_ctor_register("includes", filterx_function_includes_new));
g_assert(filterx_builtin_function_ctor_register("strftime", filterx_function_strftime_new));
g_assert(filterx_builtin_function_ctor_register("keys", filterx_function_keys_new));
}

static void
Expand Down
139 changes: 139 additions & 0 deletions lib/filterx/func-keys.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright (c) 2024 Axoflow
* Copyright (c) 2024 shifter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/

#include "filterx/func-keys.h"
#include "filterx/object-dict-interface.h"
#include "filterx/object-list-interface.h"
#include "filterx/object-primitive.h"
#include "filterx/filterx-eval.h"
#include "filterx/object-json.h"

typedef struct FilterXFunctionKeys_
{
FilterXFunction super;
FilterXExpr *object_expr;
} FilterXFunctionKeys;

static FilterXObject *
_eval(FilterXExpr *s)
{
FilterXFunctionKeys *self = (FilterXFunctionKeys *) s;

FilterXObject *object = filterx_expr_eval(self->object_expr);
if (!object)
{
filterx_eval_push_error("Failed to evaluate first argument. " FILTERX_FUNC_KEYS_USAGE, s, NULL);
return NULL;
}

FilterXObject *result = filterx_json_array_new_empty();
if (!filterx_dict_keys(object, &result))
{
filterx_eval_push_error(FILTERX_FUNC_KEYS_ERR_NONDICT FILTERX_FUNC_KEYS_USAGE, s, NULL);
filterx_object_unref(result);
return NULL;
}
return result;
}

static gboolean
_init(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXFunctionKeys *self = (FilterXFunctionKeys *) s;

if (!filterx_expr_init(self->object_expr, cfg))
return FALSE;

return filterx_function_init_method(&self->super, cfg);
}

static void
_deinit(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXFunctionKeys *self = (FilterXFunctionKeys *) s;
filterx_expr_deinit(self->object_expr, cfg);
filterx_function_deinit_method(&self->super, cfg);
}

static void
_free(FilterXExpr *s)
{
FilterXFunctionKeys *self = (FilterXFunctionKeys *) s;
filterx_expr_unref(self->object_expr);
filterx_function_free_method(&self->super);
}

static FilterXExpr *
_extract_object_expr(FilterXFunctionArgs *args, GError **error)
{
FilterXExpr *object_expr = filterx_function_args_get_expr(args, 0);
if (!object_expr)
{
g_set_error(error, FILTERX_FUNCTION_ERROR, FILTERX_FUNCTION_ERROR_CTOR_FAIL,
"argument must be set: object. " FILTERX_FUNC_KEYS_USAGE);
return NULL;
}

return object_expr;
}

static gboolean
_extract_args(FilterXFunctionKeys *self, FilterXFunctionArgs *args, GError **error)
{
if (filterx_function_args_len(args) != 1)
{
g_set_error(error, FILTERX_FUNCTION_ERROR, FILTERX_FUNCTION_ERROR_CTOR_FAIL,
FILTERX_FUNC_KEYS_ERR_NUM_ARGS FILTERX_FUNC_KEYS_USAGE);
return FALSE;
}
bazsi marked this conversation as resolved.
Show resolved Hide resolved

self->object_expr = _extract_object_expr(args, error);
if (!self->object_expr)
return FALSE;

return TRUE;
}

FilterXExpr *
filterx_function_keys_new(FilterXFunctionArgs *args, GError **error)
{
FilterXFunctionKeys *self = g_new0(FilterXFunctionKeys, 1);
filterx_function_init_instance(&self->super, "keys");
self->super.super.eval = _eval;
self->super.super.init = _init;
self->super.super.deinit = _deinit;
self->super.super.free_fn = _free;

if (!_extract_args(self, args, error) ||
!filterx_function_args_check(args, error))
goto error;

filterx_function_args_free(args);
return &self->super.super;

error:
filterx_function_args_free(args);
filterx_expr_unref(&self->super.super);
return NULL;
}
36 changes: 36 additions & 0 deletions lib/filterx/func-keys.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024 Axoflow
* Copyright (c) 2024 shifter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/

#ifndef FILTERX_FUNC_KEYS_H_INCLUDED
#define FILTERX_FUNC_KEYS_H_INCLUDED

#include "filterx/expr-function.h"

#define FILTERX_FUNC_KEYS_USAGE "Usage: keys(dict)"
#define FILTERX_FUNC_KEYS_ERR_NONDICT "Argument must be a dict. "
#define FILTERX_FUNC_KEYS_ERR_NUM_ARGS "Invalid number of arguments. "

FilterXExpr *filterx_function_keys_new(FilterXFunctionArgs *args, GError **error);

#endif
28 changes: 28 additions & 0 deletions lib/filterx/object-dict-interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "filterx/object-json.h"
#include "filterx/filterx-object-istype.h"
#include "filterx/filterx-ref.h"
#include "filterx/object-list-interface.h"
#include "str-utils.h"

gboolean
Expand Down Expand Up @@ -60,6 +61,33 @@ filterx_dict_merge(FilterXObject *s, FilterXObject *other)
return filterx_dict_iter(other, _add_elem_to_dict, self);
}

static gboolean
_add_elem_to_list(FilterXObject *key_obj, FilterXObject *value_obj, gpointer user_data)
{
FilterXObject *list = (FilterXObject *) user_data;

return filterx_list_append(list, &key_obj);
}

gboolean
filterx_dict_keys(FilterXObject *s, FilterXObject **keys)
{
g_assert(s);
g_assert(keys);
FilterXObject *obj = filterx_ref_unwrap_ro(s);
if (!filterx_object_is_type(obj, &FILTERX_TYPE_NAME(dict)))
goto error;
g_assert(filterx_object_is_type(*keys, &FILTERX_TYPE_NAME(list)));

gboolean result = filterx_dict_iter(obj, _add_elem_to_list, *keys);

filterx_object_unref(s);
return result;
error:
filterx_object_unref(s);
return FALSE;
}

static gboolean
_len(FilterXObject *s, guint64 *len)
{
Expand Down
1 change: 1 addition & 0 deletions lib/filterx/object-dict-interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct FilterXDict_

gboolean filterx_dict_iter(FilterXObject *s, FilterXDictIterFunc func, gpointer user_data);
gboolean filterx_dict_merge(FilterXObject *s, FilterXObject *other);
gboolean filterx_dict_keys(FilterXObject *s, FilterXObject **keys);

void filterx_dict_init_instance(FilterXDict *self, FilterXType *type);

Expand Down
2 changes: 2 additions & 0 deletions lib/filterx/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ add_unit_test(LIBTEST CRITERION TARGET test_expr_plus_generator DEPENDS json-plu
add_unit_test(LIBTEST CRITERION TARGET test_metrics_labels DEPENDS json-plugin ${JSONC_LIBRARY})
add_unit_test(LIBTEST CRITERION TARGET test_expr_regexp_search DEPENDS json-plugin ${JSONC_LIBRARY})
add_unit_test(LIBTEST CRITERION TARGET test_expr_regexp_subst DEPENDS json-plugin ${JSONC_LIBRARY})
add_unit_test(LIBTEST CRITERION TARGET test_object_dict_interface DEPENDS json-plugin ${JSONC_LIBRARY})
add_unit_test(LIBTEST CRITERION TARGET test_func_keys DEPENDS json-plugin ${JSONC_LIBRARY})
10 changes: 9 additions & 1 deletion lib/filterx/tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ lib_filterx_tests_TESTS = \
lib/filterx/tests/test_expr_plus \
lib/filterx/tests/test_expr_plus_generator \
lib/filterx/tests/test_expr_plus \
lib/filterx/tests/test_metrics_labels
lib/filterx/tests/test_metrics_labels \
lib/filterx/tests/test_object_dict_interface \
lib/filterx/tests/test_func_keys

EXTRA_DIST += lib/filterx/tests/CMakeLists.txt

Expand Down Expand Up @@ -117,3 +119,9 @@ lib_filterx_tests_test_expr_plus_generator_LDADD = $(TEST_LDADD) $(JSON_LIBS)

lib_filterx_tests_test_metrics_labels_CFLAGS = $(TEST_CFLAGS)
lib_filterx_tests_test_metrics_labels_LDADD = $(TEST_LDADD) $(JSON_LIBS)

lib_filterx_tests_test_object_dict_interface_CFLAGS = $(TEST_CFLAGS)
lib_filterx_tests_test_object_dict_interface_LDADD = $(TEST_LDADD) $(JSON_LIBS)

lib_filterx_tests_test_func_keys_CFLAGS = $(TEST_CFLAGS)
lib_filterx_tests_test_func_keys_LDADD = $(TEST_LDADD) $(JSON_LIBS)
Loading
Loading