Skip to content

Commit

Permalink
!refact ref istype
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAnno committed Oct 8, 2024
1 parent f99e81c commit 54aae75
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 25 deletions.
4 changes: 3 additions & 1 deletion lib/filterx/expr-assign.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
*/
#include "filterx/expr-assign.h"
#include "filterx/object-primitive.h"
#include "filterx/filterx-ref.h"
#include "scratch-buffers.h"

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

FilterXObject *value = filterx_expr_eval(self->rhs);
FilterXObject *v = filterx_expr_eval(self->rhs);
FilterXObject *value = filterx_ref_new(v);

if (!value)
return NULL;
Expand Down
3 changes: 3 additions & 0 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/filterx-ref.h"
#include "filterx/object-extractor.h"
#include "filterx/object-primitive.h"
#include "filterx/object-null.h"
Expand Down Expand Up @@ -140,6 +141,8 @@ _evaluate_as_num(FilterXObject *lhs, FilterXObject *rhs, gint operator)
static gboolean
_evaluate_type_aware(FilterXObject *lhs, FilterXObject *rhs, gint operator)
{
lhs = filterx_ref_get_readonly_value(lhs);

if (lhs->type == rhs->type &&
(filterx_object_is_type(lhs, &FILTERX_TYPE_NAME(string)) ||
filterx_object_is_type(lhs, &FILTERX_TYPE_NAME(bytes)) ||
Expand Down
3 changes: 2 additions & 1 deletion lib/filterx/expr-literal-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "filterx/expr-literal-generator.h"
#include "filterx/object-primitive.h"
#include "filterx/filterx-ref.h"

struct FilterXLiteralGeneratorElem_
{
Expand Down Expand Up @@ -83,7 +84,7 @@ _eval_elements(FilterXObject *fillable, GList *elements)
return FALSE;
}

FilterXObject *value = filterx_expr_eval(elem->value);
FilterXObject *value = filterx_ref_new(filterx_expr_eval(elem->value));
if (!value)
{
filterx_object_unref(key);
Expand Down
3 changes: 3 additions & 0 deletions lib/filterx/expr-plus-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "expr-plus.h"
#include "object-string.h"
#include "filterx-eval.h"
#include "filterx-ref.h"
#include "scratch-buffers.h"
#include "expr-generator.h"
#include "object-list-interface.h"
Expand All @@ -39,6 +40,8 @@ typedef struct FilterXOperatorPlusGenerator
static gboolean
_generate_obj(FilterXOperatorPlusGenerator *self, FilterXObject *obj, FilterXObject *fillable)
{
fillable = filterx_ref_get_writable_value(fillable);

if (filterx_object_is_type(fillable, &FILTERX_TYPE_NAME(list)))
return filterx_list_merge(fillable, obj);

Expand Down
3 changes: 3 additions & 0 deletions lib/filterx/expr-regexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "filterx/object-list-interface.h"
#include "filterx/object-dict-interface.h"
#include "filterx/expr-function.h"
#include "filterx/filterx-ref.h"
#include "compat/pcre.h"
#include "scratch-buffers.h"

Expand Down Expand Up @@ -268,6 +269,8 @@ _store_matches_to_dict(pcre2_code_8 *pattern, const FilterXReMatchState *state,
static gboolean
_store_matches(pcre2_code_8 *pattern, const FilterXReMatchState *state, FilterXObject *fillable)
{
fillable = filterx_ref_get_writable_value(fillable);

if (filterx_object_is_type(fillable, &FILTERX_TYPE_NAME(list)))
return _store_matches_to_list(pattern, state, fillable);

Expand Down
3 changes: 2 additions & 1 deletion lib/filterx/expr-set-subscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "filterx/expr-set-subscript.h"
#include "filterx/object-primitive.h"
#include "filterx/filterx-eval.h"
#include "filterx/filterx-ref.h"
#include "scratch-buffers.h"

typedef struct _FilterXSetSubscript
Expand Down Expand Up @@ -62,7 +63,7 @@ _eval(FilterXExpr *s)
goto exit;
}

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

Expand Down
3 changes: 2 additions & 1 deletion lib/filterx/expr-setattr.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "filterx/object-primitive.h"
#include "filterx/object-string.h"
#include "filterx/filterx-eval.h"
#include "filterx/filterx-ref.h"
#include "scratch-buffers.h"

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

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

Expand Down
8 changes: 5 additions & 3 deletions lib/filterx/filterx-metrics-labels.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "filterx-metrics-labels.h"
#include "filterx-eval.h"
#include "filterx-ref.h"
#include "expr-literal.h"
#include "expr-literal-generator.h"
#include "object-string.h"
Expand Down Expand Up @@ -226,14 +227,15 @@ _format_expr_to_cache(FilterXExpr *expr, DynMetricsStore *cache)

gboolean success = FALSE;

if (!filterx_object_is_type(obj, &FILTERX_TYPE_NAME(dict)))
FilterXObject *inner_obj = filterx_ref_get_readonly_value(obj);
if (!filterx_object_is_type(inner_obj, &FILTERX_TYPE_NAME(dict)))
{
filterx_eval_push_error_info("failed to format metrics labels, labels must be a dict", expr,
g_strdup_printf("got %s instead", obj->type->name), TRUE);
g_strdup_printf("got %s instead", inner_obj->type->name), TRUE);
goto exit;
}

success = filterx_dict_iter(obj, _format_dict_elem_to_cache, cache);
success = filterx_dict_iter(inner_obj, _format_dict_elem_to_cache, cache);
if (!success)
goto exit;

Expand Down
13 changes: 8 additions & 5 deletions lib/filterx/func-flatten.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "filterx/object-primitive.h"
#include "filterx/object-string.h"
#include "filterx/filterx-eval.h"
#include "filterx/filterx-ref.h"
#include "scratch-buffers.h"

#define FILTERX_FUNC_FLATTEN_USAGE "Usage: flatten(dict, separator=\".\")"
Expand Down Expand Up @@ -70,7 +71,8 @@ _collect_modifications_from_elem(FilterXObject *key, FilterXObject *value, gpoin
GString *key_buffer = ((gpointer *) user_data)[3];
gboolean is_top_level = (gboolean) GPOINTER_TO_INT(((gpointer *) user_data)[4]);

if (filterx_object_is_type(value, &FILTERX_TYPE_NAME(dict)))
FilterXObject *inner_value = filterx_ref_get_readonly_value(value);
if (filterx_object_is_type(inner_value, &FILTERX_TYPE_NAME(dict)))
{
if (is_top_level)
*top_level_dict_keys = g_list_prepend(*top_level_dict_keys, filterx_object_ref(key));
Expand All @@ -84,7 +86,7 @@ _collect_modifications_from_elem(FilterXObject *key, FilterXObject *value, gpoin
g_string_append(key_buffer, self->separator);

gpointer inner_user_data[] = { self, flattened_kvs, NULL, key_buffer, GINT_TO_POINTER(FALSE)};
gboolean result = filterx_dict_iter(value, _collect_modifications_from_elem, inner_user_data);
gboolean result = filterx_dict_iter(inner_value, _collect_modifications_from_elem, inner_user_data);

g_string_truncate(key_buffer, orig_len);
return result;
Expand Down Expand Up @@ -190,14 +192,15 @@ _eval(FilterXExpr *s)

gboolean result = FALSE;

if (!filterx_object_is_type(dict, &FILTERX_TYPE_NAME(dict)))
FilterXObject *inner_dict = filterx_ref_get_writable_value(dict);
if (!filterx_object_is_type(inner_dict, &FILTERX_TYPE_NAME(dict)))
{
filterx_eval_push_error_info("object must be a dict", self->dict_expr,
g_strdup_printf("got %s instead", dict->type->name), TRUE);
g_strdup_printf("got %s instead", inner_dict->type->name), TRUE);
goto exit;
}

result = _flatten(self, dict);
result = _flatten(self, inner_dict);

exit:
filterx_object_unref(dict);
Expand Down
26 changes: 19 additions & 7 deletions lib/filterx/func-unset-empties.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "filterx/object-list-interface.h"
#include "filterx/filterx-eval.h"
#include "filterx/filterx-globals.h"
#include "filterx/filterx-ref.h"
#include "filterx/func-flags.h"
#include "filterx/expr-literal-generator.h"
#include "filterx/expr-literal.h"
Expand Down Expand Up @@ -104,6 +105,8 @@ static gboolean _should_unset_string(FilterXFunctionUnsetEmpties *self, FilterXO
static gboolean
_should_unset(FilterXFunctionUnsetEmpties *self, FilterXObject *obj)
{
obj = filterx_ref_get_readonly_value(obj);

if (check_flag(self->flags, FILTERX_FUNC_UNSET_EMPTIES_FLAG_REPLACE_NULL) &&
filterx_object_is_type(obj, &FILTERX_TYPE_NAME(null)))
return TRUE;
Expand All @@ -129,6 +132,8 @@ _add_key_to_unset_list_if_needed(FilterXObject *key, FilterXObject *value, gpoin

if (check_flag(self->flags, FILTERX_FUNC_UNSET_EMPTIES_FLAG_RECURSIVE))
{
value = filterx_ref_get_readonly_value(value);

if (filterx_object_is_type(value, &FILTERX_TYPE_NAME(dict)) && !_process_dict(self, value))
return FALSE;
if (filterx_object_is_type(value, &FILTERX_TYPE_NAME(list)) && !_process_list(self, value))
Expand Down Expand Up @@ -189,14 +194,16 @@ _process_list(FilterXFunctionUnsetEmpties *self, FilterXObject *obj)
{
FilterXObject *elem = filterx_list_get_subscript(obj, i);

FilterXObject *inner_elem = filterx_ref_get_readonly_value(elem);

if (check_flag(self->flags, FILTERX_FUNC_UNSET_EMPTIES_FLAG_RECURSIVE))
{
if (filterx_object_is_type(elem, &FILTERX_TYPE_NAME(dict)) && !_process_dict(self, elem))
if (filterx_object_is_type(inner_elem, &FILTERX_TYPE_NAME(dict)) && !_process_dict(self, inner_elem))
{
filterx_object_unref(elem);
return FALSE;
}
if (filterx_object_is_type(elem, &FILTERX_TYPE_NAME(list)) && !_process_list(self, elem))
if (filterx_object_is_type(inner_elem, &FILTERX_TYPE_NAME(list)) && !_process_list(self, inner_elem))
{
filterx_object_unref(elem);
return FALSE;
Expand Down Expand Up @@ -247,13 +254,15 @@ _eval(FilterXExpr *s)
return NULL;
}

if (filterx_object_is_type(obj, &FILTERX_TYPE_NAME(dict)))
return _eval_on_dict(self, obj);
FilterXObject *inner_obj = filterx_ref_get_writable_value(obj);

if (filterx_object_is_type(obj, &FILTERX_TYPE_NAME(list)))
return _eval_on_list(self, obj);
if (filterx_object_is_type(inner_obj, &FILTERX_TYPE_NAME(dict)))
return _eval_on_dict(self, inner_obj);

filterx_eval_push_error("Object must be dict or list. " FILTERX_FUNC_UNSET_EMPTIES_USAGE, s, obj);
if (filterx_object_is_type(inner_obj, &FILTERX_TYPE_NAME(list)))
return _eval_on_list(self, inner_obj);

filterx_eval_push_error("Object must be dict or list. " FILTERX_FUNC_UNSET_EMPTIES_USAGE, s, inner_obj);
filterx_object_unref(obj);
return NULL;
}
Expand Down Expand Up @@ -350,6 +359,9 @@ _handle_target_object(FilterXFunctionUnsetEmpties *self, FilterXObject *target,
{
g_assert(target);
guint64 len;

target = filterx_ref_get_readonly_value(target);

if (filterx_object_is_type(target, &FILTERX_TYPE_NAME(null)))
set_flag(&self->flags, FILTERX_FUNC_UNSET_EMPTIES_FLAG_REPLACE_NULL, TRUE);
else if (filterx_object_is_type(target, &FILTERX_TYPE_NAME(list)))
Expand Down
5 changes: 5 additions & 0 deletions lib/filterx/object-dict-interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "filterx/object-extractor.h"
#include "filterx/object-string.h"
#include "filterx/object-json.h"
#include "filterx/filterx-ref.h"
#include "str-utils.h"

gboolean
Expand Down Expand Up @@ -53,6 +54,7 @@ filterx_dict_merge(FilterXObject *s, FilterXObject *other)
{
FilterXDict *self = (FilterXDict *) s;

other = filterx_ref_get_readonly_value(other);
g_assert(filterx_object_is_type(other, &FILTERX_TYPE_NAME(dict)));
return filterx_dict_iter(other, _add_elem_to_dict, self);
}
Expand Down Expand Up @@ -214,6 +216,9 @@ filterx_dict_init_instance(FilterXDict *self, FilterXType *type)
static FilterXObject *
_add(FilterXObject *lhs_object, FilterXObject *rhs_object)
{
lhs_object = filterx_ref_get_readonly_value(lhs_object);
rhs_object = filterx_ref_get_readonly_value(rhs_object);

if (!filterx_object_is_type(lhs_object, &FILTERX_TYPE_NAME(dict)) ||
!filterx_object_is_type(rhs_object, &FILTERX_TYPE_NAME(dict)))
return NULL;
Expand Down
6 changes: 6 additions & 0 deletions lib/filterx/object-json-array.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "filterx/object-list-interface.h"
#include "filterx/expr-function.h"
#include "filterx/filterx-eval.h"
#include "filterx/filterx-ref.h"

#include "logmsg/type-hinting.h"
#include "str-repr/encode.h"
Expand Down Expand Up @@ -328,6 +329,7 @@ filterx_json_array_new_from_args(FilterXExpr *s, GPtrArray *args)

FilterXObject *arg = (FilterXObject *) g_ptr_array_index(args, 0);

arg = filterx_ref_get_readonly_value(arg);
if (filterx_object_is_type(arg, &FILTERX_TYPE_NAME(json_array)))
return filterx_object_ref(arg);

Expand All @@ -354,6 +356,8 @@ filterx_json_array_new_empty(void)
const gchar *
filterx_json_array_to_json_literal(FilterXObject *s)
{
s = filterx_ref_get_readonly_value(s);

FilterXJsonArray *self = (FilterXJsonArray *) s;

if (!filterx_object_is_type(s, &FILTERX_TYPE_NAME(json_array)))
Expand All @@ -365,6 +369,8 @@ filterx_json_array_to_json_literal(FilterXObject *s)
struct json_object *
filterx_json_array_get_value(FilterXObject *s)
{
s = filterx_ref_get_readonly_value(s);

if (!filterx_object_is_type(s, &FILTERX_TYPE_NAME(json_array)))
return NULL;

Expand Down
5 changes: 5 additions & 0 deletions lib/filterx/object-json-object.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "filterx/object-primitive.h"
#include "filterx/object-string.h"
#include "filterx/filterx-weakrefs.h"
#include "filterx/filterx-ref.h"
#include "filterx/object-dict-interface.h"
#include "syslog-ng.h"
#include "str-utils.h"
Expand Down Expand Up @@ -293,6 +294,8 @@ filterx_json_object_new_empty(void)
const gchar *
filterx_json_object_to_json_literal(FilterXObject *s)
{
s = filterx_ref_get_readonly_value(s);

FilterXJsonObject *self = (FilterXJsonObject *) s;

if (!filterx_object_is_type(s, &FILTERX_TYPE_NAME(json_object)))
Expand All @@ -304,6 +307,8 @@ filterx_json_object_to_json_literal(FilterXObject *s)
struct json_object *
filterx_json_object_get_value(FilterXObject *s)
{
s = filterx_ref_get_readonly_value(s);

if (!filterx_object_is_type(s, &FILTERX_TYPE_NAME(json_object)))
return NULL;

Expand Down
5 changes: 5 additions & 0 deletions lib/filterx/object-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "filterx/object-list-interface.h"
#include "filterx/object-message-value.h"
#include "filterx/filterx-eval.h"
#include "filterx/filterx-ref.h"

#include "scanner/list-scanner/list-scanner.h"
#include "str-repr/encode.h"
Expand Down Expand Up @@ -197,6 +198,8 @@ filterx_json_new_from_args(FilterXExpr *s, GPtrArray *args)

FilterXObject *arg = (FilterXObject *) g_ptr_array_index(args, 0);

arg = filterx_ref_get_readonly_value(arg);

if (filterx_object_is_type(arg, &FILTERX_TYPE_NAME(json_array)) ||
filterx_object_is_type(arg, &FILTERX_TYPE_NAME(json_object)))
return filterx_object_ref(arg);
Expand Down Expand Up @@ -252,6 +255,8 @@ filterx_json_new_from_object(struct json_object *jso)
const gchar *
filterx_json_to_json_literal(FilterXObject *s)
{
s = filterx_ref_get_readonly_value(s);

if (filterx_object_is_type(s, &FILTERX_TYPE_NAME(json_object)))
return filterx_json_object_to_json_literal(s);
if (filterx_object_is_type(s, &FILTERX_TYPE_NAME(json_array)))
Expand Down
Loading

0 comments on commit 54aae75

Please sign in to comment.