-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
Orleans.Serialization.NodaTime.Tests/CalendarSystemCodecTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
Orleans.Serialization.NodaTime.Tests/CalendarSystemCopierTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |