ProxyBaseClasses (#27)

This commit is contained in:
Stef Heyenrath
2022-02-01 18:49:01 +01:00
committed by GitHub
parent 649ed89bb6
commit f9664e0564
45 changed files with 1305 additions and 836 deletions
@@ -1,97 +1,95 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using ProxyInterfaceSourceGenerator.Extensions;
using ProxyInterfaceSourceGenerator.Model;
namespace ProxyInterfaceSourceGenerator.FileGenerators
namespace ProxyInterfaceSourceGenerator.FileGenerators;
internal abstract class BaseGenerator
{
internal abstract class BaseGenerator
protected readonly Context Context;
protected readonly bool SupportsNullable;
protected BaseGenerator(Context context, bool supportsNullable)
{
protected readonly Context _context;
protected readonly bool _supportsNullable;
Context = context;
SupportsNullable = supportsNullable;
}
public BaseGenerator(Context context, bool supportsNullable)
protected string GetPropertyType(IPropertySymbol property, out bool isReplaced)
{
return GetReplacedType(property.Type, out isReplaced);
}
protected string GetParameterType(IParameterSymbol property, out bool isReplaced)
{
return GetReplacedType(property.Type, out isReplaced);
}
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)
{
_context = context;
_supportsNullable = supportsNullable;
}
protected string GetPropertyType(IPropertySymbol property, out bool isReplaced)
{
return GetReplacedType(property.Type, out isReplaced);
}
protected string GetParameterType(IParameterSymbol property, out bool isReplaced)
{
return GetReplacedType(property.Type, out isReplaced);
}
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 (!Context.ReplacedTypes.ContainsKey(typeSymbolAsString))
{
if (!_context.ReplacedTypes.ContainsKey(typeSymbolAsString))
{
_context.ReplacedTypes.Add(typeSymbolAsString, existing.InterfaceName);
}
isReplaced = true;
return existing.InterfaceName;
Context.ReplacedTypes.Add(typeSymbolAsString, existing.InterfaceName);
}
if (typeSymbol is INamedTypeSymbol namedTypedSymbol)
isReplaced = true;
return existing.InterfaceName;
}
if (typeSymbol is INamedTypeSymbol namedTypedSymbol)
{
var propertyTypeAsStringToBeModified = typeSymbolAsString;
foreach (var typeArgument in namedTypedSymbol.TypeArguments)
{
var propertyTypeAsStringToBeModified = typeSymbolAsString;
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)
{
var typeArgumentAsString = typeArgument.ToString();
var exist = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.RawTypeName == typeArgumentAsString);
if (exist is not null)
isReplaced = true;
if (!Context.ReplacedTypes.ContainsKey(typeArgumentAsString))
{
isReplaced = true;
if (!_context.ReplacedTypes.ContainsKey(typeArgumentAsString))
{
_context.ReplacedTypes.Add(typeArgumentAsString, exist.InterfaceName);
}
propertyTypeAsStringToBeModified = propertyTypeAsStringToBeModified.Replace(typeArgumentAsString, exist.InterfaceName);
Context.ReplacedTypes.Add(typeArgumentAsString, exist.InterfaceName);
}
}
return propertyTypeAsStringToBeModified;
propertyTypeAsStringToBeModified = propertyTypeAsStringToBeModified.Replace(typeArgumentAsString, exist.InterfaceName);
}
}
return typeSymbolAsString;
return propertyTypeAsStringToBeModified;
}
protected INamedTypeSymbol GetNamedTypeSymbolByFullName(string name, IEnumerable<string>? usings = null)
return typeSymbolAsString;
}
protected ClassSymbol GetNamedTypeSymbolByFullName(string name, IEnumerable<string>? usings = null)
{
// 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);
if (symbol is not null)
{
// 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);
if (symbol is not null)
{
return symbol;
}
return new ClassSymbol(symbol, symbol.GetBaseTypes());
}
if (usings is not null)
if (usings is not null)
{
foreach (var @using in usings)
{
foreach (var @using in usings)
symbol = Context.GeneratorExecutionContext.Compilation.GetTypeByMetadataName($"{@using}.{name}");
if (symbol is not null)
{
symbol = _context.GeneratorExecutionContext.Compilation.GetTypeByMetadataName($"{@using}.{name}");
if (symbol is not null)
{
return symbol;
}
return new ClassSymbol(symbol, symbol.GetBaseTypes());
}
}
throw new Exception($"The type '{name}' is not found.");
}
throw new Exception($"The type '{name}' is not found.");
}
}