Skip to content

Commit 7077458

Browse files
committed
grabber up
1 parent 26de632 commit 7077458

File tree

7 files changed

+178
-32
lines changed

7 files changed

+178
-32
lines changed

plugins/tasks/input-params-assert/src/main/java/com/walmartlabs/concord/plugins/input/parser/CommentParser.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@
2424
import com.fasterxml.jackson.databind.ObjectMapper;
2525
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
2626

27-
import java.util.Collections;
28-
import java.util.List;
29-
import java.util.Map;
30-
import java.util.Set;
27+
import java.util.*;
3128

3229
public class CommentParser {
3330

@@ -36,7 +33,7 @@ public class CommentParser {
3633

3734
private static final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
3835

39-
private static final Set<String> keys = Set.of("in:", "out:");
36+
private static final Set<String> keys = new HashSet<>(Arrays.asList("in:", "out:"));
4037

4138
@SuppressWarnings("unchecked")
4239
public FlowDefinition parse(List<String> commentLines) {
@@ -82,7 +79,7 @@ public Map<String, Object> parseFields(List<String> commentLines) {
8279
}
8380
}
8481

85-
StringBuilder inOut = new StringBuilder();
82+
StringBuilder inOut = new StringBuilder();
8683
for (int i = keyIndex; i < commentLines.size(); i++) {
8784
String line = trimStartChar(commentLines.get(i).trim(), '#');
8885
inOut.append(line).append('\n');

plugins/tasks/input-params-assert/src/main/java/com/walmartlabs/concord/plugins/input/parser/CommentsGrabber.java

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,45 @@ public class CommentsGrabber {
3434
public Map<String, List<String>> grab(Path file) {
3535
Map<String, List<String>> result = new LinkedHashMap<>();
3636
try (BufferedReader reader = Files.newBufferedReader(file)) {
37-
int lineNum = findKey(reader, "flows:");
38-
if (lineNum == -1) {
37+
String flowsLine = findKey(reader, "flows:");
38+
if (flowsLine == null) {
3939
return result;
4040
}
4141

42+
int flowsIndent = getIndentLevel(flowsLine);
43+
44+
int flowNameIndent = -1;
4245
List<String> lines = new ArrayList<>();
4346
String line;
4447
while ((line = reader.readLine()) != null) {
45-
if (isNewYamlBlock(line)) {
46-
break;
47-
} else if (line.trim().startsWith("#")) {
48+
if (line.trim().isEmpty()) {
49+
continue;
50+
}
51+
52+
if (line.trim().startsWith("#")) {
4853
lines.add(line);
49-
} else if (isFlowName(line)) {
54+
continue;
55+
}
56+
57+
int lineIndentLevel = getIndentLevel(line);
58+
if (lineIndentLevel <= flowsIndent) {
59+
// another tag (e.g. profiles ...)
60+
break;
61+
}
62+
63+
if (flowNameIndent < 0) {
64+
flowNameIndent = lineIndentLevel;
65+
}
66+
67+
if (flowNameIndent == lineIndentLevel) {
5068
if (!lines.isEmpty()) {
5169
line = line.trim();
5270
String flowName = line.substring(0, line.length() - 1);
5371
result.put(flowName, new ArrayList<>(lines));
5472
lines.clear();
5573
}
74+
} else {
75+
lines.clear();
5676
}
5777
}
5878

@@ -62,31 +82,23 @@ public Map<String, List<String>> grab(Path file) {
6282
}
6383
}
6484

65-
private static int findKey(BufferedReader reader, String key) throws IOException {
85+
private static String findKey(BufferedReader reader, String key) throws IOException {
6686
String line;
67-
int lineNum = 0;
6887
while ((line = reader.readLine()) != null) {
69-
lineNum++;
7088
if (key.equals(line.trim())) {
71-
return lineNum;
89+
return line;
7290
}
7391
}
74-
return -1;
92+
return null;
7593
}
7694

77-
private static boolean isNewYamlBlock(String line) {
78-
if (line.trim().isEmpty() || line.trim().startsWith("#")) {
79-
return false;
80-
}
81-
82-
return !Character.isWhitespace(line.charAt(0));
83-
}
95+
private static int getIndentLevel(String line) {
96+
int count = 0;
8497

85-
private static boolean isFlowName(String line) {
86-
if (line.trim().isEmpty() || line.trim().startsWith("#")) {
87-
return false;
98+
while (count < line.length() && Character.isWhitespace(line.charAt(count))) {
99+
count++;
88100
}
89101

90-
return Character.isWhitespace(line.charAt(0));
102+
return count;
91103
}
92104
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.walmartlabs.concord.plugins.input.parser;
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.plugins.input.FlowCallSchemaGeneratorTest;
24+
import org.junit.jupiter.api.Test;
25+
26+
import java.net.URL;
27+
import java.nio.file.Path;
28+
import java.nio.file.Paths;
29+
import java.util.List;
30+
import java.util.Map;
31+
32+
import static org.junit.jupiter.api.Assertions.*;
33+
34+
public class CommentsGrabberTest {
35+
36+
@Test
37+
public void test1() throws Exception {
38+
Map<String, List<String>> result = new CommentsGrabber().grab(readResource("grabber/001.concord.yaml"));
39+
assertTrue(result.isEmpty());
40+
}
41+
42+
@Test
43+
public void test2() throws Exception {
44+
Map<String, List<String>> result = new CommentsGrabber().grab(readResource("grabber/002.concord.yaml"));
45+
46+
assertEquals(1, result.size());
47+
assertNotNull(result.get("test"));
48+
assertEquals(4, result.get("test").size());
49+
}
50+
51+
@Test
52+
public void test3() throws Exception {
53+
Map<String, List<String>> result = new CommentsGrabber().grab(readResource("grabber/003.concord.yaml"));
54+
55+
assertEquals(1, result.size());
56+
assertNotNull(result.get("test"));
57+
assertEquals(5, result.get("test").size());
58+
}
59+
60+
private static Path readResource(String name) throws Exception {
61+
URL url = FlowCallSchemaGeneratorTest.class.getResource(name);
62+
if (url == null) {
63+
throw new RuntimeException("Resource '" + name + "' not found");
64+
}
65+
66+
return Paths.get(url.toURI());
67+
}
68+
}

plugins/tasks/input-params-assert/src/test/java/com/walmartlabs/concord/plugins/input/parser/ParamDefinitionParserTest.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
2626
import org.junit.jupiter.api.Test;
2727

28+
import java.util.Collections;
29+
import java.util.HashMap;
2830
import java.util.Map;
2931

3032
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -42,7 +44,7 @@ public void testParse_withSingleSimpleParam() {
4244

4345
Map<String, ParamDefinition> result = ParamDefinitionParser.parse(toMap(input));
4446

45-
assertEquals(Map.of("stringParam", expected), result);
47+
assertEquals(Collections.singletonMap("stringParam", expected), result);
4648
}
4749

4850
@Test
@@ -65,7 +67,11 @@ public void testParse_withTwoSimpleParam() {
6567

6668
Map<String, ParamDefinition> result = ParamDefinitionParser.parse(toMap(input));
6769

68-
assertEquals(Map.of("stringParam", expectedString, "booleanParam", expectedBoolean), result);
70+
Map<String, Object> expected = new HashMap<>();
71+
expected.put("stringParam", expectedString);
72+
expected.put("booleanParam", expectedBoolean);
73+
74+
assertEquals(expected, result);
6975
}
7076

7177
@Test
@@ -87,7 +93,11 @@ public void testParse_withComplexParam() {
8793

8894
Map<String, ParamDefinition> result = ParamDefinitionParser.parse(toMap(input));
8995

90-
assertEquals(Map.of("objectParam", expected, "objectParam.stringParam", expectedValue), result);
96+
Map<String, Object> expectedResult = new HashMap<>();
97+
expectedResult.put("objectParam", expected);
98+
expectedResult.put("objectParam.stringParam", expectedValue);
99+
100+
assertEquals(expectedResult, result);
91101
}
92102

93103
@Test
@@ -109,7 +119,11 @@ public void testParse_withArrayType() {
109119

110120
Map<String, ParamDefinition> result = ParamDefinitionParser.parse(toMap(input));
111121

112-
assertEquals(Map.of("arrayParam", expected, "arrayParam.stringParam", expectedValue), result);
122+
Map<String, Object> expectedResult = new HashMap<>();
123+
expectedResult.put("arrayParam", expected);
124+
expectedResult.put("arrayParam.stringParam", expectedValue);
125+
126+
assertEquals(expectedResult, result);
113127
}
114128

115129
private static final TypeReference<Map<String, String>> MAP_TYPE = new TypeReference<Map<String, String>>() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
configuration:
2+
runtime: concord-v2
3+
dependencies:
4+
- "mvn://com.walmartlabs.concord.plugins.basic:input-params-assert:1.102.1-SNAPSHOT"
5+
arguments:
6+
param1: "global"
7+
8+
flows:
9+
default:
10+
- call: test
11+
in:
12+
# param2: true
13+
additionalParam: "value"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
configuration:
2+
runtime: concord-v2
3+
dependencies:
4+
- "mvn://com.walmartlabs.concord.plugins.basic:input-params-assert:1.102.1-SNAPSHOT"
5+
arguments:
6+
param1: "global"
7+
8+
flows:
9+
default:
10+
- call: test
11+
in:
12+
param2: true
13+
additionalParam: "value"
14+
15+
##
16+
# in:
17+
# param1: string, optional, boo
18+
##
19+
test:
20+
- log: "me"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
configuration:
2+
runtime: concord-v2
3+
debug: true
4+
dependencies:
5+
- "mvn://com.walmartlabs.concord.plugins.basic:input-params-assert:1.102.1-SNAPSHOT"
6+
arguments:
7+
param1: "global"
8+
9+
flows:
10+
default:
11+
- call: test
12+
in:
13+
# param2: true
14+
additionalParam: "value"
15+
16+
##
17+
# in:
18+
# param1: string, mandatory, param1 description
19+
# param2: boolean, mandatory, param2 description
20+
##
21+
test:
22+
- log: "in test"

0 commit comments

Comments
 (0)