Skip to content

Commit

Permalink
Ensure that different StepLabels, even if mapped to the same name, ar…
Browse files Browse the repository at this point in the history
…e distinguishable by their names in gremlin.
  • Loading branch information
danielcweber committed Mar 15, 2019
1 parent eeec621 commit 8155779
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 16 deletions.
8 changes: 4 additions & 4 deletions ExRam.Gremlinq.Core.Tests/GroovySerializationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public void As_inlined_nested_Select()
.Select(stepLabel1, stepLabel2)))
.Should()
.SerializeToGroovy<TVisitor>("g.V().hasLabel(_a).as(_b).as(_c).select(_b, _c)")
.WithParameters("Person", "Item1", "Item2");
.WithParameters("Person", "<1>Item1", "<2>Item2");
}

[Fact]
Expand Down Expand Up @@ -756,7 +756,7 @@ public void Map_Select_operation()
.Select(stepLabel1, stepLabel2))))
.Should()
.SerializeToGroovy<TVisitor>("g.V().hasLabel(_a).as(_b).as(_c).map(__.select(_b, _c))")
.WithParameters("Person", "Item1", "Item2");
.WithParameters("Person", "<1>Item1", "<2>Item2");
}

[Fact]
Expand Down Expand Up @@ -785,8 +785,8 @@ public void Nested_Select_operations()
.As((___, tuple) => ___
.Select(stepLabel1, tuple))))
.Should()
.SerializeToGroovy<TVisitor>("g.V().hasLabel(_a).as(_b).as(_c).select(_b, _c).as(_c).select(_b, _c)")
.WithParameters("Person", "Item1", "Item2");
.SerializeToGroovy<TVisitor>("g.V().hasLabel(_a).as(_b).as(_c).select(_b, _c).as(_d).select(_b, _d)")
.WithParameters("Person", "<1>Item1", "<2>Item2", "<3>Item2");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ private enum State
}

private State _state = State.Idle;
private IGremlinQueryAdmin _currentAdmin;

private readonly StringBuilder _builder = new StringBuilder();
private readonly Stack<State> _stateQueue = new Stack<State>();
Expand Down Expand Up @@ -618,22 +619,20 @@ public virtual void Visit(ValueMapStep step)

public virtual void Visit(IGremlinQuery query)
{
var admin = query.AsAdmin();

foreach (var map in admin.StepLabelMappings)
{
_stepLabelMappings[map.Key] = map.Value;
}

var beforeState = _state;
_state = State.Idle;

foreach (var step in admin.Steps.HandleAnonymousQueries().WorkaroundTINKERPOP_2112())
var beforeAdmin = _currentAdmin;
{
step.Accept(this);
_state = State.Idle;
_currentAdmin = query.AsAdmin();

foreach (var step in _currentAdmin.Steps.HandleAnonymousQueries().WorkaroundTINKERPOP_2112())
{
step.Accept(this);
}
}

_state = beforeState;
_currentAdmin = beforeAdmin;
}

#endregion
Expand Down Expand Up @@ -825,7 +824,11 @@ protected string Cache(object constant)
{
if (!_stepLabelMappings.TryGetValue(stepLabel, out var stepLabelMapping))
{
stepLabelMapping = "l" + (_stepLabelMappings.Count + 1);
if (_currentAdmin != null && _currentAdmin.StepLabelMappings.TryGetValue(stepLabel, out var queryStepLabelMapping))
stepLabelMapping = $"<{_stepLabelMappings.Count + 1}>{queryStepLabelMapping}";
else
stepLabelMapping = "l" + (_stepLabelMappings.Count + 1);

_stepLabelMappings.Add(stepLabel, stepLabelMapping);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[
{
"<1>Item1": {
"id": 16,
"label": "Person",
"type": "vertex",
"properties": {
"Name": [
{
"id": 1,
"value": "Name of some base entity"
}
],
"Age": [
{
"id": 2,
"value": "36"
}
]
}
},
"<2>Item2": {
"id": 17,
"label": "Language",
"type": "vertex",
"properties": {
"IetfLanguageTag": [
{
"id": 3,
"value": "de"
}
]
}
}
}
]
19 changes: 19 additions & 0 deletions ExRam.Gremlinq.Providers.Tests/JsonSupportTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ private sealed class MetaPoco
private static readonly string CountryWithMetaProperties;
private static readonly string NestedArrayOfLanguagesJson;
private static readonly string SingleTimeFrameWithNumbersJson;
private static readonly string EscapedTupleOfPersonLanguageJson;
private static readonly string SinglePersonWithoutPhoneNumbersJson;
private static readonly string SinglePersonLowercasePropertiesJson;
private static readonly string Graphson3TupleOfPersonLanguageJson;
Expand All @@ -64,6 +65,7 @@ static JsonSupportTest()
SinglePersonLowercasePropertiesJson = GetJson("Single_Person_lowercase_properties");
SinglePersonWithoutPhoneNumbersJson = GetJson("Single_Person_without_PhoneNumbers");
TupleOfPersonLanguageJson = GetJson("Tuple_of_Person_Language");
EscapedTupleOfPersonLanguageJson = GetJson("Escaped_Tuple_of_Person_Language");
ArrayOfLanguages = GetJson("Array_of_Languages");
NestedArrayOfLanguagesJson = GetJson("Nested_array_of_Languages");
SingleTimeFrameJson = GetJson("Single_TimeFrame");
Expand Down Expand Up @@ -398,6 +400,23 @@ public async Task Tuple()
tuple.Item2.IetfLanguageTag.Should().Be("de");
}

[Fact]
public async Task Escaped_Tuple()
{
var tuple = await _g
.WithExecutor(new TestJsonQueryExecutor(EscapedTupleOfPersonLanguageJson))
.V()
.Cast<(Person, Language)>()
.First();

tuple.Item1.Id.Should().Be(16);
tuple.Item1.Name.Value.Should().Be("Name of some base entity");
tuple.Item1.Age.Should().Be(36);

tuple.Item2.Id.Should().Be(17);
tuple.Item2.IetfLanguageTag.Should().Be("de");
}

[Fact]
public async Task Tuple_vertex_vertex()
{
Expand Down
19 changes: 19 additions & 0 deletions ExRam.Gremlinq.Providers/Json/JsonReaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ public override bool Read()

return false;
}

public override object Value
{
get
{
if (TokenType == JsonToken.PropertyName && base.Value is string propertyName)
{
if (propertyName.StartsWith("<"))
{
var closing = propertyName.IndexOf(">");

if (closing > -1)
return propertyName.Substring(closing + 1);
}
}

return base.Value;
}
}
}

public static IEnumerable<(JsonToken tokenType, object tokenValue)> ToTokenEnumerable(this JsonReader jsonReader)
Expand Down

0 comments on commit 8155779

Please sign in to comment.