Skip to content

Commit

Permalink
Merge pull request #198 from bshifter/filterx-parse-csv-delimiter-fix
Browse files Browse the repository at this point in the history
Filterx parse csv delimiter fix
  • Loading branch information
MrAnno authored Jul 9, 2024
2 parents 1515ba2 + a3b64e4 commit f6a5977
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
4 changes: 2 additions & 2 deletions modules/csvparser/filterx-func-parse-csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ _extract_opts(FilterXFunctionParseCSV *self, FilterXFunctionArgs *args, GError *
value = filterx_function_args_get_named_literal_string(args, FILTERX_FUNC_PARSE_CSV_ARG_NAME_DELIMITER, &len, &exists);
if (exists)
{
if (len < 1)
if (len < 1 && !self->string_delimiters)
{
error_str = FILTERX_FUNC_PARSE_CSV_ARG_NAME_DELIMITER " can not be empty";
error_str = FILTERX_FUNC_PARSE_ERR_EMPTY_DELIMITER;
goto error;
}
if (!value)
Expand Down
6 changes: 6 additions & 0 deletions modules/csvparser/filterx-func-parse-csv.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@
#define FILTERX_FUNC_PARSE_CSV_USAGE "Usage: parse_csv(msg_str [" \
FILTERX_FUNC_PARSE_CSV_ARG_NAME_COLUMNS"=json_array, " \
FILTERX_FUNC_PARSE_CSV_ARG_NAME_DELIMITER"=string, " \
FILTERX_FUNC_PARSE_CSV_ARG_NAME_STRING_DELIMITERS"=json_array, " \
FILTERX_FUNC_PARSE_CSV_ARG_NAME_DIALECT"=string, " \
FILTERX_FUNC_PARSE_CSV_ARG_NAME_STRIP_WHITESPACES"=boolean, " \
FILTERX_FUNC_PARSE_CSV_ARG_NAME_GREEDY"=boolean])"
#define FILTERX_FUNC_PARSE_ERR_EMPTY_DELIMITER "Either '" \
FILTERX_FUNC_PARSE_CSV_ARG_NAME_DELIMITER"' or '" \
FILTERX_FUNC_PARSE_CSV_ARG_NAME_STRING_DELIMITERS"' must be set, and '" \
FILTERX_FUNC_PARSE_CSV_ARG_NAME_DELIMITER"' cannot be empty if '" \
FILTERX_FUNC_PARSE_CSV_ARG_NAME_STRING_DELIMITERS"' is unset"

FilterXExpr *filterx_function_parse_csv_new(const gchar *function_name, FilterXFunctionArgs *args, GError **error);
gpointer filterx_function_construct_parse_csv(Plugin *self);
Expand Down
52 changes: 52 additions & 0 deletions modules/csvparser/tests/test_filterx_func_parse_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,58 @@ Test(filterx_func_parse_csv, test_optional_argument_string_delimiters_and_delimi
g_error_free(err);
}

Test(filterx_func_parse_csv, test_optional_argument_delimiter_default_unset_when_string_delimiters_set)
{
GList *args = NULL;
args = g_list_append(args, filterx_function_arg_new(NULL,
filterx_literal_new(filterx_string_new("testfoobar,baz", -1))));
FilterXObject *string_delimiters = _generate_string_list("foo", NULL);
args = g_list_append(args, filterx_function_arg_new(FILTERX_FUNC_PARSE_CSV_ARG_NAME_STRING_DELIMITERS,
filterx_literal_new(string_delimiters)));
args = g_list_append(args, filterx_function_arg_new(FILTERX_FUNC_PARSE_CSV_ARG_NAME_DELIMITER,
filterx_literal_new(filterx_string_new("", -1))));

GError *err = NULL;
GError *args_err = NULL;
FilterXExpr *func = filterx_function_parse_csv_new("test", filterx_function_args_new(args, &args_err), &err);
cr_assert_null(args_err);
cr_assert_null(err);

FilterXObject *obj = filterx_expr_eval(func);

cr_assert_not_null(obj);
cr_assert(filterx_object_is_type(obj, &FILTERX_TYPE_NAME(json_array)));

FilterXObject *elt = filterx_list_get_subscript(obj, 1);

GString *repr = scratch_buffers_alloc();
cr_assert(filterx_object_repr(elt, repr));

cr_assert_str_eq(repr->str, "bar,baz");
filterx_object_unref(elt);
filterx_expr_unref(func);
filterx_object_unref(obj);
g_error_free(err);
}

Test(filterx_func_parse_csv, test_optional_argument_delimiter_unable_to_set_with_empty_string_delimiters)
{
GList *args = NULL;
args = g_list_append(args, filterx_function_arg_new(NULL,
filterx_literal_new(filterx_string_new("testfoobar,baz", -1))));
args = g_list_append(args, filterx_function_arg_new(FILTERX_FUNC_PARSE_CSV_ARG_NAME_DELIMITER,
filterx_literal_new(filterx_string_new("", -1))));

GError *err = NULL;
GError *args_err = NULL;
FilterXExpr *func = filterx_function_parse_csv_new("test", filterx_function_args_new(args, &args_err), &err);
cr_assert_null(args_err);
cr_assert_not_null(err);
cr_assert(strcmp(err->message, FILTERX_FUNC_PARSE_ERR_EMPTY_DELIMITER));

filterx_expr_unref(func);
g_error_free(err);
}

static void
setup(void)
Expand Down

0 comments on commit f6a5977

Please sign in to comment.