[ https://issues.apache.org/jira/browse/TINKERPOP-3028?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17852596#comment-17852596 ]
ASF GitHub Bot commented on TINKERPOP-3028: ------------------------------------------- vkagamlyk commented on code in PR #2640: URL: https://github.com/apache/tinkerpop/pull/2640#discussion_r1628506208 ########## gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/language/translator/GroovyTranslateVisitor.java: ########## @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tinkerpop.gremlin.language.translator; + +import org.apache.tinkerpop.gremlin.language.grammar.GremlinParser; + +/** + * Converts a Gremlin traversal string into a Groovy source code representation of that traversal with an aim at + * sacrificing some formatting for the ability to compile correctly. The translations may require use of TinkerPop's + * sugar syntax and therefore requires use of the {@code GremlinLoader} in the gremlin-groovy module unless you are + * specifically certain that your translations will not result in the use of that syntax. If in doubt, prefer the + * {@link JavaTranslateVisitor} instead. + * <ul> + * <li>Normalize numeric suffixes to lower case</li> + * <li>If floats are not suffixed they will translate as BigDecimal</li> + * <li>Makes anonymous traversals explicit with double underscore</li> + * <li>Makes enums explicit with their proper name</li> + * </ul> + */ +public class GroovyTranslateVisitor extends TranslateVisitor { + public GroovyTranslateVisitor() { + this("g"); + } + + public GroovyTranslateVisitor(final String graphTraversalSourceName) { + super(graphTraversalSourceName); + } + + @Override + public Void visitStructureVertex(final GremlinParser.StructureVertexContext ctx) { + sb.append("new DetachedVertex("); + visit(ctx.getChild(3)); // id + sb.append(", "); + visit(ctx.getChild(5)); // label + sb.append(")"); + return null; + } + + @Override + public Void visitIntegerLiteral(final GremlinParser.IntegerLiteralContext ctx) { + final String integerLiteral = ctx.getText().toLowerCase(); + + // check suffix + final int lastCharIndex = integerLiteral.length() - 1; + final char lastCharacter = integerLiteral.charAt(lastCharIndex); + switch (lastCharacter) { + case 'b': + // parse B/b as byte + sb.append("new Byte("); + sb.append(integerLiteral, 0, lastCharIndex); + sb.append(")"); + break; + case 's': + // parse S/s as short + sb.append("new Short("); + sb.append(integerLiteral, 0, lastCharIndex); + sb.append(")"); + break; + case 'i': + case 'l': + // parse I/i and L/l as Integer and Long respectively + sb.append(integerLiteral, 0, lastCharIndex).append(lastCharacter); Review Comment: nit: no need special handling for i/l case, it's same as default > Convert translators to make use of the grammar > ---------------------------------------------- > > Key: TINKERPOP-3028 > URL: https://issues.apache.org/jira/browse/TINKERPOP-3028 > Project: TinkerPop > Issue Type: Improvement > Components: translator > Affects Versions: 3.6.6 > Reporter: Stephen Mallette > Assignee: Stephen Mallette > Priority: Major > > {{Translator}} infrastructure uses bytecode and reflection which has a number > of technical imperfections and exemptions for it to work properly. Switching > to the grammar for translation simplifies the code, removes friction and > complexity when generating GLV tests, and should perform better. The focus > for this issue is the Java series of translators which are relied on quite > heavily. -- This message was sent by Atlassian Jira (v8.20.10#820010)