Skip to content

Commit

Permalink
Merge pull request #286 from MrAnno/filterx-func-convert-to-gen
Browse files Browse the repository at this point in the history
filterx: refactors/fixes and convert funcs to generators
  • Loading branch information
alltilla authored Sep 17, 2024
2 parents 17d3599 + 087509b commit 7296984
Show file tree
Hide file tree
Showing 51 changed files with 425 additions and 377 deletions.
8 changes: 6 additions & 2 deletions lib/filterx/expr-function.c
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ _lookup_function(GlobalConfig *cfg, const gchar *function_name, FilterXFunctionA
if (!ctor)
return NULL;

return ctor(function_name, args, error);
return ctor(args, error);
}

/* NOTE: takes the reference of "args_list" */
Expand Down Expand Up @@ -621,7 +621,11 @@ _lookup_generator_function(GlobalConfig *cfg, const gchar *function_name, Filter

if (!ctor)
return NULL;
return ctor(function_name, args, error);

FilterXExpr *func = ctor(args, error);
g_assert(!func || filterx_expr_is_generator(func));

return func;
}

/* NOTE: takes the references of objects passed in "arguments" */
Expand Down
68 changes: 19 additions & 49 deletions lib/filterx/expr-function.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ typedef struct _FilterXFunctionArg
gboolean retrieved;
} FilterXFunctionArg;

typedef FilterXExpr *(*FilterXFunctionCtor)(const gchar *, FilterXFunctionArgs *, GError **);
typedef FilterXExpr *(*FilterXFunctionCtor)(FilterXFunctionArgs *, GError **);

#define FILTERX_FUNCTION_ERROR filterx_function_error_quark()
GQuark filterx_function_error_quark(void);
Expand Down Expand Up @@ -101,68 +101,38 @@ FilterXExpr *filterx_generator_function_lookup(GlobalConfig *cfg, const gchar *f
GError **error);


#define FILTERX_SIMPLE_FUNCTION_PROTOTYPE(func_name) \
gpointer \
filterx_ ## func_name ## _construct(Plugin *self)

#define FILTERX_SIMPLE_FUNCTION_DECLARE(func_name) \
FILTERX_SIMPLE_FUNCTION_PROTOTYPE(func_name);

/* helper macros for template function plugins */
#define FILTERX_SIMPLE_FUNCTION(func_name, call) \
FILTERX_SIMPLE_FUNCTION_PROTOTYPE(func_name) \
{ \
FilterXSimpleFunctionProto f = call; \
return (gpointer) f; \
}

