Skip to content

Commit

Permalink
implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentyn Kahamlyk authored and Valentyn Kahamlyk committed Aug 2, 2024
1 parent a74f0b0 commit 5929c91
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ public Void visitNullLiteral(final GremlinParser.NullLiteralContext ctx) {
return null;
}

@Override
public Void visitGenericLiteralSet(GremlinParser.GenericLiteralSetContext ctx) {
sb.append("[");
for (int i = 0; i < ctx.genericLiteral().size(); i++) {
final GremlinParser.GenericLiteralContext genericLiteralContext = ctx.genericLiteral(i);
visit(genericLiteralContext);
if (i < ctx.genericLiteral().size() - 1)
sb.append(", ");
}
sb.append("] as Set");
return null;
}

@Override
public Void visitTraversalSourceSpawnMethod_inject(final GremlinParser.TraversalSourceSpawnMethod_injectContext ctx) {
return handleInject(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,8 @@ private String argAsString(final Object arg) {
return asString((Map) arg);
}

// todo: add set handling here when will be implemented in Grammar
if (arg instanceof Set) {
return asParameter(arg);
return asString((Set) arg);
}

// handle all iterables in similar way
Expand Down Expand Up @@ -228,6 +227,20 @@ private String asString(final Iterator itty) {
return sb.append("]").toString();
}

private String asString(final Set<?> set) {
final StringBuilder sb = new StringBuilder().append("{");

final Iterator itty = asIterator(set);

while (itty.hasNext()) {
sb.append(argAsString(itty.next()));
if (itty.hasNext())
sb.append(",");
}

return sb.append("}").toString();
}

// borrowed from Groovy translator
private String asString(final P<?> p) {
final StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ public static Collection<Object[]> data() {
"g.with(string0, set0)",
"g.With(\"x\", new HashSet<object> { 1, \"x\" })",
"g.With(\"x\", gremlingo.NewSimpleSet(1, \"x\"))",
null,
"g.with('x', [1, 'x'] as Set)",
"g.with(\"x\", new HashSet<Object>() {{ add(1); add(\"x\"); }})",
"g.with_(\"x\", new Set([1, \"x\"]))",
"g.with_('x', {1, 'x'})"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static Iterable<Object[]> generateTestParameters() {
"g.withStrategies(new SubgraphStrategy(checkAdjacentVertices:true,vertices:__.has(\"name\",P.within([\"josh\",\"lop\",\"ripple\"])))).V()"},
{g.inject(Parameter.var("x", "x")).V(Parameter.var("ids", new int[]{1, 2, 3})), "g.inject(x).V(ids)"},
{newG().inject(Parameter.value("test1"), Parameter.value("test2")), "g.inject(_0,_1)"},
{newG().inject(new HashSet<>(Arrays.asList(1, 2))), "g.inject(_0)"},
{newG().inject(new HashSet<>(Arrays.asList(1, 2))), "g.inject({1,2})"},
});
}

Expand Down

0 comments on commit 5929c91

Please sign in to comment.