This commit is contained in:
CodeLiturgy 2022-09-10 06:05:39 +01:00
parent 2ac7524e8b
commit 6fd065fc47
9 changed files with 91 additions and 41 deletions

View File

@ -31,6 +31,13 @@ namespace BlueWest.EfMethods.Sources
.WriteLine($"public sealed class {AttributeName}Attribute : Attribute")
.WriteOpeningBracket();
builder
.WriteLine("/// <summary>")
.WriteLine("/// Attribute for generating a method for creating a entity of the target property.")
.WriteLine("/// <param name=\"createType\">A typeof with the Create projection type.</param>")
.WriteLine("/// <param name=\"returnType\">The projection with the return data.</param>")
.WriteLine("/// </summary>");
builder
.WriteLine($"public {AttributeName}Attribute(Type createType = null, Type returnType = null){"{}"}")
.WriteLine();

View File

@ -29,6 +29,15 @@ namespace BlueWest.EfMethods.EfMethods.AttributeSources
.WriteLine($"public sealed class {AttributeName}Attribute : Attribute")
.WriteOpeningBracket();
builder
.WriteLine("/// <summary>")
.WriteLine("/// Attribute for generating a function to get many entities.")
.WriteLine("/// <param name=\"listPropertyNameof\">The nameof property of the list.</param>")
.WriteLine("/// <param name=\"createType\">The projection with the creation data.</param>")
.WriteLine("/// <param name=\"returnType\">The projection type with the return data.</param>")
.WriteLine("/// <param name=\"primaryKeyNameof\">Nameof with look up key of the entity containing the list.</param>")
.WriteLine("/// </summary>");
builder
.WriteLine($"public {AttributeName}Attribute(string listPropertyNameof, Type createType, Type returnType, string primaryKeyNameof){"{}"}")
.WriteLine();

View File

@ -30,6 +30,14 @@ namespace BlueWest.Sources
.WriteLine($"public sealed class {AttributeName}Attribute : Attribute")
.WriteOpeningBracket();
builder
.WriteLine("/// <summary>")
.WriteLine("/// Gets records of a list.")
.WriteLine("/// <param name=\"listPropertyNameof\">A nameof with the containing list.</param>")
.WriteLine("/// <param name=\"returnType\">The projection with the return data.</param>")
.WriteLine("/// <param name=\"primaryKeyNameof\">The lookup key of the main entity.</param>")
.WriteLine("/// </summary>");
builder
.WriteLine($"public {AttributeName}Attribute(string listPropertyNameof, Type returnType, string primaryKeyNameof){"{}"}")
.WriteLine();

View File

@ -30,7 +30,11 @@ namespace BlueWest.EfMethods.Sources
.WriteLine("[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]")
.WriteLine($"public sealed class {AttributeName}Attribute : Attribute")
.WriteOpeningBracket();
builder
.WriteLine("/// <summary>")
.WriteLine("/// Gets many records of a entity.")
.WriteLine("/// <param name=\"returnType\">The projection with the return data.</param>")
.WriteLine("/// </summary>");
builder
.WriteLine($"public {AttributeName}Attribute(Type returnType){"{}"}")
.WriteLine();

View File

@ -29,6 +29,13 @@ namespace BlueWest.EfMethods.Sources
.WriteLine($"public sealed class {AttributeName}Attribute : Attribute")
.WriteOpeningBracket();
builder
.WriteLine("/// <summary>")
.WriteLine("/// Gets one record of a entity by a look up key.")
.WriteLine("/// <param name=\"bynameof\">The lookup key.</param>")
.WriteLine("/// <param name=\"returnType\">The projection with the return data.</param>")
.WriteLine("/// </summary>");
builder
.WriteLine($"public {AttributeName}Attribute(string bynameof, Type returnType)")
.WriteOpeningBracket()

View File

@ -33,6 +33,15 @@ namespace BlueWest.Sources
.WriteLine($"public sealed class {AttributeName}Attribute : Attribute")
.WriteOpeningBracket();
builder
.WriteLine("/// <summary>")
.WriteLine("/// Gets one record of a list in a entity by the specified look up keys.")
.WriteLine("/// <param name=\"entityPrimaryKeyNameof\">The lookup key of this entity.</param>")
.WriteLine("/// <param name=\"listMemberNameof\">A nameof with the list in the main type.</param>")
.WriteLine("/// <param name=\"listPrimaryKeyNameof\">A nameof with the lookup key of the the list type.</param>")
.WriteLine("/// <param name=\"returnType\">The projection with the returning data.</param>")
.WriteLine("/// </summary>");
builder
.WriteLine($"public {AttributeName}Attribute(string entityPrimaryKeyNameof, string listMemberNameof, string listPrimaryKeyNameof, Type returnType){"{}"}")
.WriteLine();

