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: add drop/done #269

Merged
merged 12 commits into from
Sep 9, 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
6 changes: 6 additions & 0 deletions lib/filterx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ set(FILTERX_HEADERS
filterx/expr-setattr.h
filterx/expr-set-subscript.h
filterx/expr-template.h
filterx/expr-drop.h
filterx/expr-done.h
filterx/filterx-config.h
filterx/filterx-eval.h
filterx/filterx-error.h
filterx/filterx-expr.h
filterx/filterx-globals.h
filterx/filterx-object.h
Expand Down Expand Up @@ -64,8 +67,11 @@ set(FILTERX_SOURCES
filterx/expr-setattr.c
filterx/expr-set-subscript.c
filterx/expr-template.c
filterx/expr-drop.c
filterx/expr-done.c
filterx/filterx-config.c
filterx/filterx-eval.c
filterx/filterx-error.c
filterx/filterx-expr.c
filterx/filterx-globals.c
filterx/filterx-object.c
Expand Down
6 changes: 6 additions & 0 deletions lib/filterx/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ filterxinclude_HEADERS = \
lib/filterx/expr-plus.h \
lib/filterx/expr-variable.h \
lib/filterx/expr-comparison.h \
lib/filterx/expr-drop.h \
lib/filterx/expr-done.h \
lib/filterx/filterx-object.h \
lib/filterx/filterx-weakrefs.h \
lib/filterx/object-primitive.h \
lib/filterx/filterx-scope.h \
lib/filterx/filterx-eval.h \
lib/filterx/filterx-error.h \
lib/filterx/object-extractor.h \
lib/filterx/object-json.h \
lib/filterx/object-json-internal.h \
Expand Down Expand Up @@ -68,11 +71,14 @@ filterx_sources = \
lib/filterx/expr-plus.c \
lib/filterx/expr-variable.c \
lib/filterx/expr-comparison.c \
lib/filterx/expr-drop.c \
lib/filterx/expr-done.c \
lib/filterx/filterx-object.c \
lib/filterx/filterx-weakrefs.c \
lib/filterx/object-primitive.c \
lib/filterx/filterx-scope.c \
lib/filterx/filterx-eval.c \
lib/filterx/filterx-error.c \
lib/filterx/object-extractor.c \
lib/filterx/object-json.c \
lib/filterx/object-json-object.c \
Expand Down
5 changes: 5 additions & 0 deletions lib/filterx/expr-compound.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ _eval_exprs(FilterXCompoundExpr *self, FilterXObject **result)
for (gint i = 0; i < self->exprs->len; i++)
{
filterx_object_unref(*result);
FilterXEvalContext *context = filterx_eval_get_context();

if (G_UNLIKELY(context->eval_control_modifier == FXC_DROP || context->eval_control_modifier == FXC_DONE))
/* code flow modifier detected, short circuiting */
OverOrion marked this conversation as resolved.
Show resolved Hide resolved
return TRUE;

FilterXExpr *expr = g_ptr_array_index(self->exprs, i);
if (!_eval_expr(expr, result))
Expand Down
47 changes: 47 additions & 0 deletions lib/filterx/expr-done.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2024 Axoflow
* Copyright (c) 2024 Szilard Parrag <[email protected]>
*
* 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/expr-done.h"
#include "filterx/filterx-eval.h"
#include "filterx/object-primitive.h"

static FilterXObject *
_eval(FilterXExpr *s)
{
FilterXEvalContext *context = filterx_eval_get_context();
context->eval_control_modifier = FXC_DONE;

return filterx_boolean_new(TRUE);
}

FilterXExpr *
filterx_expr_done(void)
{
FilterXExpr *self = g_new0(FilterXExpr, 1);
filterx_expr_init_instance(self);
self->eval = _eval;

return self;
}
32 changes: 32 additions & 0 deletions lib/filterx/expr-done.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2024 Axoflow
* Copyright (c) 2024 Szilard Parrag <[email protected]>
*
* 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_DONE_H_INCLUDED
#define FILTERX_FUNC_DONE_H_INCLUDED

#include "filterx/expr-function.h"

FilterXExpr *filterx_expr_done(void);

#endif
46 changes: 46 additions & 0 deletions lib/filterx/expr-drop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2024 Axoflow
* Copyright (c) 2024 Szilard Parrag <[email protected]>
*
* 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/expr-drop.h"
#include "filterx/filterx-eval.h"
#include "filterx/object-primitive.h"

static FilterXObject *
_eval(FilterXExpr *s)
{
FilterXEvalContext *context = filterx_eval_get_context();
context->eval_control_modifier = FXC_DROP;

return filterx_boolean_new(TRUE);
}

FilterXExpr *
filterx_expr_drop_msg(void)
{
FilterXExpr *self = g_new0(FilterXExpr, 1);
filterx_expr_init_instance(self);
self->eval = _eval;

return self;
}
31 changes: 31 additions & 0 deletions lib/filterx/expr-drop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2024 Axoflow
* Copyright (c) 2024 Szilard Parrag <[email protected]>
*
* 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_EXPR_DROP_H_INCLUDED
#define FILTERX_EXPR_DROP_H_INCLUDED
#include "filterx/expr-function.h"

FilterXExpr *filterx_expr_drop_msg(void);

#endif
87 changes: 87 additions & 0 deletions lib/filterx/filterx-error.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2023 Balazs Scheidler <[email protected]>
*
* 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/filterx-error.h"
#include "scratch-buffers.h"

void
filterx_error_clear(FilterXError *error)
{
filterx_object_unref(error->object);
if (error->free_info)
g_free(error->info);
memset(error, 0, sizeof(*error));
}

EVTTAG *
filterx_error_format(FilterXError *error)
{

if (!error->message)
return evt_tag_str("error", "Error information unset");

const gchar *extra_info = NULL;

if (error->info)
{
extra_info = error->info;
}
else if (error->object)
{
GString *buf = scratch_buffers_alloc();

if (!filterx_object_repr(error->object, buf))
{
LogMessageValueType t;
if (!filterx_object_marshal(error->object, buf, &t))
g_assert_not_reached();
}
extra_info = buf->str;
}

return evt_tag_printf("error", "%s%s%s",
error->message,
extra_info ? ": " : "",
extra_info ? : "");
}

EVTTAG *
filterx_error_format_location(FilterXError *error)
{
return filterx_expr_format_location_tag(error->expr);
}

void
filterx_error_set_values(FilterXError *error, const gchar *message, FilterXExpr *expr, FilterXObject *object)
{
error->message = message;
error->expr = expr;
error->object = filterx_object_ref(object);
}

void
filterx_error_set_info(FilterXError *error, gchar *info, gboolean free_info)
{
error->info = info;
error->free_info = free_info;
}
48 changes: 48 additions & 0 deletions lib/filterx/filterx-error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2023 Balazs Scheidler <[email protected]>
*
* 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_ERROR_H_INCLUDED
#define FILTERX_ERROR_H_INCLUDED

#include "filterx/filterx-scope.h"
#include "filterx/filterx-expr.h"
#include "template/eval.h"

typedef struct _FilterXError
{
const gchar *message;
FilterXExpr *expr;
FilterXObject *object;
gchar *info;
gboolean free_info;
} FilterXError;

void filterx_error_clear(FilterXError *error);
EVTTAG *filterx_error_format(FilterXError *error);
EVTTAG *filterx_error_format_location(FilterXError *error);
void filterx_error_set_info(FilterXError *error, gchar *info, gboolean free_info);
void filterx_error_set_values(FilterXError *error, const gchar *message, FilterXExpr *expr, FilterXObject *object);


#endif
Loading
Loading