Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime-v2: automatically convert non serializable map.entry to serializable in expr #815

Merged
merged 3 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}"
Loading