#define FILTERX_SIMPLE_FUNCTION_PLUGIN(func_name) \
{ \
.type = LL_CONTEXT_FILTERX_SIMPLE_FUNC, \
.name = # func_name, \
.construct = filterx_ ## func_name ## _construct, \
#define _FILTERX_FUNCTION_PLUGIN(func_name, func_type) \
{ \
.type = func_type, \
.name = # func_name, \
.construct = filterx_function_ ## func_name ## _construct, \
}

#define FILTERX_FUNCTION_PROTOTYPE(func_name) \
gpointer \
filterx_function_ ## func_name ## _construct(Plugin *self)

#define FILTERX_FUNCTION_DECLARE(func_name) \
FILTERX_FUNCTION_PROTOTYPE(func_name);

#define FILTERX_FUNCTION_DECLARE(func_name) FILTERX_FUNCTION_PROTOTYPE(func_name);
#define FILTERX_FUNCTION(func_name, ctor) \
FILTERX_FUNCTION_PROTOTYPE(func_name) \
{ \
FilterXFunctionCtor f = ctor; \
return (gpointer) f; \
}
#define FILTERX_FUNCTION_PLUGIN(func_name) _FILTERX_FUNCTION_PLUGIN(func_name, LL_CONTEXT_FILTERX_FUNC)

#define FILTERX_FUNCTION_PLUGIN(func_name) \
{ \
.type = LL_CONTEXT_FILTERX_FUNC, \
.name = # func_name, \
.construct = filterx_function_ ## func_name ## _construct, \
}

#define FILTERX_GENERATOR_FUNCTION_PROTOTYPE(func_name) \
gpointer \
filterx_generator_function_ ## func_name ## _construct(Plugin *self)

#define FILTERX_GENERATOR_FUNCTION_DECLARE(func_name) \
FILTERX_GENERATOR_FUNCTION_PROTOTYPE(func_name);

#define FILTERX_GENERATOR_FUNCTION(func_name, ctor) \
FILTERX_GENERATOR_FUNCTION_PROTOTYPE(func_name) \
{ \
FilterXFunctionCtor f = ctor; \
return (gpointer) f; \
#define FILTERX_SIMPLE_FUNCTION_DECLARE(func_name) FILTERX_FUNCTION_DECLARE(func_name)
#define FILTERX_SIMPLE_FUNCTION(func_name, call) \
FILTERX_FUNCTION_PROTOTYPE(func_name) \
{ \
FilterXSimpleFunctionProto f = call; \
return (gpointer) f; \
}
#define FILTERX_SIMPLE_FUNCTION_PLUGIN(func_name) _FILTERX_FUNCTION_PLUGIN(func_name, LL_CONTEXT_FILTERX_SIMPLE_FUNC)

#define FILTERX_GENERATOR_FUNCTION_PLUGIN(func_name) \
{ \
.type = LL_CONTEXT_FILTERX_GEN_FUNC, \
.name = # func_name, \
.construct = filterx_generator_function_ ## func_name ## _construct, \
}

#define FILTERX_GENERATOR_FUNCTION_DECLARE(func_name) FILTERX_FUNCTION_DECLARE(func_name)
#define FILTERX_GENERATOR_FUNCTION(func_name, ctor) FILTERX_FUNCTION(func_name, ctor)
#define FILTERX_GENERATOR_FUNCTION_PLUGIN(func_name) _FILTERX_FUNCTION_PLUGIN(func_name, LL_CONTEXT_FILTERX_GEN_FUNC)

#endif
26 changes: 25 additions & 1 deletion lib/filterx/expr-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ _eval(FilterXExpr *s)
gboolean
filterx_expr_is_generator(FilterXExpr *s)
{
return s->eval == _eval;
return s && s->eval == _eval;
}

void
Expand Down Expand Up @@ -97,6 +97,30 @@ _create_container_free(FilterXExpr *s)
filterx_expr_free_method(s);
}

FilterXObject *
filterx_generator_create_dict_container(FilterXExprGenerator *s, FilterXExpr *fillable_parent)
{
FilterXObject *fillable_parent_obj = filterx_expr_eval_typed(fillable_parent);
if (!fillable_parent_obj)
return NULL;

FilterXObject *result = filterx_object_create_dict(fillable_parent_obj);
filterx_object_unref(fillable_parent_obj);
return result;
}

FilterXObject *
filterx_generator_create_list_container(FilterXExprGenerator *s, FilterXExpr *fillable_parent)
{
FilterXObject *fillable_parent_obj = filterx_expr_eval_typed(fillable_parent);
if (!fillable_parent_obj)
return NULL;

FilterXObject *result = filterx_object_create_list(fillable_parent_obj);
filterx_object_unref(fillable_parent_obj);
return result;
}

/* Takes reference of g and fillable_parent */
FilterXExpr *
filterx_generator_create_container_new(FilterXExpr *g, FilterXExpr *fillable_parent)
Expand Down
5 changes: 5 additions & 0 deletions lib/filterx/expr-generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ gboolean filterx_expr_is_generator(FilterXExpr *s);

FilterXExpr *filterx_generator_create_container_new(FilterXExpr *g, FilterXExpr *fillable_parent);


/* protected */
FilterXObject *filterx_generator_create_dict_container(FilterXExprGenerator *s, FilterXExpr *fillable_parent);
FilterXObject *filterx_generator_create_list_container(FilterXExprGenerator *s, FilterXExpr *fillable_parent);

#endif
51 changes: 23 additions & 28 deletions lib/filterx/expr-literal-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,30 +109,6 @@ _eval_elements(FilterXObject *fillable, GList *elements)
return TRUE;
}

static FilterXObject *
_dict_generator_create_container(FilterXExprGenerator *s, FilterXExpr *fillable_parent)
{
FilterXObject *fillable_parent_obj = filterx_expr_eval_typed(fillable_parent);
if (!fillable_parent_obj)
return NULL;

FilterXObject *result = filterx_object_create_dict(fillable_parent_obj);
filterx_object_unref(fillable_parent_obj);
return result;
}

static FilterXObject *
_list_generator_create_container(FilterXExprGenerator *s, FilterXExpr *fillable_parent)
{
FilterXObject *fillable_parent_obj = filterx_expr_eval_typed(fillable_parent);
if (!fillable_parent_obj)
return NULL;

FilterXObject *result = filterx_object_create_list(fillable_parent_obj);
filterx_object_unref(fillable_parent_obj);
return result;
}

