-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new preserve tag (preserveWhenMissing) to copy only when the ke…
…y wasn't in the target's doc. That makes it a bit easier to write '"preserveWhenMissing": "*"' and not have to spend us much effort to be more specific. Signed-off-by: Greg Schohn <[email protected]>
- Loading branch information
1 parent
95b71f4
commit 2b1f3e0
Showing
5 changed files
with
60 additions
and
25 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
56 changes: 56 additions & 0 deletions
56
...javaTransformer/src/main/java/org/opensearch/migrations/transform/PreservesProcessor.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,56 @@ | ||
package org.opensearch.migrations.transform; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class PreservesProcessor { | ||
private static final String PRESERVE_KEY = "preserve"; | ||
private static final String PRESERVE_WHEN_MISSING_KEY = "preserveWhenMissing"; | ||
|
||
private PreservesProcessor() {} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static Map<String, Object> doFinalSubstitutions(Map<String, Object> incomingJson, Map<String, Object> parsedObj) { | ||
processPreserves(incomingJson, parsedObj); | ||
|
||
processPreserves( | ||
(Map<String, Object>) incomingJson.get(JsonKeysForHttpMessage.PAYLOAD_KEY), | ||
(Map<String, Object>) parsedObj.get(JsonKeysForHttpMessage.PAYLOAD_KEY) | ||
); | ||
|
||
return parsedObj; | ||
} | ||
|
||
private static void processPreserves(Map<String, Object> source, Map<String, Object> target) { | ||
if (target == null || source == null) { | ||
return; | ||
} | ||
|
||
copyValues(source, target, PRESERVE_KEY, true); | ||
copyValues(source, target, PRESERVE_WHEN_MISSING_KEY, false); | ||
} | ||
|
||
private static void copyValues(Map<String, Object> source, Map<String, Object> target, | ||
String directiveKey, boolean forced) { | ||
Object directive = target.remove(directiveKey); | ||
if (directive == null) { | ||
return; | ||
} | ||
|
||
if (directive.equals("*")) { | ||
source.forEach((key, value) -> { | ||
if (forced || !target.containsKey(key)) { | ||
target.put(key, value); | ||
} | ||
}); | ||
} else if (directive instanceof List) { | ||
((List<String>) directive).forEach(key -> { | ||
if (source.containsKey(key) && | ||
(forced || !target.containsKey(key))) | ||
{ | ||
target.put(key, source.get(key)); | ||
} | ||
}); | ||
} | ||
} | ||
} |
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