Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using MagicOnion.Client.SourceGenerator.Tests.Verifiers;

namespace MagicOnion.Client.SourceGenerator.Tests;

public class GenerateCustomServiceNameTest
{
[Fact]
public async Task Service_Default()
{
var source = """
using MagicOnion;
using MagicOnion.Client;

namespace MyApplication1;

public interface IMyService : IService<IMyService>
{
UnaryResult MethodA();
}

[MagicOnionClientGeneration(typeof(IMyService))]
partial class MagicOnionInitializer {}
""";

await MagicOnionSourceGeneratorVerifier.RunAsync(source);
}

[Fact]
public async Task Service_CustomServiceName()
{
var source = """
using MagicOnion;
using MagicOnion.Client;

namespace MyApplication1;

[ServiceName("ICustomMyService")]
public interface IMyService : IService<IMyService>
{
UnaryResult MethodA();
}

[MagicOnionClientGeneration(typeof(IMyService))]
partial class MagicOnionInitializer {}
""";

await MagicOnionSourceGeneratorVerifier.RunAsync(source);
}

[Fact]
public async Task StreamingHub_Default()
{
var source = """
using MagicOnion;
using MagicOnion.Client;

namespace MyApplication1;

public interface IMyHub : IStreamingHub<IMyHub, IMyHubReceiver>
{
void MethodA();
}
public interface IMyHubReceiver {}

[MagicOnionClientGeneration(typeof(IMyHub))]
partial class MagicOnionInitializer {}
""";

await MagicOnionSourceGeneratorVerifier.RunAsync(source);
}

[Fact]
public async Task StreamingHub_CustomServiceName()
{
var source = """
using MagicOnion;
using MagicOnion.Client;

namespace MyApplication1;

[ServiceName("ICustomMyHub")]
public interface IMyHub : IStreamingHub<IMyHub, IMyHubReceiver>
{
void MethodA();
}
public interface IMyHubReceiver {}

[MagicOnionClientGeneration(typeof(IMyHub))]
partial class MagicOnionInitializer {}
""";

await MagicOnionSourceGeneratorVerifier.RunAsync(source);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// <auto-generated />
#pragma warning disable

namespace MyApplication1
{
using global::System;
using global::MessagePack;

partial class MagicOnionInitializer
{
/// <summary>
/// Gets the generated MessagePack formatter resolver.
/// </summary>
public static global::MessagePack.IFormatterResolver Resolver => MessagePackGeneratedResolver.Instance;
class MessagePackGeneratedResolver : global::MessagePack.IFormatterResolver
{
public static readonly global::MessagePack.IFormatterResolver Instance = new MessagePackGeneratedResolver();

MessagePackGeneratedResolver() {}

public global::MessagePack.Formatters.IMessagePackFormatter<T> GetFormatter<T>()
=> FormatterCache<T>.formatter;

static class FormatterCache<T>
{
public static readonly global::MessagePack.Formatters.IMessagePackFormatter<T> formatter;

static FormatterCache()
{
var f = MessagePackGeneratedGetFormatterHelper.GetFormatter(typeof(T));
if (f != null)
{
formatter = (global::MessagePack.Formatters.IMessagePackFormatter<T>)f;
}
}
}
}
static class MessagePackGeneratedGetFormatterHelper
{
static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> lookup;

static MessagePackGeneratedGetFormatterHelper()
{
lookup = new global::System.Collections.Generic.Dictionary<global::System.Type, int>(0)
{
};
}
internal static object GetFormatter(global::System.Type t)
{
int key;
if (!lookup.TryGetValue(t, out key))
{
return null;
}

switch (key)
{
default: return null;
}
}
}
/// <summary>Type hints for Ahead-of-Time compilation.</summary>
[Preserve]
static class TypeHints
{
[Preserve]
internal static void Register()
{
_ = MessagePackGeneratedResolver.Instance.GetFormatter<global::MessagePack.Nil>();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// <auto-generated />
#pragma warning disable

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.IMyService))
{
factory = ((global::MagicOnion.Client.MagicOnionClientFactoryDelegate<global::MyApplication1.IMyService>)((x, y) => new MagicOnionGeneratedClient.MyApplication1_MyServiceClient(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;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// <auto-generated />
#pragma warning disable

namespace MyApplication1
{
partial class MagicOnionInitializer
{
static partial class MagicOnionGeneratedClient
{
[global::MagicOnion.Ignore]
public class MyApplication1_MyServiceClient : global::MagicOnion.Client.MagicOnionClientBase<global::MyApplication1.IMyService>, global::MyApplication1.IMyService
{
class ClientCore
{
public global::MagicOnion.Client.Internal.RawMethodInvoker<global::MessagePack.Nil, global::MessagePack.Nil> MethodA;
public ClientCore(global::MagicOnion.Serialization.IMagicOnionSerializerProvider serializerProvider)
{
this.MethodA = global::MagicOnion.Client.Internal.RawMethodInvoker.Create_ValueType_ValueType<global::MessagePack.Nil, global::MessagePack.Nil>(global::Grpc.Core.MethodType.Unary, "ICustomMyService", "MethodA", serializerProvider);
}
}

readonly ClientCore core;

public MyApplication1_MyServiceClient(global::MagicOnion.Client.MagicOnionClientOptions options, global::MagicOnion.Serialization.IMagicOnionSerializerProvider serializerProvider) : base(options)
{
this.core = new ClientCore(serializerProvider);
}

private MyApplication1_MyServiceClient(global::MagicOnion.Client.MagicOnionClientOptions options, ClientCore core) : base(options)
{
this.core = core;
}

protected override global::MagicOnion.Client.MagicOnionClientBase<global::MyApplication1.IMyService> Clone(global::MagicOnion.Client.MagicOnionClientOptions options)
=> new MyApplication1_MyServiceClient(options, core);

public global::MagicOnion.UnaryResult MethodA()
=> this.core.MethodA.InvokeUnaryNonGeneric(this, "ICustomMyService/MethodA", global::MessagePack.Nil.Default);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// <auto-generated />
#pragma warning disable

namespace MyApplication1
{
using global::System;
using global::MessagePack;

partial class MagicOnionInitializer
{
/// <summary>
/// Gets the generated MessagePack formatter resolver.
/// </summary>
public static global::MessagePack.IFormatterResolver Resolver => MessagePackGeneratedResolver.Instance;
class MessagePackGeneratedResolver : global::MessagePack.IFormatterResolver
{
public static readonly global::MessagePack.IFormatterResolver Instance = new MessagePackGeneratedResolver();

MessagePackGeneratedResolver() {}

public global::MessagePack.Formatters.IMessagePackFormatter<T> GetFormatter<T>()
=> FormatterCache<T>.formatter;

static class FormatterCache<T>
{
public static readonly global::MessagePack.Formatters.IMessagePackFormatter<T> formatter;

static FormatterCache()
{
var f = MessagePackGeneratedGetFormatterHelper.GetFormatter(typeof(T));
if (f != null)
{
formatter = (global::MessagePack.Formatters.IMessagePackFormatter<T>)f;
}
}
}
}
static class MessagePackGeneratedGetFormatterHelper
{
static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> lookup;

static MessagePackGeneratedGetFormatterHelper()
{
lookup = new global::System.Collections.Generic.Dictionary<global::System.Type, int>(0)
{
};
}
internal static object GetFormatter(global::System.Type t)
{
int key;
if (!lookup.TryGetValue(t, out key))
{
return null;
}

switch (key)
{
default: return null;
}
}
}
/// <summary>Type hints for Ahead-of-Time compilation.</summary>
[Preserve]
static class TypeHints
{
[Preserve]
internal static void Register()
{
_ = MessagePackGeneratedResolver.Instance.GetFormatter<global::MessagePack.Nil>();
}
}
}
}
Loading
Loading