Fix issue #4 (string?) (#15)

* #4

* Only replace if required

* fix

* private readonly IMapper _mapper;

* CRLF
This commit is contained in:
Stef Heyenrath
2021-07-26 16:24:30 +02:00
committed by GitHub
parent 6f9a29a4b7
commit 86ef9ede98
9 changed files with 396 additions and 354 deletions
@@ -1,106 +1,109 @@
using System.Collections.Generic;
using System.Text;
using Microsoft.CodeAnalysis;
using ProxyInterfaceSourceGenerator.Enums;
using ProxyInterfaceSourceGenerator.Extensions;
using ProxyInterfaceSourceGenerator.Utils;
namespace ProxyInterfaceSourceGenerator.FileGenerators
{
internal class PartialInterfacesGenerator : BaseGenerator, IFilesGenerator
{
public PartialInterfacesGenerator(Context context) :
base(context)
{
}
public IEnumerable<FileData> GenerateFiles()
{
foreach (var ci in _context.CandidateInterfaces)
{
yield return GenerateFile(ci.Value.Namespace, ci.Value.InterfaceName, ci.Value.TypeName, ci.Value.ProxyAll);
}
}
private FileData GenerateFile(string ns, string interfaceName, string typeName, bool proxyAll)
{
var symbol = GetType(typeName);
var file = new FileData(
$"{interfaceName}.cs",
CreatePartialInterfaceCode(ns, symbol, interfaceName, proxyAll)
);
_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;
namespace {ns}
{{
public partial interface {interfaceName}
{{
{GenerateProperties(symbol, proxyAll)}
{GenerateMethods(symbol)}
}}
}}";
private string GenerateProperties(INamedTypeSymbol symbol, bool proxyAll)
{
var str = new StringBuilder();
// SimpleProperties
foreach (var property in MemberHelper.GetPublicProperties(symbol, p => p.GetTypeEnum() == TypeEnum.ValueTypeOrString))
{
str.AppendLine($" {property.ToPropertyText()}");
str.AppendLine();
}
// InterfaceProperties
foreach (var property in MemberHelper.GetPublicProperties(symbol, p => p.GetTypeEnum() == TypeEnum.Interface))
{
str.AppendLine($" {property.ToPropertyText()}");
str.AppendLine();
}
// ComplexProperties
foreach (var property in MemberHelper.GetPublicProperties(symbol, p => p.GetTypeEnum() == TypeEnum.Complex))
{
var type = GetPropertyType(property);
str.AppendLine($" {property.ToPropertyText(type)}");
str.AppendLine();
}
return str.ToString();
}
private string GenerateMethods(INamedTypeSymbol symbol)
{
var str = new StringBuilder();
foreach (var method in MemberHelper.GetPublicMethods(symbol))
{
var methodParameters = new List<string>();
foreach (var ps in method.Parameters)
{
if (ps.GetTypeEnum() == TypeEnum.Complex)
{
var type = GetParameterType(ps);
methodParameters.Add($"{type} {ps.Name}");
}
else
{
methodParameters.Add($"{ps.Type} {ps.Name}");
}
}
str.AppendLine($" {GetReplacedType(method.ReturnType)} {method.Name}({string.Join(", ", methodParameters)});");
str.AppendLine();
}
return str.ToString();
}
}
using System.Collections.Generic;
using System.Text;
using Microsoft.CodeAnalysis;
using ProxyInterfaceSourceGenerator.Enums;
using ProxyInterfaceSourceGenerator.Extensions;
using ProxyInterfaceSourceGenerator.Utils;
namespace ProxyInterfaceSourceGenerator.FileGenerators
{
internal class PartialInterfacesGenerator : BaseGenerator, IFilesGenerator
{
public PartialInterfacesGenerator(Context context) :
base(context)
{
}
public IEnumerable<FileData> GenerateFiles()
{
foreach (var ci in _context.CandidateInterfaces)
{
yield return GenerateFile(ci.Value.Namespace, ci.Value.InterfaceName, ci.Value.TypeName, ci.Value.ProxyAll);
}
}
private FileData GenerateFile(string ns, string interfaceName, string typeName, bool proxyAll)
{
var symbol = GetType(typeName);
var file = new FileData(
$"{interfaceName}.cs",
CreatePartialInterfaceCode(ns, symbol, interfaceName, proxyAll)
);
_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;
namespace {ns}
{{
public partial interface {interfaceName}
{{
{GenerateProperties(symbol, proxyAll)}
{GenerateMethods(symbol)}
}}
}}";
private string GenerateProperties(INamedTypeSymbol symbol, bool proxyAll)
{
var str = new StringBuilder();
foreach (var property in MemberHelper.GetPublicProperties(symbol))
{
switch (property.GetTypeEnum())
{
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;
}
}
return str.ToString();
}
private string GenerateMethods(INamedTypeSymbol symbol)
{
var str = new StringBuilder();
foreach (var method in MemberHelper.GetPublicMethods(symbol))
{
var methodParameters = new List<string>();
foreach (var ps in method.Parameters)
{
if (ps.GetTypeEnum() == TypeEnum.Complex)
{
var type = GetParameterType(ps, out _);
methodParameters.Add($"{type} {ps.Name}");
}
else
{
methodParameters.Add($"{ps.Type} {ps.Name}");
}
}
str.AppendLine($" {GetReplacedType(method.ReturnType, out _)} {method.Name}({string.Join(", ", methodParameters)});");
str.AppendLine();
}
return str.ToString();
}
}
}