Skip to content

Commit

Permalink
filterx: add filterx error handling to simple functions argument vali…
Browse files Browse the repository at this point in the history
…dation

This patch does two things:
1) adds FilterXExpr * argument to all simple functions
2) changes msg_error() style error handling to filterx style

Signed-off-by: Balazs Scheidler <[email protected]>
  • Loading branch information
bazsi committed Jun 9, 2024
1 parent 0badd9c commit 0ca56f0
Show file tree
Hide file tree
Showing 38 changed files with 157 additions and 162 deletions.
2 changes: 1 addition & 1 deletion lib/filterx/expr-function.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ _simple_eval(FilterXExpr *s)
FilterXSimpleFunctionProto f = self->function_proto;

g_assert(f != NULL);
FilterXObject *res = f(args);
FilterXObject *res = f(s, args);

if (args != NULL)
g_ptr_array_free(args, TRUE);
Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/expr-function.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "filterx/filterx-object.h"
#include "plugin.h"

typedef FilterXObject *(*FilterXSimpleFunctionProto)(GPtrArray *);
typedef FilterXObject *(*FilterXSimpleFunctionProto)(FilterXExpr *s, GPtrArray *);

void filterx_simple_function_argument_error(FilterXExpr *s, gchar *error_info, gboolean free_info);

Expand Down
6 changes: 4 additions & 2 deletions lib/filterx/filterx-globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "filterx/func-istype.h"
#include "filterx/func-len.h"
#include "filterx/func-vars.h"
#include "filterx/filterx-eval.h"

static GHashTable *filterx_builtin_simple_functions = NULL;
static GHashTable *filterx_builtin_function_ctors = NULL;
Expand Down Expand Up @@ -197,11 +198,12 @@ filterx_global_deinit(void)
filterx_types_deinit();
}

