From e68a155dedc1978af9662e7a95eb83d70ee42b1d Mon Sep 17 00:00:00 2001 From: Greg Schohn Date: Sat, 23 Nov 2024 19:58:33 -0500 Subject: [PATCH] Get a test for routing based upon regexes to pass for changing some content around. Many other tests fail, so this is still a WIP Signed-off-by: Greg Schohn --- .../transform/JinjavaTransformer.java | 3 +- .../jinjava/DynamicMacroFunction.java | 3 - .../transform/jinjava/RegexCaptureFilter.java | 4 +- .../main/resources/jinjava/common/route.j2 | 12 ++-- .../migrations/transform/RouteTest.java | 66 +++++++++++-------- 5 files changed, 49 insertions(+), 39 deletions(-) diff --git a/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/main/java/org/opensearch/migrations/transform/JinjavaTransformer.java b/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/main/java/org/opensearch/migrations/transform/JinjavaTransformer.java index ff4354713d..3d5c450570 100644 --- a/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/main/java/org/opensearch/migrations/transform/JinjavaTransformer.java +++ b/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/main/java/org/opensearch/migrations/transform/JinjavaTransformer.java @@ -1,5 +1,6 @@ package org.opensearch.migrations.transform; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -56,7 +57,7 @@ public JinjavaTransformer(String templateString, public Map transformJson(Map incomingJson) { var resultStr = jinjava.render(templateStr, createContextWithSourceFunction.apply(incomingJson)); log.atInfo().setMessage("output from jinjava... {}").addArgument(resultStr).log(); - var parsedObj = (Map) objectMapper.readValue(resultStr, Map.class); + var parsedObj = (Map) objectMapper.readValue(resultStr, LinkedHashMap.class); return doFinalSubstitutions(incomingJson, parsedObj); } diff --git a/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/main/java/org/opensearch/migrations/transform/jinjava/DynamicMacroFunction.java b/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/main/java/org/opensearch/migrations/transform/jinjava/DynamicMacroFunction.java index c785a3f9b4..685713fcac 100644 --- a/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/main/java/org/opensearch/migrations/transform/jinjava/DynamicMacroFunction.java +++ b/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/main/java/org/opensearch/migrations/transform/jinjava/DynamicMacroFunction.java @@ -7,9 +7,6 @@ import com.hubspot.jinjava.interpret.Context; import com.hubspot.jinjava.interpret.JinjavaInterpreter; -import com.hubspot.jinjava.lib.fn.ELFunctionDefinition; -import com.hubspot.jinjava.lib.fn.Functions; -import com.hubspot.jinjava.objects.collections.PyList; import com.hubspot.jinjava.lib.fn.MacroFunction; public class DynamicMacroFunction { diff --git a/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/main/java/org/opensearch/migrations/transform/jinjava/RegexCaptureFilter.java b/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/main/java/org/opensearch/migrations/transform/jinjava/RegexCaptureFilter.java index 7fce682c90..3612b164b6 100644 --- a/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/main/java/org/opensearch/migrations/transform/jinjava/RegexCaptureFilter.java +++ b/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/main/java/org/opensearch/migrations/transform/jinjava/RegexCaptureFilter.java @@ -2,8 +2,8 @@ import java.util.HashMap; -import com.google.re2j.Pattern; import com.google.re2j.Matcher; +import com.google.re2j.Pattern; import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.lib.filter.Filter; @@ -36,4 +36,4 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) } return null; } -} \ No newline at end of file +} diff --git a/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/main/resources/jinjava/common/route.j2 b/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/main/resources/jinjava/common/route.j2 index a436fa4318..5e5f56a064 100644 --- a/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/main/resources/jinjava/common/route.j2 +++ b/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/main/resources/jinjava/common/route.j2 @@ -1,22 +1,20 @@ {% import "common/featureEnabled.j2" as flags %} -within import {{ __macros__ | pprint }} -{% macro route(input, feature_flags, default_action, routes) %} +{% macro route(input, field_to_match, feature_flags, default_action, routes) %} {%- set ns = namespace(result=none, matched=false) -%} - {%- for feature_name, pattern_fn, action_fn in routes if not ns.matched -%} + {%- for feature_name, pattern, action_fn in routes if not ns.matched -%} {%- if not ns.matched -%} {# we haven't found a match yet, otherwise skip the rest #} - {%- set match = invoke_macro(pattern_fn, input) -%} + {%- set match = field_to_match | regex_capture(pattern) -%} {%- if match is not none -%} {%- set ns.matched = true -%} {%- if flags.is_enabled(feature_flags, feature_name) -%} - {%- set ns.result = invoke_macro(action_fn, match) -%} + {%- set ns.result = invoke_macro(action_fn, match, input) -%} {%- endif -%} {%- endif -%} {%- endif -%} {%- endfor -%} {%- if ns.result is none -%} - default answer: {{- invoke_macro(default_action, input) -}} {%- else -%} - result: {{- ns.result -}} + {{- ns.result -}} {%- endif -%} {% endmacro %} \ No newline at end of file diff --git a/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/test/java/org/opensearch/migrations/transform/RouteTest.java b/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/test/java/org/opensearch/migrations/transform/RouteTest.java index 36bbb44a04..27cf0c4764 100644 --- a/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/test/java/org/opensearch/migrations/transform/RouteTest.java +++ b/transformation/transformationPlugins/jsonMessageTransformers/jsonJinjavaTransformer/src/test/java/org/opensearch/migrations/transform/RouteTest.java @@ -1,10 +1,9 @@ package org.opensearch.migrations.transform; import java.io.IOException; +import java.util.List; import java.util.Map; -import org.opensearch.migrations.transform.flags.FeatureFlags; - import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Assertions; @@ -23,27 +22,24 @@ public Map doRouting(Map flags, Map\n" + - "{% endfor %}" + -// " - {{ matches['group'] }} - " + -// " { \"matchedVal\": \"{{ matches['group1'] }}\"}" + + "{% macro echoFirstMatch(matches, input) %}\n" + + " { \"matchedVal\": \"{{ matches['group1'] }}\"}" + + "{% endmacro %}" + + "{% macro echoFirstMatchAgain(matches, input) %}\n" + + " { \"again\": \"{{ matches['group1'] }}\"}" + + "{% endmacro %}" + + "{% macro switchStuff(matches, input) %}\n" + + " {% set swapped_list = [input.stuff[1], input.stuff[0]] %}" + + " {% set input = input + {'stuff': swapped_list} %}" + + " {{ input | tojson }} " + "{% endmacro %}" + - " {% call doDefault() %}" + - " {{input}}" + - " {% endcall %}" + - - "\n" + "{%- import \"common/route.j2\" as router -%}" + - "{{- router.route(source, flags, doDefault," + + "{{- router.route(source, source.label, flags, 'doDefault'," + " [" + - " ('labelMatches', 'matchIt', 'handleIt')" + + " ('matchA', 'Thing_A(.*)', 'echoFirstMatch')," + + " ('matchA', 'Thing_A(.*)', 'echoFirstMatchAgain')," + // make sure that we don't get duplicate results + " ('matchB', 'B(.*)', 'switchStuff')" + " ])" + "-}}"; @@ -52,7 +48,6 @@ public Map doRouting(Map flags, Map