Skip to content

Commit

Permalink
Merge pull request #44 from fauna/bt-4535-null-document
Browse files Browse the repository at this point in the history
Add support for null documents
  • Loading branch information
pnwpedro authored Jan 4, 2024
2 parents 39dfcf6 + 4a0c336 commit 31f9f15
Showing 8 changed files with 134 additions and 10 deletions.
41 changes: 39 additions & 2 deletions Fauna.Test/Serialization/Serializer.Deserialize.Tests.cs
Original file line number Diff line number Diff line change
@@ -214,11 +214,30 @@ public void DeserializeRef()
}
}";

var actual = Deserialize<Ref>(given);
var actual = Deserialize<DocumentRef>(given);
Assert.AreEqual("123", actual.Id);
Assert.AreEqual(new Module("MyColl"), actual.Collection);
}

[Test]
public void DeserializeNullRef()
{
const string given = @"
{
""@ref"":{
""id"":""123"",
""coll"":{""@mod"":""MyColl""},
""exists"":false,
""reason"":""not found""
}
}";

var actual = Deserialize<NullDocumentRef>(given);
Assert.AreEqual("123", actual.Id);
Assert.AreEqual(new Module("MyColl"), actual.Collection);
Assert.AreEqual("not found", actual.Reason);
}

[Test]
public void DeserializeNamedRef()
{
@@ -230,11 +249,29 @@ public void DeserializeNamedRef()
}
}";

var actual = Deserialize<NamedRef>(given);
var actual = Deserialize<NamedDocumentRef>(given);
Assert.AreEqual("RefName", actual.Name);
Assert.AreEqual(new Module("MyColl"), actual.Collection);
}

[Test]
public void DeserializeNullNamedRef()
{
const string given = @"
{
""@ref"":{
""name"":""RefName"",
""coll"":{""@mod"":""MyColl""},
""exists"":false,
""reason"":""not found""
}
}";

var actual = Deserialize<NullNamedDocumentRef>(given);
Assert.AreEqual("RefName", actual.Name);
Assert.AreEqual(new Module("MyColl"), actual.Collection);
Assert.AreEqual("not found", actual.Reason);
}

[Test]
public void DeserializeObject()
16 changes: 16 additions & 0 deletions Fauna.Test/Types/NullDocumentRef.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Fauna.Types;
using NUnit.Framework;

namespace Fauna.Test.Types;

[TestFixture]
public class NullDocumentRefTests
{

[Test]
public void ReasonDefaultTest()
{
var doc = new NullDocumentRef();
Assert.IsNull(doc.Reason);
}
}
16 changes: 16 additions & 0 deletions Fauna.Test/Types/NullNamedDocumentRef.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Fauna.Types;
using NUnit.Framework;

namespace Fauna.Test.Types;

[TestFixture]
public class NullNamedDocumentRefTests
{

[Test]
public void ExistsAndReasonDefaultTest()
{
var doc = new NullNamedDocumentRef();
Assert.IsNull(doc.Reason);
}
}
41 changes: 36 additions & 5 deletions Fauna/Serialization/Serializer.Deserialize.cs
Original file line number Diff line number Diff line change
@@ -39,14 +39,16 @@ public static T Deserialize<T>(SerializationContext context, ref Utf8FaunaReader
private static object? DeserializeRefInternal(ref Utf8FaunaReader reader, SerializationContext context,
Type? targetType = null)
{
if (targetType != null && targetType != typeof(Ref) && targetType != typeof(NamedRef))
if (targetType != null && targetType != typeof(DocumentRef) && targetType != typeof(NamedDocumentRef) && targetType != typeof(NullDocumentRef) && targetType != typeof(NullNamedDocumentRef))
{
throw new ArgumentException($"Unsupported target type for ref. Must be a ref or undefined, but was {targetType}");
}

string? id = null;
string? name = null;
Module? coll = null;
var exists = true;
string? reason = null;
var allProps = new Dictionary<string, object?>();


@@ -70,6 +72,14 @@ public static T Deserialize<T>(SerializationContext context, ref Utf8FaunaReader
coll = Deserialize<Module>(context, ref reader);
allProps["coll"] = coll;
break;
case "exists":
exists = Deserialize<bool>(context, ref reader);
allProps["exists"] = exists;
break;
case "reason":
reason = Deserialize<string>(context, ref reader);
allProps["reason"] = reason;
break;
default:
allProps[fieldName] = Deserialize(context, ref reader);
break;
@@ -80,21 +90,42 @@ public static T Deserialize<T>(SerializationContext context, ref Utf8FaunaReader
$"Unexpected token while deserializing into Document: {reader.CurrentTokenType}");
}


if (id != null && coll != null)
{
return new Ref
if (exists)
{
return new DocumentRef
{
Id = id,
Collection = coll,
};
}

return new NullDocumentRef
{
Id = id,
Collection = coll
Collection = coll,
Reason = reason
};
}

if (name != null && coll != null)
{
return new NamedRef
if (exists)
{
return new NamedDocumentRef
{
Name = name,
Collection = coll,
};
}

return new NullNamedDocumentRef
{
Name = name,
Collection = coll
Collection = coll,
Reason = reason
};
}

2 changes: 1 addition & 1 deletion Fauna/Types/Ref.cs → Fauna/Types/DocumentRef.cs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ namespace Fauna.Types;
/// <summary>
/// Represents a document ref.
/// </summary>
public class Ref
public class DocumentRef
{
/// <summary>
/// Gets the string value of the ref id.
4 changes: 2 additions & 2 deletions Fauna/Types/NamedRef.cs → Fauna/Types/NamedDocumentRef.cs
Original file line number Diff line number Diff line change
@@ -2,9 +2,9 @@ namespace Fauna.Types;

/// <summary>
/// Represents a document ref that has a "name" instead of an "id". For example, a Role document reference is
/// represented as a NamedRef.
/// represented as a NamedDocumentRef.
/// </summary>
public class NamedRef
public class NamedDocumentRef
{
/// <summary>
/// Gets the string value of the ref name.
12 changes: 12 additions & 0 deletions Fauna/Types/NullDocumentRef.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Fauna.Types;

public class NullDocumentRef : DocumentRef
{
/// <summary>
/// Gets or sets the reason that the document is null.
/// </summary>
/// <value>
/// A string representing the reason that the document is null.
/// </value>
public string? Reason { get; set; } = null;
}
12 changes: 12 additions & 0 deletions Fauna/Types/NullNamedDocumentRef.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Fauna.Types;

public class NullNamedDocumentRef : NamedDocumentRef
{
/// <summary>
/// Gets or sets the reason that the document is null.
/// </summary>
/// <value>
/// A string representing the reason that the document is null.
/// </value>
public string? Reason { get; set; } = null;
}

0 comments on commit 31f9f15

Please sign in to comment.