1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
/*
* Copyright (c) 2024 Attila Szakacs
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; 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-func-format-kv.h"
#include "filterx/expr-literal.h"
#include "filterx/object-string.h"
#include "filterx/object-null.h"
#include "filterx/object-dict-interface.h"
#include "filterx/object-list-interface.h"
#include "filterx/filterx-eval.h"
#include "scratch-buffers.h"
#include "utf8utils.h"
#define FILTERX_FUNC_FORMAT_KV_USAGE "Usage: format_kv(kvs_dict, value_separator=\"=\", pair_separator=\", \")"
typedef struct FilterXFunctionFormatKV_
{
FilterXFunction super;
FilterXExpr *kvs;
gchar value_separator;
gchar *pair_separator;
} FilterXFunctionFormatKV;
static gboolean
_append_kv_to_buffer(FilterXObject *key, FilterXObject *value, gpointer user_data)
{
FilterXFunctionFormatKV *self = ((gpointer *) user_data)[0];
GString *buffer = ((gpointer *) user_data)[1];
if (filterx_object_is_type(value, &FILTERX_TYPE_NAME(dict)) ||
filterx_object_is_type(value, &FILTERX_TYPE_NAME(list)))
{
msg_debug("FilterX: format_kv(): skipping object, type not supported",
evt_tag_str("type", value->type->name));
return TRUE;
}
if (buffer->len)
g_string_append(buffer, self->pair_separator);
if (!filterx_object_repr_append(key, buffer))
return FALSE;
g_string_append_c(buffer, self->value_separator);
gsize len_before_value = buffer->len;
if (!filterx_object_repr_append(value, buffer))
return FALSE;
/* TODO: make the characters here configurable. */
if (memchr(buffer->str + len_before_value, ' ', buffer->len - len_before_value) != NULL)
{
ScratchBuffersMarker marker;
GString *value_buffer = scratch_buffers_alloc_and_mark(&marker);
g_string_assign(value_buffer, buffer->str + len_before_value);
g_string_truncate(buffer, len_before_value);
g_string_append_c(buffer, '"');
append_unsafe_utf8_as_escaped_binary(buffer, value_buffer->str, value_buffer->len, "\"");
g_string_append_c(buffer, '"');
scratch_buffers_reclaim_marked(marker);
}
return TRUE;
}
static FilterXObject *
_eval(FilterXExpr *s)
{
FilterXFunctionFormatKV *self = (FilterXFunctionFormatKV *) s;
FilterXObject *kvs = filterx_expr_eval_typed(self->kvs);
if (!kvs)
{
filterx_eval_push_error("Failed to evaluate kvs_dict. " FILTERX_FUNC_FORMAT_KV_USAGE, s, NULL);
return NULL;
}
if (!filterx_object_is_type(kvs, &FILTERX_TYPE_NAME(dict)))
{
filterx_eval_push_error("kvs_dict must be a dict. " FILTERX_FUNC_FORMAT_KV_USAGE, s, kvs);
filterx_object_unref(kvs);
return NULL;
}
GString *formatted = scratch_buffers_alloc();
gpointer user_data[] = { self, formatted };
gboolean success = filterx_dict_iter(kvs, _append_kv_to_buffer, user_data);
filterx_object_unref(kvs);
return success ? filterx_string_new(formatted->str, formatted->len) : NULL;
}
static void
_free(FilterXExpr *s)
{
FilterXFunctionFormatKV *self = (FilterXFunctionFormatKV *) s;
filterx_expr_unref(self->kvs);
g_free(self->pair_separator);
filterx_function_free_method(&self->super);
}
static gboolean
_extract_value_separator_arg(FilterXFunctionFormatKV *self, FilterXFunctionArgs *args, GError **error)
{
gboolean exists;
gsize value_separator_len;
const gchar *value_separator = filterx_function_args_get_named_literal_string(args, "value_separator",
&value_separator_len, &exists);
if (!exists)
return TRUE;
if (!value_separator)
{
g_set_error(error, FILTERX_FUNCTION_ERROR, FILTERX_FUNCTION_ERROR_CTOR_FAIL,
"value_separator must be a string literal. " FILTERX_FUNC_FORMAT_KV_USAGE);
return FALSE;
}
if (value_separator_len != 1)
{
g_set_error(error, FILTERX_FUNCTION_ERROR, FILTERX_FUNCTION_ERROR_CTOR_FAIL,
"value_separator must be a single character. " FILTERX_FUNC_FORMAT_KV_USAGE);
return FALSE;
}
self->value_separator = value_separator[0];
return TRUE;
}
static gboolean
_extract_pair_separator_arg(FilterXFunctionFormatKV *self, FilterXFunctionArgs *args, GError **error)
{
gboolean exists;
gsize pair_separator_len;
const gchar *pair_separator = filterx_function_args_get_named_literal_string(args, "pair_separator",
&pair_separator_len, &exists);
if (!exists)
return TRUE;
if (!pair_separator)
{
g_set_error(error, FILTERX_FUNCTION_ERROR, FILTERX_FUNCTION_ERROR_CTOR_FAIL,
"pair_separator must be a string literal. " FILTERX_FUNC_FORMAT_KV_USAGE);
return FALSE;
}
if (pair_separator_len == 0)
{
g_set_error(error, FILTERX_FUNCTION_ERROR, FILTERX_FUNCTION_ERROR_CTOR_FAIL,
"pair_separator must be non-zero length. " FILTERX_FUNC_FORMAT_KV_USAGE);
return FALSE;
}
g_free(self->pair_separator);
self->pair_separator = g_strdup(pair_separator);
return TRUE;
}
static gboolean
_extract_arguments(FilterXFunctionFormatKV *self, FilterXFunctionArgs *args, GError **error)
{
gsize args_len = filterx_function_args_len(args);
if (args_len != 1)
{
g_set_error(error, FILTERX_FUNCTION_ERROR, FILTERX_FUNCTION_ERROR_CTOR_FAIL,
"invalid number of arguments. " FILTERX_FUNC_FORMAT_KV_USAGE);
return FALSE;
}
self->kvs = filterx_function_args_get_expr(args, 0);
if (!self->kvs)
{
g_set_error(error, FILTERX_FUNCTION_ERROR, FILTERX_FUNCTION_ERROR_CTOR_FAIL,
"kvs_dict must be set. " FILTERX_FUNC_FORMAT_KV_USAGE);
return FALSE;
}
if (!_extract_value_separator_arg(self, args, error))
return FALSE;
if (!_extract_pair_separator_arg(self, args, error))
return FALSE;
return TRUE;
}
FilterXFunction *
filterx_function_format_kv_new(const gchar *function_name, FilterXFunctionArgs *args, GError **error)
{
FilterXFunctionFormatKV *self = g_new0(FilterXFunctionFormatKV, 1);
filterx_function_init_instance(&self->super, function_name);
self->super.super.eval = _eval;
self->super.super.free_fn = _free;
self->value_separator = '=';
self->pair_separator = g_strdup(", ");
if (!_extract_arguments(self, args, error))
goto error;
filterx_function_args_free(args);
return &self->super;
error:
filterx_function_args_free(args);
filterx_expr_unref(&self->super.super);
return NULL;
}
gpointer
filterx_function_construct_format_kv(Plugin *self)
{
return (gpointer) filterx_function_format_kv_new;
}
|