Skip to content

Commit

Permalink
Add nullable value type support.
Browse files Browse the repository at this point in the history
  • Loading branch information
SvenGroot committed Oct 26, 2023
1 parent 0e6c2a2 commit cf55c07
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/Ookii.Jumbo.Generator/WritableGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ private void GenerateMemberSerialization(ISymbol member)
}

bool closeIf = false;
if (property.Type.IsReferenceType && property.GetAttribute(_typeHelper.WritableNotNullAttribute!) == null)
bool nullableValueType = property.Type.IsNullableValueType();
if ((nullableValueType || property.Type.IsReferenceType)
&& property.GetAttribute(_typeHelper.WritableNotNullAttribute!) == null)
{
_builder.AppendLine($"if (this.{property.Name} == null)");
_builder.OpenBlock();
Expand All @@ -96,6 +98,10 @@ private void GenerateMemberSerialization(ISymbol member)
{
_builder.AppendLine($"((Ookii.Jumbo.IO.IWritable)this.{property.Name}).Write(writer);");
}
else if (nullableValueType)
{
_builder.AppendLine($"Ookii.Jumbo.IO.ValueWriter.WriteValue(this.{property.Name}!.Value, writer);");
}
else
{
_builder.AppendLine($"Ookii.Jumbo.IO.ValueWriter.WriteValue(this.{property.Name}, writer);");
Expand All @@ -122,9 +128,10 @@ private void GenerateMemberDeserialization(ISymbol member)
return;
}

var type = property.Type.WithNullableAnnotation(NullableAnnotation.NotAnnotated);
var type = property.Type.WithNullableAnnotation(NullableAnnotation.NotAnnotated).GetUnderlyingType();
bool closeIf = false;
if (property.Type.IsReferenceType && property.GetAttribute(_typeHelper.WritableNotNullAttribute!) == null)
if ((property.Type.IsReferenceType || property.Type.IsNullableValueType())
&& property.GetAttribute(_typeHelper.WritableNotNullAttribute!) == null)
{
_builder.AppendLine($"if (!reader.ReadBoolean())");
_builder.OpenBlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Ookii.Jumbo.Test
{
[TestFixture]
public class WritableUtilityTests
public class GeneratedWritableTests
{
[Test]
public void TestSerialization()
Expand All @@ -19,6 +19,7 @@ public void TestSerialization()
StringProperty = "Hello",
AnotherStringProperty = null,
BooleanProperty = true,
NullableProperty = 42,
WritableProperty = new Utf8String("47"),
AnotherWritableProperty = null,
DateProperty = DateTime.UtcNow,
Expand Down Expand Up @@ -49,6 +50,7 @@ public void TestSerialization()
Assert.AreEqual(expected.StringProperty, actual.StringProperty);
Assert.AreEqual(expected.AnotherStringProperty, actual.AnotherStringProperty);
Assert.AreEqual(expected.IntProperty, actual.IntProperty);
Assert.AreEqual(expected.NullableProperty, actual.NullableProperty);
Assert.AreEqual(expected.BooleanProperty, actual.BooleanProperty);
Assert.AreEqual(expected.WritableProperty, actual.WritableProperty);
Assert.AreEqual(expected.AnotherWritableProperty, actual.AnotherWritableProperty);
Expand Down Expand Up @@ -93,6 +95,7 @@ public TestClass(int n)
public string StringProperty { get; set; } = default!;
public string? AnotherStringProperty { get; set; }
public int IntProperty { get; private set; }
public int? NullableProperty { get; set; }
public bool BooleanProperty { get; set; }
public Utf8String? WritableProperty { get; set; }
public Utf8String? AnotherWritableProperty { get; set; }
Expand Down

0 comments on commit cf55c07

Please sign in to comment.