static gboolean
_literal_generator_generate(FilterXExprGenerator *s, FilterXObject *fillable)
{
Expand Down Expand Up @@ -174,13 +150,32 @@ filterx_literal_dict_generator_foreach(FilterXExpr *s, FilterXLiteralDictGenerat
return TRUE;
}

gboolean
filterx_literal_list_generator_foreach(FilterXExpr *s, FilterXLiteralListGeneratorForeachFunc func, gpointer user_data)
{
FilterXExprLiteralGenerator *self = (FilterXExprLiteralGenerator *) s;

gsize i = 0;
for (GList *link = self->elements; link; link = link->next)
{
FilterXLiteralGeneratorElem *elem = (FilterXLiteralGeneratorElem *) link->data;

if (!func(i, elem->value, user_data))
return FALSE;

i++;
}

return TRUE;
}

FilterXExpr *
filterx_literal_dict_generator_new(void)
{
FilterXExprLiteralGenerator *self = g_new0(FilterXExprLiteralGenerator, 1);

_literal_generator_init_instance(self);
self->super.create_container = _dict_generator_create_container;
self->super.create_container = filterx_generator_create_dict_container;

return &self->super.super;
}
Expand All @@ -191,7 +186,7 @@ filterx_literal_list_generator_new(void)
FilterXExprLiteralGenerator *self = g_new0(FilterXExprLiteralGenerator, 1);

_literal_generator_init_instance(self);
self->super.create_container = _list_generator_create_container;
self->super.create_container = filterx_generator_create_list_container;

return &self->super.super;
}
Expand Down Expand Up @@ -298,12 +293,12 @@ gboolean
filterx_expr_is_literal_dict_generator(FilterXExpr *s)
{
FilterXExprGenerator *generator = (FilterXExprGenerator *) s;
return filterx_expr_is_generator(s) && generator->create_container == _dict_generator_create_container;
return filterx_expr_is_generator(s) && generator->create_container == filterx_generator_create_dict_container;
}

gboolean
filterx_expr_is_literal_list_generator(FilterXExpr *s)
{
FilterXExprGenerator *generator = (FilterXExprGenerator *) s;
return filterx_expr_is_generator(s) && generator->create_container == _list_generator_create_container;
return filterx_expr_is_generator(s) && generator->create_container == filterx_generator_create_list_container;
}
3 changes: 3 additions & 0 deletions lib/filterx/expr-literal-generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "filterx/expr-generator.h"

typedef gboolean (*FilterXLiteralDictGeneratorForeachFunc)(FilterXExpr *, FilterXExpr *, gpointer);
typedef gboolean (*FilterXLiteralListGeneratorForeachFunc)(gsize, FilterXExpr *, gpointer);

typedef struct FilterXLiteralGeneratorElem_ FilterXLiteralGeneratorElem;
typedef struct FilterXExprLiteralGenerator_ FilterXExprLiteralGenerator;
Expand All @@ -39,6 +40,8 @@ FilterXExpr *filterx_literal_list_generator_new(void);
void filterx_literal_generator_set_elements(FilterXExpr *s, GList *elements);
gboolean filterx_literal_dict_generator_foreach(FilterXExpr *s, FilterXLiteralDictGeneratorForeachFunc func,
gpointer user_data);
gboolean filterx_literal_list_generator_foreach(FilterXExpr *s, FilterXLiteralListGeneratorForeachFunc func,
gpointer user_data);

