From dcf3da3917a9626d432ef26089bb05c6a15158ed Mon Sep 17 00:00:00 2001 From: Florian Hockmann Date: Sat, 10 Mar 2018 19:17:15 +0100 Subject: [PATCH 1/4] TINKERPOP-1901 Transformed Gremlin.Net enums into classes --- docs/src/reference/gremlin-variants.asciidoc | 2 +- gremlin-dotnet/glv/Enum.template | 7 +- gremlin-dotnet/glv/generate.groovy | 21 +---- .../Gremlin.Net/Process/Traversal/Barrier.cs | 9 +- .../Process/Traversal/Cardinality.cs | 13 ++- .../Gremlin.Net/Process/Traversal/Column.cs | 11 ++- .../Process/Traversal/Direction.cs | 13 ++- .../Process/Traversal/EnumWrapper.cs | 52 +++++++++++ .../Process/Traversal/GraphSONVersion.cs | 11 ++- .../Process/Traversal/GryoVersion.cs | 9 +- .../Process/Traversal/NamingConversions.cs | 91 ------------------- .../Gremlin.Net/Process/Traversal/Operator.cs | 29 +++--- .../Gremlin.Net/Process/Traversal/Order.cs | 21 +++-- .../src/Gremlin.Net/Process/Traversal/Pick.cs | 11 ++- .../src/Gremlin.Net/Process/Traversal/Pop.cs | 13 ++- .../Gremlin.Net/Process/Traversal/Scope.cs | 11 ++- .../src/Gremlin.Net/Process/Traversal/T.cs | 15 ++- .../Structure/IO/GraphSON/EnumSerializer.cs | 5 +- .../Structure/IO/GraphSON/GraphSONWriter.cs | 2 +- .../TraversalEnumParameter.cs | 5 +- 20 files changed, 181 insertions(+), 170 deletions(-) create mode 100644 gremlin-dotnet/src/Gremlin.Net/Process/Traversal/EnumWrapper.cs delete mode 100644 gremlin-dotnet/src/Gremlin.Net/Process/Traversal/NamingConversions.cs diff --git a/docs/src/reference/gremlin-variants.asciidoc b/docs/src/reference/gremlin-variants.asciidoc index f06fe02efff..ace81194c2b 100644 --- a/docs/src/reference/gremlin-variants.asciidoc +++ b/docs/src/reference/gremlin-variants.asciidoc @@ -362,7 +362,7 @@ terminal/action methods off of `ITraversal`. === Static Enums and Methods -Gremlin has various tokens (e.g. `T`, `P`, `Order`, `Operator`, etc.) that are represented in Gremlin.Net as Enums. +Gremlin has various tokens (e.g. `T`, `P`, `Order`, `Operator`, etc.) that are represented in Gremlin.Net as classes. These can be used analogously to how they are used in Gremlin-Java. diff --git a/gremlin-dotnet/glv/Enum.template b/gremlin-dotnet/glv/Enum.template index 1fddab109db..fd093129774 100644 --- a/gremlin-dotnet/glv/Enum.template +++ b/gremlin-dotnet/glv/Enum.template @@ -26,8 +26,13 @@ namespace Gremlin.Net.Process.Traversal { #pragma warning disable 1591 - public enum <%= enumClass.simpleName %> + public class <%= enumClass.simpleName %> : EnumWrapper { + private <%= enumClass.simpleName %>(string enumValue) + : base("<%= enumClass.simpleName %>", enumValue) + { + } + <%= constants %> } diff --git a/gremlin-dotnet/glv/generate.groovy b/gremlin-dotnet/glv/generate.groovy index 12cfa88a3a5..7894793ef65 100644 --- a/gremlin-dotnet/glv/generate.groovy +++ b/gremlin-dotnet/glv/generate.groovy @@ -339,36 +339,23 @@ def toCSharpName = { enumClass, itemName -> return itemName.substring(0, 1).toUpperCase() + itemName.substring(1) } -def createEnum = { enumClass, csharpToJava -> +def createEnum = { enumClass -> def b = ["enumClass": enumClass, "constants": enumClass.getEnumConstants(). sort { a, b -> a.name() <=> b.name() }. collect { value -> def csharpName = toCSharpName(enumClass, value.name()) - csharpToJava.put(enumClass.simpleName + "." + csharpName, value.name()) - return csharpName - }.join(",\n\t\t")] + return "public static ${enumClass.simpleName} ${csharpName} => new ${enumClass.simpleName}(\"${value.name()}\");" + }.join("\n\t\t")] def enumTemplate = engine.createTemplate(new File("${projectBaseDir}/glv/Enum.template")).make(b) def enumFile = new File("${projectBaseDir}/src/Gremlin.Net/Process/Traversal/" + enumClass.getSimpleName() + ".cs") enumFile.newWriter().withWriter{ it << enumTemplate } } -def enumCSharpToJavaNames = [:] CoreImports.getClassImports().findAll { Enum.class.isAssignableFrom(it) }. sort { a, b -> a.getSimpleName() <=> b.getSimpleName() }. - each { createEnum(it, enumCSharpToJavaNames) } - -def lastIndex = (enumCSharpToJavaNames.size() - 1); -def body = new StringBuilder() -enumCSharpToJavaNames.eachWithIndex{ node, i -> - body.append("""{"$node.key", "$node.value"}""") - body.append(i == lastIndex ? "\n" : ",\n ") -} - -def namingConversionsTemplate = engine.createTemplate(new File("${projectBaseDir}/glv/NamingConversions.template")).make(["body":body]) -def namingConversionsFile = new File("${projectBaseDir}/src/Gremlin.Net/Process/Traversal/NamingConversions.cs") -namingConversionsFile.newWriter().withWriter{ it << namingConversionsTemplate } + each { createEnum(it) } def determineVersion = { def env = System.getenv() diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Barrier.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Barrier.cs index 555d372589d..6c00f9981c7 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Barrier.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Barrier.cs @@ -26,9 +26,14 @@ namespace Gremlin.Net.Process.Traversal { #pragma warning disable 1591 - public enum Barrier + public class Barrier : EnumWrapper { - NormSack + private Barrier(string enumValue) + : base("Barrier", enumValue) + { + } + + public static Barrier NormSack => new Barrier("normSack"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Cardinality.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Cardinality.cs index 5a07258cd97..f158153a44d 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Cardinality.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Cardinality.cs @@ -26,11 +26,16 @@ namespace Gremlin.Net.Process.Traversal { #pragma warning disable 1591 - public enum Cardinality + public class Cardinality : EnumWrapper { - List, - Set, - Single + private Cardinality(string enumValue) + : base("Cardinality", enumValue) + { + } + + public static Cardinality List => new Cardinality("list"); + public static Cardinality Set => new Cardinality("set"); + public static Cardinality Single => new Cardinality("single"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Column.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Column.cs index 432323f1ec4..3daaa4d7c2a 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Column.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Column.cs @@ -26,10 +26,15 @@ namespace Gremlin.Net.Process.Traversal { #pragma warning disable 1591 - public enum Column + public class Column : EnumWrapper { - Keys, - Values + private Column(string enumValue) + : base("Column", enumValue) + { + } + + public static Column Keys => new Column("keys"); + public static Column Values => new Column("values"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Direction.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Direction.cs index 6f197483807..9566d512e3f 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Direction.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Direction.cs @@ -26,11 +26,16 @@ namespace Gremlin.Net.Process.Traversal { #pragma warning disable 1591 - public enum Direction + public class Direction : EnumWrapper { - Both, - In, - Out + private Direction(string enumValue) + : base("Direction", enumValue) + { + } + + public static Direction Both => new Direction("BOTH"); + public static Direction In => new Direction("IN"); + public static Direction Out => new Direction("OUT"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/EnumWrapper.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/EnumWrapper.cs new file mode 100644 index 00000000000..66b8c5a8363 --- /dev/null +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/EnumWrapper.cs @@ -0,0 +1,52 @@ +#region License + +/* + * 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. + */ + +#endregion + +namespace Gremlin.Net.Process.Traversal +{ + /// + /// Represents an enum. + /// + public abstract class EnumWrapper + { + /// + /// Gets the name of the enum. + /// + public string EnumName { get; } + + /// + /// Gets the value of the enum. + /// + public string EnumValue { get; } + + /// + /// Initializes a new instance of the class. + /// + /// The name of the enum. + /// The value of the enum. + protected EnumWrapper(string enumName, string enumValue) + { + EnumName = enumName; + EnumValue = enumValue; + } + } +} \ No newline at end of file diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GraphSONVersion.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GraphSONVersion.cs index e978bc6b19a..ad73ad1eb85 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GraphSONVersion.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GraphSONVersion.cs @@ -26,10 +26,15 @@ namespace Gremlin.Net.Process.Traversal { #pragma warning disable 1591 - public enum GraphSONVersion + public class GraphSONVersion : EnumWrapper { - V1_0, - V2_0 + private GraphSONVersion(string enumValue) + : base("GraphSONVersion", enumValue) + { + } + + public static GraphSONVersion V1_0 => new GraphSONVersion("V1_0"); + public static GraphSONVersion V2_0 => new GraphSONVersion("V2_0"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GryoVersion.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GryoVersion.cs index 5ee735882be..45387e27a10 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GryoVersion.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GryoVersion.cs @@ -26,9 +26,14 @@ namespace Gremlin.Net.Process.Traversal { #pragma warning disable 1591 - public enum GryoVersion + public class GryoVersion : EnumWrapper { - V1_0 + private GryoVersion(string enumValue) + : base("GryoVersion", enumValue) + { + } + + public static GryoVersion V1_0 => new GryoVersion("V1_0"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/NamingConversions.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/NamingConversions.cs deleted file mode 100644 index aecdc570dc7..00000000000 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/NamingConversions.cs +++ /dev/null @@ -1,91 +0,0 @@ -#region License - -/* - * 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. - */ - -#endregion - -using System.Collections.Generic; - -// THIS IS A GENERATED FILE - DO NOT MODIFY THIS FILE DIRECTLY - see pom.xml -namespace Gremlin.Net.Process.Traversal -{ - internal static class NamingConversions - { - /// - /// Gets the Java name equivalent for a given enum value - /// - internal static string GetEnumJavaName(string typeName, string value) - { - var key = $"{typeName}.{value}"; - string javaName; - if (!CSharpToJavaEnums.TryGetValue(key, out javaName)) - { - throw new KeyNotFoundException($"Java name for {key} not found"); - } - return javaName; - } - - internal static readonly IDictionary CSharpToJavaEnums = new Dictionary - { - {"Barrier.NormSack", "normSack"}, - {"Cardinality.List", "list"}, - {"Cardinality.Set", "set"}, - {"Cardinality.Single", "single"}, - {"Column.Keys", "keys"}, - {"Column.Values", "values"}, - {"Direction.Both", "BOTH"}, - {"Direction.In", "IN"}, - {"Direction.Out", "OUT"}, - {"GraphSONVersion.V1_0", "V1_0"}, - {"GraphSONVersion.V2_0", "V2_0"}, - {"GryoVersion.V1_0", "V1_0"}, - {"Operator.AddAll", "addAll"}, - {"Operator.And", "and"}, - {"Operator.Assign", "assign"}, - {"Operator.Div", "div"}, - {"Operator.Max", "max"}, - {"Operator.Min", "min"}, - {"Operator.Minus", "minus"}, - {"Operator.Mult", "mult"}, - {"Operator.Or", "or"}, - {"Operator.Sum", "sum"}, - {"Operator.SumLong", "sumLong"}, - {"Order.Decr", "decr"}, - {"Order.Incr", "incr"}, - {"Order.KeyDecr", "keyDecr"}, - {"Order.KeyIncr", "keyIncr"}, - {"Order.Shuffle", "shuffle"}, - {"Order.ValueDecr", "valueDecr"}, - {"Order.ValueIncr", "valueIncr"}, - {"Pick.Any", "any"}, - {"Pick.None", "none"}, - {"Pop.All", "all"}, - {"Pop.First", "first"}, - {"Pop.Last", "last"}, - {"Scope.Global", "global"}, - {"Scope.Local", "local"}, - {"T.Id", "id"}, - {"T.Key", "key"}, - {"T.Label", "label"}, - {"T.Value", "value"} - - }; - } -} \ No newline at end of file diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Operator.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Operator.cs index 72b00486cbd..699a5a5a54f 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Operator.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Operator.cs @@ -26,19 +26,24 @@ namespace Gremlin.Net.Process.Traversal { #pragma warning disable 1591 - public enum Operator + public class Operator : EnumWrapper { - AddAll, - And, - Assign, - Div, - Max, - Min, - Minus, - Mult, - Or, - Sum, - SumLong + private Operator(string enumValue) + : base("Operator", enumValue) + { + } + + public static Operator AddAll => new Operator("addAll"); + public static Operator And => new Operator("and"); + public static Operator Assign => new Operator("assign"); + public static Operator Div => new Operator("div"); + public static Operator Max => new Operator("max"); + public static Operator Min => new Operator("min"); + public static Operator Minus => new Operator("minus"); + public static Operator Mult => new Operator("mult"); + public static Operator Or => new Operator("or"); + public static Operator Sum => new Operator("sum"); + public static Operator SumLong => new Operator("sumLong"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Order.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Order.cs index 1f12710942e..93bf7bca654 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Order.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Order.cs @@ -26,15 +26,20 @@ namespace Gremlin.Net.Process.Traversal { #pragma warning disable 1591 - public enum Order + public class Order : EnumWrapper { - Decr, - Incr, - KeyDecr, - KeyIncr, - Shuffle, - ValueDecr, - ValueIncr + private Order(string enumValue) + : base("Order", enumValue) + { + } + + public static Order Decr => new Order("decr"); + public static Order Incr => new Order("incr"); + public static Order KeyDecr => new Order("keyDecr"); + public static Order KeyIncr => new Order("keyIncr"); + public static Order Shuffle => new Order("shuffle"); + public static Order ValueDecr => new Order("valueDecr"); + public static Order ValueIncr => new Order("valueIncr"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Pick.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Pick.cs index e6394aebc22..a47d4a6eb97 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Pick.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Pick.cs @@ -26,10 +26,15 @@ namespace Gremlin.Net.Process.Traversal { #pragma warning disable 1591 - public enum Pick + public class Pick : EnumWrapper { - Any, - None + private Pick(string enumValue) + : base("Pick", enumValue) + { + } + + public static Pick Any => new Pick("any"); + public static Pick None => new Pick("none"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Pop.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Pop.cs index a7a8403802c..9e97a09de93 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Pop.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Pop.cs @@ -26,11 +26,16 @@ namespace Gremlin.Net.Process.Traversal { #pragma warning disable 1591 - public enum Pop + public class Pop : EnumWrapper { - All, - First, - Last + private Pop(string enumValue) + : base("Pop", enumValue) + { + } + + public static Pop All => new Pop("all"); + public static Pop First => new Pop("first"); + public static Pop Last => new Pop("last"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Scope.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Scope.cs index d5af93ac804..65a6e67bb61 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Scope.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Scope.cs @@ -26,10 +26,15 @@ namespace Gremlin.Net.Process.Traversal { #pragma warning disable 1591 - public enum Scope + public class Scope : EnumWrapper { - Global, - Local + private Scope(string enumValue) + : base("Scope", enumValue) + { + } + + public static Scope Global => new Scope("global"); + public static Scope Local => new Scope("local"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/T.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/T.cs index c21b50ac84a..9eba458dd85 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/T.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/T.cs @@ -26,12 +26,17 @@ namespace Gremlin.Net.Process.Traversal { #pragma warning disable 1591 - public enum T + public class T : EnumWrapper { - Id, - Key, - Label, - Value + private T(string enumValue) + : base("T", enumValue) + { + } + + public static T Id => new T("id"); + public static T Key => new T("key"); + public static T Label => new T("label"); + public static T Value => new T("value"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/EnumSerializer.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/EnumSerializer.cs index b4ab8703835..ccea7cdfc8d 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/EnumSerializer.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/EnumSerializer.cs @@ -30,9 +30,8 @@ internal class EnumSerializer : IGraphSONSerializer { public Dictionary Dictify(dynamic objectData, GraphSONWriter writer) { - var enumName = objectData.GetType().Name; - var valueJavaName = NamingConversions.GetEnumJavaName(enumName, objectData.ToString()); - return GraphSONUtil.ToTypedValue(enumName, valueJavaName); + EnumWrapper enumToSerialize = objectData; + return GraphSONUtil.ToTypedValue(enumToSerialize.EnumName, enumToSerialize.EnumValue); } } } \ No newline at end of file diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs index a60a5584708..d5b7acce5ba 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs @@ -53,7 +53,7 @@ public class GraphSONWriter {typeof(Guid), new UuidSerializer()}, {typeof(DateTimeOffset), new DateSerializer()}, {typeof(Type), new ClassSerializer()}, - {typeof(Enum), new EnumSerializer()}, + {typeof(EnumWrapper), new EnumSerializer()}, {typeof(TraversalPredicate), new TraversalPredicateSerializer()}, {typeof(Vertex), new VertexSerializer()}, {typeof(Edge), new EdgeSerializer()}, diff --git a/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/TraversalEvaluation/TraversalEnumParameter.cs b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/TraversalEvaluation/TraversalEnumParameter.cs index c305df71745..9457cae427d 100644 --- a/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/TraversalEvaluation/TraversalEnumParameter.cs +++ b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/TraversalEvaluation/TraversalEnumParameter.cs @@ -26,7 +26,6 @@ using System.Linq; using System.Reflection; using Gremlin.Net.Process.Traversal; -using TEnum = Gremlin.Net.Process.Traversal.T; namespace Gremlin.Net.IntegrationTest.Gherkin.TraversalEvaluation { @@ -38,7 +37,7 @@ internal class TraversalEnumParameter : ITokenParameter, IEquatable EnumTypesByName = typeof(Scope).GetTypeInfo().Assembly - .GetTypes().Where(t => t.GetTypeInfo().IsEnum && t.GetTypeInfo().IsPublic) + .GetTypes().Where(t => t.GetTypeInfo().IsSubclassOf(typeof(EnumWrapper)) && t.GetTypeInfo().IsPublic) .ToDictionary(e => e.Name, e => e); private readonly object _value; @@ -55,7 +54,7 @@ public TraversalEnumParameter(string text) } _type = type; var valueName = text.Substring(separatorIndex + 1); - _value = Enum.Parse(type, GetCsharpName(valueName)); + _value = _type.GetProperty(GetCsharpName(valueName)).GetValue(null); } private string GetCsharpName(string valueText) From d012171e97d5d87108eef3d0257a8b859f77de04 Mon Sep 17 00:00:00 2001 From: Florian Hockmann Date: Sat, 10 Mar 2018 19:21:46 +0100 Subject: [PATCH 2/4] TINKERPOP-1901 Add interfaces for tokens in Gremlin.Net These interfaces simply represent their Java counterparts which allows to use them as arguments in Gremlin steps. --- CHANGELOG.asciidoc | 1 + gremlin-dotnet/glv/Enum.template | 2 +- gremlin-dotnet/glv/generate.groovy | 20 ++++++--- .../Gremlin.Net/Process/Traversal/Barrier.cs | 2 +- .../Gremlin.Net/Process/Traversal/Column.cs | 2 +- .../Process/Traversal/GraphTraversal.cs | 45 +++++++++++-------- .../Process/Traversal/GraphTraversalSource.cs | 14 ++++-- .../Process/Traversal/IBiFunction.cs | 33 ++++++++++++++ .../Process/Traversal/IBinaryOperator.cs | 34 ++++++++++++++ .../Process/Traversal/IComparator.cs | 32 +++++++++++++ .../Process/Traversal/IConsumer.cs | 33 ++++++++++++++ .../Process/Traversal/IFunction.cs | 32 +++++++++++++ .../Process/Traversal/IPredicate.cs | 32 +++++++++++++ .../Gremlin.Net/Process/Traversal/Operator.cs | 2 +- .../Gremlin.Net/Process/Traversal/Order.cs | 2 +- .../src/Gremlin.Net/Process/Traversal/T.cs | 2 +- .../Process/Traversal/TraversalPredicate.cs | 2 +- .../src/Gremlin.Net/Process/Traversal/__.cs | 28 ++++++------ 18 files changed, 270 insertions(+), 48 deletions(-) create mode 100644 gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IBiFunction.cs create mode 100644 gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IBinaryOperator.cs create mode 100644 gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IComparator.cs create mode 100644 gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IConsumer.cs create mode 100644 gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IFunction.cs create mode 100644 gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IPredicate.cs diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 341a39280b0..568097c01b4 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -23,6 +23,7 @@ image::https://raw.githubusercontent.com/apache/tinkerpop/master/docs/static/ima [[release-3-2-8]] === TinkerPop 3.2.8 (Release Date: NOT OFFICIALLY RELEASED YET) +* Enums are now represented as classes in Gremlin.Net which allows to use them as arguments in more steps. * Bumped to Groovy 2.4.14. * Added `checkAdjacentVertices` option to `SubgraphStrategy`. * Modified `GremlinDslProcessor` so that it generated the `getAnonymousTraversalClass()` method to return the DSL version of `__`. diff --git a/gremlin-dotnet/glv/Enum.template b/gremlin-dotnet/glv/Enum.template index fd093129774..0be23c786c1 100644 --- a/gremlin-dotnet/glv/Enum.template +++ b/gremlin-dotnet/glv/Enum.template @@ -26,7 +26,7 @@ namespace Gremlin.Net.Process.Traversal { #pragma warning disable 1591 - public class <%= enumClass.simpleName %> : EnumWrapper + public class <%= enumClass.simpleName %> : <%= implementedTypes %> { private <%= enumClass.simpleName %>(string enumValue) : base("<%= enumClass.simpleName %>", enumValue) diff --git a/gremlin-dotnet/glv/generate.groovy b/gremlin-dotnet/glv/generate.groovy index 7894793ef65..6d89b2f958a 100644 --- a/gremlin-dotnet/glv/generate.groovy +++ b/gremlin-dotnet/glv/generate.groovy @@ -48,17 +48,17 @@ def toCSharpTypeMap = ["Long": "long", "TraversalMetrics": "E2", "Traversal": "ITraversal", "Traversal[]": "ITraversal[]", - "Predicate": "TraversalPredicate", + "Predicate": "IPredicate", "P": "TraversalPredicate", "TraversalStrategy": "ITraversalStrategy", "TraversalStrategy[]": "ITraversalStrategy[]", - "Function": "object", - "BiFunction": "object", + "Function": "IFunction", + "BiFunction": "IBiFunction", "UnaryOperator": "object", - "BinaryOperator": "object", - "Consumer": "object", + "BinaryOperator": "IBinaryOperator", + "Consumer": "IConsumer", "Supplier": "object", - "Comparator": "object", + "Comparator": "IComparator", "VertexProgram": "object"] def useE2 = ["E2", "E2"]; @@ -341,6 +341,14 @@ def toCSharpName = { enumClass, itemName -> def createEnum = { enumClass -> def b = ["enumClass": enumClass, + "implementedTypes": enumClass.getInterfaces(). + collect { it.getSimpleName() }. + findAll { toCSharpTypeMap.containsKey(it) }. + findAll { toCSharpTypeMap[it] != "object" }. + sort { a, b -> a <=> b }. + collect(["EnumWrapper"]) { typeName -> + return toCSharpTypeMap[typeName] + }.join(", "), "constants": enumClass.getEnumConstants(). sort { a, b -> a.name() <=> b.name() }. collect { value -> diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Barrier.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Barrier.cs index 6c00f9981c7..0ae4fa2f110 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Barrier.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Barrier.cs @@ -26,7 +26,7 @@ namespace Gremlin.Net.Process.Traversal { #pragma warning disable 1591 - public class Barrier : EnumWrapper + public class Barrier : EnumWrapper, IConsumer { private Barrier(string enumValue) : base("Barrier", enumValue) diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Column.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Column.cs index 3daaa4d7c2a..9fa2039065c 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Column.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Column.cs @@ -26,7 +26,7 @@ namespace Gremlin.Net.Process.Traversal { #pragma warning disable 1591 - public class Column : EnumWrapper + public class Column : EnumWrapper, IFunction { private Column(string enumValue) : base("Column", enumValue) diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GraphTraversal.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GraphTraversal.cs index d8e26ad1fa8..7100ea36f47 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GraphTraversal.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GraphTraversal.cs @@ -187,7 +187,7 @@ public GraphTraversal Barrier () /// /// Adds the barrier step to this . /// - public GraphTraversal Barrier (object barrierConsumer) + public GraphTraversal Barrier (IConsumer barrierConsumer) { Bytecode.AddStep("barrier", barrierConsumer); return Wrap(this); @@ -236,7 +236,7 @@ public GraphTraversal BothV () /// /// Adds the branch step to this . /// - public GraphTraversal Branch (object function) + public GraphTraversal Branch (IFunction function) { Bytecode.AddStep("branch", function); return Wrap(this); @@ -263,7 +263,7 @@ public GraphTraversal By () /// /// Adds the by step to this . /// - public GraphTraversal By (object comparator) + public GraphTraversal By (IComparator comparator) { Bytecode.AddStep("by", comparator); return Wrap(this); @@ -272,7 +272,16 @@ public GraphTraversal By (object comparator) /// /// Adds the by step to this . /// - public GraphTraversal By (object function, object comparator) + public GraphTraversal By (IFunction function) + { + Bytecode.AddStep("by", function); + return Wrap(this); + } + + /// + /// Adds the by step to this . + /// + public GraphTraversal By (IFunction function, IComparator comparator) { Bytecode.AddStep("by", function, comparator); return Wrap(this); @@ -299,7 +308,7 @@ public GraphTraversal By (string key) /// /// Adds the by step to this . /// - public GraphTraversal By (string key, object comparator) + public GraphTraversal By (string key, IComparator comparator) { Bytecode.AddStep("by", key, comparator); return Wrap(this); @@ -326,7 +335,7 @@ public GraphTraversal By (ITraversal traversal) /// /// Adds the by step to this . /// - public GraphTraversal By (ITraversal traversal, object comparator) + public GraphTraversal By (ITraversal traversal, IComparator comparator) { Bytecode.AddStep("by", traversal, comparator); return Wrap(this); @@ -346,7 +355,7 @@ public GraphTraversal Cap (string sideEffectKey, params string[] side /// /// Adds the choose step to this . /// - public GraphTraversal Choose (object choiceFunction) + public GraphTraversal Choose (IFunction choiceFunction) { Bytecode.AddStep("choose", choiceFunction); return Wrap(this); @@ -355,7 +364,7 @@ public GraphTraversal Choose (object choiceFunction) /// /// Adds the choose step to this . /// - public GraphTraversal Choose (TraversalPredicate choosePredicate, ITraversal trueChoice) + public GraphTraversal Choose (IPredicate choosePredicate, ITraversal trueChoice) { Bytecode.AddStep("choose", choosePredicate, trueChoice); return Wrap(this); @@ -364,7 +373,7 @@ public GraphTraversal Choose (TraversalPredicate choosePredicate, ITr /// /// Adds the choose step to this . /// - public GraphTraversal Choose (TraversalPredicate choosePredicate, ITraversal trueChoice, ITraversal falseChoice) + public GraphTraversal Choose (IPredicate choosePredicate, ITraversal trueChoice, ITraversal falseChoice) { Bytecode.AddStep("choose", choosePredicate, trueChoice, falseChoice); return Wrap(this); @@ -496,7 +505,7 @@ public GraphTraversal Emit () /// /// Adds the emit step to this . /// - public GraphTraversal Emit (TraversalPredicate emitPredicate) + public GraphTraversal Emit (IPredicate emitPredicate) { Bytecode.AddStep("emit", emitPredicate); return Wrap(this); @@ -514,7 +523,7 @@ public GraphTraversal Emit (ITraversal emitTraversal) /// /// Adds the filter step to this . /// - public GraphTraversal Filter (TraversalPredicate predicate) + public GraphTraversal Filter (IPredicate predicate) { Bytecode.AddStep("filter", predicate); return Wrap(this); @@ -532,7 +541,7 @@ public GraphTraversal Filter (ITraversal filterTraversal) /// /// Adds the flatMap step to this . /// - public GraphTraversal FlatMap (object function) + public GraphTraversal FlatMap (IFunction function) { Bytecode.AddStep("flatMap", function); return Wrap(this); @@ -559,7 +568,7 @@ public GraphTraversal> Fold () /// /// Adds the fold step to this . /// - public GraphTraversal Fold (E2 seed, object foldFunction) + public GraphTraversal Fold (E2 seed, IBiFunction foldFunction) { Bytecode.AddStep("fold", seed, foldFunction); return Wrap(this); @@ -942,7 +951,7 @@ public GraphTraversal Loops () /// /// Adds the map step to this . /// - public GraphTraversal Map (object function) + public GraphTraversal Map (IFunction function) { Bytecode.AddStep("map", function); return Wrap(this); @@ -1302,7 +1311,7 @@ public GraphTraversal Sack () /// /// Adds the sack step to this . /// - public GraphTraversal Sack (object sackOperator) + public GraphTraversal Sack (IBiFunction sackOperator) { Bytecode.AddStep("sack", sackOperator); return Wrap(this); @@ -1311,7 +1320,7 @@ public GraphTraversal Sack (object sackOperator) /// /// Adds the sack step to this . /// - public GraphTraversal Sack (object sackOperator, string elementPropertyKey) + public GraphTraversal Sack (IBiFunction sackOperator, string elementPropertyKey) { Bytecode.AddStep("sack", sackOperator, elementPropertyKey); return Wrap(this); @@ -1387,7 +1396,7 @@ public GraphTraversal> Select (string selectKey1, /// /// Adds the sideEffect step to this . /// - public GraphTraversal SideEffect (object consumer) + public GraphTraversal SideEffect (IConsumer consumer) { Bytecode.AddStep("sideEffect", consumer); return Wrap(this); @@ -1591,7 +1600,7 @@ public GraphTraversal Union (params ITraversal[] unionTraversals) /// /// Adds the until step to this . /// - public GraphTraversal Until (TraversalPredicate untilPredicate) + public GraphTraversal Until (IPredicate untilPredicate) { Bytecode.AddStep("until", untilPredicate); return Wrap(this); diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GraphTraversalSource.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GraphTraversalSource.cs index 2cbe4de47ac..9c325592122 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GraphTraversalSource.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GraphTraversalSource.cs @@ -96,7 +96,7 @@ public GraphTraversalSource WithSack(object initialValue) return source; } - public GraphTraversalSource WithSack(object initialValue, object mergeOperator) + public GraphTraversalSource WithSack(object initialValue, IBinaryOperator mergeOperator) { var source = new GraphTraversalSource(new List(TraversalStrategies), new Bytecode(Bytecode)); @@ -104,7 +104,15 @@ public GraphTraversalSource WithSack(object initialValue, object mergeOperator) return source; } - public GraphTraversalSource WithSack(object initialValue, object splitOperator, object mergeOperator) + public GraphTraversalSource WithSack(object initialValue, object splitOperator) + { + var source = new GraphTraversalSource(new List(TraversalStrategies), + new Bytecode(Bytecode)); + source.Bytecode.AddSource("withSack", initialValue, splitOperator); + return source; + } + + public GraphTraversalSource WithSack(object initialValue, object splitOperator, IBinaryOperator mergeOperator) { var source = new GraphTraversalSource(new List(TraversalStrategies), new Bytecode(Bytecode)); @@ -120,7 +128,7 @@ public GraphTraversalSource WithSideEffect(string key, object initialValue) return source; } - public GraphTraversalSource WithSideEffect(string key, object initialValue, object reducer) + public GraphTraversalSource WithSideEffect(string key, object initialValue, IBinaryOperator reducer) { var source = new GraphTraversalSource(new List(TraversalStrategies), new Bytecode(Bytecode)); diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IBiFunction.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IBiFunction.cs new file mode 100644 index 00000000000..15c9f917572 --- /dev/null +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IBiFunction.cs @@ -0,0 +1,33 @@ +#region License + +/* + * 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. + */ + +#endregion + +namespace Gremlin.Net.Process.Traversal +{ + /// + /// Represents a function that accepts two arguments and produces a result. This is the two-arity specialization of + /// . + /// + public interface IBiFunction + { + } +} \ No newline at end of file diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IBinaryOperator.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IBinaryOperator.cs new file mode 100644 index 00000000000..dcd7aed14b0 --- /dev/null +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IBinaryOperator.cs @@ -0,0 +1,34 @@ +#region License + +/* + * 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. + */ + +#endregion + +namespace Gremlin.Net.Process.Traversal +{ + /// + /// Represents an operation upon two operands of the same type, producing a result of the same type as the operands. + /// This is a specialization of for the case where the operands and the result are all of + /// the same type. + /// + public interface IBinaryOperator : IBiFunction + { + } +} \ No newline at end of file diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IComparator.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IComparator.cs new file mode 100644 index 00000000000..b7a48e0d72a --- /dev/null +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IComparator.cs @@ -0,0 +1,32 @@ +#region License + +/* + * 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. + */ + +#endregion + +namespace Gremlin.Net.Process.Traversal +{ + /// + /// A comparison function, which imposes a total ordering on some collection of objects. + /// + public interface IComparator + { + } +} \ No newline at end of file diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IConsumer.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IConsumer.cs new file mode 100644 index 00000000000..7eed5e29534 --- /dev/null +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IConsumer.cs @@ -0,0 +1,33 @@ +#region License + +/* + * 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. + */ + +#endregion + +namespace Gremlin.Net.Process.Traversal +{ + /// + /// Represents an operation that accepts a single input argument and returns no result. Unlike most other functional + /// interfaces, Consumer is expected to operate via side-effects. + /// + public interface IConsumer + { + } +} \ No newline at end of file diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IFunction.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IFunction.cs new file mode 100644 index 00000000000..075154a833d --- /dev/null +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IFunction.cs @@ -0,0 +1,32 @@ +#region License + +/* + * 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. + */ + +#endregion + +namespace Gremlin.Net.Process.Traversal +{ + /// + /// Represents a function that accepts one argument and produces a result. + /// + public interface IFunction + { + } +} \ No newline at end of file diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IPredicate.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IPredicate.cs new file mode 100644 index 00000000000..a7e5e3de329 --- /dev/null +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/IPredicate.cs @@ -0,0 +1,32 @@ +#region License + +/* + * 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. + */ + +#endregion + +namespace Gremlin.Net.Process.Traversal +{ + /// + /// Represents a predicate (boolean-valued function) of one argument. + /// + public interface IPredicate + { + } +} \ No newline at end of file diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Operator.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Operator.cs index 699a5a5a54f..411619767cc 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Operator.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Operator.cs @@ -26,7 +26,7 @@ namespace Gremlin.Net.Process.Traversal { #pragma warning disable 1591 - public class Operator : EnumWrapper + public class Operator : EnumWrapper, IBinaryOperator { private Operator(string enumValue) : base("Operator", enumValue) diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Order.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Order.cs index 93bf7bca654..d20906affaf 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Order.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Order.cs @@ -26,7 +26,7 @@ namespace Gremlin.Net.Process.Traversal { #pragma warning disable 1591 - public class Order : EnumWrapper + public class Order : EnumWrapper, IComparator { private Order(string enumValue) : base("Order", enumValue) diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/T.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/T.cs index 9eba458dd85..c0e5927024c 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/T.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/T.cs @@ -26,7 +26,7 @@ namespace Gremlin.Net.Process.Traversal { #pragma warning disable 1591 - public class T : EnumWrapper + public class T : EnumWrapper, IFunction { private T(string enumValue) : base("T", enumValue) diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/TraversalPredicate.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/TraversalPredicate.cs index b854213f8b3..e8b5be8e647 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/TraversalPredicate.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/TraversalPredicate.cs @@ -26,7 +26,7 @@ namespace Gremlin.Net.Process.Traversal /// /// Represents a predicate (boolean-valued function) used in a . /// - public class TraversalPredicate + public class TraversalPredicate : IPredicate { /// /// Initializes a new instance of the class. diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/__.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/__.cs index 1788badc1f5..9620e7fb0c8 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/__.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/__.cs @@ -153,7 +153,7 @@ public static GraphTraversal Barrier() /// /// Spawns a and adds the barrier step to that traversal. /// - public static GraphTraversal Barrier(object barrierConsumer) + public static GraphTraversal Barrier(IConsumer barrierConsumer) { return new GraphTraversal().Barrier(barrierConsumer); } @@ -197,7 +197,7 @@ public static GraphTraversal BothV() /// /// Spawns a and adds the branch step to that traversal. /// - public static GraphTraversal Branch(object function) + public static GraphTraversal Branch(IFunction function) { return new GraphTraversal().Branch(function); } @@ -223,7 +223,7 @@ public static GraphTraversal Cap(string sideEffectKey, params st /// /// Spawns a and adds the choose step to that traversal. /// - public static GraphTraversal Choose(object choiceFunction) + public static GraphTraversal Choose(IFunction choiceFunction) { return new GraphTraversal().Choose(choiceFunction); } @@ -231,7 +231,7 @@ public static GraphTraversal Choose(object choiceFunction) /// /// Spawns a and adds the choose step to that traversal. /// - public static GraphTraversal Choose(TraversalPredicate choosePredicate, ITraversal trueChoice) + public static GraphTraversal Choose(IPredicate choosePredicate, ITraversal trueChoice) { return new GraphTraversal().Choose(choosePredicate, trueChoice); } @@ -239,7 +239,7 @@ public static GraphTraversal Choose(TraversalPredicate choosePre /// /// Spawns a and adds the choose step to that traversal. /// - public static GraphTraversal Choose(TraversalPredicate choosePredicate, ITraversal trueChoice, ITraversal falseChoice) + public static GraphTraversal Choose(IPredicate choosePredicate, ITraversal trueChoice, ITraversal falseChoice) { return new GraphTraversal().Choose(choosePredicate, trueChoice, falseChoice); } @@ -357,7 +357,7 @@ public static GraphTraversal Emit() /// /// Spawns a and adds the emit step to that traversal. /// - public static GraphTraversal Emit(TraversalPredicate emitPredicate) + public static GraphTraversal Emit(IPredicate emitPredicate) { return new GraphTraversal().Emit(emitPredicate); } @@ -373,7 +373,7 @@ public static GraphTraversal Emit(ITraversal emitTraversal) /// /// Spawns a and adds the filter step to that traversal. /// - public static GraphTraversal Filter(TraversalPredicate predicate) + public static GraphTraversal Filter(IPredicate predicate) { return new GraphTraversal().Filter(predicate); } @@ -389,7 +389,7 @@ public static GraphTraversal Filter(ITraversal filterTraversal) /// /// Spawns a and adds the flatMap step to that traversal. /// - public static GraphTraversal FlatMap(object function) + public static GraphTraversal FlatMap(IFunction function) { return new GraphTraversal().FlatMap(function); } @@ -413,7 +413,7 @@ public static GraphTraversal> Fold() /// /// Spawns a and adds the fold step to that traversal. /// - public static GraphTraversal Fold(E2 seed, object foldFunction) + public static GraphTraversal Fold(E2 seed, IBiFunction foldFunction) { return new GraphTraversal().Fold(seed, foldFunction); } @@ -739,7 +739,7 @@ public static GraphTraversal Loops() /// /// Spawns a and adds the map step to that traversal. /// - public static GraphTraversal Map(object function) + public static GraphTraversal Map(IFunction function) { return new GraphTraversal().Map(function); } @@ -997,7 +997,7 @@ public static GraphTraversal Sack() /// /// Spawns a and adds the sack step to that traversal. /// - public static GraphTraversal Sack(object sackOperator) + public static GraphTraversal Sack(IBiFunction sackOperator) { return new GraphTraversal().Sack(sackOperator); } @@ -1005,7 +1005,7 @@ public static GraphTraversal Sack(object sackOperator) /// /// Spawns a and adds the sack step to that traversal. /// - public static GraphTraversal Sack(object sackOperator, string elementPropertyKey) + public static GraphTraversal Sack(IBiFunction sackOperator, string elementPropertyKey) { return new GraphTraversal().Sack(sackOperator, elementPropertyKey); } @@ -1073,7 +1073,7 @@ public static GraphTraversal> Select(string /// /// Spawns a and adds the sideEffect step to that traversal. /// - public static GraphTraversal SideEffect(object consumer) + public static GraphTraversal SideEffect(IConsumer consumer) { return new GraphTraversal().SideEffect(consumer); } @@ -1239,7 +1239,7 @@ public static GraphTraversal Union(params ITraversal[] traversal /// /// Spawns a and adds the until step to that traversal. /// - public static GraphTraversal Until(TraversalPredicate untilPredicate) + public static GraphTraversal Until(IPredicate untilPredicate) { return new GraphTraversal().Until(untilPredicate); } From 4d5768b0aa53e06e95738f3faf904d3290a91794 Mon Sep 17 00:00:00 2001 From: Florian Hockmann Date: Mon, 12 Mar 2018 16:37:30 +0100 Subject: [PATCH 3/4] TINKERPOP-1901 Use spaces to indent enum values Previously, the first enum value used spaces, whereas all following values used tabs. Now, spaces are used consistently. --- gremlin-dotnet/glv/Enum.template | 4 ++-- gremlin-dotnet/glv/generate.groovy | 2 +- .../Gremlin.Net/Process/Traversal/Barrier.cs | 2 +- .../Process/Traversal/Cardinality.cs | 8 +++---- .../Gremlin.Net/Process/Traversal/Column.cs | 6 ++--- .../Process/Traversal/Direction.cs | 8 +++---- .../Process/Traversal/GraphSONVersion.cs | 6 ++--- .../Process/Traversal/GryoVersion.cs | 2 +- .../Gremlin.Net/Process/Traversal/Operator.cs | 24 +++++++++---------- .../Gremlin.Net/Process/Traversal/Order.cs | 16 ++++++------- .../src/Gremlin.Net/Process/Traversal/Pick.cs | 6 ++--- .../src/Gremlin.Net/Process/Traversal/Pop.cs | 8 +++---- .../Gremlin.Net/Process/Traversal/Scope.cs | 6 ++--- .../src/Gremlin.Net/Process/Traversal/T.cs | 10 ++++---- 14 files changed, 54 insertions(+), 54 deletions(-) diff --git a/gremlin-dotnet/glv/Enum.template b/gremlin-dotnet/glv/Enum.template index 0be23c786c1..1f110768455 100644 --- a/gremlin-dotnet/glv/Enum.template +++ b/gremlin-dotnet/glv/Enum.template @@ -32,8 +32,8 @@ namespace Gremlin.Net.Process.Traversal : base("<%= enumClass.simpleName %>", enumValue) { } - - <%= constants %> +<% constants.each { constant -> %> + <%= constant %><%}%> } #pragma warning restore 1591 diff --git a/gremlin-dotnet/glv/generate.groovy b/gremlin-dotnet/glv/generate.groovy index 6d89b2f958a..040430754a0 100644 --- a/gremlin-dotnet/glv/generate.groovy +++ b/gremlin-dotnet/glv/generate.groovy @@ -354,7 +354,7 @@ def createEnum = { enumClass -> collect { value -> def csharpName = toCSharpName(enumClass, value.name()) return "public static ${enumClass.simpleName} ${csharpName} => new ${enumClass.simpleName}(\"${value.name()}\");" - }.join("\n\t\t")] + }] def enumTemplate = engine.createTemplate(new File("${projectBaseDir}/glv/Enum.template")).make(b) def enumFile = new File("${projectBaseDir}/src/Gremlin.Net/Process/Traversal/" + enumClass.getSimpleName() + ".cs") diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Barrier.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Barrier.cs index 0ae4fa2f110..df6d349b370 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Barrier.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Barrier.cs @@ -32,7 +32,7 @@ private Barrier(string enumValue) : base("Barrier", enumValue) { } - + public static Barrier NormSack => new Barrier("normSack"); } diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Cardinality.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Cardinality.cs index f158153a44d..d2cbcf2b311 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Cardinality.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Cardinality.cs @@ -32,10 +32,10 @@ private Cardinality(string enumValue) : base("Cardinality", enumValue) { } - - public static Cardinality List => new Cardinality("list"); - public static Cardinality Set => new Cardinality("set"); - public static Cardinality Single => new Cardinality("single"); + + public static Cardinality List => new Cardinality("list"); + public static Cardinality Set => new Cardinality("set"); + public static Cardinality Single => new Cardinality("single"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Column.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Column.cs index 9fa2039065c..9e0451e401d 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Column.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Column.cs @@ -32,9 +32,9 @@ private Column(string enumValue) : base("Column", enumValue) { } - - public static Column Keys => new Column("keys"); - public static Column Values => new Column("values"); + + public static Column Keys => new Column("keys"); + public static Column Values => new Column("values"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Direction.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Direction.cs index 9566d512e3f..5a93f0a7242 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Direction.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Direction.cs @@ -32,10 +32,10 @@ private Direction(string enumValue) : base("Direction", enumValue) { } - - public static Direction Both => new Direction("BOTH"); - public static Direction In => new Direction("IN"); - public static Direction Out => new Direction("OUT"); + + public static Direction Both => new Direction("BOTH"); + public static Direction In => new Direction("IN"); + public static Direction Out => new Direction("OUT"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GraphSONVersion.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GraphSONVersion.cs index ad73ad1eb85..0c926070692 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GraphSONVersion.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GraphSONVersion.cs @@ -32,9 +32,9 @@ private GraphSONVersion(string enumValue) : base("GraphSONVersion", enumValue) { } - - public static GraphSONVersion V1_0 => new GraphSONVersion("V1_0"); - public static GraphSONVersion V2_0 => new GraphSONVersion("V2_0"); + + public static GraphSONVersion V1_0 => new GraphSONVersion("V1_0"); + public static GraphSONVersion V2_0 => new GraphSONVersion("V2_0"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GryoVersion.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GryoVersion.cs index 45387e27a10..fcc746e514b 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GryoVersion.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/GryoVersion.cs @@ -32,7 +32,7 @@ private GryoVersion(string enumValue) : base("GryoVersion", enumValue) { } - + public static GryoVersion V1_0 => new GryoVersion("V1_0"); } diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Operator.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Operator.cs index 411619767cc..8f0c9b33df0 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Operator.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Operator.cs @@ -32,18 +32,18 @@ private Operator(string enumValue) : base("Operator", enumValue) { } - - public static Operator AddAll => new Operator("addAll"); - public static Operator And => new Operator("and"); - public static Operator Assign => new Operator("assign"); - public static Operator Div => new Operator("div"); - public static Operator Max => new Operator("max"); - public static Operator Min => new Operator("min"); - public static Operator Minus => new Operator("minus"); - public static Operator Mult => new Operator("mult"); - public static Operator Or => new Operator("or"); - public static Operator Sum => new Operator("sum"); - public static Operator SumLong => new Operator("sumLong"); + + public static Operator AddAll => new Operator("addAll"); + public static Operator And => new Operator("and"); + public static Operator Assign => new Operator("assign"); + public static Operator Div => new Operator("div"); + public static Operator Max => new Operator("max"); + public static Operator Min => new Operator("min"); + public static Operator Minus => new Operator("minus"); + public static Operator Mult => new Operator("mult"); + public static Operator Or => new Operator("or"); + public static Operator Sum => new Operator("sum"); + public static Operator SumLong => new Operator("sumLong"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Order.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Order.cs index d20906affaf..44ccb9bed2d 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Order.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Order.cs @@ -32,14 +32,14 @@ private Order(string enumValue) : base("Order", enumValue) { } - - public static Order Decr => new Order("decr"); - public static Order Incr => new Order("incr"); - public static Order KeyDecr => new Order("keyDecr"); - public static Order KeyIncr => new Order("keyIncr"); - public static Order Shuffle => new Order("shuffle"); - public static Order ValueDecr => new Order("valueDecr"); - public static Order ValueIncr => new Order("valueIncr"); + + public static Order Decr => new Order("decr"); + public static Order Incr => new Order("incr"); + public static Order KeyDecr => new Order("keyDecr"); + public static Order KeyIncr => new Order("keyIncr"); + public static Order Shuffle => new Order("shuffle"); + public static Order ValueDecr => new Order("valueDecr"); + public static Order ValueIncr => new Order("valueIncr"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Pick.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Pick.cs index a47d4a6eb97..d4132ea806a 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Pick.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Pick.cs @@ -32,9 +32,9 @@ private Pick(string enumValue) : base("Pick", enumValue) { } - - public static Pick Any => new Pick("any"); - public static Pick None => new Pick("none"); + + public static Pick Any => new Pick("any"); + public static Pick None => new Pick("none"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Pop.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Pop.cs index 9e97a09de93..8a157c5227c 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Pop.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Pop.cs @@ -32,10 +32,10 @@ private Pop(string enumValue) : base("Pop", enumValue) { } - - public static Pop All => new Pop("all"); - public static Pop First => new Pop("first"); - public static Pop Last => new Pop("last"); + + public static Pop All => new Pop("all"); + public static Pop First => new Pop("first"); + public static Pop Last => new Pop("last"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Scope.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Scope.cs index 65a6e67bb61..ee67c390f96 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Scope.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/Scope.cs @@ -32,9 +32,9 @@ private Scope(string enumValue) : base("Scope", enumValue) { } - - public static Scope Global => new Scope("global"); - public static Scope Local => new Scope("local"); + + public static Scope Global => new Scope("global"); + public static Scope Local => new Scope("local"); } #pragma warning restore 1591 diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/T.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/T.cs index c0e5927024c..bae131492de 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/T.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/T.cs @@ -32,11 +32,11 @@ private T(string enumValue) : base("T", enumValue) { } - - public static T Id => new T("id"); - public static T Key => new T("key"); - public static T Label => new T("label"); - public static T Value => new T("value"); + + public static T Id => new T("id"); + public static T Key => new T("key"); + public static T Label => new T("label"); + public static T Value => new T("value"); } #pragma warning restore 1591 From df989dc40ab7cf834b6ed275c453c4bb220315bf Mon Sep 17 00:00:00 2001 From: Florian Hockmann Date: Tue, 13 Mar 2018 19:26:00 +0100 Subject: [PATCH 4/4] TINKERPOP-1901 Describe improved Gremlin.Net tokens in upgrade docs --- docs/src/upgrade/release-3.2.x-incubating.asciidoc | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/src/upgrade/release-3.2.x-incubating.asciidoc b/docs/src/upgrade/release-3.2.x-incubating.asciidoc index 7d2a80c6197..0cb8ddc10af 100644 --- a/docs/src/upgrade/release-3.2.x-incubating.asciidoc +++ b/docs/src/upgrade/release-3.2.x-incubating.asciidoc @@ -42,6 +42,17 @@ by clients that might mysteriously disappear without properly closing their conn See: link:https://issues.apache.org/jira/browse/TINKERPOP-1726[TINKERPOP-1726] +==== Gremlin.Net Tokens Improved + +The various Gremlin tokens (e.g. `T`, `Order`, `Operator`, etc.) that were implemented as Enums before in Gremlin.Net +are now implemented as classes. This mainly allows them to implement interfaces which their Java counterparts already +did. `T` for example now implements the new interface `IFunction` which simply mirrors its Java counterpart `Function`. +Steps that expect objects for those interfaces as arguments now explicitly use the interface. Before, they used just +`object` as the type for these arguments which made it hard for users to know what kind of `object` they can use. +However, usage of these tokens themselves shouldn't change at all (e.g. `T.Id` is still `T.Id`). + +See: link:https://issues.apache.org/jira/browse/TINKERPOP-1901[TINKERPOP-1901] + === Upgrading for Providers ==== Graph System Providers