FilterXObject *filterx_typecast_get_arg(GPtrArray *args, gchar *alt_msg)
FilterXObject *
filterx_typecast_get_arg(FilterXExpr *s, GPtrArray *args)
{
if (args == NULL || args->len != 1)
{
msg_error(alt_msg ? alt_msg : "filterx: typecast functions must have exactly 1 argument");
filterx_simple_function_argument_error(s, "Requires exactly one argument", FALSE);
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/filterx-globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ FilterXType *filterx_type_lookup(const gchar *type_name);
gboolean filterx_type_register(const gchar *type_name, FilterXType *fxtype);

// Helpers
FilterXObject *filterx_typecast_get_arg(GPtrArray *args, gchar *alt_msg);
FilterXObject *filterx_typecast_get_arg(FilterXExpr *s, GPtrArray *args);

#endif
8 changes: 4 additions & 4 deletions lib/filterx/func-len.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@

#include "filterx/func-len.h"
#include "filterx/object-primitive.h"
#include "filterx/filterx-eval.h"

#define FILTERX_FUNC_LEN_USAGE "Usage: len(object)"

FilterXObject *
filterx_simple_function_len(GPtrArray *args)
filterx_simple_function_len(FilterXExpr *s, GPtrArray *args)
{
if (args == NULL || args->len != 1)
{
msg_error("FilterX: len: invalid number of arguments. " FILTERX_FUNC_LEN_USAGE);
filterx_simple_function_argument_error(s, "Requires exactly one argument", FALSE);
return NULL;
}

Expand All @@ -41,8 +42,7 @@ filterx_simple_function_len(GPtrArray *args)
gboolean success = filterx_object_len(object, &len);
if (!success)
{
msg_error("FilterX: len: object type is not supported",
evt_tag_str("type", object->type->name));
filterx_eval_push_error("Object does not support len()", s, object);
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/func-len.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@

#include "filterx/expr-function.h"

FilterXObject *filterx_simple_function_len(GPtrArray *args);
FilterXObject *filterx_simple_function_len(FilterXExpr *s, GPtrArray *args);

#endif
4 changes: 2 additions & 2 deletions lib/filterx/func-vars.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ _add_to_dict(FilterXVariable *variable, gpointer user_data)
}

FilterXObject *
filterx_simple_function_vars(GPtrArray *args)
filterx_simple_function_vars(FilterXExpr *s, GPtrArray *args)
{
if (args && args->len != 0)
{
msg_error("filterx: vars() function does not take any arguments");
filterx_simple_function_argument_error(s, "Incorrect number of arguments", FALSE);
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/func-vars.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@

#include "filterx/expr-function.h"

FilterXObject *filterx_simple_function_vars(GPtrArray *args);
FilterXObject *filterx_simple_function_vars(FilterXExpr *s, GPtrArray *args);

#endif
14 changes: 5 additions & 9 deletions lib/filterx/object-datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,9 @@ filterx_datetime_get_value(FilterXObject *s)


FilterXObject *
filterx_typecast_datetime(GPtrArray *args)
filterx_typecast_datetime(FilterXExpr *s, GPtrArray *args)
{
FilterXObject *object = filterx_typecast_get_arg(args,
"FilterX: Failed to create datetime object: invalid number of arguments. "
"Usage: datetime($isodate_str), datetime($unix_int_ms) or datetime($unix_float_s)");
FilterXObject *object = filterx_typecast_get_arg(s, args);
if (!object)
return NULL;

Expand All @@ -144,7 +142,7 @@ filterx_typecast_datetime(GPtrArray *args)
}
else if (filterx_object_is_type(object, &FILTERX_TYPE_NAME(string)))
{
return filterx_typecast_datetime_isodate(args);
return filterx_typecast_datetime_isodate(s, args);
}
msg_error("filterx: invalid typecast",
evt_tag_str("from", object->type->name),
Expand All @@ -153,11 +151,9 @@ filterx_typecast_datetime(GPtrArray *args)
}

FilterXObject *
filterx_typecast_datetime_isodate(GPtrArray *args)
filterx_typecast_datetime_isodate(FilterXExpr *s, GPtrArray *args)
{
FilterXObject *object = filterx_typecast_get_arg(args,
"FilterX: Failed to create datetime object: invalid number of arguments. "
"Usage: datetime($isodate_str), datetime($unix_int_ms) or datetime($unix_float_s)");
FilterXObject *object = filterx_typecast_get_arg(s, args);
if (!object)
return NULL;

Expand Down
4 changes: 2 additions & 2 deletions lib/filterx/object-datetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ FILTERX_DECLARE_TYPE(datetime);

FilterXObject *filterx_datetime_new(const UnixTime *ut);
UnixTime filterx_datetime_get_value(FilterXObject *s);
FilterXObject *filterx_typecast_datetime(GPtrArray *args);
FilterXObject *filterx_typecast_datetime_isodate(GPtrArray *args);
FilterXObject *filterx_typecast_datetime(FilterXExpr *s, GPtrArray *args);
FilterXObject *filterx_typecast_datetime_isodate(FilterXExpr *, GPtrArray *args);
FilterXFunction *filterx_function_strptime_new(const gchar *function_name, FilterXFunctionArgs *args, GError **error);

gboolean datetime_repr(const UnixTime *ut, GString *repr);
Expand Down
13 changes: 6 additions & 7 deletions lib/filterx/object-json-array.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "filterx/object-message-value.h"
#include "filterx/filterx-weakrefs.h"
#include "filterx/object-list-interface.h"
#include "filterx/expr-function.h"
#include "filterx/filterx-eval.h"

#include "scanner/list-scanner/list-scanner.h"
#include "str-repr/encode.h"
Expand Down Expand Up @@ -304,15 +306,14 @@ filterx_json_array_new_from_syslog_ng_list(const gchar *repr, gssize repr_len)
}

FilterXObject *
filterx_json_array_new_from_args(GPtrArray *args)
filterx_json_array_new_from_args(FilterXExpr *s, GPtrArray *args)
{
if (!args || args->len == 0)
return filterx_json_array_new_empty();

if (args->len != 1)
{
msg_error("FilterX: Failed to create JSON array: invalid number of arguments. "
"Usage: json_array() or json_array($raw_json_string) or json_array($existing_json_array)");
filterx_simple_function_argument_error(s, "Requires zero or one argument", FALSE);
return NULL;
}

Expand All @@ -338,10 +339,8 @@ filterx_json_array_new_from_args(GPtrArray *args)
return filterx_json_array_new_from_repr(repr, repr_len);

error:
msg_error("FilterX: Failed to create JSON object: invalid argument type. "
"Usage: json_array() or json_array($raw_json_string) or json_array($syslog_ng_list) or "
"json_array($existing_json_array)",
evt_tag_str("type", arg->type->name));
filterx_eval_push_error_info("Argument must be a json array, a string or a syslog-ng list", s,
g_strdup_printf("got \"%s\" instead", arg->type->name), TRUE);
return NULL;
}

Expand Down
9 changes: 3 additions & 6 deletions lib/filterx/object-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,14 @@ filterx_json_new_from_repr(const gchar *repr, gssize repr_len)
}

FilterXObject *
filterx_json_new_from_args(GPtrArray *args)
filterx_json_new_from_args(FilterXExpr *s, GPtrArray *args)
{
if (!args || args->len == 0)
return filterx_json_object_new_empty();

if (args->len != 1)
{
msg_error("FilterX: Failed to create JSON object: invalid number of arguments. "
"Usage: json() or json($raw_json_string) or json($existing_json)");
filterx_eval_push_error("Too many arguments", s, NULL);
return NULL;
}

Expand Down Expand Up @@ -220,9 +219,7 @@ filterx_json_new_from_args(GPtrArray *args)
return filterx_json_new_from_repr(repr, repr_len);

error:
msg_error("FilterX: Failed to create JSON object: invalid argument type. "
"Usage: json() or json($raw_json_string) or json($syslog_ng_list) or json($existing_json)",
evt_tag_str("type", arg->type->name));
filterx_eval_push_error_info("Argument must be a json, a string or a syslog-ng list", s, g_strdup_printf("got \"%s\" instead", arg->type->name), TRUE);
return NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/filterx/object-json.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ FilterXObject *filterx_json_array_new_from_syslog_ng_list(const gchar *repr, gss
FilterXObject *filterx_json_object_new_empty(void);
FilterXObject *filterx_json_array_new_empty(void);

FilterXObject *filterx_json_new_from_args(GPtrArray *args);
FilterXObject *filterx_json_array_new_from_args(GPtrArray *args);
FilterXObject *filterx_json_new_from_args(FilterXExpr *s, GPtrArray *args);
FilterXObject *filterx_json_array_new_from_args(FilterXExpr *s, GPtrArray *args);

FilterXObject *filterx_json_new_from_object(struct json_object *object);

Expand Down
12 changes: 6 additions & 6 deletions lib/filterx/object-primitive.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ filterx_primitive_get_value(FilterXObject *s)
}

FilterXObject *
filterx_typecast_boolean(GPtrArray *args)
filterx_typecast_boolean(FilterXExpr *s, GPtrArray *args)
{
FilterXObject *object = filterx_typecast_get_arg(args, NULL);
FilterXObject *object = filterx_typecast_get_arg(s, args);
if (!object)
return NULL;

Expand All @@ -247,9 +247,9 @@ filterx_typecast_boolean(GPtrArray *args)
}

FilterXObject *
filterx_typecast_integer(GPtrArray *args)
filterx_typecast_integer(FilterXExpr *s, GPtrArray *args)
{
FilterXObject *object = filterx_typecast_get_arg(args, NULL);
FilterXObject *object = filterx_typecast_get_arg(s, args);
if (!object)
return NULL;

Expand Down Expand Up @@ -281,9 +281,9 @@ filterx_typecast_integer(GPtrArray *args)
}

FilterXObject *
filterx_typecast_double(GPtrArray *args)
filterx_typecast_double(FilterXExpr *s, GPtrArray *args)
{
FilterXObject *object = filterx_typecast_get_arg(args, NULL);
FilterXObject *object = filterx_typecast_get_arg(s, args);
if (!object)
return NULL;

Expand Down
6 changes: 3 additions & 3 deletions lib/filterx/object-primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ FilterXObject *filterx_boolean_new(gboolean value);
FilterXObject *filterx_enum_new(GlobalConfig *cfg, const gchar *namespace_name, const gchar *enum_name);
GenericNumber filterx_primitive_get_value(FilterXObject *s);

FilterXObject *filterx_typecast_boolean(GPtrArray *args);
FilterXObject *filterx_typecast_integer(GPtrArray *args);
FilterXObject *filterx_typecast_double(GPtrArray *args);
FilterXObject *filterx_typecast_boolean(FilterXExpr *s, GPtrArray *args);
FilterXObject *filterx_typecast_integer(FilterXExpr *s, GPtrArray *args);
FilterXObject *filterx_typecast_double(FilterXExpr *s, GPtrArray *args);

gboolean bool_repr(gboolean bool_val, GString *repr);
gboolean double_repr(double val, GString *repr);
Expand Down
12 changes: 6 additions & 6 deletions lib/filterx/object-string.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ filterx_protobuf_new(const gchar *mem, gssize mem_len)
}

FilterXObject *
filterx_typecast_string(GPtrArray *args)
filterx_typecast_string(FilterXExpr *s, GPtrArray *args)
{
FilterXObject *object = filterx_typecast_get_arg(args, NULL);
FilterXObject *object = filterx_typecast_get_arg(s, args);
if (!object)
return NULL;

Expand All @@ -235,9 +235,9 @@ filterx_typecast_string(GPtrArray *args)
}

FilterXObject *
filterx_typecast_bytes(GPtrArray *args)
filterx_typecast_bytes(FilterXExpr *s, GPtrArray *args)
{
FilterXObject *object = filterx_typecast_get_arg(args, NULL);
FilterXObject *object = filterx_typecast_get_arg(s, args);
if (!object)
return NULL;

Expand Down Expand Up @@ -268,9 +268,9 @@ filterx_typecast_bytes(GPtrArray *args)
}

FilterXObject *
filterx_typecast_protobuf(GPtrArray *args)
filterx_typecast_protobuf(FilterXExpr *s, GPtrArray *args)
{
FilterXObject *object = filterx_typecast_get_arg(args, NULL);
FilterXObject *object = filterx_typecast_get_arg(s, args);
if (!object)
return NULL;

Expand Down
6 changes: 3 additions & 3 deletions lib/filterx/object-string.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ FILTERX_DECLARE_TYPE(protobuf);
const gchar *filterx_string_get_value(FilterXObject *s, gsize *length);
const gchar *filterx_bytes_get_value(FilterXObject *s, gsize *length);
const gchar *filterx_protobuf_get_value(FilterXObject *s, gsize *length);
FilterXObject *filterx_typecast_string(GPtrArray *args);
FilterXObject *filterx_typecast_bytes(GPtrArray *args);
FilterXObject *filterx_typecast_protobuf(GPtrArray *args);
FilterXObject *filterx_typecast_string(FilterXExpr *s, GPtrArray *args);
FilterXObject *filterx_typecast_bytes(FilterXExpr *s, GPtrArray *args);
FilterXObject *filterx_typecast_protobuf(FilterXExpr *s, GPtrArray *args);

FilterXObject *filterx_string_new(const gchar *str, gssize str_len);
FilterXObject *filterx_bytes_new(const gchar *str, gssize str_len);
Expand Down
4 changes: 2 additions & 2 deletions lib/filterx/tests/test_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#define TEST_BUILTIN_FUNCTION_NAME "TEST_BUILTIN_DUMMY_KEY"

FilterXObject *
test_builtin_simple_dummy_function(GPtrArray *args)
test_builtin_simple_dummy_function(FilterXExpr *s, GPtrArray *args)
{
return filterx_string_new("test-builtin-functions", -1);
}
Expand Down Expand Up @@ -82,7 +82,7 @@ Test(builtin_functions, test_builtin_simple_functions_lookup)
cr_assert(func != NULL);

// check dummy function as result
FilterXObject *res = func(NULL);
FilterXObject *res = func(NULL, NULL);
cr_assert(res != NULL);
cr_assert(filterx_object_is_type(res, &FILTERX_TYPE_NAME(string)));
gsize len;
Expand Down
4 changes: 2 additions & 2 deletions lib/filterx/tests/test_expr_condition.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ Test(expr_condition, test_condition_do_not_allow_to_add_else_into_else, .signal=
}

FilterXObject *
_fail_func(GPtrArray *args)
_fail_func(FilterXExpr *s, GPtrArray *args)
{
return NULL;
}
Expand All @@ -365,7 +365,7 @@ Test(expr_condition, test_condition_return_null_on_illegal_expr)
}

FilterXObject *
_dummy_func(GPtrArray *args)
_dummy_func(FilterXExpr *s, GPtrArray *args)
{
return filterx_string_new("foobar", -1);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/tests/test_expr_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include "apphook.h"
#include "scratch-buffers.h"

FilterXObject *test_dummy_function(GPtrArray *args)
FilterXObject *test_dummy_function(FilterXExpr *s, GPtrArray *args)
{
GString *repr = scratch_buffers_alloc();
GString *out = scratch_buffers_alloc();
Expand Down
Loading

0 comments on commit 0ca56f0

Please sign in to comment.