Add support for generics (#20)

* wip

* .

* method

* TT

* TT

* .

* .

* mp
This commit is contained in:
Stef Heyenrath
2021-08-01 10:34:25 +02:00
committed by GitHub
parent 296988b13f
commit 390093d007
18 changed files with 322 additions and 169 deletions
@@ -1,6 +1,5 @@
using System;
using System.Linq;
// using AnyOfTypes;
using Microsoft.CodeAnalysis;
namespace ProxyInterfaceSourceGenerator.FileGenerators
@@ -30,7 +29,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
var typeSymbolAsString = typeSymbol.ToString();
var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == typeSymbolAsString);
var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.RawTypeName == typeSymbolAsString);
if (existing is not null)
{
if (!_context.ReplacedTypes.ContainsKey(typeSymbolAsString))
@@ -48,7 +47,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
foreach (var typeArgument in namedTypedSymbol.TypeArguments)
{
var typeArgumentAsString = typeArgument.ToString();
var exist = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == typeArgumentAsString);
var exist = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.RawTypeName == typeArgumentAsString);
if (exist is not null)
{
isReplaced = true;
@@ -68,13 +67,13 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
return typeSymbolAsString;
}
protected INamedTypeSymbol GetType(string name)
protected INamedTypeSymbol GetNamedTypeSymbolByFullName(string fullName)
{
// The GetTypeByMetadataName method returns null if no type matches the full name or if 2 or more types (in different assemblies) match the full name.
var symbol = _context.GeneratorExecutionContext.Compilation.GetTypeByMetadataName(name);
var symbol = _context.GeneratorExecutionContext.Compilation.GetTypeByMetadataName(fullName);
if (symbol is null)
{
throw new Exception($"The type '{name}' is not found.");
throw new Exception($"The type '{fullName}' is not found.");
}
return symbol;
@@ -1,8 +1,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using ProxyInterfaceSourceGenerator.Enums;
using ProxyInterfaceSourceGenerator.Extensions;
using ProxyInterfaceSourceGenerator.SyntaxReceiver;
using ProxyInterfaceSourceGenerator.Utils;
namespace ProxyInterfaceSourceGenerator.FileGenerators
@@ -18,72 +20,62 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
{
foreach (var ci in _context.CandidateInterfaces)
{
yield return GenerateFile(ci.Value.Namespace, ci.Value.InterfaceName, ci.Value.TypeName, ci.Value.ProxyAll);
yield return GenerateFile(ci.Value);
}
}
private FileData GenerateFile(string ns, string interfaceName, string typeName, bool proxyAll)
private FileData GenerateFile(ProxyData pd)
{
var symbol = GetType(typeName);
var targetClassSymbol = GetNamedTypeSymbolByFullName(pd.TypeName);
var interfaceName = targetClassSymbol.ResolveInterfaceNameWithOptionalTypeConstraints(pd.InterfaceName);
var file = new FileData(
$"{interfaceName}.cs",
CreatePartialInterfaceCode(ns, symbol, interfaceName, proxyAll)
$"{pd.FileName}.cs",
CreatePartialInterfaceCode(pd.Namespace, targetClassSymbol, interfaceName, pd.ProxyAll)
);
_context.GeneratedData.Add(new() { InterfaceName = interfaceName, ClassName = null, FileData = file });
// _context.GeneratedData.Add(new() { InterfaceName = interfaceName, ClassName = null, FileData = file });
return file;
}
private string CreatePartialInterfaceCode(string ns, INamedTypeSymbol symbol, string interfaceName, bool proxyAll) => $@"using System;
private string CreatePartialInterfaceCode(string ns, INamedTypeSymbol targetClassSymbol, string interfaceName, bool proxyAll) => $@"using System;
namespace {ns}
{{
public partial interface {interfaceName}
{{
{GenerateProperties(symbol, proxyAll)}
{GenerateProperties(targetClassSymbol, proxyAll)}
{GenerateMethods(symbol)}
{GenerateMethods(targetClassSymbol)}
}}
}}";
private string GenerateProperties(INamedTypeSymbol symbol, bool proxyAll)
private string GenerateProperties(INamedTypeSymbol targetClassSymbol, bool proxyAll)
{
var str = new StringBuilder();
foreach (var property in MemberHelper.GetPublicProperties(symbol))
foreach (var property in MemberHelper.GetPublicProperties(targetClassSymbol))
{
switch (property.GetTypeEnum())
var type = GetPropertyType(property, out var isReplaced);
if (isReplaced)
{
case TypeEnum.ValueTypeOrString:
case TypeEnum.Interface:
str.AppendLine($" {property.ToPropertyText()}");
str.AppendLine();
break;
default:
var type = GetPropertyType(property, out var isReplaced);
if (isReplaced)
{
str.AppendLine($" {property.ToPropertyText(type)}");
}
else
{
str.AppendLine($" {property.ToPropertyText()}");
}
str.AppendLine();
break;
str.AppendLine($" {property.ToPropertyText(type)}");
}
else
{
str.AppendLine($" {property.ToPropertyText()}");
}
str.AppendLine();
}
return str.ToString();
}
private string GenerateMethods(INamedTypeSymbol symbol)
private string GenerateMethods(INamedTypeSymbol targetClassSymbol)
{
var str = new StringBuilder();
foreach (var method in MemberHelper.GetPublicMethods(symbol))
foreach (var method in MemberHelper.GetPublicMethods(targetClassSymbol))
{
var methodParameters = new List<string>();
foreach (var ps in method.Parameters)
@@ -91,8 +83,8 @@ namespace {ns}
var type = ps.GetTypeEnum() == TypeEnum.Complex ? GetParameterType(ps, out _) : ps.Type.ToString();
methodParameters.Add($"{ps.GetParamsPrefix()}{ps.GetRefPrefix()}{type} {ps.GetSanitizedName()}{ps.GetDefaultValue()}");
}
str.AppendLine($" {GetReplacedType(method.ReturnType, out _)} {method.Name}({string.Join(", ", methodParameters)});");
str.AppendLine($" {GetReplacedType(method.ReturnType, out _)} {method.GetMethodNameWithOptionalTypeParameters()}({string.Join(", ", methodParameters)}){method.GetWhereStatement()};");
str.AppendLine();
}
@@ -5,6 +5,7 @@ using System.Text;
using Microsoft.CodeAnalysis;
using ProxyInterfaceSourceGenerator.Enums;
using ProxyInterfaceSourceGenerator.Extensions;
using ProxyInterfaceSourceGenerator.SyntaxReceiver;
using ProxyInterfaceSourceGenerator.Utils;
namespace ProxyInterfaceSourceGenerator.FileGenerators
@@ -19,38 +20,41 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
{
foreach (var ci in _context.CandidateInterfaces)
{
yield return GenerateFile(ci.Value.Namespace, ci.Value.InterfaceName, ci.Value.ClassName, ci.Value.TypeName, ci.Value.ProxyAll);
yield return GenerateFile(ci.Value); //.Namespace, ci.Value.InterfaceName, ci.Value.ClassName, ci.Value.TypeName, ci.Value.ProxyAll);
}
}
private FileData GenerateFile(string ns, string interfaceName, string className, string typeName, bool proxyAll)
private FileData GenerateFile(ProxyData pd)
{
var symbol = GetType(typeName);
var targetClassSymbol = GetNamedTypeSymbolByFullName(pd.TypeName);
var interfaceName = targetClassSymbol.ResolveInterfaceNameWithOptionalTypeConstraints(pd.InterfaceName);
var className = targetClassSymbol.ResolveProxyClassName();
var constructorName = $"{targetClassSymbol.Name}Proxy";
var file = new FileData(
$"{className}Proxy.cs",
CreateProxyClassCode(ns, symbol, interfaceName, className, proxyAll)
$"{pd.FileName}Proxy.cs",
CreateProxyClassCode(pd.Namespace, targetClassSymbol, interfaceName, className, constructorName)
);
_context.GeneratedData.Add(new() { InterfaceName = interfaceName, ClassName = className, FileData = file });
// _context.GeneratedData.Add(new() { InterfaceName = interfaceName, ClassName = pd.ClassName, FileData = file });
return file;
}
private string CreateProxyClassCode(string ns, INamedTypeSymbol symbol, string interfaceName, string className, bool proxyAll) => $@"using System;
private string CreateProxyClassCode(string ns, INamedTypeSymbol targetClassSymbol, string interfaceName, string className, string constructorName) => $@"using System;
using AutoMapper;
namespace {ns}
{{
public class {className}Proxy : {interfaceName}
public class {className} : {interfaceName}
{{
public {symbol} _Instance {{ get; }}
public {targetClassSymbol} _Instance {{ get; }}
{GeneratePublicProperties(symbol, proxyAll)}
{GeneratePublicProperties(targetClassSymbol, false)}
{GeneratePublicMethods(symbol)}
{GeneratePublicMethods(targetClassSymbol)}
public {className}Proxy({symbol} instance)
public {constructorName}({targetClassSymbol} instance)
{{
_Instance = instance;
@@ -86,43 +90,31 @@ namespace {ns}
return str.ToString();
}
private string GeneratePublicProperties(INamedTypeSymbol symbol, bool proxyAll)
private string GeneratePublicProperties(INamedTypeSymbol targetClassSymbol, bool proxyAll)
{
var str = new StringBuilder();
foreach (var property in MemberHelper.GetPublicProperties(symbol))
foreach (var property in MemberHelper.GetPublicProperties(targetClassSymbol))
{
switch (property.GetTypeEnum())
var type = GetPropertyType(property, out var isReplaced);
if (isReplaced)
{
case TypeEnum.ValueTypeOrString:
case TypeEnum.Interface:
str.AppendLine($" public {property.ToPropertyTextForClass()}");
str.AppendLine();
break;
default:
var type = GetPropertyType(property, out var isReplaced);
if (isReplaced)
{
str.AppendLine($" public {property.ToPropertyTextForClass(type)}");
}
else
{
str.AppendLine($" public {property.ToPropertyTextForClass()}");
}
str.AppendLine();
break;
str.AppendLine($" public {property.ToPropertyTextForClass(type)}");
}
else
{
str.AppendLine($" public {property.ToPropertyTextForClass()}");
}
str.AppendLine();
}
return str.ToString();
}
private string GeneratePublicMethods(INamedTypeSymbol symbol)
private string GeneratePublicMethods(INamedTypeSymbol targetClassSymbol)
{
var str = new StringBuilder();
foreach (var method in MemberHelper.GetPublicMethods(symbol))
foreach (var method in MemberHelper.GetPublicMethods(targetClassSymbol))
{
var methodParameters = new List<string>();
var invokeParameters = new List<string>();
@@ -137,7 +129,7 @@ namespace {ns}
string returnTypeAsString = GetReplacedType(method.ReturnType, out var returnIsReplaced);
str.AppendLine($" public {returnTypeAsString} {method.Name}({string.Join(", ", methodParameters)})");
str.AppendLine($" public {returnTypeAsString} {method.GetMethodNameWithOptionalTypeParameters()}({string.Join(", ", methodParameters)}){method.GetWhereStatement()}");
str.AppendLine(" {");
foreach (var ps in method.Parameters)
{
@@ -165,11 +157,11 @@ namespace {ns}
if (returnTypeAsString == "void")
{
str.AppendLine($" _Instance.{method.Name}({string.Join(", ", invokeParameters)});");
str.AppendLine($" _Instance.{method.GetMethodNameWithOptionalTypeParameters()}({string.Join(", ", invokeParameters)});");
}
else
{
str.AppendLine($" var {alternateReturnVariableName} = _Instance.{method.Name}({string.Join(", ", invokeParameters)});");
str.AppendLine($" var {alternateReturnVariableName} = _Instance.{method.GetMethodNameWithOptionalTypeParameters()}({string.Join(", ", invokeParameters)});");
}
foreach (var ps in method.Parameters.Where(p => p.RefKind == RefKind.Out))