Repository: tinkerpop Updated Branches: refs/heads/master 23ca117b9 -> 034a3be7b
TINKERPOP-1866 Add a deserializer for g:T to Gremlin.Net Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/4016c80b Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/4016c80b Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/4016c80b Branch: refs/heads/master Commit: 4016c80b1dac10aff1509dbfabf268d4bb9b5c50 Parents: ffabea8 Author: Florian Hockmann <f...@florian-hockmann.de> Authored: Tue Mar 20 15:45:11 2018 +0100 Committer: Stephen Mallette <sp...@genoprime.com> Committed: Thu Mar 22 16:50:15 2018 -0400 ---------------------------------------------------------------------- .../Process/Traversal/EnumWrapper.cs | 31 ++++++++++++++- .../Structure/IO/GraphSON/GraphSONReader.cs | 1 + .../Structure/IO/GraphSON/TDeserializer.cs | 42 ++++++++++++++++++++ .../Gherkin/CommonSteps.cs | 4 +- .../Gherkin/GherkinTestRunner.cs | 4 -- .../IO/GraphSON/GraphSONReaderTests.cs | 12 ++++++ 6 files changed, 88 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/4016c80b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/EnumWrapper.cs ---------------------------------------------------------------------- diff --git a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/EnumWrapper.cs b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/EnumWrapper.cs index 66b8c5a..20a0348 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/EnumWrapper.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Process/Traversal/EnumWrapper.cs @@ -21,12 +21,14 @@ #endregion +using System; + namespace Gremlin.Net.Process.Traversal { /// <summary> /// Represents an enum. /// </summary> - public abstract class EnumWrapper + public abstract class EnumWrapper : IEquatable<EnumWrapper> { /// <summary> /// Gets the name of the enum. @@ -48,5 +50,32 @@ namespace Gremlin.Net.Process.Traversal EnumName = enumName; EnumValue = enumValue; } + + /// <inheritdoc /> + public bool Equals(EnumWrapper other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return string.Equals(EnumName, other.EnumName) && string.Equals(EnumValue, other.EnumValue); + } + + /// <inheritdoc /> + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((EnumWrapper) obj); + } + + /// <inheritdoc /> + public override int GetHashCode() + { + unchecked + { + return ((EnumName != null ? EnumName.GetHashCode() : 0) * 397) ^ + (EnumValue != null ? EnumValue.GetHashCode() : 0); + } + } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/4016c80b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONReader.cs ---------------------------------------------------------------------- diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONReader.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONReader.cs index fbec67d..94fcd8d 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONReader.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONReader.cs @@ -52,6 +52,7 @@ namespace Gremlin.Net.Structure.IO.GraphSON {"g:Property", new PropertyDeserializer()}, {"g:VertexProperty", new VertexPropertyDeserializer()}, {"g:Path", new PathDeserializer()}, + {"g:T", new TDeserializer()}, {"gx:BigDecimal", new DecimalConverter()} }; http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/4016c80b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/TDeserializer.cs ---------------------------------------------------------------------- diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/TDeserializer.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/TDeserializer.cs new file mode 100644 index 0000000..e79783c --- /dev/null +++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/TDeserializer.cs @@ -0,0 +1,42 @@ +#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; +using System.Linq; +using System.Reflection; +using Gremlin.Net.Process.Traversal; +using Newtonsoft.Json.Linq; + +namespace Gremlin.Net.Structure.IO.GraphSON +{ + internal class TDeserializer : IGraphSONDeserializer + { + public dynamic Objectify(JToken graphsonObject, GraphSONReader reader) + { + var tValue = graphsonObject.ToString(); + return typeof(T).GetProperties() + .First(p => string.Equals(p.Name, tValue, StringComparison.OrdinalIgnoreCase)) + .GetValue(null); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/4016c80b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/CommonSteps.cs ---------------------------------------------------------------------- diff --git a/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/CommonSteps.cs b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/CommonSteps.cs index 9322da3..31948f4 100644 --- a/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/CommonSteps.cs +++ b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/CommonSteps.cs @@ -26,6 +26,7 @@ using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Reflection; using System.Text.RegularExpressions; using Gherkin.Ast; using Gremlin.Net.IntegrationTest.Gherkin.Attributes; @@ -239,7 +240,8 @@ namespace Gremlin.Net.IntegrationTest.Gherkin private static object ToT(string enumName, string graphName) { - return Enum.Parse(typeof(T), TraversalParser.GetCsharpName(enumName)); + return typeof(T).GetProperties() + .First(p => string.Equals(p.Name, enumName, StringComparison.OrdinalIgnoreCase)).GetValue(null); } private static object ToNumber(string stringNumber, string graphName) http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/4016c80b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/GherkinTestRunner.cs ---------------------------------------------------------------------- diff --git a/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/GherkinTestRunner.cs b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/GherkinTestRunner.cs index 52646a7..68d10fe 100644 --- a/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/GherkinTestRunner.cs +++ b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/GherkinTestRunner.cs @@ -40,10 +40,6 @@ namespace Gremlin.Net.IntegrationTest.Gherkin private static readonly IDictionary<string, IgnoreReason> IgnoredScenarios = new Dictionary<string, IgnoreReason> { - { "g_V_valueMapXtrueX", IgnoreReason.TraversalTDeserializationNotSupported }, // TINKERPOP-1866 - { "g_V_valueMapXtrue_name_ageX", IgnoreReason.TraversalTDeserializationNotSupported }, // TINKERPOP-1866 - { "g_V_hasLabelXpersonX_filterXoutEXcreatedXX_valueMapXtrueX", IgnoreReason.TraversalTDeserializationNotSupported }, // TINKERPOP-1866 - {"g_V_asXaX_hasXname_markoX_outXcreatedX_asXbX_addVXselectXaX_labelX_propertyXtest_selectXbX_labelX_valueMapXtrueX", IgnoreReason.TraversalTDeserializationNotSupported}, {"g_V_storeXaX_byXoutEXcreatedX_countX_out_out_storeXaX_byXinEXcreatedX_weight_sumX", IgnoreReason.ReceivedDataDoesntMatchExpected} }; http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/4016c80b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs ---------------------------------------------------------------------- diff --git a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs index ae4392b..c93630f 100644 --- a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs +++ b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs @@ -23,6 +23,7 @@ using System; using System.Collections.Generic; +using Gremlin.Net.Process.Traversal; using Gremlin.Net.Structure; using Gremlin.Net.Structure.IO.GraphSON; using Moq; @@ -220,6 +221,17 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON Assert.Equal(new List<object> {5, 6}, deserializedValue); } + [Theory, MemberData(nameof(Versions))] + public void ShouldDeserializeT(int version) + { + var graphSon = "{\"@type\":\"g:T\",\"@value\":\"label\"}"; + var reader = CreateStandardGraphSONReader(version); + + T readT = reader.ToObject(JObject.Parse(graphSon)); + + Assert.Equal(T.Label, readT); + } + [Fact] public void ShouldDeserializePathFromGraphSON2() {