Fixed TryFindProxyDataByTypeName (#30)
* pnp * . * . * okee * ns * . * o * . * , * x * t * co * . * r * CastTo
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using ProxyInterfaceSourceGenerator.Extensions;
|
||||
using ProxyInterfaceSourceGenerator.Models;
|
||||
@@ -26,14 +27,118 @@ internal abstract class BaseGenerator
|
||||
return GetReplacedType(property.Type, out isReplaced);
|
||||
}
|
||||
|
||||
protected bool TryFindProxyDataByTypeName(string type, [NotNullWhen(true)] out ProxyData? proxyData)
|
||||
{
|
||||
proxyData = Context.CandidateInterfaces.Values.FirstOrDefault(x => x.FullRawTypeName == type);
|
||||
if (proxyData != null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach (var ci in Context.CandidateInterfaces.Values)
|
||||
{
|
||||
foreach (var u in ci.Usings)
|
||||
{
|
||||
if ($"{u}.{ci.FullRawTypeName}" == type)
|
||||
{
|
||||
proxyData = ci;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected string GetWhereStatementFromMethod(IMethodSymbol method)
|
||||
{
|
||||
if (!method.IsGenericMethod)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var list = new List<string>();
|
||||
foreach (var typeParameterSymbol in method.TypeParameters)
|
||||
{
|
||||
if (TryGetWhereConstraints(typeParameterSymbol, false, out var constraint))
|
||||
{
|
||||
list.Add(constraint.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
return string.Concat(list);
|
||||
}
|
||||
|
||||
protected string ResolveInterfaceNameWithOptionalTypeConstraints(INamedTypeSymbol namedTypeSymbol, string interfaceName)
|
||||
{
|
||||
if (!namedTypeSymbol.IsGenericType)
|
||||
{
|
||||
return interfaceName;
|
||||
}
|
||||
|
||||
var str = new StringBuilder($"{interfaceName}<{string.Join(", ", namedTypeSymbol.TypeArguments.Select(ta => ta.Name))}>");
|
||||
|
||||
foreach (var typeParameterSymbol in namedTypeSymbol.TypeArguments.OfType<ITypeParameterSymbol>())
|
||||
{
|
||||
if (TryGetWhereConstraints(typeParameterSymbol, false, out var constraint))
|
||||
{
|
||||
str.Append(constraint);
|
||||
}
|
||||
}
|
||||
|
||||
return str.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// https://www.codeproject.com/Articles/871704/Roslyn-Code-Analysis-in-Easy-Samples-Part-2
|
||||
/// </summary>
|
||||
public bool TryGetWhereConstraints(ITypeParameterSymbol typeParameterSymbol, bool replaceIt, [NotNullWhen(true)] out ConstraintInfo? constraint)
|
||||
{
|
||||
var constraints = new List<string>();
|
||||
if (typeParameterSymbol.HasReferenceTypeConstraint)
|
||||
{
|
||||
constraints.Add("class");
|
||||
}
|
||||
|
||||
if (typeParameterSymbol.HasValueTypeConstraint)
|
||||
{
|
||||
constraints.Add("struct");
|
||||
}
|
||||
|
||||
if (typeParameterSymbol.HasConstructorConstraint)
|
||||
{
|
||||
constraints.Add("new()");
|
||||
}
|
||||
|
||||
foreach (var namedTypeSymbol in typeParameterSymbol.ConstraintTypes.OfType<INamedTypeSymbol>())
|
||||
{
|
||||
if (replaceIt)
|
||||
{
|
||||
constraints.Add(GetReplacedType(namedTypeSymbol, out _));
|
||||
}
|
||||
else
|
||||
{
|
||||
constraints.Add(namedTypeSymbol.GetFullType());
|
||||
}
|
||||
}
|
||||
|
||||
if (!constraints.Any())
|
||||
{
|
||||
constraint = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
constraint = new(typeParameterSymbol.Name, constraints);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected string GetReplacedType(ITypeSymbol typeSymbol, out bool isReplaced)
|
||||
{
|
||||
isReplaced = false;
|
||||
|
||||
var typeSymbolAsString = typeSymbol.ToString();
|
||||
|
||||
var existing = Context.CandidateInterfaces.Values.FirstOrDefault(x => x.RawTypeName == typeSymbolAsString);
|
||||
if (existing is not null)
|
||||
if (TryFindProxyDataByTypeName(typeSymbolAsString, out var existing))
|
||||
{
|
||||
if (!Context.ReplacedTypes.ContainsKey(typeSymbolAsString))
|
||||
{
|
||||
@@ -50,17 +155,17 @@ internal abstract class BaseGenerator
|
||||
foreach (var typeArgument in namedTypedSymbol.TypeArguments)
|
||||
{
|
||||
var typeArgumentAsString = typeArgument.ToString();
|
||||
var exist = Context.CandidateInterfaces.Values.FirstOrDefault(x => x.RawTypeName == typeArgumentAsString);
|
||||
if (exist is not null)
|
||||
|
||||
if (TryFindProxyDataByTypeName(typeArgumentAsString, out var existingTypeArgument))
|
||||
{
|
||||
isReplaced = true;
|
||||
|
||||
if (!Context.ReplacedTypes.ContainsKey(typeArgumentAsString))
|
||||
{
|
||||
Context.ReplacedTypes.Add(typeArgumentAsString, exist.FullInterfaceName);
|
||||
Context.ReplacedTypes.Add(typeArgumentAsString, existingTypeArgument.FullInterfaceName);
|
||||
}
|
||||
|
||||
propertyTypeAsStringToBeModified = propertyTypeAsStringToBeModified.Replace(typeArgumentAsString, exist.FullInterfaceName);
|
||||
propertyTypeAsStringToBeModified = propertyTypeAsStringToBeModified.Replace(typeArgumentAsString, existingTypeArgument.FullInterfaceName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user