Fix InterfaceName + update Properties, add private Fields

This commit is contained in:
Stef Heyenrath
2021-07-24 09:23:02 +02:00
parent 36fdd645b1
commit afe1710816
12 changed files with 98 additions and 56 deletions
@@ -1,6 +1,6 @@
namespace ProxyInterfaceSourceGenerator.FileGenerators
{
internal record Data(string FileName, string Text)
internal record FileData(string FileName, string Text)
{
}
}
@@ -2,6 +2,6 @@
{
internal interface IFileGenerator
{
Data GenerateFile();
FileData GenerateFile();
}
}
@@ -4,6 +4,6 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
{
internal interface IFilesGenerator
{
IEnumerable<Data> GenerateFiles();
IEnumerable<FileData> GenerateFiles();
}
}
@@ -21,7 +21,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
_candidateInterfaces = candidateInterfaces;
}
public IEnumerable<Data> GenerateFiles()
public IEnumerable<FileData> GenerateFiles()
{
foreach (var ci in _candidateInterfaces)
{
@@ -31,9 +31,10 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
throw new Exception($"The type '{ci.Value.TypeName}' is not found.");
}
string interfaceName = $"I{ci.Value.TypeName.Split('.').Last()}";
yield return new Data($"I{interfaceName}.cs", CreatePartialInterfaceCode(symbol, interfaceName, ci.Value.ProxyAll));
yield return new FileData(
$"{ci.Value.InterfaceName}.cs",
CreatePartialInterfaceCode(symbol, ci.Value.InterfaceName, ci.Value.ProxyAll)
);
}
}
@@ -43,46 +44,35 @@ namespace {symbol.ContainingNamespace}
{{
public partial interface {interfaceName}
{{
{GenerateSimpleProperties(symbol)}
{GenerateInterfaceProperties(symbol)}
{GenerateComplexProperties(symbol, proxyAll)}
{GenerateProperties(symbol, proxyAll)}
{GenerateMethods(symbol)}
}}
}}";
private string GenerateSimpleProperties(INamedTypeSymbol symbol)
private string GenerateProperties(INamedTypeSymbol symbol, bool proxyAll)
{
var str = new StringBuilder();
// SimpleProperties
foreach (var property in MemberHelper.GetPublicProperties(symbol, p => p.Type.IsValueType || p.Type.ToString() == "string"))
{
str.AppendLine($" {property.ToPropertyTextForInterface()}");
str.AppendLine($" {property.ToPropertyText()}");
str.AppendLine();
}
return str.ToString();
}
private string GenerateInterfaceProperties(INamedTypeSymbol symbol)
{
var str = new StringBuilder();
// InterfaceProperties
foreach (var property in MemberHelper.GetPublicProperties(symbol,
p => !(p.Type.IsValueType || p.Type.ToString() == "string"),
p => p.Type.TypeKind == TypeKind.Interface)
)
{
str.AppendLine($" {property.ToPropertyTextForInterface()}");
str.AppendLine($" {property.ToPropertyText()}");
str.AppendLine();
}
return "// GenerateInterfaceProperties";// str.ToString();
}
private string GenerateComplexProperties(INamedTypeSymbol symbol, bool proxyAll)
{
var filters = new List<Func<IPropertySymbol, bool>>
// ComplexProperties
var complexFilters = new List<Func<IPropertySymbol, bool>>
{
p => !(p.Type.IsValueType || p.Type.ToString() == "string"),
p => p.Type.TypeKind != TypeKind.Interface
@@ -93,14 +83,13 @@ namespace {symbol.ContainingNamespace}
}
var str = new StringBuilder();
foreach (var property in MemberHelper.GetPublicProperties(symbol, filters.ToArray()))
foreach (var property in MemberHelper.GetPublicProperties(symbol, complexFilters.ToArray()))
{
str.AppendLine($" {property.ToPropertyTextForInterface()}");
str.AppendLine($" {property.ToPropertyText()}");
str.AppendLine();
}
return "// GenerateComplexProperties";// str.ToString();
return str.ToString();
}
private string GenerateMethods(INamedTypeSymbol symbol)
@@ -112,7 +101,7 @@ namespace {symbol.ContainingNamespace}
str.AppendLine();
}
return str.ToString();
return "// Methods"; // str.ToString();
}
}
}
@@ -4,9 +4,9 @@
{
private const string ClassName = "ProxyAttribute";
public Data GenerateFile()
public FileData GenerateFile()
{
return new Data($"{ClassName}.cs", $@"using System;
return new FileData($"{ClassName}.cs", $@"using System;
namespace ProxyInterfaceGenerator
{{
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -21,7 +20,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
_candidateInterfaces = candidateInterfaces;
}
public IEnumerable<Data> GenerateFiles()
public IEnumerable<FileData> GenerateFiles()
{
foreach (var ci in _candidateInterfaces)
{
@@ -31,43 +30,91 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
throw new Exception($"The type '{ci.Value.TypeName}' is not found.");
}
string className = $"{ci.Value.TypeName.Split('.').Last()}";
yield return new Data($"{className}Proxy.cs", CreateProxyClassCode(symbol, className));
yield return new FileData(
$"{ci.Value.ClassName}Proxy.cs",
CreateProxyClassCode(symbol, ci.Value.InterfaceName, ci.Value.ClassName, ci.Value.ProxyAll)
);
}
}
private string CreateProxyClassCode(INamedTypeSymbol symbol, string className) => $@"using System;
private string CreateProxyClassCode(INamedTypeSymbol symbol, string interfaceName, string className, bool proxyAll) => $@"using System;
namespace {symbol.ContainingNamespace}
{{
public class {className}Proxy : I{className}
public class {className}Proxy : {interfaceName}
{{
private {className} _instance;
{GenerateComplexFields(symbol,proxyAll)}
public {className}Proxy({className} instance)
{{
_instance = instance;
}}
{GenerateSimpleProperties(symbol)}
{GenerateProperties(symbol, proxyAll)}
{GenerateMethods(symbol)}
}}
}}";
private string GenerateSimpleProperties(INamedTypeSymbol symbol)
private string GenerateComplexFields(INamedTypeSymbol symbol, bool proxyAll)
{
var str = new StringBuilder();
foreach (var property in GetComplexProperties(symbol, proxyAll))
{
str.AppendLine($" private {property.Type} _{property.Name};");
}
return str.ToString();
}
private string GenerateProperties(INamedTypeSymbol symbol, bool proxyAll)
{
var str = new StringBuilder();
// SimpleProperties
foreach (var property in MemberHelper.GetPublicProperties(symbol, p => p.Type.IsValueType || p.Type.ToString() == "string"))
{
str.AppendLine($" public {property.ToPropertyTextForClass()}");
str.AppendLine();
}
// InterfaceProperties
foreach (var property in MemberHelper.GetPublicProperties(symbol,
p => !(p.Type.IsValueType || p.Type.ToString() == "string"),
p => p.Type.TypeKind == TypeKind.Interface)
)
{
str.AppendLine($" public {property.ToPropertyTextForClass()}");
str.AppendLine();
}
// ComplexProperties
foreach (var property in GetComplexProperties(symbol, proxyAll))
{
str.AppendLine($" public {property.ToPropertyTextForClass()}");
str.AppendLine();
}
return str.ToString();
}
private IEnumerable<IPropertySymbol> GetComplexProperties(INamedTypeSymbol symbol, bool proxyAll)
{
var complexFilters = new List<Func<IPropertySymbol, bool>>
{
p => !(p.Type.IsValueType || p.Type.ToString() == "string"),
p => p.Type.TypeKind != TypeKind.Interface
};
if (proxyAll)
{
}
return MemberHelper.GetPublicProperties(symbol, complexFilters.ToArray());
}
private string GenerateMethods(INamedTypeSymbol symbol)
{
var str = new StringBuilder();