Skip to content

Commit

Permalink
Create JsonJSTransformerProvider for custom scripts
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Kurait <[email protected]>
  • Loading branch information
AndreKurait committed Jan 9, 2025
1 parent 7e12bac commit 1fe744a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
buildscript {
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.1'
}
}

plugins {
id 'io.freefair.lombok'
}

dependencies {
implementation project(':transformation:transformationPlugins:jsonMessageTransformers:jsonMessageTransformerInterface')
implementation project(':transformation:transformationPlugins:jsonMessageTransformers:jsonJSTransformer')

implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core'
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind'
}

tasks.named('test') {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.opensearch.migrations.transform;

import java.util.Map;
import java.util.function.Function;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;

public class JsonJSTransformerProvider implements IJsonTransformerProvider {

public static final String INITIALIZATION_SCRIPT = "initializationScript";
public static final String INVOCATION_SCRIPT = "invocationScript";
public static final String BINDINGS_PROVIDER = "bindingsProvider";

public final static ObjectMapper mapper = new ObjectMapper();

@SneakyThrows
@Override
public IJsonTransformer createTransformer(Object jsonConfig) {
if (jsonConfig == null || (jsonConfig instanceof String && ((String) jsonConfig).isEmpty())) {
throw new IllegalArgumentException("Configuration must contain 'initializationScript', 'invocationScript', and 'bindingsProvider'.");
} else if (!(jsonConfig instanceof Map)) {
throw new IllegalArgumentException(getConfigUsageStr());
}

var config = (Map<String, Object>) jsonConfig;
String initializationScript = (String) config.get(INITIALIZATION_SCRIPT);
String invocationScript = (String) config.get(INVOCATION_SCRIPT);
Function<Object, Object> bindingsProvider = (Function<Object, Object>) config.get(BINDINGS_PROVIDER);

if (initializationScript == null || invocationScript == null || bindingsProvider == null) {
throw new IllegalArgumentException("'initializationScript', 'invocationScript', and 'bindingsProvider' must be provided.");
}

return new JavascriptTransformer(initializationScript, invocationScript, bindingsProvider);
}

private String getConfigUsageStr() {
return this.getClass().getName() + " expects the incoming configuration to be a Map<String, Object>, " +
"with values '" + INITIALIZATION_SCRIPT + "', '" + INVOCATION_SCRIPT + "', and '" + BINDINGS_PROVIDER + "'.";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.opensearch.migrations.transform.JsonJSTransformerProvider
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ plugins {
dependencies {
implementation project(':transformation:transformationPlugins:jsonMessageTransformers:jsonMessageTransformerInterface')
implementation project(':transformation:transformationPlugins:jsonMessageTransformers:jsonTypeMappingsSanitizationTransformer')
implementation project(':transformation:transformationPlugins:jsonMessageTransformers:jsonJSTransformerProvider')

implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core'
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;

public class TypeMappingSanitizationTransformerProvider implements IJsonTransformerProvider {
public class TypeMappingSanitizationTransformerProvider extends JsonJSTransformerProvider {

public static final String FEATURE_FLAGS = "featureFlags";
public static final String STATIC_MAPPINGS = "staticMappings";
Expand All @@ -21,6 +21,7 @@ public class TypeMappingSanitizationTransformerProvider implements IJsonTransfor

@SneakyThrows
@Override
@SuppressWarnings("unchecked")
public IJsonTransformer createTransformer(Object jsonConfig) {
try {
if ((jsonConfig == null) ||
Expand Down

0 comments on commit 1fe744a

Please sign in to comment.