tinkerpop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From spmalle...@apache.org
Subject tinkerpop git commit: TINKERPOP-2040 wip-tp32
Date Tue, 25 Sep 2018 10:49:40 GMT
Repository: tinkerpop
Updated Branches:
  refs/heads/TINKERPOP-2040 [created] 4831e9d6c


TINKERPOP-2040 wip-tp32


Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/4831e9d6
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/4831e9d6
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/4831e9d6

Branch: refs/heads/TINKERPOP-2040
Commit: 4831e9d6cefb1c75e78b65f9d5b51c3ca1a2b12a
Parents: abfe968
Author: Stephen Mallette <spmva@genoprime.com>
Authored: Thu Sep 20 11:53:07 2018 -0400
Committer: Stephen Mallette <spmva@genoprime.com>
Committed: Tue Sep 25 06:42:29 2018 -0400

----------------------------------------------------------------------
 .../gremlin/process/traversal/Translator.java   |  7 ++++++
 .../groovy/jsr223/GroovyTranslatorTest.java     | 26 ++++++++++++++++++++
 .../gremlin/groovy/jsr223/GroovyTranslator.java | 16 +++++++++---
 3 files changed, 46 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/4831e9d6/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Translator.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Translator.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Translator.java
index 7e97fb3..87565a4 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Translator.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Translator.java
@@ -19,6 +19,8 @@
 
 package org.apache.tinkerpop.gremlin.process.traversal;
 
+import java.util.function.UnaryOperator;
+
 /**
  * A Translator will translate {@link Bytecode} into another representation. That representation
may be a
  * Java instance via {@link StepTranslator} or a String script in some language via {@link
ScriptTranslator}.
@@ -57,6 +59,11 @@ public interface Translator<S, T> {
 
     public interface ScriptTranslator extends Translator<String, String> {
 
+        public interface TypeTranslator extends UnaryOperator<Object> {
+            public static TypeTranslator identity() {
+                return t -> t;
+            }
+        }
     }
 
     public interface StepTranslator<S extends TraversalSource, T extends Traversal.Admin<?,
?>> extends Translator<S, T> {

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/4831e9d6/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GroovyTranslatorTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GroovyTranslatorTest.java
b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GroovyTranslatorTest.java
index 942f1a5..6ac8ac5 100644
--- a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GroovyTranslatorTest.java
+++ b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GroovyTranslatorTest.java
@@ -45,6 +45,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.UUID;
 import java.util.function.Function;
 
 import static org.junit.Assert.assertEquals;
@@ -152,6 +153,31 @@ public class GroovyTranslatorTest extends AbstractGremlinTest {
     }
 
     @Test
+    public void shouldOverrideDefaultTypeTranslationWithSomethingBonkers() {
+        final String thingToSuffixAllStringsWith = "-why-would-anyone-do-this";
+        final GraphTraversalSource g = graph.traversal();
+        final String script = GroovyTranslator.of("g", x -> x instanceof String ? x +
thingToSuffixAllStringsWith : x).
+                translate(g.V("yyy", "xxx").id().asAdmin().getBytecode());
+        assertEquals(String.format("g.V(\"yyy%s\",\"xxx%s\").id()", thingToSuffixAllStringsWith,
thingToSuffixAllStringsWith), script);
+    }
+
+    @Test
+    public void shouldIncludeNewTypeTranslationForUuid() {
+        final UUID uuid = UUID.fromString("ffffffff-fd49-1e4b-0000-00000d4b8a1d");
+        final GraphTraversalSource g = graph.traversal();
+
+        // without type translation we get uglinesss
+        final String scriptBad = GroovyTranslator.of("g").
+                translate(g.V(uuid).id().asAdmin().getBytecode());
+        assertEquals(String.format("g.V(%s).id()", uuid), scriptBad);
+
+        // with type translation we get valid gremlin
+        final String scriptGood = GroovyTranslator.of("g", x -> x instanceof UUID ? String.format("%s",
uuid) : x).
+                translate(g.V(uuid).id().asAdmin().getBytecode());
+        assertEquals(String.format("g.V(\"%s\").id()", uuid), scriptGood);
+    }
+
+    @Test
     public void shouldHaveValidToString() {
         assertEquals("translator[h:gremlin-groovy]", GroovyTranslator.of("h").toString());
     }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/4831e9d6/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GroovyTranslator.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GroovyTranslator.java
b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GroovyTranslator.java
index 0452e0b..0dc92a5 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GroovyTranslator.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GroovyTranslator.java
@@ -43,7 +43,9 @@ import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
+import java.util.function.UnaryOperator;
 
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
@@ -53,13 +55,19 @@ public final class GroovyTranslator implements Translator.ScriptTranslator
{
     private static final boolean IS_TESTING = Boolean.valueOf(System.getProperty("is.testing",
"false"));
 
     private final String traversalSource;
+    private final TypeTranslator typeTranslator;
 
-    private GroovyTranslator(final String traversalSource) {
+    private GroovyTranslator(final String traversalSource, final TypeTranslator typeTranslator)
{
         this.traversalSource = traversalSource;
+        this.typeTranslator = typeTranslator;
     }
 
     public static final GroovyTranslator of(final String traversalSource) {
-        return new GroovyTranslator(traversalSource);
+        return of(traversalSource, TypeTranslator.identity());
+    }
+
+    public static final GroovyTranslator of(final String traversalSource, final TypeTranslator
typeTranslator) {
+        return new GroovyTranslator(traversalSource, Optional.ofNullable(typeTranslator).orElse(TypeTranslator.identity()));
     }
 
     ///////
@@ -108,7 +116,9 @@ public final class GroovyTranslator implements Translator.ScriptTranslator
{
         return traversalScript.toString();
     }
 
-    private String convertToString(final Object object) {
+    private String convertToString(final Object o) {
+        final Object object = typeTranslator.apply(o);
+
         if (object instanceof Bytecode.Binding)
             return ((Bytecode.Binding) object).variable();
         else if (object instanceof Bytecode)


Mime
View raw message