Before trying to replace with type syntax
This commit is contained in:
parent
9211cde83c
commit
4c4413da41
|
@ -19,23 +19,6 @@ namespace MapTo
|
||||||
{
|
{
|
||||||
private readonly List<SymbolDisplayPart> _ignoredNamespaces;
|
private readonly List<SymbolDisplayPart> _ignoredNamespaces;
|
||||||
|
|
||||||
protected EfGeneratorContext(
|
|
||||||
Compilation compilation,
|
|
||||||
SourceGenerationOptions sourceGenerationOptions,
|
|
||||||
MemberDeclarationSyntax memberSyntax)
|
|
||||||
{
|
|
||||||
Compilation = compilation;
|
|
||||||
_ignoredNamespaces = new();
|
|
||||||
Diagnostics = ImmutableArray<Diagnostic>.Empty;
|
|
||||||
Usings = ImmutableArray.Create("System", Constants.RootNamespace);
|
|
||||||
SourceGenerationOptions = sourceGenerationOptions;
|
|
||||||
MemberSyntax = memberSyntax;
|
|
||||||
|
|
||||||
MappingContextTypeSymbol = compilation.GetTypeByMetadataNameOrThrow(MappingContextSource.FullyQualifiedName);
|
|
||||||
|
|
||||||
AddUsingIfRequired(sourceGenerationOptions.SupportNullableStaticAnalysis, "System.Diagnostics.CodeAnalysis");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public ImmutableArray<Diagnostic> Diagnostics { get; private set; }
|
public ImmutableArray<Diagnostic> Diagnostics { get; private set; }
|
||||||
|
|
||||||
|
@ -51,6 +34,29 @@ namespace MapTo
|
||||||
|
|
||||||
protected ImmutableArray<string> Usings { get; private set; }
|
protected ImmutableArray<string> Usings { get; private set; }
|
||||||
|
|
||||||
|
|
||||||
|
protected INamedTypeSymbol EfUpdateMethodsTypeSymbol { get; }
|
||||||
|
|
||||||
|
protected INamedTypeSymbol EfAddMethodsTypeSymbol { get; }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
protected EfGeneratorContext(
|
||||||
|
Compilation compilation,
|
||||||
|
SourceGenerationOptions sourceGenerationOptions,
|
||||||
|
MemberDeclarationSyntax memberSyntax)
|
||||||
|
{
|
||||||
|
Compilation = compilation;
|
||||||
|
_ignoredNamespaces = new();
|
||||||
|
Diagnostics = ImmutableArray<Diagnostic>.Empty;
|
||||||
|
Usings = ImmutableArray.Create("System", Constants.RootNamespace);
|
||||||
|
SourceGenerationOptions = sourceGenerationOptions;
|
||||||
|
MemberSyntax = memberSyntax;
|
||||||
|
EfUpdateMethodsTypeSymbol = compilation.GetTypeByMetadataNameOrThrow(EfUpdateMethodsAttributeSource.FullyQualifiedName);
|
||||||
|
EfAddMethodsTypeSymbol = compilation.GetTypeByMetadataNameOrThrow(EfAddMethodsAttributeSource.FullyQualifiedName);
|
||||||
|
AddUsingIfRequired(sourceGenerationOptions.SupportNullableStaticAnalysis, "System.Diagnostics.CodeAnalysis");
|
||||||
|
}
|
||||||
|
|
||||||
public static EfGeneratorContext Create(
|
public static EfGeneratorContext Create(
|
||||||
Compilation compilation,
|
Compilation compilation,
|
||||||
SourceGenerationOptions sourceGenerationOptions,
|
SourceGenerationOptions sourceGenerationOptions,
|
||||||
|
@ -236,19 +242,6 @@ namespace MapTo
|
||||||
private EfMethodsModel? CreateMappingModel()
|
private EfMethodsModel? CreateMappingModel()
|
||||||
{
|
{
|
||||||
var semanticModel = Compilation.GetSemanticModel(MemberSyntax.SyntaxTree);
|
var semanticModel = Compilation.GetSemanticModel(MemberSyntax.SyntaxTree);
|
||||||
/*if (semanticModel.GetDeclaredSymbol(MemberSyntax) is not INamedTypeSymbol typeSymbol)
|
|
||||||
{
|
|
||||||
AddDiagnostic(DiagnosticsFactory.TypeNotFoundError(MemberSyntax.GetLocation(), MemberSyntax.Identifier.ValueText));
|
|
||||||
return null;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
|
|
||||||
// Main Entity
|
|
||||||
var entityData = GetEntityTypeData(MemberSyntax, semanticModel);
|
|
||||||
|
|
||||||
string entityTypeName = entityData.Name;
|
|
||||||
|
|
||||||
string entityTypeFullName = entityData.ToDisplayString();
|
|
||||||
|
|
||||||
// get containing class type information
|
// get containing class type information
|
||||||
ClassDeclarationSyntax classDeclarationSyntax = MemberSyntax.Parent as ClassDeclarationSyntax;
|
ClassDeclarationSyntax classDeclarationSyntax = MemberSyntax.Parent as ClassDeclarationSyntax;
|
||||||
|
@ -260,57 +253,70 @@ namespace MapTo
|
||||||
|
|
||||||
var contextTypeFullName = $"{contextNamespace}.{dbContextName}";
|
var contextTypeFullName = $"{contextNamespace}.{dbContextName}";
|
||||||
|
|
||||||
var propertyName = entityTypeName;
|
|
||||||
|
|
||||||
if (MemberSyntax is PropertyDeclarationSyntax propertyDeclarationSyntax)
|
var addAttributeSymbols = classDeclarationSyntax.DescendantNodes()
|
||||||
|
.OfType<MemberDeclarationSyntax>()
|
||||||
|
.Where(x => x.DescendantNodes().OfType<AttributeSyntax>().Any(
|
||||||
|
syntax => syntax.Name.ToString() == EfUpdateMethodsAttributeSource.AttributeName
|
||||||
|
))
|
||||||
|
.ToImmutableArray();
|
||||||
|
|
||||||
|
|
||||||
|
var updateAttributesMembers = classDeclarationSyntax.DescendantNodes()
|
||||||
|
.OfType<MemberDeclarationSyntax>()
|
||||||
|
.Where(x => x.DescendantNodes().OfType<AttributeSyntax>().Any(
|
||||||
|
syntax => syntax.Name.ToString() == EfUpdateMethodsAttributeSource.AttributeName
|
||||||
|
))
|
||||||
|
.ToImmutableArray();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
List<IEfMethodsModel> methodsModels = new List<IEfMethodsModel>();
|
||||||
|
|
||||||
|
foreach (var uProperty in updateAttributesMembers)
|
||||||
{
|
{
|
||||||
propertyName = propertyDeclarationSyntax.Identifier.ValueText;
|
var entityTypeData = GetEntityTypeData(uProperty, semanticModel);
|
||||||
}
|
string entityIdentifierName = entityTypeData.Name;
|
||||||
if (MemberSyntax is FieldDeclarationSyntax fieldDeclaration)
|
string entityFullName = entityTypeData.ToDisplayString();
|
||||||
{
|
var propertyNamex = (uProperty as PropertyDeclarationSyntax ).Identifier.ValueText;
|
||||||
// TODO: Test
|
var entityDataModel = new EfEntityDataModel(propertyNamex, entityFullName, entityIdentifierName);
|
||||||
propertyName = fieldDeclaration.ToString();
|
var newUpdateModel = ExtractEfUpdateMethodsModel(semanticModel, uProperty, entityDataModel);
|
||||||
|
methodsModels.Add(newUpdateModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
var efEntity = new EfEntityDataModel(propertyName, entityTypeFullName, entityTypeName);
|
|
||||||
|
|
||||||
|
foreach (var uProperty in addAttributeSymbols)
|
||||||
var addConfig = ExtractEfAddMethodsModel(semanticModel, efEntity);
|
|
||||||
|
|
||||||
if (addConfig == null)
|
|
||||||
{
|
{
|
||||||
AddDiagnostic(DiagnosticsFactory.MapFromAttributeNotFoundError(MemberSyntax.GetLocation()));
|
var entityTypeData = GetEntityTypeData(uProperty, semanticModel);
|
||||||
return null;
|
string entityIdentifierName = entityTypeData.Name;
|
||||||
|
string entityFullName = entityTypeData.ToDisplayString();
|
||||||
|
var propertyNamex = (uProperty as PropertyDeclarationSyntax ).Identifier.ValueText;
|
||||||
|
var entityDataModel = new EfEntityDataModel(propertyNamex, entityFullName, entityIdentifierName);
|
||||||
|
var newAddModel = ExtractEfAddMethodsModel(semanticModel, uProperty, entityDataModel);
|
||||||
|
methodsModels.Add(newAddModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
var updateConfig = ExtractEfUpdateMethodsModel(semanticModel, efEntity);
|
|
||||||
|
|
||||||
if (updateConfig == null)
|
SpinWait.SpinUntil(() => Debugger.IsAttached);
|
||||||
|
|
||||||
|
var classType = semanticModel.GetTypeInfo(classDeclarationSyntax);
|
||||||
|
if (classType.Type != null)
|
||||||
{
|
{
|
||||||
AddDiagnostic(DiagnosticsFactory.MapFromAttributeNotFoundError(MemberSyntax.GetLocation()));
|
var addAttributes = GetSymbolsFromAttribute(classType.Type, EfAddMethodsTypeSymbol);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create list of configurations
|
|
||||||
|
|
||||||
List<IEfMethodsModel> models = new List<IEfMethodsModel>();
|
|
||||||
|
|
||||||
models.Add(addConfig);
|
|
||||||
models.Add(updateConfig);
|
|
||||||
|
|
||||||
|
|
||||||
return new EfMethodsModel(
|
return new EfMethodsModel(
|
||||||
SourceGenerationOptions,
|
SourceGenerationOptions,
|
||||||
contextNamespace,
|
contextNamespace,
|
||||||
dbContextName, contextTypeFullName,
|
dbContextName, contextTypeFullName,
|
||||||
models.ToImmutableArray(),
|
methodsModels.ToImmutableArray(),
|
||||||
Usings);
|
Usings);
|
||||||
}
|
}
|
||||||
|
|
||||||
private EfAddMethodsModel ExtractEfAddMethodsModel(SemanticModel semanticModel, EfEntityDataModel efEntityDataModel)
|
private EfAddMethodsModel ExtractEfAddMethodsModel(SemanticModel semanticModel, MemberDeclarationSyntax memberSyntax, EfEntityDataModel efEntityDataModel)
|
||||||
{
|
{
|
||||||
var efAddAttributeTypeSymbols =
|
var efAddAttributeTypeSymbols =
|
||||||
GetEntityTypeSymbol(MemberSyntax, EfAddMethodsAttributeSource.AttributeName, semanticModel);
|
GetEntityTypeSymbol(memberSyntax, EfAddMethodsAttributeSource.AttributeName, semanticModel);
|
||||||
|
|
||||||
string createTypeIdentifierName = efEntityDataModel.EntityTypeIdentifierName;
|
string createTypeIdentifierName = efEntityDataModel.EntityTypeIdentifierName;
|
||||||
string createTypeFullName = efEntityDataModel.EntityTypeFullName;
|
string createTypeFullName = efEntityDataModel.EntityTypeFullName;
|
||||||
|
@ -335,12 +341,12 @@ namespace MapTo
|
||||||
returnTypeIdentifierName);
|
returnTypeIdentifierName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private EfUpdateMethodsModel ExtractEfUpdateMethodsModel(SemanticModel semanticModel,EfEntityDataModel efEntityDataModel)
|
private EfUpdateMethodsModel ExtractEfUpdateMethodsModel(SemanticModel semanticModel, MemberDeclarationSyntax memberDeclarationSyntax, EfEntityDataModel efEntityDataModel)
|
||||||
{
|
{
|
||||||
var efAddAttributeTypeSymbols =
|
var efAddAttributeTypeSymbols =
|
||||||
GetEntityTypeSymbol(MemberSyntax, EfUpdateMethodsAttributeSource.AttributeName, semanticModel);
|
GetEntityTypeSymbol(MemberSyntax, EfUpdateMethodsAttributeSource.AttributeName, semanticModel);
|
||||||
|
|
||||||
var keyPropertyName = ExtractNameOfMemberName(MemberSyntax, EfUpdateMethodsAttributeSource.AttributeName, semanticModel);
|
var keyPropertyName = ExtractNameOfMemberName(memberDeclarationSyntax, EfUpdateMethodsAttributeSource.AttributeName, semanticModel);
|
||||||
|
|
||||||
var keyMemberType = FindFirstTypeInfoOfMemberExpressionSyntax(semanticModel, MemberSyntax);
|
var keyMemberType = FindFirstTypeInfoOfMemberExpressionSyntax(semanticModel, MemberSyntax);
|
||||||
|
|
||||||
|
@ -386,12 +392,20 @@ namespace MapTo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected ImmutableArray<IPropertySymbol> GetSymbolsFromAttribute(ITypeSymbol typeSymbol, INamedTypeSymbol attributeTypeSymbol)
|
||||||
|
{
|
||||||
|
return typeSymbol
|
||||||
|
.GetAllMembers()
|
||||||
|
.OfType<IPropertySymbol>()
|
||||||
|
.Where(p => p.HasAttribute(attributeTypeSymbol))
|
||||||
|
.ToImmutableArray()!;
|
||||||
|
}
|
||||||
|
|
||||||
private static ITypeSymbol GetEntityTypeData(MemberDeclarationSyntax memberDeclarationSyntax, SemanticModel? semanticModel = null)
|
private static ITypeSymbol GetEntityTypeData(MemberDeclarationSyntax memberDeclarationSyntax, SemanticModel? semanticModel = null)
|
||||||
{
|
{
|
||||||
var member = (memberDeclarationSyntax as PropertyDeclarationSyntax).Type;
|
var member = (memberDeclarationSyntax as PropertyDeclarationSyntax).Type;
|
||||||
var genericSyntax = member as GenericNameSyntax;
|
var genericSyntax = member as GenericNameSyntax;
|
||||||
var typeInfo = ModelExtensions.GetTypeInfo(semanticModel, genericSyntax);
|
var typeInfo = semanticModel.GetTypeInfo( genericSyntax);
|
||||||
var firstTypeArgument = (typeInfo.ConvertedType as INamedTypeSymbol).TypeArguments[0];
|
var firstTypeArgument = (typeInfo.ConvertedType as INamedTypeSymbol).TypeArguments[0];
|
||||||
return firstTypeArgument;
|
return firstTypeArgument;
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,7 +49,6 @@ namespace MapTo
|
||||||
|
|
||||||
private static void AddGeneratedExtensions(GeneratorExecutionContext context, Compilation compilation, IEnumerable<CandidateMember> candidateMembers, SourceGenerationOptions options)
|
private static void AddGeneratedExtensions(GeneratorExecutionContext context, Compilation compilation, IEnumerable<CandidateMember> candidateMembers, SourceGenerationOptions options)
|
||||||
{
|
{
|
||||||
//SpinWait.SpinUntil(() => Debugger.IsAttached);
|
|
||||||
|
|
||||||
foreach (var candidateMember in candidateMembers)
|
foreach (var candidateMember in candidateMembers)
|
||||||
{
|
{
|
||||||
|
|
|
@ -70,8 +70,6 @@ namespace MapTo.Sources
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
builder
|
builder
|
||||||
// End class declaration
|
// End class declaration
|
||||||
.WriteClosingBracket()
|
.WriteClosingBracket()
|
||||||
|
|
Loading…
Reference in New Issue