Skip to content

Commit 8cda37a

Browse files
author
Yunling Tao
committed
add splitByElement() to parse nested lists or sets
1 parent 8a40642 commit 8cda37a

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ This release also includes changes from <<release-3-7-XXX, 3.7.XXX>>.
107107
* Updated `asString()` step to throw `IllegalArgumentException` with `null` inputs for casting step consistency
108108
* Renamed `MergeElementStep` to `MergeElementStep` as it is a base class to `mergeV()` and `mergeE()`.
109109
* Renamed `MergeStep` of `merge()` to `MergeElementStep` for consistency.
110+
* Added `splitByElement()` in `gremlin-test` for parsing nested list and set
110111
111112
== TinkerPop 3.7.0 (Gremfir Master of the Pan Flute)
112113

gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/features/StepDefinition.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public final class StepDefinition {
184184
private List<Pair<Pattern, Function<String,Object>>> objectMatcherConverters = new ArrayList<Pair<Pattern, Function<String,Object>>>() {{
185185
// expects json so that should port to the Gremlin script form - replace curly json braces with square ones
186186
// for Gremlin sake.
187-
add(Pair.with(Pattern.compile("m\\[(.*)\\]"), s -> {
187+
add(Pair.with(Pattern.compile("^m\\[(.*)\\]$"), s -> {
188188
try {
189189
// read tree from JSON - can't parse right to Map as each m[] level needs to be managed individually
190190
return convertToObject(mapper.readTree(s));
@@ -193,16 +193,14 @@ public final class StepDefinition {
193193
}
194194
}));
195195

196-
add(Pair.with(Pattern.compile("l\\[\\]"), s -> Collections.emptyList()));
197-
add(Pair.with(Pattern.compile("l\\[(.*)\\]"), s -> {
198-
final String[] items = s.split(",");
199-
return Stream.of(items).map(String::trim).map(x -> convertToObject(x)).collect(Collectors.toList());
196+
add(Pair.with(Pattern.compile("^l\\[\\]$"), s -> new ArrayList<>()));
197+
add(Pair.with(Pattern.compile("^l\\[(.*)\\]$"), s -> {
198+
return splitByElement(s).stream().map(x -> convertToObject(x)).collect(Collectors.toList());
200199
}));
201200

202-
add(Pair.with(Pattern.compile("s\\[\\]"), s -> Collections.emptySet()));
203-
add(Pair.with(Pattern.compile("s\\[(.*)\\]"), s -> {
204-
final String[] items = s.split(",");
205-
return Stream.of(items).map(String::trim).map(x -> convertToObject(x)).collect(Collectors.toSet());
201+
add(Pair.with(Pattern.compile("^s\\[\\]$"), s -> new HashSet<>()));
202+
add(Pair.with(Pattern.compile("^s\\[(.*)\\]$"), s -> {
203+
return splitByElement(s).stream().map(x -> convertToObject(x)).collect(Collectors.toSet());
206204
}));
207205

208206
// return the string values as is, used to wrap results that may contain other regex patterns
@@ -642,6 +640,27 @@ private String convertToString(final String pkey, final String pvalue) {
642640
return String.format("\"%s\"", pvalue);
643641
}
644642

643+
private List<String> splitByElement(String s) {
644+
if (s.trim().isEmpty()) return Collections.emptyList();
645+
646+
List<String> items = new ArrayList<>();
647+
int depth = 0, start = 0;
648+
649+
for (int i = 0; i < s.length(); i++) {
650+
char c = s.charAt(i);
651+
if (c == '[' || c == '{') depth++;
652+
else if (c == ']' || c == '}') depth--;
653+
else if (c == ',' && depth == 0) {
654+
String item = s.substring(start, i).trim();
655+
if (!item.isEmpty()) items.add(item);
656+
start = i + 1;
657+
}
658+
}
659+
String lastItem = s.substring(start).trim();
660+
if (!lastItem.isEmpty()) items.add(lastItem);
661+
return items;
662+
}
663+
645664
private Object convertToObject(final Object pvalue) {
646665
final Object v;
647666
// we may get some json stuff if it's a m[]

0 commit comments

Comments
 (0)