-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create JsonJSTransformerProvider for custom scripts
Signed-off-by: Andre Kurait <[email protected]>
- Loading branch information
1 parent
7e12bac
commit 1fe744a
Showing
5 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
...tion/transformationPlugins/jsonMessageTransformers/jsonJSTransformerProvider/build.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
42 changes: 42 additions & 0 deletions
42
...Provider/src/main/java/org/opensearch/migrations/transform/JsonJSTransformerProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "'."; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
.../resources/META-INF/services/org.opensearch.migrations.transform.IJsonTransformerProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.opensearch.migrations.transform.JsonJSTransformerProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters