Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,39 @@ private static object ToNumber(string stringNumber, string graphName)
{
return new List<object?>(0);
}
return stringList.Split(',').Select(x => ParseValue(x, graphName)).ToList();
return SplitByElement(stringList).Select(x => ParseValue(x, graphName)).ToList();
}

private static List<string> SplitByElement(string s)
{
var result = new List<string>();
var depth = 0;
var current = new System.Text.StringBuilder();
foreach (var c in s)
{
if (c == '[')
{
depth++;
current.Append(c);
}
else if (c == ']')
{
depth--;
current.Append(c);
}
else if (c == ',' && depth == 0)
{
result.Add(current.ToString().Trim());
current.Clear();
}
else
{
current.Append(c);
}
}
if (current.Length > 0)
result.Add(current.ToString().Trim());
return result;
}

private static object ToDateTime(string date, string graphName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,8 @@ private static IDictionary<string, List<Func<GraphTraversalSource, IDictionary<s
{"g_V_age_foldX0_plusX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.V().Values<object>("age").Fold<object>(0, Operator.Sum)}},
{"g_injectXa1_b2X_foldXm_addAllX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.Inject<object>(new Dictionary<object, object> {{ "a", 1 }}, new Dictionary<object, object> {{ "b", 2 }}).Fold<object>(new Dictionary<object, object> {}, Operator.AddAll)}},
{"g_injectXa1_b2_b4X_foldXm_addAllX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.Inject<object>(new Dictionary<object, object> {{ "a", 1 }}, new Dictionary<object, object> {{ "b", 2 }}, new Dictionary<object, object> {{ "b", 4 }}).Fold<object>(new Dictionary<object, object> {}, Operator.AddAll)}},
{"g_injectXlist1_list2X_fold", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.Inject<object>(new List<object> { 1, 2 }, new List<object> { 3, 4 }).Fold()}},
{"g_injectXlist1_list2_list3X_fold", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.Inject<object>(new List<object> { 1, 2 }, new List<object> { 3, 4 }, new List<object> { 5, 6 }).Fold()}},
{"g_VX1X_formatXstrX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.V().Has("name", "marko").Format("Hello world")}},
{"g_V_formatXstrX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.V().Format("%{name} is %{age} years old")}},
{"g_injectX1X_asXageX_V_formatXstrX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.Inject<object>(1).As("age").V().Format("%{name} is %{age} years old")}},
Expand Down
31 changes: 29 additions & 2 deletions gremlin-go/driver/cucumber/cucumberSteps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,41 @@ func toPath(stringObjects, graphName string) interface{} {
}
}

// splitByElement splits a string on commas while respecting bracket nesting depth,
// so that nested tokens like l[1,2,3] inside s[l[1,2,3],l[4,5,6]] are not split incorrectly.
func splitByElement(s string) []string {
var result []string
depth := 0
current := strings.Builder{}
for _, c := range s {
switch {
case c == '[':
depth++
current.WriteRune(c)
case c == ']':
depth--
current.WriteRune(c)
case c == ',' && depth == 0:
result = append(result, strings.TrimSpace(current.String()))
current.Reset()
default:
current.WriteRune(c)
}
}
if current.Len() > 0 {
result = append(result, strings.TrimSpace(current.String()))
}
return result
}

// Parse list.
func toList(stringList, graphName string) interface{} {
listVal := make([]interface{}, 0)
if len(stringList) == 0 {
return listVal
}

for _, str := range strings.Split(stringList, ",") {
for _, str := range splitByElement(stringList) {
listVal = append(listVal, parseValue(str, graphName))
}
return listVal
Expand All @@ -266,7 +293,7 @@ func toSet(stringSet, graphName string) interface{} {
if len(stringSet) == 0 {
return setVal
}
for _, str := range strings.Split(stringSet, ",") {
for _, str := range splitByElement(stringSet) {
setVal.Add(parseValue(str, graphName))
}
return setVal
Expand Down
2 changes: 2 additions & 0 deletions gremlin-go/driver/cucumber/gremlin.go
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,8 @@ var translationMap = map[string][]func(g *gremlingo.GraphTraversalSource, p map[
"g_V_age_foldX0_plusX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().Values("age").Fold(0, gremlingo.Operator.Sum)}},
"g_injectXa1_b2X_foldXm_addAllX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(map[interface{}]interface{}{"a": 1 }, map[interface{}]interface{}{"b": 2 }).Fold(map[interface{}]interface{}{ }, gremlingo.Operator.AddAll)}},
"g_injectXa1_b2_b4X_foldXm_addAllX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(map[interface{}]interface{}{"a": 1 }, map[interface{}]interface{}{"b": 2 }, map[interface{}]interface{}{"b": 4 }).Fold(map[interface{}]interface{}{ }, gremlingo.Operator.AddAll)}},
"g_injectXlist1_list2X_fold": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject([]interface{}{1, 2}, []interface{}{3, 4}).Fold()}},
"g_injectXlist1_list2_list3X_fold": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject([]interface{}{1, 2}, []interface{}{3, 4}, []interface{}{5, 6}).Fold()}},
"g_VX1X_formatXstrX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().Has("name", "marko").Format("Hello world")}},
"g_V_formatXstrX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().Format("%{name} is %{age} years old")}},
"g_injectX1X_asXageX_V_formatXstrX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(1).As("age").V().Format("%{name} is %{age} years old")}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,11 +437,33 @@ function toMerge(value) {
return merge[value];
}

function splitByElement(s) {
let depth = 0;
let current = '';
const results = [];
for (const c of s) {
if (c === '[') {
depth++;
current += c;
} else if (c === ']') {
depth--;
current += c;
} else if (c === ',' && depth === 0) {
results.push(current.trim());
current = '';
} else {
current += c;
}
}
if (current.length > 0) results.push(current.trim());
return results;
}

function toArray(stringList) {
if (stringList === '') {
return new Array(0);
}
return stringList.split(',').map(x => parseValue.call(this, x));
return splitByElement(stringList).map(x => parseValue.call(this, x));
}

function toSet(stringList) {
Expand All @@ -450,7 +472,7 @@ function toSet(stringList) {
}

const s = new Set();
stringList.split(',').forEach(x => s.add(parseValue.call(this, x)));
splitByElement(stringList).forEach(x => s.add(parseValue.call(this, x)));
return s;
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 23 additions & 2 deletions gremlin-python/src/main/python/tests/feature/feature_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,27 @@ def nothing_happening(step):
return


def _split_by_element(s):
depth = 0
current = []
results = []
for c in s:
if c == '[':
depth += 1
current.append(c)
elif c == ']':
depth -= 1
current.append(c)
elif c == ',' and depth == 0:
results.append(''.join(current).strip())
current = []
else:
current.append(c)
if current:
results.append(''.join(current).strip())
return results


def _convert(val, ctx):
graph_name = ctx.graph_name
if isinstance(val, dict): # convert dictionary keys/values
Expand All @@ -242,9 +263,9 @@ def _convert(val, ctx):
n[tuple(k) if isinstance(k, (set, list)) else k] = _convert(value, ctx)
return n
elif isinstance(val, str) and re.match(r"^l\[.*\]$", val): # parse list
return [] if val == "l[]" else list(map((lambda x: _convert(x, ctx)), val[2:-1].split(",")))
return [] if val == "l[]" else list(map((lambda x: _convert(x, ctx)), _split_by_element(val[2:-1])))
elif isinstance(val, str) and re.match(r"^s\[.*\]$", val): # parse set
return set() if val == "s[]" else set(map((lambda x: _convert(x, ctx)), val[2:-1].split(",")))
return set() if val == "s[]" else set(map((lambda x: _convert(x, ctx)), _split_by_element(val[2:-1])))
elif isinstance(val, str) and re.match(r"^str\[.*\]$", val): # return string as is
return val[4:-1]
elif isinstance(val, str) and re.match(r"^dt\[.*\]$", val): # parse datetime
Expand Down
2 changes: 2 additions & 0 deletions gremlin-python/src/main/python/tests/feature/gremlin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,8 @@
'g_V_age_foldX0_plusX': [(lambda g:g.V().values('age').fold(0, Operator.sum_))],
'g_injectXa1_b2X_foldXm_addAllX': [(lambda g:g.inject({ 'a': 1 }, { 'b': 2 }).fold({ }, Operator.add_all))],
'g_injectXa1_b2_b4X_foldXm_addAllX': [(lambda g:g.inject({ 'a': 1 }, { 'b': 2 }, { 'b': 4 }).fold({ }, Operator.add_all))],
'g_injectXlist1_list2X_fold': [(lambda g:g.inject([1, 2], [3, 4]).fold())],
'g_injectXlist1_list2_list3X_fold': [(lambda g:g.inject([1, 2], [3, 4], [5, 6]).fold())],
'g_VX1X_formatXstrX': [(lambda g:g.V().has('name', 'marko').format_('Hello world'))],
'g_V_formatXstrX': [(lambda g:g.V().format_('%{name} is %{age} years old'))],
'g_injectX1X_asXageX_V_formatXstrX': [(lambda g:g.inject(1).as_('age').V().format_('%{name} is %{age} years old'))],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ public final class StepDefinition {
}));
add(Pair.with(Pattern.compile("l\\[\\]"), s -> "[]"));
add(Pair.with(Pattern.compile("l\\[(.*)\\]"), s -> {
final String[] items = s.split(",");
final String listItems = Stream.of(items).map(String::trim).map(x -> convertToString(x)).collect(Collectors.joining(","));
final List<String> items = splitByElement(s);
final String listItems = items.stream().map(String::trim).map(x -> convertToString(x)).collect(Collectors.joining(","));
return String.format("[%s]", listItems);
}));
add(Pair.with(Pattern.compile("s\\[\\]"), s -> String.format("{}")));
add(Pair.with(Pattern.compile("s\\[(.*)\\]"), s -> {
final String[] items = s.split(",");
final String listItems = Stream.of(items).map(String::trim).map(x -> convertToString(x)).collect(Collectors.joining(","));
final List<String> items = splitByElement(s);
final String listItems = items.stream().map(String::trim).map(x -> convertToString(x)).collect(Collectors.joining(","));
return String.format("{%s}", listItems);
}));
add(Pair.with(Pattern.compile("d\\[(NaN)\\]"), s -> "NaN"));
Expand Down Expand Up @@ -195,14 +195,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());
final List<String> items = splitByElement(s);
return items.stream().map(String::trim).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());
final List<String> items = splitByElement(s);
return items.stream().map(String::trim).map(x -> convertToObject(x)).collect(Collectors.toSet());
}));

