generated from Game-as-a-Service/Gaas-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Monopoly.Web.Generator): 將Generator的provider回傳進行重構
- Loading branch information
Showing
2 changed files
with
103 additions
and
60 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
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(); | ||
} | ||
} | ||
} |
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