Skip to content

Commit

Permalink
Serialization for CalendarSystem.
Browse files Browse the repository at this point in the history
  • Loading branch information
havarnov committed Apr 24, 2023
1 parent 47d2ce2 commit 905fa2c
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Orleans.Serialization.NodaTime.Tests/CalendarSystemCodecTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using NodaTime;
using Orleans.Serialization.Cloning;
using Orleans.Serialization.TestKit;
using Xunit.Abstractions;

namespace Orleans.Serialization.NodaTime.Tests;

public class CalendarSystemCodecTests: FieldCodecTester<CalendarSystem?, CalendarSystemCodec>
{
public CalendarSystemCodecTests(ITestOutputHelper output) : base(output)
{
}

protected override void Configure(ISerializerBuilder builder)
{
builder.Services.AddSingleton<IGeneralizedCopier, CalendarSystemCopier>();
base.Configure(builder);
}

protected override CalendarSystem? CreateValue() => CalendarSystem.Iso;

protected override CalendarSystem?[] TestValues =>
CalendarSystem
.Ids
.Select(CalendarSystem.ForId)
.ToArray();
}
23 changes: 23 additions & 0 deletions Orleans.Serialization.NodaTime.Tests/CalendarSystemCopierTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Linq;
using NodaTime;
using Orleans.Serialization.TestKit;
using Xunit.Abstractions;

namespace Orleans.Serialization.NodaTime.Tests;

public class CalendarSystemCopierTests: CopierTester<CalendarSystem?, CalendarSystemCopier>
{
public CalendarSystemCopierTests(ITestOutputHelper output) : base(output)
{
}

protected override bool IsImmutable => true;

protected override CalendarSystem? CreateValue() => CalendarSystem.Iso;

protected override CalendarSystem?[] TestValues =>
CalendarSystem
.Ids
.Select(CalendarSystem.ForId)
.ToArray();
}
60 changes: 60 additions & 0 deletions Orleans.Serialization.NodaTime/CalendarSystemCodec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Buffers;
using System.Text;
using NodaTime;
using Orleans.Serialization.Buffers;
using Orleans.Serialization.Codecs;
using Orleans.Serialization.WireProtocol;

namespace Orleans.Serialization.NodaTime;

[RegisterSerializer]
public class CalendarSystemCodec : IFieldCodec<CalendarSystem?>
{
public void WriteField<TBufferWriter>(
ref Writer<TBufferWriter> writer,
uint fieldIdDelta,
Type expectedType,
CalendarSystem? value)
where TBufferWriter : IBufferWriter<byte>
{
// If the value is null, write it as the null reference.
if (value is null)
{
ReferenceCodec.WriteNullReference(ref writer, fieldIdDelta);
return;
}

ReferenceCodec.MarkValueField(writer.Session);
writer.WriteFieldHeader(fieldIdDelta, expectedType, typeof(CalendarSystem), WireType.LengthPrefixed);
var bytes = Encoding.UTF8.GetBytes(value.Id);
writer.WriteVarUInt32((uint)bytes.Length);
writer.Write(bytes);
}

public CalendarSystem? ReadValue<TInput>(ref Reader<TInput> reader, Field field)
{
// This will only be true if the value is null.
if (field.WireType == WireType.Reference)
{
ReferenceCodec.MarkValueField(reader.Session);
var reference = reader.ReadVarUInt32();
if (reference != 0)
{
ThrowInvalidReference(reference);
}

return null;
}

ReferenceCodec.MarkValueField(reader.Session);
field.EnsureWireType(WireType.LengthPrefixed);
var length = reader.ReadVarUInt32();
var buffer = reader.ReadBytes(length);
var id = Encoding.UTF8.GetString(buffer);
return CalendarSystem.ForId(id);
}

private static void ThrowInvalidReference(uint reference) =>
throw new ReferenceNotFoundException(typeof(CalendarSystem), reference);
}
19 changes: 19 additions & 0 deletions Orleans.Serialization.NodaTime/CalendarSystemCopier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using NodaTime;
using Orleans.Serialization.Cloning;

namespace Orleans.Serialization.NodaTime;

[RegisterCopier]
public class CalendarSystemCopier: IDeepCopier<CalendarSystem?>, IGeneralizedCopier
{
public CalendarSystem? DeepCopy(CalendarSystem? input, CopyContext context)
{
return input;
}

public bool IsSupportedType(Type type)
{
return true;
}
}

0 comments on commit 905fa2c

Please sign in to comment.