Skip to content

Commit ddb7733

Browse files
committed
runtime-v2: hasFlow function
1 parent 48e852d commit ddb7733

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

runtime/v2/runner/src/main/java/com/walmartlabs/concord/runtime/v2/runner/el/LazyExpressionEvaluator.java

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ private static FunctionMapper createFunctionMapper() {
209209
functions.put("evalAsMap", EvalAsMapFunction.getMethod());
210210
functions.put("isDebug", IsDebugFunction.getMethod());
211211
functions.put("throw", ThrowFunction.getMethod());
212+
functions.put("hasFlow", HasFlowFunction.getMethod());
212213
return new FunctionMapper(functions);
213214
}
214215

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.walmartlabs.concord.runtime.v2.runner.el.functions;
2+
3+
/*-
4+
* *****
5+
* Concord
6+
* -----
7+
* Copyright (C) 2017 - 2023 Walmart Inc.
8+
* -----
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* =====
21+
*/
22+
23+
import com.walmartlabs.concord.runtime.v2.model.ProcessDefinition;
24+
import com.walmartlabs.concord.runtime.v2.model.Profile;
25+
import com.walmartlabs.concord.runtime.v2.runner.el.ThreadLocalEvalContext;
26+
import com.walmartlabs.concord.runtime.v2.sdk.Context;
27+
28+
import java.lang.reflect.Method;
29+
30+
public final class HasFlowFunction {
31+
32+
public static Method getMethod() {
33+
try {
34+
return HasFlowFunction.class.getMethod("hasFlow", String.class);
35+
} catch (Exception e) {
36+
throw new RuntimeException("Method not found");
37+
}
38+
}
39+
40+
public static boolean hasFlow(String name) {
41+
if (name == null || name.trim().isEmpty()) {
42+
return false;
43+
}
44+
45+
Context ctx = ThreadLocalEvalContext.get().context();
46+
if (ctx == null) {
47+
return false;
48+
}
49+
50+
ProcessDefinition pd = ctx.execution().processDefinition();
51+
if (pd.flows().containsKey(name)) {
52+
return true;
53+
}
54+
55+
for (String activeProfile : ctx.processConfiguration().processInfo().activeProfiles()) {
56+
boolean fromProfile = pd.profiles().getOrDefault(activeProfile, Profile.builder().build()).flows().containsKey(name);
57+
if (fromProfile) {
58+
return true;
59+
}
60+
}
61+
return false;
62+
}
63+
}

runtime/v2/runner/src/test/java/com/walmartlabs/concord/runtime/v2/runner/MainTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,18 @@ public void testArrayEvalSerialize() throws Exception {
15301530
assertLog(log, ".*" + Pattern.quote("{dev=dev-cloud1}, {prod=prod-cloud1}, {test=test-cloud1}, {perf=perf-cloud2}, {ci=perf-ci}") + ".*");
15311531
}
15321532

1533+
@Test
1534+
public void testHasFlow() throws Exception {
1535+
deploy("hasFlow");
1536+
1537+
save(ProcessConfiguration.builder()
1538+
.build());
1539+
1540+
byte[] log = run();
1541+
assertLog(log, ".*123: false.*");
1542+
assertLog(log, ".*myFlow: true.*");
1543+
}
1544+
15331545
private void deploy(String resource) throws URISyntaxException, IOException {
15341546
Path src = Paths.get(MainTest.class.getResource(resource).toURI());
15351547
IOUtils.copy(src, workDir);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
flows:
2+
default:
3+
- log: "123: ${hasFlow('123')}"
4+
- log: "myFlow: ${hasFlow('myFlow')}"
5+
6+
myFlow:
7+
- log: "my"

0 commit comments

Comments
 (0)