use of com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator in project concord by walmartlabs.
the class LoopWrapper method eval.
@Override
@SuppressWarnings("unchecked")
public void eval(Runtime runtime, State state, ThreadId threadId) {
Frame frame = state.peekFrame(threadId);
frame.pop();
Serializable value = items;
if (value == null) {
// value is null, not going to run the wrapped command at all
return;
}
Step currentStep = null;
if (cmd instanceof StepCommand) {
currentStep = ((StepCommand<?>) cmd).getStep();
}
// create the context explicitly
ContextFactory contextFactory = runtime.getService(ContextFactory.class);
Context ctx = contextFactory.create(runtime, state, threadId, currentStep);
ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
value = ee.eval(EvalContextFactory.global(ctx), value, Serializable.class);
// prepare items
// store items in an ArrayList because it is Serializable
ArrayList<Serializable> items;
if (value == null) {
// value is null, not going to run the wrapped command at all
return;
} else if (value instanceof Collection) {
Collection<Serializable> v = (Collection<Serializable>) value;
if (v.isEmpty()) {
// no items, nothing to do
return;
}
items = new ArrayList<>(v);
} else if (value instanceof Map) {
Map<Serializable, Serializable> m = (Map<Serializable, Serializable>) value;
items = m.entrySet().stream().map(e -> new AbstractMap.SimpleImmutableEntry<>(e.getKey(), e.getValue())).collect(Collectors.toCollection(ArrayList::new));
} else if (value.getClass().isArray()) {
items = new ArrayList<>(Arrays.asList((Serializable[]) value));
} else {
throw new IllegalArgumentException("'withItems' accepts only Lists of items, Java Maps or arrays of values. Got: " + value.getClass());
}
items.forEach(LoopWrapper::assertItem);
if (items.isEmpty()) {
return;
}
eval(state, threadId, items);
}
use of com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator in project concord by walmartlabs.
the class RetryWrapper method eval.
@Override
public void eval(Runtime runtime, State state, ThreadId threadId) {
Frame frame = state.peekFrame(threadId);
frame.pop();
// wrap the command into a frame with an exception handler
Frame inner = Frame.builder().nonRoot().exceptionHandler(new NextRetry(cmd)).commands(cmd).build();
// create the context explicitly
ContextFactory contextFactory = runtime.getService(ContextFactory.class);
Context ctx = contextFactory.create(runtime, state, threadId, getCurrentStep());
ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
int times = retry.times();
if (retry.timesExpression() != null) {
Number n = ee.eval(EvalContextFactory.global(ctx), retry.timesExpression(), Number.class);
if (n != null) {
times = n.intValue();
}
}
Duration delay = retry.delay();
if (retry.delayExpression() != null) {
Number n = ee.eval(EvalContextFactory.global(ctx), retry.delayExpression(), Number.class);
if (n != null) {
delay = Duration.ofSeconds(n.longValue());
}
}
inner.setLocal(Constants.Runtime.RETRY_ATTEMPT_NUMBER, 0);
inner.setLocal(RETRY_CFG, Retry.builder().from(retry).times(times).delay(delay).build());
state.pushFrame(threadId, inner);
}
use of com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator in project concord by walmartlabs.
the class ScriptCallCommand method execute.
@Override
protected void execute(Runtime runtime, State state, ThreadId threadId) {
state.peekFrame(threadId).pop();
Context ctx = runtime.getService(Context.class);
ExpressionEvaluator expressionEvaluator = runtime.getService(ExpressionEvaluator.class);
ScriptEvaluator scriptEvaluator = runtime.getService(ScriptEvaluator.class);
ResourceResolver resourceResolver = runtime.getService(ResourceResolver.class);
ScriptCall call = getStep();
ScriptCallOptions opts = Objects.requireNonNull(call.getOptions());
Map<String, Object> input = VMUtils.prepareInput(expressionEvaluator, ctx, opts.input(), opts.inputExpression());
String language = getLanguage(expressionEvaluator, scriptEvaluator, ctx, call);
Reader content = getContent(expressionEvaluator, resourceResolver, ctx, call);
ScriptResult scriptResult;
try {
scriptResult = scriptEvaluator.eval(ctx, language, content, input);
} finally {
try {
content.close();
} catch (IOException e) {
// we don't have to do anything about it, but we're going to log the error just in case
log.warn("Error while closing the script's reader: {}", e.getMessage() + ". This is most likely a bug.");
}
}
OutputUtils.process(runtime, ctx, scriptResult.items(), opts.out(), opts.outExpr());
}
use of com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator in project concord by walmartlabs.
the class SetVariablesCommand method execute.
@Override
@SuppressWarnings("unchecked")
protected void execute(Runtime runtime, State state, ThreadId threadId) {
state.peekFrame(threadId).pop();
SetVariablesStep step = getStep();
Context ctx = runtime.getService(Context.class);
// eval the input
ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
Map<String, Object> vars = ee.evalAsMap(EvalContextFactory.scope(ctx), step.getVars());
vars.forEach((k, v) -> {
Object o = ctx.variables().get(k);
if (o instanceof Map && v instanceof Map) {
v = deepMerge((Map<String, Object>) o, (Map<String, Object>) v);
}
ctx.variables().set(k, v);
});
}
use of com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator in project concord by walmartlabs.
the class EvalAsMapFunction method evalAsMap.
@SuppressWarnings("unchecked")
public static Map<String, Object> evalAsMap(Object value) {
Context ctx = ThreadLocalEvalContext.get().context();
if (ctx == null) {
return null;
}
ExpressionEvaluator ee = ctx.execution().runtime().getService(ExpressionEvaluator.class);
Map<String, Object> evalValue = ee.evalAsMap(EvalContextFactory.scope(ctx), value);
Map<String, Object> result = new HashMap<>();
evalValue.forEach((k, v) -> {
Object o = ctx.variables().get(k);
if (o instanceof Map && v instanceof Map) {
v = deepMerge((Map<String, Object>) o, (Map<String, Object>) v);
}
if (v != null) {
result.put(k, v);
}
});
return result;
}
Aggregations