diff --git a/runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/el/LazyExpressionEvaluator.java b/runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/el/LazyExpressionEvaluator.java index 3cc7887674..d5d370a958 100644 --- a/runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/el/LazyExpressionEvaluator.java +++ b/runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/el/LazyExpressionEvaluator.java @@ -209,6 +209,7 @@ private static FunctionMapper createFunctionMapper() { functions.put("evalAsMap", EvalAsMapFunction.getMethod()); functions.put("isDebug", IsDebugFunction.getMethod()); functions.put("throw", ThrowFunction.getMethod()); + functions.put("hasFlow", HasFlowFunction.getMethod()); functions.put("uuid", UuidFunction.getMethod()); return new FunctionMapper(functions); } diff --git a/runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/el/functions/HasFlowFunction.java b/runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/el/functions/HasFlowFunction.java new file mode 100644 index 0000000000..b261c3b9f3 --- /dev/null +++ b/runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/el/functions/HasFlowFunction.java @@ -0,0 +1,63 @@ +package com.walmartlabs.concord.runtime.v2.runner.el.functions; + +/*- + * ***** + * Concord + * ----- + * Copyright (C) 2017 - 2023 Walmart Inc. + * ----- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ===== + */ + +import com.walmartlabs.concord.runtime.v2.model.ProcessDefinition; +import com.walmartlabs.concord.runtime.v2.model.Profile; +import com.walmartlabs.concord.runtime.v2.runner.el.ThreadLocalEvalContext; +import com.walmartlabs.concord.runtime.v2.sdk.Context; + +import java.lang.reflect.Method; + +public final class HasFlowFunction { + + public static Method getMethod() { + try { + return HasFlowFunction.class.getMethod("hasFlow", String.class); + } catch (Exception e) { + throw new RuntimeException("Method not found"); + } + } + + public static boolean hasFlow(String name) { + if (name == null || name.trim().isEmpty()) { + return false; + } + + Context ctx = ThreadLocalEvalContext.get().context(); + if (ctx == null) { + return false; + } + + ProcessDefinition pd = ctx.execution().processDefinition(); + if (pd.flows().containsKey(name)) { + return true; + } + + for (String activeProfile : ctx.processConfiguration().processInfo().activeProfiles()) { + boolean fromProfile = pd.profiles().getOrDefault(activeProfile, Profile.builder().build()).flows().containsKey(name); + if (fromProfile) { + return true; + } + } + return false; + } +} diff --git a/runtime/v2/runner/src/test/java/com/walmartlabs/concord/runtime/v2/runner/MainTest.java b/runtime/v2/runner/src/test/java/com/walmartlabs/concord/runtime/v2/runner/MainTest.java index c780f857f3..2bc2d5ed03 100644 --- a/runtime/v2/runner/src/test/java/com/walmartlabs/concord/runtime/v2/runner/MainTest.java +++ b/runtime/v2/runner/src/test/java/com/walmartlabs/concord/runtime/v2/runner/MainTest.java @@ -1530,6 +1530,18 @@ 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 testHasFlow() throws Exception { + deploy("hasFlow"); + + save(ProcessConfiguration.builder() + .build()); + + byte[] log = run(); + assertLog(log, ".*123: false.*"); + assertLog(log, ".*myFlow: true.*"); + } + @Test public void testUuidFunc() throws Exception { deploy("uuid"); diff --git a/runtime/v2/runner/src/test/resources/com/walmartlabs/concord/runtime/v2/runner/hasFlow/concord.yml b/runtime/v2/runner/src/test/resources/com/walmartlabs/concord/runtime/v2/runner/hasFlow/concord.yml new file mode 100644 index 0000000000..4643deed57 --- /dev/null +++ b/runtime/v2/runner/src/test/resources/com/walmartlabs/concord/runtime/v2/runner/hasFlow/concord.yml @@ -0,0 +1,7 @@ +flows: + default: + - log: "123: ${hasFlow('123')}" + - log: "myFlow: ${hasFlow('myFlow')}" + + myFlow: + - log: "my" \ No newline at end of file