FilterXExpr *filterx_literal_inner_dict_generator_new(FilterXExpr *root_literal_generator, GList *elements);
FilterXExpr *filterx_literal_inner_list_generator_new(FilterXExpr *root_literal_generator, GList *elements);
Expand Down
20 changes: 6 additions & 14 deletions lib/filterx/expr-regexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,18 +393,10 @@ _regexp_search_generator_create_container(FilterXExprGenerator *s, FilterXExpr *
{
FilterXExprRegexpSearchGenerator *self = (FilterXExprRegexpSearchGenerator *) s;

FilterXObject *fillable_parent_obj = filterx_expr_eval_typed(fillable_parent);
if (!fillable_parent_obj)
return NULL;

FilterXObject *result;
if (_has_named_capture_groups(self->pattern))
result = filterx_object_create_dict(fillable_parent_obj);
else
result = filterx_object_create_list(fillable_parent_obj);
return filterx_generator_create_dict_container(s, fillable_parent);

filterx_object_unref(fillable_parent_obj);
return result;
return filterx_generator_create_list_container(s, fillable_parent);
}

static void
Expand Down Expand Up @@ -452,11 +444,11 @@ _extract_search_args(FilterXExprRegexpSearchGenerator *self, FilterXFunctionArgs

/* Takes reference of lhs */
FilterXExpr *
filterx_generator_function_regexp_search_new(const gchar *function_name, FilterXFunctionArgs *args, GError **error)
filterx_generator_function_regexp_search_new(FilterXFunctionArgs *args, GError **error)
{
FilterXExprRegexpSearchGenerator *self = g_new0(FilterXExprRegexpSearchGenerator, 1);

filterx_generator_function_init_instance(&self->super, function_name);
filterx_generator_function_init_instance(&self->super, "regexp_search");
self->super.super.generate = _regexp_search_generator_generate;
self->super.super.super.free_fn = _regexp_search_generator_free;
self->super.super.create_container = _regexp_search_generator_create_container;
Expand Down Expand Up @@ -700,10 +692,10 @@ _opts_init(FilterXFuncRegexpSubstOpts *opts)
}

FilterXExpr *
filterx_function_regexp_subst_new(const gchar *function_name, FilterXFunctionArgs *args, GError **error)
filterx_function_regexp_subst_new(FilterXFunctionArgs *args, GError **error)
{
FilterXFuncRegexpSubst *self = g_new0(FilterXFuncRegexpSubst, 1);
filterx_function_init_instance(&self->super, function_name);
filterx_function_init_instance(&self->super, "regexp_subst");
self->super.super.eval = _subst_eval;
self->super.super.free_fn = _subst_free;

Expand Down
5 changes: 2 additions & 3 deletions lib/filterx/expr-regexp.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ typedef struct FilterXFuncRegexpSubstOpts_

FilterXExpr *filterx_expr_regexp_match_new(FilterXExpr *lhs, const gchar *pattern);
FilterXExpr *filterx_expr_regexp_nomatch_new(FilterXExpr *lhs, const gchar *pattern);
FilterXExpr *filterx_generator_function_regexp_search_new(const gchar *function_name, FilterXFunctionArgs *args,
GError **error);
FilterXExpr *filterx_function_regexp_subst_new(const gchar *function_name, FilterXFunctionArgs *args, GError **error);
FilterXExpr *filterx_generator_function_regexp_search_new(FilterXFunctionArgs *args, GError **error);
FilterXExpr *filterx_function_regexp_subst_new(FilterXFunctionArgs *args, GError **error);
gboolean filterx_regexp_subst_is_jit_enabled(FilterXExpr *s);

#endif
4 changes: 2 additions & 2 deletions lib/filterx/expr-unset.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ _free(FilterXExpr *s)
}

FilterXExpr *
filterx_function_unset_new(const gchar *function_name, FilterXFunctionArgs *args, GError **error)
filterx_function_unset_new(FilterXFunctionArgs *args, GError **error)
{
FilterXExprUnset *self = g_new0(FilterXExprUnset, 1);
filterx_function_init_instance(&self->super, function_name);
filterx_function_init_instance(&self->super, "unset");

self->super.super.eval = _eval;
self->super.super.free_fn = _free;
Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/expr-unset.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@

#include "filterx/expr-function.h"

FilterXExpr *filterx_function_unset_new(const gchar *function_name, FilterXFunctionArgs *args, GError **error);
FilterXExpr *filterx_function_unset_new(FilterXFunctionArgs *args, GError **error);

#endif
4 changes: 2 additions & 2 deletions lib/filterx/func-flatten.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ _extract_args(FilterXFunctionFlatten *self, FilterXFunctionArgs *args, GError **
}

FilterXExpr *
filterx_function_flatten_new(const gchar *function_name, FilterXFunctionArgs *args, GError **error)
filterx_function_flatten_new(FilterXFunctionArgs *args, GError **error)
{
FilterXFunctionFlatten *self = g_new0(FilterXFunctionFlatten, 1);
filterx_function_init_instance(&self->super, function_name);
filterx_function_init_instance(&self->super, "flatten");
self->super.super.eval = _eval;
self->super.super.free_fn = _free;

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

#include "filterx/expr-function.h"

FilterXExpr *filterx_function_flatten_new(const gchar *function_name, FilterXFunctionArgs *args, GError **error);
FilterXExpr *filterx_function_flatten_new(FilterXFunctionArgs *args, GError **error);

#endif
4 changes: 2 additions & 2 deletions lib/filterx/func-istype.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ _extract_args(FilterXFunctionIsType *self, FilterXFunctionArgs *args, GError **e
}

FilterXExpr *
filterx_function_istype_new(const gchar *function_name, FilterXFunctionArgs *args, GError **error)
filterx_function_istype_new(FilterXFunctionArgs *args, GError **error)
{
FilterXFunctionIsType *self = g_new0(FilterXFunctionIsType, 1);
filterx_function_init_instance(&self->super, function_name);
filterx_function_init_instance(&self->super, "istype");
self->super.super.eval = _eval;
self->super.super.free_fn = _free;

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

#include "filterx/expr-function.h"

FilterXExpr *filterx_function_istype_new(const gchar *function_name, FilterXFunctionArgs *args, GError **error);
FilterXExpr *filterx_function_istype_new(FilterXFunctionArgs *args, GError **error);

#endif
Loading

0 comments on commit 7296984

Please sign in to comment.