Add support for generics (#20)
* wip * . * method * TT * TT * . * . * mp
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user