Skip to content

Commit

Permalink
runtime-v2: automatically convert nonserializable map.entry to seriza…
Browse files Browse the repository at this point in the history
…lizable
  • Loading branch information
brig committed Oct 2, 2023
1 parent 48e852d commit a9782e6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,34 @@
* =====
*/

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

public final class VariablesSanitizer {

@SuppressWarnings("unchecked")
public static Object sanitize(Object scriptObj) {
if (scriptObj instanceof Set) {
Set<Object> c = (Set<Object>) scriptObj;
public static Object sanitize(Object obj) {
if (obj instanceof Set) {
Set<Object> c = (Set<Object>) obj;
return c.stream()
.map(VariablesSanitizer::sanitize)
.collect(Collectors.toSet());
} else if (scriptObj instanceof Collection) {
Collection<Object> c = (Collection<Object>) scriptObj;
} else if (obj instanceof Collection) {
Collection<Object> c = (Collection<Object>) obj;
return c.stream()
.map(VariablesSanitizer::sanitize)
.collect(Collectors.toList());
} else if (scriptObj instanceof Map) {
Map<Object, Object> m = (Map<Object, Object>) scriptObj;
} else if (obj instanceof Map) {
Map<Object, Object> m = (Map<Object, Object>) obj;
Map<Object, Object> result = new LinkedHashMap<>();
m.forEach((key, value) -> result.put(sanitize(key), sanitize(value)));
return result;
} else if (obj instanceof Map.Entry && !(obj instanceof Serializable)) {
Map.Entry<?, ?> e = (Map.Entry<?, ?>) obj;
return new AbstractMap.SimpleEntry<>(e.getKey(), e.getValue());
} else {
return scriptObj;
return obj;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.walmartlabs.concord.runtime.v2.model.Expression;
import com.walmartlabs.concord.runtime.v2.model.ExpressionOptions;
import com.walmartlabs.concord.runtime.v2.runner.script.VariablesSanitizer;
import com.walmartlabs.concord.runtime.v2.sdk.EvalContextFactory;
import com.walmartlabs.concord.runtime.v2.sdk.ExpressionEvaluator;
import com.walmartlabs.concord.runtime.v2.sdk.Context;
Expand Down Expand Up @@ -58,6 +59,7 @@ protected void execute(Runtime runtime, State state, ThreadId threadId) {
EvalContextFactory ecf = runtime.getService(EvalContextFactory.class);
ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
Object result = ee.eval(ecf.global(ctx), expr, Object.class);
result = VariablesSanitizer.sanitize(result);

ExpressionOptions opts = Objects.requireNonNull(step.getOptions());
if (!opts.outExpr().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,17 @@ public void testArrayEvalSerialize() throws Exception {
assertLog(log, ".*" + Pattern.quote("{dev=dev-cloud1}, {prod=prod-cloud1}, {test=test-cloud1}, {perf=perf-cloud2}, {ci=perf-ci}") + ".*");
}

@Test
public void testEntrySetSerialization() throws Exception {
deploy("entrySetSerialization");

save(ProcessConfiguration.builder()
.build());

byte[] log = run();
assertLog(log, ".*myList: \\[k=v\\].*");
}

private void deploy(String resource) throws URISyntaxException, IOException {
Path src = Paths.get(MainTest.class.getResource(resource).toURI());
IOUtils.copy(src, workDir);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
flows:
default:
- set:
myMap:
k: v

- expr: ${myMap.entrySet().stream().toList()}
out: myList

- log: "myList: ${myList}"

0 comments on commit a9782e6

Please sign in to comment.