-
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
117 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,52 @@ | ||
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 (ReferenceCodec.TryWriteReferenceField(ref writer, fieldIdDelta, expectedType, value)) | ||
{ | ||
return; | ||
} | ||
|
||
if (value is null) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
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) | ||
{ | ||
if (field.WireType == WireType.Reference) | ||
{ | ||
return ReferenceCodec.ReadReference<CalendarSystem?, TInput>(ref reader, field); | ||
} | ||
|
||
field.EnsureWireType(WireType.LengthPrefixed); | ||
var length = reader.ReadVarUInt32(); | ||
var buffer = reader.ReadBytes(length); | ||
var id = Encoding.UTF8.GetString(buffer); | ||
var value = CalendarSystem.ForId(id); | ||
ReferenceCodec.RecordObject(reader.Session, value); | ||
return value; | ||
} | ||
} |
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,13 @@ | ||
using NodaTime; | ||
using Orleans.Serialization.Cloning; | ||
|
||
namespace Orleans.Serialization.NodaTime; | ||
|
||
[RegisterCopier] | ||
public class CalendarSystemCopier: IDeepCopier<CalendarSystem?> | ||
{ | ||
public CalendarSystem? DeepCopy(CalendarSystem? input, CopyContext context) | ||
{ | ||
return input; | ||
} | ||
} |