Search in sources :

Example 1 with WithItems

use of com.walmartlabs.concord.runtime.v2.model.WithItems 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);
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) CompilerContext(com.walmartlabs.concord.runtime.v2.runner.compiler.CompilerContext) Serializable(java.io.Serializable) Step(com.walmartlabs.concord.runtime.v2.model.Step) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator) ContextFactory(com.walmartlabs.concord.runtime.v2.runner.context.ContextFactory) EvalContextFactory(com.walmartlabs.concord.runtime.v2.runner.el.EvalContextFactory)

Example 2 with WithItems

use of com.walmartlabs.concord.runtime.v2.model.WithItems in project concord by walmartlabs.

the class FlowCallStepSerializer method serializeOptions.

private static void serializeOptions(FlowCallOptions options, JsonGenerator gen) throws IOException {
    if (options == null) {
        return;
    }
    writeNotEmptyObjectField("in", options.input(), gen);
    writeNotEmptyObjectField("in", options.inputExpression(), gen);
    writeNotEmptyObjectField("out", options.out(), gen);
    writeNotEmptyObjectField("out", options.outExpr(), gen);
    if (options.withItems() != null) {
        WithItems items = Objects.requireNonNull(options.withItems());
        SerializerUtils.writeWithItems(items, gen);
    }
    writeLoop(options.loop(), gen);
    if (options.retry() != null) {
        gen.writeObjectField("retry", options.retry());
    }
    writeNotEmptyObjectField("error", options.errorSteps(), gen);
    writeNotEmptyObjectField("meta", options.meta(), gen);
}
Also used : WithItems(com.walmartlabs.concord.runtime.v2.model.WithItems)

Example 3 with WithItems

use of com.walmartlabs.concord.runtime.v2.model.WithItems in project concord by walmartlabs.

the class ScriptCallStepSerializer method serializeOptions.

private static void serializeOptions(ScriptCallOptions options, JsonGenerator gen) throws IOException {
    if (options == null) {
        return;
    }
    if (options.body() != null) {
        gen.writeObjectField("body", options.body());
    }
    writeNotEmptyObjectField("in", options.input(), gen);
    writeNotEmptyObjectField("in", options.inputExpression(), gen);
    writeNotEmptyObjectField("out", options.out(), gen);
    writeNotEmptyObjectField("out", options.outExpr(), gen);
    if (options.withItems() != null) {
        WithItems items = Objects.requireNonNull(options.withItems());
        writeWithItems(items, gen);
    }
    writeLoop(options.loop(), gen);
    if (options.retry() != null) {
        gen.writeObjectField("retry", options.retry());
    }
    writeNotEmptyObjectField("error", options.errorSteps(), gen);
    writeNotEmptyObjectField("meta", options.meta(), gen);
}
Also used : WithItems(com.walmartlabs.concord.runtime.v2.model.WithItems)

Example 4 with WithItems

use of com.walmartlabs.concord.runtime.v2.model.WithItems in project concord by walmartlabs.

the class GroupOfStepsCompiler method compile.

@Override
public Command compile(CompilerContext context, GroupOfSteps step) {
    Command cmd = compile(context, step.getSteps());
    GroupOfStepsOptions options = Objects.requireNonNull(step.getOptions());
    WithItems withItems = options.withItems();
    if (withItems != null) {
        return WithItemsWrapper.of(cmd, withItems, options.out(), Collections.emptyMap());
    }
    Loop loop = options.loop();
    if (loop != null) {
        cmd = LoopWrapper.of(context, cmd, loop, options.out(), Collections.emptyMap());
    }
    List<Step> errorSteps = options.errorSteps();
    if (!options.errorSteps().isEmpty()) {
        cmd = new ErrorWrapper(cmd, compile(context, errorSteps));
    }
    return cmd;
}
Also used : BlockCommand(com.walmartlabs.concord.runtime.v2.runner.vm.BlockCommand) Command(com.walmartlabs.concord.svm.Command) ErrorWrapper(com.walmartlabs.concord.runtime.v2.runner.vm.ErrorWrapper)

Example 5 with WithItems

use of com.walmartlabs.concord.runtime.v2.model.WithItems in project concord by walmartlabs.

the class WithItemsWrapper method eval.

@Override
@SuppressWarnings("unchecked")
public void eval(Runtime runtime, State state, ThreadId threadId) {
    Frame frame = state.peekFrame(threadId);
    frame.pop();
    Serializable value = withItems.value();
    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(WithItemsWrapper::assertItem);
    if (items.isEmpty()) {
        return;
    }
    eval(state, threadId, items);
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) Serializable(java.io.Serializable) Step(com.walmartlabs.concord.runtime.v2.model.Step) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator) ContextFactory(com.walmartlabs.concord.runtime.v2.runner.context.ContextFactory) EvalContextFactory(com.walmartlabs.concord.runtime.v2.runner.el.EvalContextFactory)

Aggregations

WithItems (com.walmartlabs.concord.runtime.v2.model.WithItems)3 Step (com.walmartlabs.concord.runtime.v2.model.Step)2 ContextFactory (com.walmartlabs.concord.runtime.v2.runner.context.ContextFactory)2 EvalContextFactory (com.walmartlabs.concord.runtime.v2.runner.el.EvalContextFactory)2 ExpressionEvaluator (com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)2 Context (com.walmartlabs.concord.runtime.v2.sdk.Context)2 Serializable (java.io.Serializable)2 TaskCallOptions (com.walmartlabs.concord.runtime.v2.model.TaskCallOptions)1 CompilerContext (com.walmartlabs.concord.runtime.v2.runner.compiler.CompilerContext)1 BlockCommand (com.walmartlabs.concord.runtime.v2.runner.vm.BlockCommand)1 ErrorWrapper (com.walmartlabs.concord.runtime.v2.runner.vm.ErrorWrapper)1 Command (com.walmartlabs.concord.svm.Command)1