-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from bshifter/filterx-null-coalesce
Filterx null coalesce operator
- Loading branch information
Showing
16 changed files
with
449 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2024 Axoflow | ||
* Copyright (c) 2024 shifter <shifter@axoflow.com> | ||
* | ||
* 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-null-coalesce.h" | ||
#include "filterx/object-null.h" | ||
#include "filterx/filterx-eval.h" | ||
#include "filterx/object-message-value.h" | ||
|
||
typedef struct _FilterXNullCoalesce FilterXNullCoalesce; | ||
|
||
struct _FilterXNullCoalesce | ||
{ | ||
FilterXBinaryOp super; | ||
}; | ||
|
||
static FilterXObject * | ||
_eval(FilterXExpr *s) | ||
{ | ||
FilterXNullCoalesce *self = (FilterXNullCoalesce *) s; | ||
|
||
FilterXObject *lhs_object = filterx_expr_eval(self->super.lhs); | ||
if (!lhs_object || filterx_object_is_type(lhs_object, &FILTERX_TYPE_NAME(null)) | ||
|| (filterx_object_is_type(lhs_object, &FILTERX_TYPE_NAME(message_value)) | ||
&& filterx_message_value_get_type(lhs_object) == LM_VT_NULL)) | ||
{ | ||
if (!lhs_object) | ||
{ | ||
msg_debug("FILTERX null coalesce supressing error:", | ||
filterx_format_last_error()); | ||
filterx_eval_clear_errors(); | ||
} | ||
FilterXObject *rhs_object = filterx_expr_eval(self->super.rhs); | ||
filterx_object_unref(lhs_object); | ||
return rhs_object; | ||
} | ||
return lhs_object; | ||
} | ||
|
||
FilterXExpr * | ||
filterx_null_coalesce_new(FilterXExpr *lhs, FilterXExpr *rhs) | ||
{ | ||
FilterXNullCoalesce *self = g_new0(FilterXNullCoalesce, 1); | ||
filterx_binary_op_init_instance(&self->super, lhs, rhs); | ||
self->super.super.eval = _eval; | ||
return &self->super.super; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2024 Axoflow | ||
* Copyright (c) 2024 shifter <shifter@axoflow.com> | ||
* | ||
* 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_NULL_COALESCE_H_INCLUDED | ||
#define FILTERX_NULL_COALESCE_H_INCLUDED | ||
|
||
#include "filterx/filterx-expr.h" | ||
|
||
FilterXExpr *filterx_null_coalesce_new(FilterXExpr *lhs, FilterXExpr *rhs); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* Copyright (c) 2024 Axoflow | ||
* Copyright (c) 2024 shifter <shifter@axoflow.com> | ||
* | ||
* 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 <criterion/criterion.h> | ||
#include "libtest/cr_template.h" | ||
#include "libtest/filterx-lib.h" | ||
|
||
#include "filterx/filterx-object.h" | ||
#include "filterx/object-primitive.h" | ||
#include "filterx/expr-comparison.h" | ||
#include "filterx/filterx-expr.h" | ||
#include "filterx/expr-literal.h" | ||
#include "filterx/object-string.h" | ||
#include "filterx/object-null.h" | ||
#include "filterx/object-datetime.h" | ||
#include "filterx/object-message-value.h" | ||
#include "filterx/expr-null-coalesce.h" | ||
#include "filterx/func-istype.h" | ||
#include "filterx/filterx-eval.h" | ||
#include "filterx/func-len.h" | ||
#include "filterx/expr-function.h" | ||
|
||
#include "apphook.h" | ||
#include "scratch-buffers.h" | ||
|
||
Test(expr_null_coalesce, test_coalescing_soft_null) | ||
{ | ||
FilterXExpr *coalesce = filterx_null_coalesce_new(filterx_literal_new(filterx_null_new()), | ||
filterx_non_literal_new(filterx_test_unknown_object_new())); | ||
cr_assert(coalesce); | ||
FilterXObject *res = filterx_expr_eval(coalesce); | ||
cr_assert(res); | ||
cr_assert(filterx_object_is_type(res, &FILTERX_TYPE_NAME(test_unknown_object))); | ||
filterx_expr_unref(coalesce); | ||
filterx_object_unref(res); | ||
} | ||
|
||
Test(expr_null_coalesce, test_coalescing_supressing_lhs_eval_error) | ||
{ | ||
FilterXExpr *err_expr = filterx_dummy_error_new("lhs error"); | ||
cr_assert_not_null(err_expr); | ||
|
||
// passing errorous expression as lhs | ||
FilterXExpr *coalesce = filterx_null_coalesce_new(err_expr, filterx_non_literal_new(filterx_test_unknown_object_new())); | ||
cr_assert(coalesce); | ||
|
||
// eval returns rhs value, since lhs fails during eval | ||
FilterXObject *res = filterx_expr_eval(coalesce); | ||
cr_assert(res); | ||
cr_assert(filterx_object_is_type(res, &FILTERX_TYPE_NAME(test_unknown_object))); | ||
|
||
// lhs expr eval errors must supressed by null_coalesce | ||
const gchar *last_error = filterx_eval_get_last_error(); | ||
cr_assert_null(last_error); | ||
|
||
filterx_expr_unref(coalesce); | ||
filterx_object_unref(res); | ||
} | ||
|
||
Test(expr_null_coalesce, test_coalescing_keep_rhs_eval_error) | ||
{ | ||
const gchar *error_msg = "rhs error"; | ||
FilterXExpr *err_expr = filterx_dummy_error_new(error_msg); | ||
|
||
// passing errorous expression as rhs | ||
FilterXExpr *coalesce = filterx_null_coalesce_new(filterx_non_literal_new(filterx_null_new()), | ||
err_expr); | ||
cr_assert(coalesce); | ||
|
||
// null_coalesce returns null, since lhs is null and rhs fails | ||
FilterXObject *res = filterx_expr_eval(coalesce); | ||
cr_assert_null(res); | ||
|
||
// rhs expr eval errors must remain intact | ||
const gchar *last_error = filterx_eval_get_last_error(); | ||
cr_assert_not_null(last_error); | ||
cr_assert_str_eq(error_msg, last_error); | ||
|
||
filterx_expr_unref(coalesce); | ||
filterx_object_unref(res); | ||
} | ||
|
||
Test(expr_null_coalesce, test_coalescing_keep_rhs_eval_error_on_double_fail) | ||
{ | ||
FilterXExpr *err_expr_lhs = filterx_dummy_error_new("lhs error"); | ||
|
||
const gchar *error_msg = "rhs error"; | ||
FilterXExpr *err_expr_rhs = filterx_dummy_error_new(error_msg); | ||
|
||
// passing errorous expressions | ||
FilterXExpr *coalesce = filterx_null_coalesce_new(err_expr_lhs, err_expr_rhs); | ||
cr_assert(coalesce); | ||
|
||
// null_coalesce returns null, since both lhs and rhs fails | ||
FilterXObject *res = filterx_expr_eval(coalesce); | ||
cr_assert_null(res); | ||
|
||
// rhs expr eval errors must remain intact | ||
const gchar *last_error = filterx_eval_get_last_error(); | ||
cr_assert_not_null(last_error); | ||
cr_assert_str_eq(error_msg, last_error); | ||
|
||
filterx_expr_unref(coalesce); | ||
filterx_object_unref(res); | ||
} | ||
|
||
static void | ||
setup(void) | ||
{ | ||
app_startup(); | ||
init_libtest_filterx(); | ||
} | ||
|
||
static void | ||
teardown(void) | ||
{ | ||
scratch_buffers_explicit_gc(); | ||
deinit_libtest_filterx(); | ||
app_shutdown(); | ||
} | ||
|
||
TestSuite(expr_null_coalesce, .init = setup, .fini = teardown); |
Oops, something went wrong.