Skip to content

Commit

Permalink
Merge pull request #48 from SamHard/issue16-serializing-subquery
Browse files Browse the repository at this point in the history
Issue #16: Fix query serialization of anonymous traversals
  • Loading branch information
evo-terren authored Mar 7, 2019
2 parents 5e05542 + 472ac91 commit 94e1fe8
Show file tree
Hide file tree
Showing 2 changed files with 439 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/Gremlin.Net.CosmosDb/Serialization/GremlinQuerySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ internal sealed class GremlinQuerySerializer : IDisposable
DateTimeZoneHandling = DateTimeZoneHandling.Utc
};

private static readonly string[] INSTRUCTIONS_REQUIRING_DOUBLE_UNDERSCORES_FOR_ANONYMOUS
= new[] { "in", "not", "V" };

private readonly JsonSerializerSettings _serializerSettings;
private readonly TextWriter _writer;

Expand Down Expand Up @@ -62,18 +65,39 @@ public GremlinQuerySerializer(TextWriter writer, JsonSerializerSettings serializ
/// <param name="traversal">The traversal.</param>
/// <exception cref="ArgumentNullException">traversal</exception>
public void Serialize(ITraversal traversal)
{
Serialize(traversal, startAnonTraversal: false);
}

/// <summary>
/// Serializes the specified traversal.
/// </summary>
/// <param name="traversal">The traversal.</param>
/// <param name="startAnonTraversal">true indicates that we're starting an anonymous traversal</param>
/// <exception cref="ArgumentNullException">traversal</exception>
private void Serialize(ITraversal traversal, bool startAnonTraversal)
{
if (traversal == null)
throw new ArgumentNullException(nameof(traversal));

Serialize(traversal.Bytecode);
Serialize(traversal.Bytecode, startAnonTraversal);
}

/// <summary>
/// Serializes the specified bytecode.
/// </summary>
/// <param name="bytecode">The bytecode.</param>
private void Serialize(Bytecode bytecode)
{
Serialize(bytecode, startAnonTraversal: false);
}

/// <summary>
/// Serializes the specified bytecode.
/// </summary>
/// <param name="bytecode">The bytecode.</param>
/// <param name="startAnonTraversal">true indicates that we're starting an anonymous traversal</param>
private void Serialize(Bytecode bytecode, bool startAnonTraversal)
{
var first = true;
foreach (var instr in bytecode.SourceInstructions)
Expand All @@ -84,16 +108,25 @@ private void Serialize(Bytecode bytecode)
_writer.Write(instr.OperatorName);

first = false;
startAnonTraversal = false;
}

foreach (var instr in bytecode.StepInstructions)
{
if (!first)
{
_writer.Write('.');
}
else if (startAnonTraversal
&& INSTRUCTIONS_REQUIRING_DOUBLE_UNDERSCORES_FOR_ANONYMOUS.Contains(instr.OperatorName))
{
_writer.Write("__.");
}

Serialize(instr);

first = false;
startAnonTraversal = false;
}
}

Expand Down Expand Up @@ -167,6 +200,8 @@ private void Serialize(P predicate)
var predicateValues = predicate.Value as IEnumerable<dynamic>;
if (predicateValues?.Any() ?? false)
SerializeListWithCommas(predicateValues);
else if (predicate.Value is ITraversal traversal)
Serialize(traversal, startAnonTraversal: true);
else
Serialize(predicate.Value);
}
Expand Down Expand Up @@ -223,7 +258,10 @@ private void SerializeListWithCommas(IEnumerable<dynamic> list)
if (addComma)
_writer.Write(',');

Serialize(value);
if (value is ITraversal traversal)
Serialize(traversal, startAnonTraversal: true);
else
Serialize(value);

addComma = true;
}
Expand Down
Loading

0 comments on commit 94e1fe8

Please sign in to comment.