Skip to content

Commit

Permalink
Merge pull request #58 from nenoNaninu/support_inheritance
Browse files Browse the repository at this point in the history
Support inheritance
  • Loading branch information
nenoNaninu authored Jan 9, 2024
2 parents 2a85097 + a0d2f9d commit 32272e0
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 6 deletions.
34 changes: 34 additions & 0 deletions examples/Server/Hubs/InheritHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.AspNetCore.SignalR;
using Shared;

namespace Server.Hubs;

public class InheritHub : Hub<IUnaryHubReceiver>, IInheritHub
{
private readonly ILogger<InheritHub> _logger;

public InheritHub(ILogger<InheritHub> logger)
{
_logger = logger;
}

public Task<int> Add(int x, int y)
{
return Task.FromResult(x + y);
}

public Task<string> Cat(string x, string y)
{
return Task.FromResult(x + y);
}

public Task<UserDefinedType> Echo(UserDefinedType instance)
{
return Task.FromResult(instance);
}

public Task<string> Get()
{
return Task.FromResult("TypedSignalR.Client.DevTools");
}
}
2 changes: 2 additions & 0 deletions examples/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@
app.MapHub<AuthUnaryHub>("/hubs/AuthUnaryHub");
app.MapHub<AuthUnaryHub2>("/hubs/AuthUnaryHub2");

app.MapHub<InheritHub>("/hubs/InheritHub");

app.Run();
30 changes: 30 additions & 0 deletions examples/Shared/IInheritHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Threading.Tasks;
using TypedSignalR.Client;

namespace Shared;

public interface IHubBaseBase
{
Task<string> Get();
}

public interface IHubBase1 : IHubBaseBase
{
Task<int> Add(int x, int y);
}

public interface IHubBase2 : IHubBaseBase
{
Task<string> Cat(string x, string y);
}

[Hub]
public interface IInheritHub : IHubBase1, IHubBase2
{
Task<UserDefinedType> Echo(UserDefinedType instance);
}

[Receiver]
public interface IInheritHubReceiver
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,34 @@ public TypeMetadata(ITypeSymbol typeSymbol)
{
TypeSymbol = typeSymbol;

Methods = typeSymbol.GetMembers()
.OfType<IMethodSymbol>()
.Where(static x => x.MethodKind is MethodKind.Ordinary)
.Select(static x => new MethodMetadata(x))
.ToArray();
Methods = GetMethods(typeSymbol);

InterfaceName = typeSymbol.Name;
InterfaceFullName = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
CollisionFreeName = InterfaceFullName.Replace('.', '_').Replace(':', '_');
}

private static IReadOnlyList<MethodMetadata> GetMethods(ITypeSymbol typeSymbol)
{
var methods = typeSymbol.GetMembers()
.OfType<IMethodSymbol>()
.Where(static x => x.MethodKind is MethodKind.Ordinary)
.Select(static x => new MethodMetadata(x));

var allInterfaces = typeSymbol.AllInterfaces;

if (allInterfaces.IsEmpty)
{
return methods.ToArray();
}

var allMethods = allInterfaces
.SelectMany(static x => x.GetMembers())
.OfType<IMethodSymbol>()
.Where(static x => x.MethodKind is MethodKind.Ordinary)
.Select(static x => new MethodMetadata(x))
.Concat(methods);

return allMethods.ToArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,32 @@ public static bool ValidateHubTypeRule(
return false;
}

bool isValid = ValidateHubTypeRuleCore(context, hubTypeSymbol, specialSymbols, accessLocation);

var allInterfaces = hubTypeSymbol.AllInterfaces;

if (allInterfaces.IsEmpty)
{
return isValid;
}

foreach (var typeSymbol in allInterfaces)
{
isValid &= ValidateHubTypeRuleCore(context, typeSymbol, specialSymbols, accessLocation);
}

return isValid;
}

private static bool ValidateHubTypeRuleCore(
SourceProductionContext context,
ITypeSymbol typeSymbol,
SpecialSymbols specialSymbols,
Location accessLocation)
{
bool isValid = true;

foreach (ISymbol memberSymbol in hubTypeSymbol.GetMembers())
foreach (ISymbol memberSymbol in typeSymbol.GetMembers())
{
if (memberSymbol is IMethodSymbol methodSymbol)
{
Expand Down

0 comments on commit 32272e0

Please sign in to comment.