use of com.walmartlabs.concord.runtime.v2.model.SwitchStep in project concord by walmartlabs.
the class SwitchCompiler method compile.
@Override
public Command compile(CompilerContext context, SwitchStep step) {
List<Map.Entry<String, Command>> caseCommands = new ArrayList<>();
for (Map.Entry<String, List<Step>> kv : step.getCaseSteps()) {
caseCommands.add(new AbstractMap.SimpleEntry<>(kv.getKey(), compile(context, kv.getValue())));
}
Command defaultCommand = compile(context, step.getDefaultSteps());
return new SwitchCommand(step, caseCommands, defaultCommand);
}
use of com.walmartlabs.concord.runtime.v2.model.SwitchStep in project concord by walmartlabs.
the class SwitchCommand method execute.
@Override
protected void execute(Runtime runtime, State state, ThreadId threadId) {
Frame frame = state.peekFrame(threadId);
frame.pop();
SwitchStep step = getStep();
String expr = step.getExpression();
Context ctx = runtime.getService(Context.class);
EvalContext evalContext = EvalContextFactory.global(ctx);
ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
String switchResult = ee.eval(evalContext, expr, String.class);
boolean caseFound = false;
for (Map.Entry<String, Command> kv : caseCommands) {
String caseLabel = ee.eval(evalContext, kv.getKey(), String.class);
if (Objects.equals(switchResult, caseLabel)) {
frame.push(kv.getValue());
caseFound = true;
break;
}
}
if (!caseFound && defaultCommand != null) {
frame.push(defaultCommand);
}
// TODO: log case not found?
}
Aggregations