Skip to content

Commit

Permalink
Merge pull request #397 from alltilla/filterx-set-fields
Browse files Browse the repository at this point in the history
filterx: `set_fields()`
  • Loading branch information
MrAnno authored Nov 29, 2024
2 parents fa90673 + 633e2b7 commit bbb2176
Show file tree
Hide file tree
Showing 8 changed files with 632 additions and 36 deletions.
2 changes: 2 additions & 0 deletions lib/filterx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ set(FILTERX_HEADERS
filterx/func-istype.h
filterx/func-len.h
filterx/func-sdata.h
filterx/func-set-fields.h
filterx/func-str-transform.h
filterx/func-str.h
filterx/func-unset-empties.h
Expand Down Expand Up @@ -104,6 +105,7 @@ set(FILTERX_SOURCES
filterx/func-istype.c
filterx/func-len.c
filterx/func-sdata.c
filterx/func-set-fields.c
filterx/func-str-transform.c
filterx/func-str.c
filterx/func-unset-empties.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 @@ -45,6 +45,7 @@ filterxinclude_HEADERS = \
lib/filterx/func-istype.h \
lib/filterx/func-len.h \
lib/filterx/func-sdata.h \
lib/filterx/func-set-fields.h \
lib/filterx/func-str-transform.h \
lib/filterx/func-str.h \
lib/filterx/func-unset-empties.h \
Expand Down Expand Up @@ -106,6 +107,7 @@ filterx_sources = \
lib/filterx/func-istype.c \
lib/filterx/func-len.c \
lib/filterx/func-sdata.c \
lib/filterx/func-set-fields.c \
lib/filterx/func-str-transform.c \
lib/filterx/func-str.c \
lib/filterx/func-unset-empties.c \
Expand Down
86 changes: 50 additions & 36 deletions lib/filterx/expr-literal-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,40 +196,6 @@ _literal_generator_init_instance(FilterXExprLiteralGenerator *self)
self->super.super.free_fn = _literal_generator_free;
}

gboolean
filterx_literal_dict_generator_foreach(FilterXExpr *s, FilterXLiteralDictGeneratorForeachFunc func, gpointer user_data)
{
FilterXExprLiteralGenerator *self = (FilterXExprLiteralGenerator *) s;

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

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

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)
Expand Down Expand Up @@ -388,6 +354,54 @@ filterx_expr_is_literal_generator(FilterXExpr *s)
guint
filterx_expr_literal_generator_len(FilterXExpr *s)
{
FilterXExprLiteralGenerator *self = (FilterXExprLiteralGenerator *) s;
return g_list_length(self->elements);
GList *elements = NULL;
if (_filterx_expr_is_inner_dict_generator(s))
elements = ((FilterXLiteralInnerGenerator *) s)->elements;
else
elements = ((FilterXExprLiteralGenerator *) s)->elements;

return g_list_length(elements);
}

gboolean
filterx_literal_dict_generator_foreach(FilterXExpr *s, FilterXLiteralDictGeneratorForeachFunc func, gpointer user_data)
{
GList *elements = NULL;
if (_filterx_expr_is_inner_dict_generator(s))
elements = ((FilterXLiteralInnerGenerator *) s)->elements;
else
elements = ((FilterXExprLiteralGenerator *) s)->elements;

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

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

return TRUE;
}

gboolean
filterx_literal_list_generator_foreach(FilterXExpr *s, FilterXLiteralListGeneratorForeachFunc func, gpointer user_data)
{
GList *elements = NULL;
if (_filterx_expr_is_inner_list_generator(s))
elements = ((FilterXLiteralInnerGenerator *) s)->elements;
else
elements = ((FilterXExprLiteralGenerator *) s)->elements;

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

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

i++;
}

return TRUE;
}
2 changes: 2 additions & 0 deletions lib/filterx/filterx-globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "filterx/func-len.h"
#include "filterx/func-vars.h"
#include "filterx/func-unset-empties.h"
#include "filterx/func-set-fields.h"
#include "filterx/func-str.h"
#include "filterx/func-str-transform.h"
#include "filterx/func-flatten.h"
Expand Down Expand Up @@ -135,6 +136,7 @@ _ctors_init(void)
g_assert(filterx_builtin_function_ctor_register("strptime", filterx_function_strptime_new));
g_assert(filterx_builtin_function_ctor_register("istype", filterx_function_istype_new));
g_assert(filterx_builtin_function_ctor_register("unset_empties", filterx_function_unset_empties_new));
g_assert(filterx_builtin_function_ctor_register("set_fields", filterx_function_set_fields_new));
g_assert(filterx_builtin_function_ctor_register("regexp_subst", filterx_function_regexp_subst_new));
g_assert(filterx_builtin_function_ctor_register("unset", filterx_function_unset_new));
g_assert(filterx_builtin_function_ctor_register("flatten", filterx_function_flatten_new));
Expand Down
Loading

0 comments on commit bbb2176

Please sign in to comment.