-
-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for private member serialization
- Loading branch information
Showing
18 changed files
with
694 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
16 changes: 16 additions & 0 deletions
16
tests/MessagePack.SourceGenerator.ExecutionTests/HasPrivateSerializedMembers.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,16 @@ | ||
// Copyright (c) All contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
[MessagePackObject] | ||
internal partial record HasPrivateSerializedMembers | ||
{ | ||
[Key(0)] | ||
private int value; | ||
|
||
[IgnoreMember] | ||
internal int ValueAccessor | ||
{ | ||
get => this.value; | ||
set => this.value = value; | ||
} | ||
} |
2 changes: 0 additions & 2 deletions
2
tests/MessagePack.SourceGenerator.Tests/GenericsFormatterTests.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
47 changes: 47 additions & 0 deletions
47
tests/MessagePack.SourceGenerator.Tests/PrivateMemberAccessTests.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,47 @@ | ||
// Copyright (c) All contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using VerifyCS = CSharpSourceGeneratorVerifier<MessagePack.SourceGenerator.MessagePackGenerator>; | ||
|
||
public class PrivateMemberAccessTests(ITestOutputHelper logger) | ||
{ | ||
[Theory] | ||
[InlineData("class")] | ||
[InlineData("record")] | ||
[InlineData("struct")] | ||
public async Task FormatterForClassWithPrivateMembers(string type) | ||
{ | ||
string testSource = $$""" | ||
using System; | ||
using MessagePack; | ||
|
||
[MessagePackObject] | ||
partial {{type}} MyObject | ||
{ | ||
[Key(0)] | ||
private int value; | ||
} | ||
"""; | ||
await VerifyCS.Test.RunDefaultAsync(logger, testSource, testMethod: $"{nameof(FormatterForClassWithPrivateMembers)}({type})"); | ||
} | ||
|
||
[Theory] | ||
[InlineData("class")] | ||
[InlineData("record")] | ||
[InlineData("struct")] | ||
public async Task FormatterForClassWithPrivateMembers_NonPartialTypes(string type) | ||
{ | ||
string testSource = $$""" | ||
using System; | ||
using MessagePack; | ||
|
||
[MessagePackObject] | ||
{|MsgPack011:{{type}} MyObject|} | ||
{ | ||
[Key(0)] | ||
private int value; | ||
} | ||
"""; | ||
await VerifyCS.Test.RunDefaultAsync(logger, testSource, testMethod: $"{nameof(FormatterForClassWithPrivateMembers_NonPartialTypes)}({type})"); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...ts/Resources/FormatterForClassWithPrivateMembers(class)/Formatters.MyObjectFormatter.g.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,41 @@ | ||
// <auto-generated /> | ||
|
||
#pragma warning disable 618, 612, 414, 168, CS1591, SA1129, SA1309, SA1312, SA1403, SA1649 | ||
|
||
#pragma warning disable CS8669 // We may leak nullable annotations into generated code. | ||
|
||
namespace MessagePack { | ||
|
||
using MsgPack = global::MessagePack; | ||
|
||
partial class GeneratedMessagePackResolver | ||
{ | ||
private sealed class MyObjectFormatter : MsgPack::Formatters.IMessagePackFormatter<global::MyObject> | ||
{ | ||
|
||
public void Serialize(ref MsgPack::MessagePackWriter writer, global::MyObject value, MsgPack::MessagePackSerializerOptions options) | ||
{ | ||
if (value == null) | ||
{ | ||
writer.WriteNil(); | ||
return; | ||
} | ||
|
||
writer.WriteArrayHeader(0); | ||
} | ||
|
||
public global::MyObject Deserialize(ref MsgPack::MessagePackReader reader, MsgPack::MessagePackSerializerOptions options) | ||
{ | ||
if (reader.TryReadNil()) | ||
{ | ||
return null; | ||
} | ||
|
||
reader.Skip(); | ||
return new global::MyObject(); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
.../FormatterForClassWithPrivateMembers(class)/MessagePack.GeneratedMessagePackResolver.g.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,63 @@ | ||
// <auto-generated /> | ||
|
||
#pragma warning disable 618, 612, 414, 168, CS1591, SA1129, SA1309, SA1312, SA1403, SA1649 | ||
|
||
using MsgPack = global::MessagePack; | ||
|
||
[assembly: MsgPack::Internal.GeneratedAssemblyMessagePackResolverAttribute(typeof(MessagePack.GeneratedMessagePackResolver), 3, 0)] | ||
|
||
namespace MessagePack { | ||
|
||
/// <summary>A MessagePack resolver that uses generated formatters for types in this assembly.</summary> | ||
partial class GeneratedMessagePackResolver : MsgPack::IFormatterResolver | ||
{ | ||
/// <summary>An instance of this resolver that only returns formatters specifically generated for types in this assembly.</summary> | ||
public static readonly MsgPack::IFormatterResolver Instance = new GeneratedMessagePackResolver(); | ||
|
||
private GeneratedMessagePackResolver() | ||
{ | ||
} | ||
|
||
public MsgPack::Formatters.IMessagePackFormatter<T> GetFormatter<T>() | ||
{ | ||
return FormatterCache<T>.Formatter; | ||
} | ||
|
||
private static class FormatterCache<T> | ||
{ | ||
internal static readonly MsgPack::Formatters.IMessagePackFormatter<T> Formatter; | ||
|
||
static FormatterCache() | ||
{ | ||
var f = GeneratedMessagePackResolverGetFormatterHelper.GetFormatter(typeof(T)); | ||
if (f != null) | ||
{ | ||
Formatter = (MsgPack::Formatters.IMessagePackFormatter<T>)f; | ||
} | ||
} | ||
} | ||
|
||
private static class GeneratedMessagePackResolverGetFormatterHelper | ||
{ | ||
private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> closedTypeLookup = new(1) | ||
{ | ||
{ typeof(global::MyObject), 0 }, | ||
}; | ||
|
||
internal static object GetFormatter(global::System.Type t) | ||
{ | ||
if (closedTypeLookup.TryGetValue(t, out int closedKey)) | ||
{ | ||
return closedKey switch | ||
{ | ||
0 => new MyObjectFormatter(), | ||
_ => null, // unreachable | ||
}; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
...s/Resources/FormatterForClassWithPrivateMembers(record)/Formatters.MyObjectFormatter.g.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,41 @@ | ||
// <auto-generated /> | ||
|
||
#pragma warning disable 618, 612, 414, 168, CS1591, SA1129, SA1309, SA1312, SA1403, SA1649 | ||
|
||
#pragma warning disable CS8669 // We may leak nullable annotations into generated code. | ||
|
||
namespace MessagePack { | ||
|
||
using MsgPack = global::MessagePack; | ||
|
||
partial class GeneratedMessagePackResolver | ||
{ | ||
private sealed class MyObjectFormatter : MsgPack::Formatters.IMessagePackFormatter<global::MyObject> | ||
{ | ||
|
||
public void Serialize(ref MsgPack::MessagePackWriter writer, global::MyObject value, MsgPack::MessagePackSerializerOptions options) | ||
{ | ||
if (value == null) | ||
{ | ||
writer.WriteNil(); | ||
return; | ||
} | ||
|
||
writer.WriteArrayHeader(0); | ||
} | ||
|
||
public global::MyObject Deserialize(ref MsgPack::MessagePackReader reader, MsgPack::MessagePackSerializerOptions options) | ||
{ | ||
if (reader.TryReadNil()) | ||
{ | ||
return null; | ||
} | ||
|
||
reader.Skip(); | ||
return new global::MyObject(); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
...FormatterForClassWithPrivateMembers(record)/MessagePack.GeneratedMessagePackResolver.g.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,63 @@ | ||
// <auto-generated /> | ||
|
||
#pragma warning disable 618, 612, 414, 168, CS1591, SA1129, SA1309, SA1312, SA1403, SA1649 | ||
|
||
using MsgPack = global::MessagePack; | ||
|
||
[assembly: MsgPack::Internal.GeneratedAssemblyMessagePackResolverAttribute(typeof(MessagePack.GeneratedMessagePackResolver), 3, 0)] | ||
|
||
namespace MessagePack { | ||
|
||
/// <summary>A MessagePack resolver that uses generated formatters for types in this assembly.</summary> | ||
partial class GeneratedMessagePackResolver : MsgPack::IFormatterResolver | ||
{ | ||
/// <summary>An instance of this resolver that only returns formatters specifically generated for types in this assembly.</summary> | ||
public static readonly MsgPack::IFormatterResolver Instance = new GeneratedMessagePackResolver(); | ||
|
||
private GeneratedMessagePackResolver() | ||
{ | ||
} | ||
|
||
public MsgPack::Formatters.IMessagePackFormatter<T> GetFormatter<T>() | ||
{ | ||
return FormatterCache<T>.Formatter; | ||
} | ||
|
||
private static class FormatterCache<T> | ||
{ | ||
internal static readonly MsgPack::Formatters.IMessagePackFormatter<T> Formatter; | ||
|
||
static FormatterCache() | ||
{ | ||
var f = GeneratedMessagePackResolverGetFormatterHelper.GetFormatter(typeof(T)); | ||
if (f != null) | ||
{ | ||
Formatter = (MsgPack::Formatters.IMessagePackFormatter<T>)f; | ||
} | ||
} | ||
} | ||
|
||
private static class GeneratedMessagePackResolverGetFormatterHelper | ||
{ | ||
private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> closedTypeLookup = new(1) | ||
{ | ||
{ typeof(global::MyObject), 0 }, | ||
}; | ||
|
||
internal static object GetFormatter(global::System.Type t) | ||
{ | ||
if (closedTypeLookup.TryGetValue(t, out int closedKey)) | ||
{ | ||
return closedKey switch | ||
{ | ||
0 => new MyObjectFormatter(), | ||
_ => null, // unreachable | ||
}; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...s/Resources/FormatterForClassWithPrivateMembers(struct)/Formatters.MyObjectFormatter.g.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,35 @@ | ||
// <auto-generated /> | ||
|
||
#pragma warning disable 618, 612, 414, 168, CS1591, SA1129, SA1309, SA1312, SA1403, SA1649 | ||
|
||
#pragma warning disable CS8669 // We may leak nullable annotations into generated code. | ||
|
||
namespace MessagePack { | ||
|
||
using MsgPack = global::MessagePack; | ||
|
||
partial class GeneratedMessagePackResolver | ||
{ | ||
private sealed class MyObjectFormatter : MsgPack::Formatters.IMessagePackFormatter<global::MyObject> | ||
{ | ||
|
||
public void Serialize(ref MsgPack::MessagePackWriter writer, global::MyObject value, MsgPack::MessagePackSerializerOptions options) | ||
{ | ||
writer.WriteArrayHeader(0); | ||
} | ||
|
||
public global::MyObject Deserialize(ref MsgPack::MessagePackReader reader, MsgPack::MessagePackSerializerOptions options) | ||
{ | ||
if (reader.TryReadNil()) | ||
{ | ||
throw new global::System.InvalidOperationException("typecode is null, struct not supported"); | ||
} | ||
|
||
reader.Skip(); | ||
return new global::MyObject(); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.