Skip to content

Commit

Permalink
Get a test for routing based upon regexes to pass for changing some c…
Browse files Browse the repository at this point in the history
…ontent around.

Many other tests fail, so this is still a WIP

Signed-off-by: Greg Schohn <greg.schohn@gmail.com>
  • Loading branch information
gregschohn committed Nov 24, 2024
1 parent 12c0cfa commit e68a155
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -56,7 +57,7 @@ public JinjavaTransformer(String templateString,
public Map<String, Object> transformJson(Map<String, Object> incomingJson) {
var resultStr = jinjava.render(templateStr, createContextWithSourceFunction.apply(incomingJson));
log.atInfo().setMessage("output from jinjava... {}").addArgument(resultStr).log();
var parsedObj = (Map<String,Object>) objectMapper.readValue(resultStr, Map.class);
var parsedObj = (Map<String,Object>) objectMapper.readValue(resultStr, LinkedHashMap.class);
return doFinalSubstitutions(incomingJson, parsedObj);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -36,4 +36,4 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args)
}
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -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 %}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,27 +22,24 @@ public Map<String, Object> doRouting(Map<String, Object> flags, Map<String, Obje
" {}" +
"{%- endmacro -%}\n" +

"{%- macro matchIt(doc) -%}" +
" {{ doc.label | regex_capture('Thing_A(.*)') }}" +
"{%- endmacro -%}\n" +
"{% macro handleIt(matches) %}\n" +
"{% for key, value in matches.items() %}\n" + // TODO - this is a string, not an object!

// " {{ key }}: {{ value }}<br>\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')" +
" ])" +
"-}}";

Expand All @@ -52,7 +48,6 @@ public Map<String, Object> doRouting(Map<String, Object> flags, Map<String, Obje
return transformed.transformJson(inputDoc);
}


@Test
public void test() {
var flags = Map.of(
Expand All @@ -79,18 +74,37 @@ public void test() {
}

@Test
public void testA() {
public void testA() throws IOException {
var flagAOff = Map.of(
"first", false,
"second", (Object) true);
var doc = Map.of(
"matchA", false,
"matchB", (Object) true);
var docA = Map.of(
"label", "Thing_A_and more!",
"stuff", Map.of(
"inner1", "data1",
"inner2", "data2"
)
);
Assertions.assertEquals("_and more!", doRouting(null, doc).get("matchedVal"));
Assertions.assertTrue(doRouting(flagAOff, doc).isEmpty());
var docB = Map.of(
"label", "B-hive",
"stuff", List.of(
"data1",
"data2"
)
);
{
var resultMap = doRouting(null, docA);
Assertions.assertEquals(1, resultMap.size());
Assertions.assertEquals("_and more!", resultMap.get("matchedVal"));
}
{
var resultMap = doRouting(flagAOff, docA);
Assertions.assertTrue(resultMap.isEmpty());
}
{
var resultMap = doRouting(flagAOff, docB);
Assertions.assertEquals("{\"stuff\":[\"data2\",\"data1\"],\"label\":\"B-hive\"}",
objectMapper.writeValueAsString(resultMap));
}
}
}

0 comments on commit e68a155

Please sign in to comment.