-
-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #700 from Cysharp/feature/FixCodeGenerationMemoryPack
Fix code generation for MemoryPack
- Loading branch information
Showing
12 changed files
with
255 additions
and
415 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
Binary file modified
BIN
+1 KB
(100%)
...lient/MagicOnion.Client.SourceGenerator.Unity/MagicOnion.Client.SourceGenerator.Unity.dll
Binary file not shown.
34 changes: 34 additions & 0 deletions
34
tests/MagicOnion.Client.SourceGenerator.Tests/GenerateMemoryPackTest.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,34 @@ | ||
using MagicOnion.Client.SourceGenerator.Tests.Verifiers; | ||
|
||
namespace MagicOnion.Client.SourceGenerator.Tests; | ||
|
||
public class GenerateMemoryPackTest | ||
{ | ||
[Fact] | ||
public async Task Generic() | ||
{ | ||
var source = """ | ||
using MagicOnion; | ||
using MagicOnion.Client; | ||
using MemoryPack; | ||
namespace MyApplication1; | ||
public interface IGreeterService : IService<IGreeterService> | ||
{ | ||
UnaryResult<MyGenericObject<(string, int)>> HelloAsync(string name, int age); | ||
} | ||
[MemoryPackable] | ||
public class MyGenericObject<T> {} | ||
[MagicOnionClientGeneration(typeof(IGreeterService), Serializer = GenerateSerializerType.MemoryPack)] | ||
partial class MagicOnionInitializer {} | ||
"""; | ||
|
||
await MagicOnionSourceGeneratorVerifier.RunAsync( | ||
source, | ||
verifierOptions: VerifierOptions.Default with { UseMemoryPack = true } | ||
); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...ources/GenerateMemoryPackTest/Generic/0000_MagicOnionClientSourceGeneratorAttributes.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,42 @@ | ||
// <auto-generated /> | ||
namespace MagicOnion.Client | ||
{ | ||
/// <summary> | ||
/// Marker attribute for generating clients of MagicOnion. | ||
/// The source generator collects the classes specified by this attribute and uses them to generate source. | ||
/// </summary> | ||
[global::System.Diagnostics.Conditional("__MagicOnion_Client_SourceGenerator__DesignTimeOnly__")] | ||
[global::System.AttributeUsage(global::System.AttributeTargets.Class, AllowMultiple = false)] | ||
internal class MagicOnionClientGenerationAttribute : global::System.Attribute | ||
{ | ||
/// <summary> | ||
/// Gets or sets whether to disable automatically calling `Register` during start-up. (Automatic registration requires .NET 5+ or Unity) | ||
/// </summary> | ||
public bool DisableAutoRegistration { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or set the serializer used for message serialization. The default value is <see cref="GenerateSerializerType.MessagePack"/>. | ||
/// </summary> | ||
public global::MagicOnion.Client.GenerateSerializerType Serializer { get; set; } = global::MagicOnion.Client.GenerateSerializerType.MessagePack; | ||
|
||
/// <summary> | ||
/// Gets or set the namespace of pre-generated MessagePackFormatters. The default value is <c>MessagePack.Formatters</c>. | ||
/// </summary> | ||
public string MessagePackFormatterNamespace { get; set; } = "MessagePack.Formatters"; | ||
|
||
public global::System.Type[] TypesContainedInTargetAssembly { get; } | ||
|
||
/// <param name="typesContainedInTargetAssembly">Types contained in the scan target assembly</param> | ||
public MagicOnionClientGenerationAttribute(params global::System.Type[] typesContainedInTargetAssembly) | ||
{ | ||
TypesContainedInTargetAssembly = typesContainedInTargetAssembly; | ||
} | ||
} | ||
|
||
// This enum must be mirror of `SerializerType` (MagicOnionClientSourceGenerator) | ||
internal enum GenerateSerializerType | ||
{ | ||
MessagePack = 0, | ||
MemoryPack = 1, | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...ts/Resources/GenerateMemoryPackTest/Generic/0001_MyApplication1_GreeterServiceClient.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,50 @@ | ||
// <auto-generated /> | ||
#pragma warning disable CS0618 // 'member' is obsolete: 'text' | ||
#pragma warning disable CS0612 // 'member' is obsolete | ||
#pragma warning disable CS8019 // Unnecessary using directive. | ||
|
||
namespace MyApplication1 | ||
{ | ||
using global::System; | ||
using global::Grpc.Core; | ||
using global::MagicOnion; | ||
using global::MagicOnion.Client; | ||
using global::MessagePack; | ||
|
||
partial class MagicOnionInitializer | ||
{ | ||
static partial class MagicOnionGeneratedClient | ||
{ | ||
[global::MagicOnion.Ignore] | ||
public class MyApplication1_GreeterServiceClient : global::MagicOnion.Client.MagicOnionClientBase<global::MyApplication1.IGreeterService>, global::MyApplication1.IGreeterService | ||
{ | ||
class ClientCore | ||
{ | ||
public global::MagicOnion.Client.Internal.RawMethodInvoker<global::MagicOnion.DynamicArgumentTuple<global::System.String, global::System.Int32>, global::MyApplication1.MyGenericObject<global::System.ValueTuple<global::System.String, global::System.Int32>>> HelloAsync; | ||
public ClientCore(global::MagicOnion.Serialization.IMagicOnionSerializerProvider serializerProvider) | ||
{ | ||
this.HelloAsync = global::MagicOnion.Client.Internal.RawMethodInvoker.Create_ValueType_RefType<global::MagicOnion.DynamicArgumentTuple<global::System.String, global::System.Int32>, global::MyApplication1.MyGenericObject<global::System.ValueTuple<global::System.String, global::System.Int32>>>(global::Grpc.Core.MethodType.Unary, "IGreeterService", "HelloAsync", serializerProvider); | ||
} | ||
} | ||
|
||
readonly ClientCore core; | ||
|
||
public MyApplication1_GreeterServiceClient(global::MagicOnion.Client.MagicOnionClientOptions options, global::MagicOnion.Serialization.IMagicOnionSerializerProvider serializerProvider) : base(options) | ||
{ | ||
this.core = new ClientCore(serializerProvider); | ||
} | ||
|
||
private MyApplication1_GreeterServiceClient(MagicOnionClientOptions options, ClientCore core) : base(options) | ||
{ | ||
this.core = core; | ||
} | ||
|
||
protected override global::MagicOnion.Client.MagicOnionClientBase<global::MyApplication1.IGreeterService> Clone(global::MagicOnion.Client.MagicOnionClientOptions options) | ||
=> new MyApplication1_GreeterServiceClient(options, core); | ||
|
||
public global::MagicOnion.UnaryResult<global::MyApplication1.MyGenericObject<global::System.ValueTuple<global::System.String, global::System.Int32>>> HelloAsync(global::System.String name, global::System.Int32 age) | ||
=> this.core.HelloAsync.InvokeUnary(this, "IGreeterService/HelloAsync", new global::MagicOnion.DynamicArgumentTuple<global::System.String, global::System.Int32>(name, age)); | ||
} | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...es/GenerateMemoryPackTest/Generic/0002_MyApplication1_MagicOnionInitializer_Resolver.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,21 @@ | ||
// <auto-generated /> | ||
#pragma warning disable CS0618 // 'member' is obsolete: 'text' | ||
#pragma warning disable CS0612 // 'member' is obsolete | ||
#pragma warning disable CS8019 // Unnecessary using directive. | ||
|
||
namespace MyApplication1 | ||
{ | ||
using global::System; | ||
using global::MemoryPack; | ||
partial class MagicOnionInitializer | ||
{ | ||
/// <summary> | ||
/// Registers the generated MemoryPackFormatters. | ||
/// </summary> | ||
public static void RegisterMemoryPackFormatters() | ||
{ | ||
global::MemoryPack.MemoryPackFormatterProvider.Register(new global::MagicOnion.Serialization.MemoryPack.DynamicArgumentTupleFormatter<global::System.String, global::System.Int32>()); | ||
global::MemoryPack.MemoryPackFormatterProvider.Register(new global::MemoryPack.Formatters.ValueTupleFormatter<global::System.String, global::System.Int32>()); | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
...s/Resources/GenerateMemoryPackTest/Generic/0003_MyApplication1_MagicOnionInitializer.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,95 @@ | ||
// <auto-generated /> | ||
#pragma warning disable CS0618 // 'member' is obsolete: 'text' | ||
#pragma warning disable CS0612 // 'member' is obsolete | ||
#pragma warning disable CS8019 // Unnecessary using directive. | ||
namespace MyApplication1 | ||
{ | ||
using global::System; | ||
using global::System.Collections.Generic; | ||
using global::System.Linq; | ||
using global::MagicOnion; | ||
using global::MagicOnion.Client; | ||
|
||
partial class PreserveAttribute : global::System.Attribute {} | ||
|
||
partial class MagicOnionInitializer | ||
{ | ||
static bool isRegistered = false; | ||
readonly static MagicOnionGeneratedClientFactoryProvider provider = new(); | ||
|
||
/// <summary> | ||
/// Gets the generated MagicOnionClientFactoryProvider. | ||
/// </summary> | ||
public static global::MagicOnion.Client.IMagicOnionClientFactoryProvider ClientFactoryProvider => provider; | ||
|
||
/// <summary> | ||
/// Gets the generated StreamingHubClientFactoryProvider. | ||
/// </summary> | ||
public static global::MagicOnion.Client.IStreamingHubClientFactoryProvider StreamingHubClientFactoryProvider => provider; | ||
#if UNITY_2019_4_OR_NEWER | ||
[global::UnityEngine.RuntimeInitializeOnLoadMethod(UnityEngine.RuntimeInitializeLoadType.BeforeSceneLoad)] | ||
#elif NET5_0_OR_GREATER | ||
[global::System.Runtime.CompilerServices.ModuleInitializer] | ||
#endif | ||
internal static void Register() => TryRegisterProviderFactory(); | ||
|
||
/// <summary> | ||
/// Register the generated client factory providers if it's not registered yet. This method will register only once. | ||
/// </summary> | ||
public static bool TryRegisterProviderFactory() | ||
{ | ||
if (isRegistered) return false; | ||
isRegistered = true; | ||
|
||
global::MagicOnion.Client.MagicOnionClientFactoryProvider.Default = | ||
(global::MagicOnion.Client.MagicOnionClientFactoryProvider.Default is global::MagicOnion.Client.ImmutableMagicOnionClientFactoryProvider immutableMagicOnionClientFactoryProvider) | ||
? immutableMagicOnionClientFactoryProvider.Add(provider) | ||
: new global::MagicOnion.Client.ImmutableMagicOnionClientFactoryProvider(provider); | ||
|
||
global::MagicOnion.Client.StreamingHubClientFactoryProvider.Default = | ||
(global::MagicOnion.Client.StreamingHubClientFactoryProvider.Default is global::MagicOnion.Client.ImmutableStreamingHubClientFactoryProvider immutableStreamingHubClientFactoryProvider) | ||
? immutableStreamingHubClientFactoryProvider.Add(provider) | ||
: new global::MagicOnion.Client.ImmutableStreamingHubClientFactoryProvider(provider); | ||
|
||
return true; | ||
} | ||
|
||
class MagicOnionGeneratedClientFactoryProvider : global::MagicOnion.Client.IMagicOnionClientFactoryProvider, global::MagicOnion.Client.IStreamingHubClientFactoryProvider | ||
{ | ||
bool global::MagicOnion.Client.IMagicOnionClientFactoryProvider.TryGetFactory<T>(out global::MagicOnion.Client.MagicOnionClientFactoryDelegate<T> factory) | ||
=> (factory = MagicOnionClientFactoryCache<T>.Factory) != null; | ||
|
||
bool global::MagicOnion.Client.IStreamingHubClientFactoryProvider.TryGetFactory<TStreamingHub, TReceiver>(out global::MagicOnion.Client.StreamingHubClientFactoryDelegate<TStreamingHub, TReceiver> factory) | ||
=> (factory = StreamingHubClientFactoryCache<TStreamingHub, TReceiver>.Factory) != null; | ||
|
||
static class MagicOnionClientFactoryCache<T> where T : global::MagicOnion.IService<T> | ||
{ | ||
public readonly static global::MagicOnion.Client.MagicOnionClientFactoryDelegate<T> Factory; | ||
|
||
static MagicOnionClientFactoryCache() | ||
{ | ||
object factory = default(global::MagicOnion.Client.MagicOnionClientFactoryDelegate<T>); | ||
|
||
if (typeof(T) == typeof(global::MyApplication1.IGreeterService)) | ||
{ | ||
factory = ((global::MagicOnion.Client.MagicOnionClientFactoryDelegate<global::MyApplication1.IGreeterService>)((x, y) => new MagicOnionGeneratedClient.MyApplication1_GreeterServiceClient(x, y))); | ||
} | ||
Factory = (global::MagicOnion.Client.MagicOnionClientFactoryDelegate<T>)factory; | ||
} | ||
} | ||
|
||
static class StreamingHubClientFactoryCache<TStreamingHub, TReceiver> where TStreamingHub : global::MagicOnion.IStreamingHub<TStreamingHub, TReceiver> | ||
{ | ||
public readonly static global::MagicOnion.Client.StreamingHubClientFactoryDelegate<TStreamingHub, TReceiver> Factory; | ||
|
||
static StreamingHubClientFactoryCache() | ||
{ | ||
object factory = default(global::MagicOnion.Client.StreamingHubClientFactoryDelegate<TStreamingHub, TReceiver>); | ||
|
||
|
||
Factory = (global::MagicOnion.Client.StreamingHubClientFactoryDelegate<TStreamingHub, TReceiver>)factory; | ||
} | ||
} | ||
} | ||
} | ||
} |
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
3 changes: 0 additions & 3 deletions
3
tests/MagicOnion.Serialization.MemoryPack.Tests/GenerateClient.cmd
This file was deleted.
Oops, something went wrong.
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
7 changes: 7 additions & 0 deletions
7
tests/MagicOnion.Serialization.MemoryPack.Tests/MagicOnionGeneratedClientInitializer.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,7 @@ | ||
using MagicOnion.Client; | ||
|
||
namespace MagicOnion.Serialization.MemoryPack.Tests; | ||
|
||
[MagicOnionClientGeneration(typeof(MagicOnionGeneratedClientInitializer), Serializer = GenerateSerializerType.MemoryPack)] | ||
public partial class MagicOnionGeneratedClientInitializer | ||
{} |
Oops, something went wrong.