Skip to content

Commit

Permalink
feat(Monopoly.Web.Generator): 將Generator的provider回傳進行重構
Browse files Browse the repository at this point in the history
  • Loading branch information
aa89227 committed May 15, 2024
1 parent d7ec056 commit 5101d82
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 60 deletions.
94 changes: 94 additions & 0 deletions Monopoly.Web.Generators/TransformationResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using Microsoft.CodeAnalysis;

namespace Monopoly.Web.Generators;

public class TransformationResult(
string name,
string @namespace,
string requestInterfaceName,
IMethodSymbol[] requestMethods,
IMethodSymbol[] responseMethods)
: IEquatable<TransformationResult>
{
public string Name { get; } = name;
public string Namespace { get; } = @namespace;
public string RequestInterfaceName { get; } = requestInterfaceName;
public IMethodSymbol[] RequestMethods { get; } = requestMethods;
public IMethodSymbol[] ResponseMethods { get; } = responseMethods;

public bool Equals(TransformationResult? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Name == other.Name
&& Namespace == other.Namespace
&& RequestInterfaceName == other.RequestInterfaceName
&& CompareIMethodSymbol(RequestMethods, other.RequestMethods)
&& CompareIMethodSymbol(ResponseMethods, other.ResponseMethods);
}

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj.GetType() == GetType() && Equals((TransformationResult)obj);
}

private static bool CompareIMethodSymbol(ReadOnlySpan<IMethodSymbol> x, ReadOnlySpan<IMethodSymbol> y)
{
if (x.Length != y.Length)
{
return false;
}

for (var i = 0; i < x.Length; i++)
{
if (new MethodSymbolComparer().Equals(x[i], y[i]) == false)
{
return false;
}
}

return true;
}

public override int GetHashCode()
{
unchecked
{
var hashCode = Name.GetHashCode();
hashCode = (hashCode * 397) ^ Namespace.GetHashCode();
hashCode = (hashCode * 397) ^ RequestInterfaceName.GetHashCode();
hashCode = (hashCode * 397) ^ RequestMethods.GetHashCode();
hashCode = (hashCode * 397) ^ ResponseMethods.GetHashCode();
return hashCode;
}
}

private class MethodSymbolComparer : IEqualityComparer<IMethodSymbol>
{
public bool Equals(IMethodSymbol x, IMethodSymbol y)
{
return x.Name == y.Name
&& x.Parameters.SequenceEqual(y.Parameters, new ParameterSymbolComparer());
}

public int GetHashCode(IMethodSymbol obj)
{
return obj.Name.GetHashCode();
}
}

private class ParameterSymbolComparer : IEqualityComparer<IParameterSymbol>
{
public bool Equals(IParameterSymbol x, IParameterSymbol y)
{
return x.Type.ToString() == y.Type.ToString() && x.Name == y.Name;
}

public int GetHashCode(IParameterSymbol obj)
{
return obj.Type.ToString().GetHashCode();
}
}
}
69 changes: 9 additions & 60 deletions Monopoly.Web.Generators/TypeClientGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Immutable;
using System.Text;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
Expand Down Expand Up @@ -67,7 +66,7 @@ public partial class {{nodeInfo.Name}} : {{nodeInfo.RequestInterfaceName}}
});
}

private static string GenerateRequests(IEnumerable<MethodSymbolWrapper> nodeInfoHubRequestMethods)
private static string GenerateRequests(IEnumerable<IMethodSymbol> nodeInfoHubRequestMethods)
{
var methods = nodeInfoHubRequestMethods.Select(x =>
{
Expand All @@ -84,7 +83,7 @@ private static string GenerateRequests(IEnumerable<MethodSymbolWrapper> nodeInfo
return string.Join("\n", methods);
}

private static string GenerateEventRegistration(IEnumerable<MethodSymbolWrapper> responseMethods)
private static string GenerateEventRegistration(IEnumerable<IMethodSymbol> responseMethods)
{
var events = responseMethods.Select(x =>
{
Expand All @@ -98,7 +97,7 @@ private static string GenerateEventRegistration(IEnumerable<MethodSymbolWrapper>
return string.Join("\n", events);
}

private static string GenerateDelegates(IEnumerable<MethodSymbolWrapper> responseMethods)
private static string GenerateDelegates(IEnumerable<IMethodSymbol> responseMethods)
{
var delegates = responseMethods.Select(x =>
{
Expand All @@ -111,10 +110,7 @@ private static string GenerateDelegates(IEnumerable<MethodSymbolWrapper> respons
return string.Join("\n", delegates);
}

private static (string Name, string Namespace, string RequestInterfaceName, ImmutableArray<MethodSymbolWrapper>
RequestMethods,
ImmutableArray<MethodSymbolWrapper> ResponseMethods)
TransformContext(GeneratorAttributeSyntaxContext ctx)
private static TransformationResult TransformContext(GeneratorAttributeSyntaxContext ctx)
{
var attribute = ctx.Attributes
.First(x => x.AttributeClass?.Name == "TypedHubClientAttribute");
Expand All @@ -128,63 +124,16 @@ private static (string Name, string Namespace, string RequestInterfaceName, Immu
var requestMethods = hubRequestType
.GetMembers()
.OfType<IMethodSymbol>()
.Select(x => new MethodSymbolWrapper(x))
.ToImmutableArray();
.ToArray();

var responseMethods = hubResponseType!
.GetMembers()
.OfType<IMethodSymbol>()
.Select(x => new MethodSymbolWrapper(x))
.ToImmutableArray();
.ToArray();

return (ctx.TargetSymbol.Name, ctx.TargetSymbol.ContainingNamespace.ToString(), hubRequestInterfaceName,
return new TransformationResult(ctx.TargetSymbol.Name, ctx.TargetSymbol.ContainingNamespace.ToString(),
hubRequestInterfaceName,
requestMethods,
responseMethods);
}

private class MethodSymbolWrapper(IMethodSymbol methodSymbol)
{
public string Name => methodSymbol.Name;
public ImmutableArray<IParameterSymbol> Parameters => methodSymbol.Parameters;

public override bool Equals(object? obj)
{
if (obj is MethodSymbolWrapper other)
{
return Name == other.Name
&& Parameters.SequenceEqual(other.Parameters, new ParameterSymbolComparer());
}

return false;
}

public override int GetHashCode()
{
var hash = 17;
hash = hash * 23 + SymbolEqualityComparer.Default.GetHashCode(methodSymbol);

hash = hash * 23 + Name.GetHashCode();

return Enumerable.Aggregate(
Parameters, hash,
(current, parameter) =>
current * 23 + SymbolEqualityComparer.Default.GetHashCode(parameter));
}

private class ParameterSymbolComparer : IEqualityComparer<IParameterSymbol>
{
public bool Equals(IParameterSymbol x, IParameterSymbol y)
{
return SymbolEqualityComparer.Default.Equals(x.Type, y.Type) && x.Name == y.Name;
}

public int GetHashCode(IParameterSymbol obj)
{
var hash = 17;
hash = hash * 23 + (obj.Name.GetHashCode());
hash = hash * 23 + SymbolEqualityComparer.Default.GetHashCode(obj.Type);
return hash;
}
}
}
}

0 comments on commit 5101d82

Please sign in to comment.