View File

@ -27,6 +27,15 @@ namespace BlueWest.EfMethods.Sources
.WriteLine($"public sealed class {AttributeName}Attribute : Attribute")
.WriteOpeningBracket();
builder
.WriteLine("/// <summary>")
.WriteLine("/// Updates a entity with projection data.")
.WriteLine("/// <param name=\"updateType\">A projection type with the update data.</param>")
.WriteLine("/// <param name=\"returnType\">Type of the projection with the return data.</param>")
.WriteLine("/// <param name=\"keyPropertyMemberName\">A nameof with the lookup key of this entity.</param>")
.WriteLine("/// </summary>");
builder
.WriteLine($"public {AttributeName}Attribute(Type updateType, Type returnType, string keyPropertyMemberName){"{}"}")
.WriteLine();

View File

@ -25,7 +25,6 @@ namespace BlueWest.EfMethods
public ImmutableArray<Diagnostic> Diagnostics { get; private set; }
public EfMethodsModel? Model { get; private set; }
protected Compilation Compilation { get; }
protected INamedTypeSymbol MappingContextTypeSymbol { get; }
protected SourceGenerationOptions SourceGenerationOptions { get; }
protected TypeDeclarationSyntax TypeSyntax { get; }
@ -136,10 +135,10 @@ namespace BlueWest.EfMethods
}
protected string ExtractNameOfMemberName(MemberDeclarationSyntax memberDeclarationSyntax, string attributeName, SemanticModel? semanticModel = null, int skip = 0)
protected string ExtractNameOfMemberName(MemberDeclarationSyntax memberDeclarationSyntax, string attributeName, int skip = 0)
{
var attributeData = memberDeclarationSyntax.GetAttribute(attributeName);
var sourceSymbol = ExtractNameOfMemberName(attributeData, semanticModel, skip);
var sourceSymbol = ExtractNameOfMemberName(attributeData, skip);
return sourceSymbol;
}
@ -190,8 +189,9 @@ namespace BlueWest.EfMethods
return sourceTypeExpressionSyntax;
}
protected string ExtractNameOfMemberName(SyntaxNode? attributeSyntax, SemanticModel semanticModel, int skip)
protected string ExtractNameOfMemberName(SyntaxNode? attributeSyntax, int skip)
{
if (attributeSyntax is null)
{
return string.Empty;
@ -258,7 +258,7 @@ namespace BlueWest.EfMethods
var semanticModel = Compilation.GetSemanticModel(TypeSyntax.SyntaxTree);
// get containing class type information
ClassDeclarationSyntax classDeclarationSyntax = TypeSyntax as ClassDeclarationSyntax;
ClassDeclarationSyntax classDeclarationSyntax = (TypeSyntax as ClassDeclarationSyntax)!;
// context name
var dbContextName = classDeclarationSyntax.Identifier.ValueText;
@ -392,11 +392,11 @@ namespace BlueWest.EfMethods
var efAddAttributeTypeSymbols =
GetEntityTypeSymbol(uProperty, EfUpdateMethodsAttributeSource.AttributeName, semanticModel);
var keyPropertyName = ExtractNameOfMemberName(uProperty, EfUpdateMethodsAttributeSource.AttributeName, semanticModel);
var keyPropertyName = ExtractNameOfMemberName(uProperty, EfUpdateMethodsAttributeSource.AttributeName);
var keyMemberType = FindTypeInfoOfMemberExpressionSyntax(semanticModel, TypeSyntax);
//var secondTypeInfo = FindSecondTypeInfoOfMemberExpressionSyntax(semanticModel, TypeSyntax);
var keyMemberFullName = keyMemberType.Value.Type.ToDisplayString();
var keyMemberFullName = keyMemberType?.Type.ToDisplayString();
//var keyMemberName = keyMemberType.Value.Type.Name;
// Try grabbing string literal if there's no nameof in it
@ -407,8 +407,6 @@ namespace BlueWest.EfMethods
.Token.ValueText ?? "Id";
}
if (efAddAttributeTypeSymbols == null) return null;
string updateTypeIdentifierName = entityDataModel.EntityTypeIdentifierName;
string updateTypeFullName = entityDataModel.EntityTypeFullName;
string returnTypeIdentifierName = entityDataModel.EntityTypeIdentifierName;
@ -429,14 +427,13 @@ namespace BlueWest.EfMethods
return new EfUpdateMethodsModel(entityDataModel, updateTypeFullName, updateTypeIdentifierName, returnTypeFullName,
returnTypeIdentifierName, keyPropertyName, keyMemberFullName);
returnTypeIdentifierName, keyPropertyName, keyMemberFullName ?? string.Empty);
}
private EfGetOneWithModel ExtractEfGetEntityWithModel(MemberDeclarationSyntax uProperty, SemanticModel semanticModel, EfEntityDataModel entityDataModel)
{
var efTypeofSymbols =GetEntityTypeSymbol(uProperty, EfGetOneByAttributeSource.AttributeName, semanticModel);
if (efTypeofSymbols == null) return null;
string returnTypeIdentifierName = entityDataModel.EntityTypeIdentifierName;
string returnTypeFullName = entityDataModel.EntityTypeFullName;
@ -458,7 +455,6 @@ namespace BlueWest.EfMethods
{
var efTypeofSymbols =GetEntityTypeSymbol(uProperty, EfGetOneByAttributeSource.AttributeName, semanticModel);
if (efTypeofSymbols == null) return null;
string returnTypeIdentifierName = entityDataModel.EntityTypeIdentifierName;
string returnTypeFullName = entityDataModel.EntityTypeFullName;
@ -481,20 +477,11 @@ namespace BlueWest.EfMethods
{
var efTypeofSymbols = GetEntityTypeSymbol(uProperty, EfGetOneByAttributeSource.AttributeName, semanticModel);
var byParamPropertyName = ExtractNameOfMemberName(uProperty, EfGetOneByAttributeSource.AttributeName, semanticModel);
var byParamPropertyName = ExtractNameOfMemberName(uProperty, EfGetOneByAttributeSource.AttributeName);
var keyMemberType = FindTypeInfoOfMemberExpressionSyntax(semanticModel, TypeSyntax);
var byParamFullTypeName = keyMemberType.Value.Type.ToDisplayString();
var byParamFullTypeName = keyMemberType?.Type.ToDisplayString();
// Try grabbing string literal if there's no nameof in it
if (byParamPropertyName == string.Empty)
{
byParamPropertyName = GetEntityStringLiteralSymbol(TypeSyntax, EfGetOneByAttributeSource.AttributeName, semanticModel)
.FirstOrDefault()?
.Token.ValueText ?? "Id";
}
if (efTypeofSymbols == null) return null;
string returnTypeIdentifierName = entityDataModel.EntityTypeIdentifierName;
string returnTypeFullName = entityDataModel.EntityTypeFullName;
@ -508,7 +495,7 @@ namespace BlueWest.EfMethods
}
return new EfGetOneByModel(entityDataModel, byParamPropertyName, byParamFullTypeName, returnTypeFullName, returnTypeIdentifierName);
return new EfGetOneByModel(entityDataModel, byParamPropertyName, byParamFullTypeName ?? string.Empty, returnTypeFullName, returnTypeIdentifierName);
}
@ -517,10 +504,12 @@ namespace BlueWest.EfMethods
var efTypeofSymbols = GetEntityTypeSymbol(uProperty, EfGetListAttributeSource.AttributeName, semanticModel);
var listPropertyName = ExtractNameOfMemberName(uProperty, EfGetListAttributeSource.AttributeName, semanticModel);
var keyPropertyName = ExtractNameOfMemberName(uProperty, EfGetListAttributeSource.AttributeName, semanticModel, 1);;
var listPropertyName = ExtractNameOfMemberName(uProperty, EfGetListAttributeSource.AttributeName);
var keyPropertyName = ExtractNameOfMemberName(uProperty, EfGetListAttributeSource.AttributeName, 1);;
var keyMemberType = FindTypeInfoOfMemberExpressionSyntax(semanticModel, TypeSyntax);
#pragma warning disable CS8629
var keyFullTypeName = keyMemberType.Value.Type.ToDisplayString();
#pragma warning restore CS8629
// Try grabbing string literal if there's no nameof in it
@ -550,11 +539,15 @@ namespace BlueWest.EfMethods
{
var listPropertyName = ExtractNameOfMemberName(uProperty, EfAddToListAttributeSource.AttributeName, semanticModel);
var keyPropertyName = ExtractNameOfMemberName(uProperty, EfAddToListAttributeSource.AttributeName, semanticModel, 1);;
var listPropertyName = ExtractNameOfMemberName(uProperty, EfAddToListAttributeSource.AttributeName);
var keyPropertyName = ExtractNameOfMemberName(uProperty, EfAddToListAttributeSource.AttributeName, 1);;
var keyMemberType = FindTypeInfoOfMemberExpressionSyntax(semanticModel, TypeSyntax);
#pragma warning disable CS8629
var keyFullTypeName = keyMemberType.Value.Type.ToDisplayString();
#pragma warning restore CS8629
#pragma warning disable CS8629
var listEntityTypeInfo = (FindTypeInfoOfMemberExpressionSyntax(semanticModel, TypeSyntax, 2).Value.Type as INamedTypeSymbol).TypeArguments.FirstOrDefault();
#pragma warning restore CS8629
var listEntityIdentifierName = listEntityTypeInfo.Name;
var listEntityFullTypeName = listEntityTypeInfo.ToDisplayString();
@ -611,17 +604,19 @@ namespace BlueWest.EfMethods
var efTypeofSymbols = GetEntityTypeSymbol(uProperty, EfGetOneFromListAttributeSource.AttributeName, semanticModel);
var attributeSymbol = uProperty.GetAttribute(EfGetOneFromListAttributeSource.AttributeName);
var keyPropertyName = ExtractNameOfMemberName(uProperty, EfGetOneFromListAttributeSource.AttributeName, semanticModel);
var listKeyPropertyName = ExtractNameOfMemberName(uProperty, EfGetOneFromListAttributeSource.AttributeName, semanticModel, 2);
var keyPropertyName = ExtractNameOfMemberName(uProperty, EfGetOneFromListAttributeSource.AttributeName);
var listKeyPropertyName = ExtractNameOfMemberName(uProperty, EfGetOneFromListAttributeSource.AttributeName, 2);
#pragma warning disable CS8604
var keyMemberType = FindTypeInfoOfMemberExpressionSyntax(semanticModel, attributeSymbol);
#pragma warning restore CS8604
var listKeyTypeInfo = FindTypeInfoOfMemberExpressionSyntax(semanticModel, attributeSymbol, 0);
var keyFullTypeName = keyMemberType.Value.Type.ToDisplayString();
var listEntityTypeInfo = (FindTypeInfoOfMemberExpressionSyntax(semanticModel, attributeSymbol, 1).Value.Type as INamedTypeSymbol).TypeArguments.FirstOrDefault();
var keyFullTypeName = keyMemberType?.Type.ToDisplayString();
var listEntityTypeInfo = (FindTypeInfoOfMemberExpressionSyntax(semanticModel, attributeSymbol, 1)?.Type as INamedTypeSymbol).TypeArguments.FirstOrDefault();
var listEntityIdentifierName = listEntityTypeInfo.Name;
var listEntityFullTypeName = listEntityTypeInfo.ToDisplayString();
var listKeyFullTypeName =listKeyTypeInfo.Value.Type.ToDisplayString();;
var listKeyTypeName = listKeyTypeInfo.Value.Type.Name;
var listPropertyName = ExtractNameOfMemberName(uProperty, EfGetOneFromListAttributeSource.AttributeName, semanticModel, 1);;
var listKeyFullTypeName =listKeyTypeInfo?.Type.ToDisplayString();;
var listKeyTypeName = listKeyTypeInfo?.Type.Name;
var listPropertyName = ExtractNameOfMemberName(uProperty, EfGetOneFromListAttributeSource.AttributeName, 1);;
string returnTypeIdentifierName = entityDataModel.EntityTypeIdentifierName;
@ -640,10 +635,10 @@ namespace BlueWest.EfMethods
listEntityIdentifierName,
listEntityFullTypeName,
keyPropertyName,
keyFullTypeName,
keyFullTypeName ?? string.Empty,
listKeyPropertyName,
listKeyFullTypeName,
listKeyTypeName,
listKeyFullTypeName?? string.Empty,
listKeyTypeName?? string.Empty,
returnTypeIdentifierName,
returnTypeFullName);
}
@ -653,7 +648,9 @@ namespace BlueWest.EfMethods
{
var member = (memberDeclarationSyntax as PropertyDeclarationSyntax).Type;
var genericSyntax = member as GenericNameSyntax;
#pragma warning disable CS8604
var typeInfo = semanticModel.GetTypeInfo( genericSyntax);
#pragma warning restore CS8604
var firstTypeArgument = (typeInfo.ConvertedType as INamedTypeSymbol).TypeArguments[0];
return firstTypeArgument;
}

View File

@ -27,7 +27,7 @@ namespace BlueWest.EfMethods.Sources
.WriteLine($"namespace {model.Namespace}")
.WriteOpeningBracket()
// Class declaration
.WriteLine($"public static partial class {model.ContextTypeName}Extensions")
.WriteLine($"internal static partial class {model.ContextTypeName}Extensions")
.WriteOpeningBracket()
.WriteLine();