Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
Signed-off-by: Attila Szakacs <[email protected]>
  • Loading branch information
alltilla committed Jul 30, 2024
1 parent 9a9615d commit 60d7633
Show file tree
Hide file tree
Showing 38 changed files with 860 additions and 513 deletions.
2 changes: 2 additions & 0 deletions lib/filterx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(FILTERX_HEADERS
filterx/filterx-pipe.h
filterx/filterx-scope.h
filterx/filterx-weakrefs.h
filterx/object-extractor.h
filterx/object-datetime.h
filterx/object-json.h
filterx/object-json-internal.h
Expand Down Expand Up @@ -67,6 +68,7 @@ set(FILTERX_SOURCES
filterx/filterx-pipe.c
filterx/filterx-scope.c
filterx/filterx-weakrefs.c
filterx/object-extractor.c
filterx/object-datetime.c
filterx/object-json.c
filterx/object-json-object.c
Expand Down
2 changes: 2 additions & 0 deletions lib/filterx/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ filterxinclude_HEADERS = \
lib/filterx/object-primitive.h \
lib/filterx/filterx-scope.h \
lib/filterx/filterx-eval.h \
lib/filterx/object-extractor.h \
lib/filterx/object-json.h \
lib/filterx/object-json-internal.h \
lib/filterx/object-string.h \
Expand Down Expand Up @@ -67,6 +68,7 @@ filterx_sources = \
lib/filterx/object-primitive.c \
lib/filterx/filterx-scope.c \
lib/filterx/filterx-eval.c \
lib/filterx/object-extractor.c \
lib/filterx/object-json.c \
lib/filterx/object-json-object.c \
lib/filterx/object-json-array.c \
Expand Down
60 changes: 31 additions & 29 deletions lib/filterx/expr-comparison.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "filterx/expr-comparison.h"
#include "filterx/object-datetime.h"
#include "filterx/filterx-globals.h"
#include "filterx/object-extractor.h"
#include "filterx/object-primitive.h"
#include "filterx/object-null.h"
#include "filterx/object-string.h"
Expand All @@ -44,43 +45,43 @@ typedef struct _FilterXComparison
static void
_convert_filterx_object_to_generic_number(FilterXObject *obj, GenericNumber *gn)
{
if (filterx_object_is_type(obj, &FILTERX_TYPE_NAME(integer)) ||
filterx_object_is_type(obj, &FILTERX_TYPE_NAME(double)) ||
filterx_object_is_type(obj, &FILTERX_TYPE_NAME(boolean)))
*gn = filterx_primitive_get_value(obj);
else if (filterx_object_is_type(obj, &FILTERX_TYPE_NAME(string)))
if (filterx_object_extract_generic_number(obj, gn))
return;

const gchar *str;
if (filterx_object_extract_string(obj, &str, NULL))
{
if (!parse_generic_number(filterx_string_get_value(obj, NULL), gn))
if (!parse_generic_number(str, gn))
gn_set_nan(gn);
return;
}
else if (filterx_object_is_type(obj, &FILTERX_TYPE_NAME(null)))
gn_set_int64(gn, 0);
else if (filterx_object_is_type(obj, &FILTERX_TYPE_NAME(datetime)))

if (filterx_object_extract_null(obj))
{
const UnixTime utime = filterx_datetime_get_value(obj);
uint64_t unix_epoch = unix_time_to_unix_epoch(utime);
gn_set_int64(gn, (uint64_t)unix_epoch);
gn_set_int64(gn, 0);
return;
}
else

UnixTime utime;
if (filterx_object_extract_datetime(obj, &utime))
{
gn_set_nan(gn);
uint64_t unix_epoch = unix_time_to_unix_epoch(utime);
gn_set_int64(gn, (uint64_t) unix_epoch);
return;
}

gn_set_nan(gn);
}

static const gchar *
_convert_filterx_object_to_string(FilterXObject *obj, gsize *len)
{
if (filterx_object_is_type(obj, &FILTERX_TYPE_NAME(string)))
{
return filterx_string_get_value(obj, len);
}
else if (filterx_object_is_type(obj, &FILTERX_TYPE_NAME(bytes)))
const gchar *str;
if (filterx_object_extract_string(obj, &str, len) ||
filterx_object_extract_bytes(obj, &str, len) ||
filterx_object_extract_protobuf(obj, &str, len))
{
return filterx_bytes_get_value(obj, len);
}
else if (filterx_object_is_type(obj, &FILTERX_TYPE_NAME(protobuf)))
{
return filterx_protobuf_get_value(obj, len);
return str;
}

GString *buffer = scratch_buffers_alloc();
Expand Down Expand Up @@ -183,20 +184,21 @@ _eval(FilterXExpr *s)
{
FilterXComparison *self = (FilterXComparison *) s;

FilterXObject *lhs_object = filterx_expr_eval_typed(self->super.lhs);
gint compare_mode = self->operator & FCMPX_MODE_MASK;
gint operator = self->operator & FCMPX_OP_MASK;
gboolean typed_eval_needed = compare_mode & FCMPX_TYPE_AWARE || compare_mode & FCMPX_TYPE_AND_VALUE_BASED;

FilterXObject *lhs_object = typed_eval_needed ? filterx_expr_eval_typed(self->super.lhs) : filterx_expr_eval(self->super.lhs);
if (!lhs_object)
return NULL;

FilterXObject *rhs_object = filterx_expr_eval_typed(self->super.rhs);
FilterXObject *rhs_object = typed_eval_needed ? filterx_expr_eval_typed(self->super.rhs) : filterx_expr_eval(self->super.rhs);
if (!rhs_object)
{
filterx_object_unref(lhs_object);
return NULL;
}

gint compare_mode = self->operator & FCMPX_MODE_MASK;
gint operator = self->operator & FCMPX_OP_MASK;

gboolean result = TRUE;
if (compare_mode & FCMPX_TYPE_AWARE)
result = _evaluate_type_aware(lhs_object, rhs_object, operator);
Expand Down
1 change: 1 addition & 0 deletions lib/filterx/expr-function.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ _get_literal_string_from_expr(FilterXExpr *expr, gsize *len)
if (!obj)
goto error;

/* Literal message values don't exist, so we don't need to use the extractor. */
str = filterx_string_get_value(obj, len);

/*
Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/expr-get-subscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ _eval(FilterXExpr *s)
if (!variable)
return NULL;

FilterXObject *key = filterx_expr_eval_typed(self->key);
FilterXObject *key = filterx_expr_eval(self->key);
if (!key)
goto exit;
result = filterx_object_get_subscript(variable, key);
Expand Down
4 changes: 2 additions & 2 deletions lib/filterx/expr-literal-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ _eval_elements(FilterXObject *fillable, GList *elements)
FilterXObject *key = NULL;
if (elem->key)
{
key = filterx_expr_eval_typed(elem->key);
key = filterx_expr_eval(elem->key);
if (!key)
return FALSE;
}

FilterXObject *value = filterx_expr_eval_typed(elem->value);
FilterXObject *value = filterx_expr_eval(elem->value);
if (!value)
{
filterx_object_unref(key);
Expand Down
4 changes: 2 additions & 2 deletions lib/filterx/expr-plus.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ _eval(FilterXExpr *s)
{
FilterXOperatorPlus *self = (FilterXOperatorPlus *) s;

FilterXObject *lhs_object = filterx_expr_eval_typed(self->super.lhs);
FilterXObject *lhs_object = filterx_expr_eval(self->super.lhs);
if (!lhs_object)
return NULL;

FilterXObject *rhs_object = filterx_expr_eval_typed(self->super.rhs);
FilterXObject *rhs_object = filterx_expr_eval(self->super.rhs);
if (!rhs_object)
{
filterx_object_unref(lhs_object);
Expand Down
20 changes: 3 additions & 17 deletions lib/filterx/expr-regexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
*/

#include "filterx/expr-regexp.h"
#include "filterx/object-primitive.h"
#include "filterx/object-extractor.h"
#include "filterx/object-string.h"
#include "filterx/object-message-value.h"
#include "filterx/object-primitive.h"
#include "filterx/object-list-interface.h"
#include "filterx/object-dict-interface.h"
#include "compat/pcre.h"
Expand Down Expand Up @@ -142,21 +142,7 @@ _match(FilterXExpr *lhs_expr, pcre2_code_8 *pattern, FilterXReMatchState *state)
if (!state->lhs_obj)
goto error;

if (filterx_object_is_type(state->lhs_obj, &FILTERX_TYPE_NAME(message_value)))
{
if (filterx_message_value_get_type(state->lhs_obj) != LM_VT_STRING)
{
msg_error("FilterX: Regexp matching left hand side must be string type",
evt_tag_str("type", state->lhs_obj->type->name));
goto error;
}
state->lhs_str = filterx_message_value_get_value(state->lhs_obj, &state->lhs_str_len);
}
else if (filterx_object_is_type(state->lhs_obj, &FILTERX_TYPE_NAME(string)))
{
state->lhs_str = filterx_string_get_value(state->lhs_obj, &state->lhs_str_len);
}
else
if (!filterx_object_extract_string(state->lhs_obj, &state->lhs_str, &state->lhs_str_len))
{
msg_error("FilterX: Regexp matching left hand side must be string type",
evt_tag_str("type", state->lhs_obj->type->name));
Expand Down
4 changes: 2 additions & 2 deletions lib/filterx/expr-set-subscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ _eval(FilterXExpr *s)

if (self->key)
{
key = filterx_expr_eval_typed(self->key);
key = filterx_expr_eval(self->key);
if (!key)
goto exit;
}
Expand All @@ -62,7 +62,7 @@ _eval(FilterXExpr *s)
goto exit;
}

new_value = filterx_expr_eval_typed(self->new_value);
new_value = filterx_expr_eval(self->new_value);
if (!new_value)
goto exit;

Expand Down
4 changes: 2 additions & 2 deletions lib/filterx/expr-setattr.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ _eval(FilterXExpr *s)
FilterXSetAttr *self = (FilterXSetAttr *) s;
FilterXObject *result = NULL;

FilterXObject *object = filterx_expr_eval_typed(self->object);
FilterXObject *object = filterx_expr_eval(self->object);
if (!object)
return NULL;

Expand All @@ -50,7 +50,7 @@ _eval(FilterXExpr *s)
goto exit;
}

FilterXObject *new_value = filterx_expr_eval_typed(self->new_value);
FilterXObject *new_value = filterx_expr_eval(self->new_value);
if (!new_value)
goto exit;

Expand Down
6 changes: 4 additions & 2 deletions lib/filterx/func-str-transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

#include "filterx/func-str-transform.h"
#include "filterx/object-extractor.h"
#include "filterx/object-string.h"
#include "filterx/filterx-eval.h"

Expand All @@ -35,10 +36,11 @@ _extract_str_arg(FilterXExpr *s, GPtrArray *args, gssize *len)
return NULL;
}

const gchar *str;
gsize inner_len;
FilterXObject *object = g_ptr_array_index(args, 0);
const gchar *str = filterx_string_get_value(object, &inner_len);
if (!str)

if (!filterx_object_extract_string(object, &str, &inner_len))
{
filterx_simple_function_argument_error(s, "Object must be string", FALSE);
return NULL;
Expand Down
13 changes: 7 additions & 6 deletions lib/filterx/func-unset-empties.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

#include "filterx/func-unset-empties.h"
#include "filterx/object-extractor.h"
#include "filterx/object-string.h"
#include "filterx/object-primitive.h"
#include "filterx/object-null.h"
Expand All @@ -45,13 +46,13 @@ static gboolean _process_list(FilterXFunctionUnsetEmpties *self, FilterXObject *
static gboolean
_should_unset(FilterXFunctionUnsetEmpties *self, FilterXObject *obj)
{
if (filterx_object_is_type(obj, &FILTERX_TYPE_NAME(string)))
gsize str_len;
const gchar *str;
if (filterx_object_extract_string(obj, &str, &str_len))
{
gsize len;
const gchar *value = filterx_string_get_value(obj, &len);
return len == 0 ||
strcasecmp(value, "n/a") == 0 ||
strcmp(value, "-") == 0;
return str_len == 0 ||
strcasecmp(str, "n/a") == 0 ||
strcmp(str, "-") == 0;
}

if (filterx_object_is_type(obj, &FILTERX_TYPE_NAME(null)))
Expand Down
Loading

0 comments on commit 60d7633

Please sign in to comment.