-
Notifications
You must be signed in to change notification settings - Fork 838
Add splitByElement() method for parsing nested list or set #3216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yunlingTao
wants to merge
1
commit into
apache:3.8-dev
Choose a base branch
from
yunlingTao:split_by_element
base: 3.8-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+29
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -184,7 +184,7 @@ public final class StepDefinition { | |
private List<Pair<Pattern, Function<String,Object>>> objectMatcherConverters = new ArrayList<Pair<Pattern, Function<String,Object>>>() {{ | ||
// expects json so that should port to the Gremlin script form - replace curly json braces with square ones | ||
// for Gremlin sake. | ||
add(Pair.with(Pattern.compile("m\\[(.*)\\]"), s -> { | ||
add(Pair.with(Pattern.compile("^m\\[(.*)\\]$"), s -> { | ||
try { | ||
// read tree from JSON - can't parse right to Map as each m[] level needs to be managed individually | ||
return convertToObject(mapper.readTree(s)); | ||
|
@@ -193,16 +193,14 @@ public final class StepDefinition { | |
} | ||
})); | ||
|
||
add(Pair.with(Pattern.compile("l\\[\\]"), s -> Collections.emptyList())); | ||
add(Pair.with(Pattern.compile("l\\[(.*)\\]"), s -> { | ||
final String[] items = s.split(","); | ||
return Stream.of(items).map(String::trim).map(x -> convertToObject(x)).collect(Collectors.toList()); | ||
add(Pair.with(Pattern.compile("^l\\[\\]$"), s -> Collections.emptyList())); | ||
add(Pair.with(Pattern.compile("^l\\[(.*)\\]$"), s -> { | ||
return splitByElement(s).stream().map(x -> convertToObject(x)).collect(Collectors.toList()); | ||
})); | ||
|
||
add(Pair.with(Pattern.compile("s\\[\\]"), s -> Collections.emptySet())); | ||
add(Pair.with(Pattern.compile("s\\[(.*)\\]"), s -> { | ||
final String[] items = s.split(","); | ||
return Stream.of(items).map(String::trim).map(x -> convertToObject(x)).collect(Collectors.toSet()); | ||
add(Pair.with(Pattern.compile("^s\\[\\]$"), s -> Collections.emptySet())); | ||
add(Pair.with(Pattern.compile("^s\\[(.*)\\]$"), s -> { | ||
return splitByElement(s).stream().map(x -> convertToObject(x)).collect(Collectors.toSet()); | ||
})); | ||
|
||
// 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) { | |
return String.format("\"%s\"", pvalue); | ||
} | ||
|
||
private List<String> splitByElement(String s) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to add a scenario to a feature test would would execute this new logic. Perhaps adding a scenario to the |
||
if (s.trim().isEmpty()) return Collections.emptyList(); | ||
|
||
List<String> items = new ArrayList<>(); | ||
int depth = 0, start = 0; | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
char c = s.charAt(i); | ||
if (c == '[' || c == '{') depth++; | ||
else if (c == ']' || c == '}') depth--; | ||
else if (c == ',' && depth == 0) { | ||
String item = s.substring(start, i).trim(); | ||
if (!item.isEmpty()) items.add(item); | ||
start = i + 1; | ||
} | ||
} | ||
String lastItem = s.substring(start).trim(); | ||
if (!lastItem.isEmpty()) items.add(lastItem); | ||
return items; | ||
} | ||
|
||
private Object convertToObject(final Object pvalue) { | ||
final Object v; | ||
// we may get some json stuff if it's a m[] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do other GLVs need a similar change? For example the parsing logic for lists and sets in
feature-steps.js
andfeature_steps.py
.