diff --git a/CHANGELOG.md b/CHANGELOG.md index e280b1c7e..a88e9e69f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ ### Bugfix +* fix `generic_adder` not creating new field from type `list` + ## v10.0.3 ### Bugfix * fix loading of configuration inside the `AutoRuleCorpusTester` for `logprep test integration` diff --git a/doc/source/development/notebooks/processor_examples/generic_adder.ipynb b/doc/source/development/notebooks/processor_examples/generic_adder.ipynb new file mode 100644 index 000000000..141207e8b --- /dev/null +++ b/doc/source/development/notebooks/processor_examples/generic_adder.ipynb @@ -0,0 +1,199 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Calculator\n", + "\n", + "This presentations goal it to introduce the features of the `Calculator` and how to configure it." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### The challenge\n", + "\n", + "I want calculate with field values." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "from this:" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "document = {\n", + " 'message': {\n", + " \"time_in_ms\": \"bla\",\n", + " \"tags\": [\"hello\"]\n", + " }\n", + " }" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "to this:" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "expected = {\n", + " 'message': {\n", + " \"time_in_ms\": \"bla\",\n", + " \"tags\": [\"hello\", \"new\"]\n", + " }\n", + " }" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Create rule and processor" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "create the rule:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "create the processor config:" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "processor_config = {\n", + " \"almighty generic adder\":{ \n", + " \"type\": \"generic_adder\",\n", + " \"specific_rules\": [{\"filter\": \"*\", \"generic_adder\": {\"extend_target_list\": True, \"add\": {\"message.tags\": \"New\"}} }],\n", + " \"generic_rules\": [],\n", + " }\n", + " }" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "create the processor with the factory:" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "generic_adder" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from unittest import mock\n", + "from logprep.factory import Factory\n", + "\n", + "mock_logger = mock.MagicMock()\n", + "calculator = Factory.create(processor_config, mock_logger)\n", + "calculator" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Process event" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "before: {'message': {'time_in_ms': 'bla', 'tags': ['hello']}}\n", + "after: {'message': {'time_in_ms': 'bla', 'tags': ['hello', 'New']}}\n", + "False\n" + ] + } + ], + "source": [ + "from copy import deepcopy\n", + "mydocument = deepcopy(document)\n", + "\n", + "\n", + "print(f\"before: {mydocument}\")\n", + "calculator.process(mydocument)\n", + "print(f\"after: {mydocument}\")\n", + "print(mydocument == expected)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.11.0 ('.venv': venv)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.7" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "586280540a85d3e21edc698fe7b86af2848b9b02644e6c22463da25c40a3f1be" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/logprep/processor/generic_adder/rule.py b/logprep/processor/generic_adder/rule.py index e29064352..f49b7e77a 100644 --- a/logprep/processor/generic_adder/rule.py +++ b/logprep/processor/generic_adder/rule.py @@ -98,7 +98,7 @@ from attrs import define, field, validators -from logprep.processor.base.rule import Rule, InvalidRuleDefinitionError +from logprep.processor.base.rule import InvalidRuleDefinitionError, Rule from logprep.processor.field_manager.rule import FieldManagerRule from logprep.util.getter import GetterFactory @@ -119,7 +119,7 @@ class Config(Rule.Config): add: dict = field( validator=validators.deep_mapping( key_validator=validators.instance_of(str), - value_validator=validators.instance_of((str, bool)), + value_validator=validators.instance_of((str, bool, list)), ), default={}, ) diff --git a/tests/unit/processor/generic_adder/test_generic_adder.py b/tests/unit/processor/generic_adder/test_generic_adder.py index 109a0026c..b66f11fdc 100644 --- a/tests/unit/processor/generic_adder/test_generic_adder.py +++ b/tests/unit/processor/generic_adder/test_generic_adder.py @@ -282,6 +282,19 @@ class TestGenericAdder(BaseProcessorTestCase): "dotted": {"added": {"field": "yet_another_value"}}, }, ), + ( + "Extend list field with 'extend_target_list' enabled", + { + "filter": "*", + "generic_adder": { + "add": { + "some_added_field": ["some value"], + }, + }, + }, + {"extend_generic_test": "Test"}, + {"extend_generic_test": "Test", "some_added_field": ["some value"]}, + ), ] failure_test_cases = [ # testcase, rule, event, expected, error_message