-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1086 from AndreKurait/JmesPathPreconditions
Implement Transformation JMES Path Predicates
- Loading branch information
Showing
34 changed files
with
528 additions
and
94 deletions.
There are no files selected for viewing
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
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
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
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
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
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
24 changes: 24 additions & 0 deletions
24
...eTransformer/src/main/java/org/opensearch/migrations/transform/JsonJMESPathPredicate.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,24 @@ | ||
package org.opensearch.migrations.transform; | ||
|
||
import java.util.Map; | ||
|
||
import io.burt.jmespath.BaseRuntime; | ||
import io.burt.jmespath.Expression; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class JsonJMESPathPredicate implements IJsonPredicate { | ||
|
||
Expression<Object> expression; | ||
|
||
public JsonJMESPathPredicate(BaseRuntime<Object> runtime, String script) { | ||
this.expression = runtime.compile(script); | ||
} | ||
|
||
@Override | ||
public boolean test(Map<String, Object> incomingJson) { | ||
var output = expression.search(incomingJson); | ||
log.atDebug().setMessage("output={}").addArgument(output).log(); | ||
return (Boolean) output; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ider/src/main/java/org/opensearch/migrations/transform/JsonJMESPathPredicateProvider.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,45 @@ | ||
package org.opensearch.migrations.transform; | ||
|
||
import java.util.Map; | ||
|
||
import io.burt.jmespath.BaseRuntime; | ||
import io.burt.jmespath.jcf.JcfRuntime; | ||
|
||
public class JsonJMESPathPredicateProvider implements IJsonPredicateProvider { | ||
|
||
public static final String SCRIPT_KEY = "script"; | ||
private BaseRuntime<Object> adapterRuntime; | ||
|
||
public JsonJMESPathPredicateProvider() { | ||
this.adapterRuntime = new JcfRuntime(); | ||
} | ||
|
||
@Override | ||
public IJsonPredicate createPredicate(Object jsonConfig) { | ||
try { | ||
if (jsonConfig instanceof Map) { | ||
@SuppressWarnings("unchecked") | ||
var jsonConfigMap = (Map<String, Object>) jsonConfig; | ||
if (jsonConfigMap.size() != 1) { | ||
throw new IllegalArgumentException(getConfigUsageStr()); | ||
} | ||
var scriptValue = jsonConfigMap.get(SCRIPT_KEY); | ||
if (!(scriptValue instanceof String)) { | ||
throw new IllegalArgumentException(getConfigUsageStr()); | ||
} | ||
return new JsonJMESPathPredicate(adapterRuntime, (String) scriptValue); | ||
} | ||
throw new IllegalArgumentException(getConfigUsageStr()); | ||
} catch (ClassCastException e) { | ||
throw new IllegalArgumentException(getConfigUsageStr(), e); | ||
} | ||
} | ||
|
||
private String getConfigUsageStr() { | ||
return this.getClass().getName() | ||
+ " expects the incoming configuration " | ||
+ "to be a Map<String,Object>. " | ||
+ "Each of the Maps should have one key-value of \"script\": \"...\". " | ||
+ "Script values should be a fully-formed inlined JsonPath queries."; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...in/resources/META-INF/services/org.opensearch.migrations.transform.IJsonPredicateProvider
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.JsonJMESPathPredicateProvider |
72 changes: 0 additions & 72 deletions
72
...ansformerProvider/src/test/java/org/opensearch/migrations/replay/JsonTransformerTest.java
This file was deleted.
Oops, something went wrong.
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
7 changes: 7 additions & 0 deletions
7
...ransformerInterface/src/main/java/org/opensearch/migrations/transform/IJsonPredicate.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,7 @@ | ||
package org.opensearch.migrations.transform; | ||
|
||
import java.util.Map; | ||
import java.util.function.Predicate; | ||
|
||
public interface IJsonPredicate extends Predicate<Map<String, Object>> { | ||
} |
23 changes: 23 additions & 0 deletions
23
...erInterface/src/main/java/org/opensearch/migrations/transform/IJsonPredicateProvider.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,23 @@ | ||
package org.opensearch.migrations.transform; | ||
|
||
import lombok.NonNull; | ||
|
||
public interface IJsonPredicateProvider { | ||
/** | ||
* Create a new Predicate from the given configuration. This Predicate | ||
* will be used repeatedly and concurrently from different threads against | ||
* messages. | ||
* @param jsonConfig is a List, Map, String, or null that should be used to configure the | ||
* IJsonPredicate that is being created | ||
* @return | ||
*/ | ||
IJsonPredicate createPredicate(Object jsonConfig); | ||
|
||
/** | ||
* Friendly name that can be used as a key to identify Predicate providers. | ||
* @return | ||
*/ | ||
default @NonNull String getName() { | ||
return this.getClass().getSimpleName(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...terface/src/main/java/org/opensearch/migrations/transform/JsonConditionalTransformer.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,21 @@ | ||
package org.opensearch.migrations.transform; | ||
|
||
import java.util.Map; | ||
|
||
public class JsonConditionalTransformer implements IJsonTransformer { | ||
IJsonPredicate jsonPredicate; | ||
IJsonTransformer jsonTransformer; | ||
|
||
public JsonConditionalTransformer(IJsonPredicate jsonPredicate, IJsonTransformer jsonTransformer) { | ||
this.jsonPredicate = jsonPredicate; | ||
this.jsonTransformer = jsonTransformer; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> transformJson(Map<String, Object> incomingJson) { | ||
if (jsonPredicate.test(incomingJson)) { | ||
return jsonTransformer.transformJson(incomingJson); | ||
} | ||
return incomingJson; | ||
} | ||
} |
Oops, something went wrong.