// return the string values as is, used to wrap results that may contain other regex patterns
Expand Down Expand Up @@ -684,6 +684,33 @@ private Object convertToObject(final Object pvalue) {
return String.format("%s", v);
}

/**
* Splits a string on commas while respecting bracket nesting, so that nested collection tokens
* like {@code l[1,2,3]} inside a set {@code s[l[1,2,3],l[4,5,6]]} are not incorrectly split.
*/
private static List<String> splitByElement(final String s) {
final List<String> result = new ArrayList<>();
int depth = 0;
final StringBuilder current = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
final char c = s.charAt(i);
if (c == '[') {
depth++;
current.append(c);
} else if (c == ']') {
depth--;
current.append(c);
} else if (c == ',' && depth == 0) {
result.add(current.toString());
current.setLength(0);
} else {
current.append(c);
}
}
if (current.length() > 0) result.add(current.toString());
return result;
}

private static Triplet<String,String,String> getEdgeTriplet(final String e) {
final Matcher m = edgeTripletPattern.matcher(e);
if (m.matches()) {
Expand Down
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are only tests here for nested list, but this PR also changes set. Should add test for set as well as test for extra-depth in nesting.

Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,26 @@ Feature: Step - fold()
When iterated to list
Then the result should be unordered
| result |
| m[{"a":"d[1].i", "b":"d[4].i"}] |
| m[{"a":"d[1].i", "b":"d[4].i"}] |

Scenario: g_injectXlist1_list2X_fold
Given the empty graph
And the traversal of
"""
g.inject([1, 2], [3, 4]).fold()
"""
When iterated to list
Then the result should be unordered
| result |
| l[l[d[1].i,d[2].i],l[d[3].i,d[4].i]] |

Scenario: g_injectXlist1_list2_list3X_fold
Given the empty graph
And the traversal of
"""
g.inject([1, 2], [3, 4], [5, 6]).fold()
"""
When iterated to list
Then the result should be unordered
| result |
| l[l[d[1].i,d[2].i],l[d[3].i,d[4].i],l[d[5].i,d[6].i]] |
Loading