From 6fd065fc4798699394837e770d087ef110aa8e55 Mon Sep 17 00:00:00 2001 From: CodeLiturgy Date: Sat, 10 Sep 2022 06:05:39 +0100 Subject: [PATCH] stable --- .../EfAddMethodsAttributeSource.cs | 7 ++ .../EfAddToListAttributeSource.cs | 9 +++ .../EfGetListAttributeSource.cs | 8 ++ .../EfGetManyAttributeSource.cs | 6 +- .../EfGetOneByAttributeSource.cs | 7 ++ .../EfGetOneFromListAttributeSource.cs | 9 +++ .../EfUpdateMethodsAttributeSource.cs | 9 +++ src/BlueWest.EfMethods/EfGeneratorContext.cs | 75 +++++++++---------- src/BlueWest.EfMethods/EfMethodsSource.cs | 2 +- 9 files changed, 91 insertions(+), 41 deletions(-) diff --git a/src/BlueWest.EfMethods/AttributeSources/EfAddMethodsAttributeSource.cs b/src/BlueWest.EfMethods/AttributeSources/EfAddMethodsAttributeSource.cs index 05aff2d..7b38198 100644 --- a/src/BlueWest.EfMethods/AttributeSources/EfAddMethodsAttributeSource.cs +++ b/src/BlueWest.EfMethods/AttributeSources/EfAddMethodsAttributeSource.cs @@ -31,6 +31,13 @@ namespace BlueWest.EfMethods.Sources .WriteLine($"public sealed class {AttributeName}Attribute : Attribute") .WriteOpeningBracket(); + builder + .WriteLine("/// ") + .WriteLine("/// Attribute for generating a method for creating a entity of the target property.") + .WriteLine("/// A typeof with the Create projection type.") + .WriteLine("/// The projection with the return data.") + .WriteLine("/// "); + builder .WriteLine($"public {AttributeName}Attribute(Type createType = null, Type returnType = null){"{}"}") .WriteLine(); diff --git a/src/BlueWest.EfMethods/AttributeSources/EfAddToListAttributeSource.cs b/src/BlueWest.EfMethods/AttributeSources/EfAddToListAttributeSource.cs index aac5979..e78f3dc 100644 --- a/src/BlueWest.EfMethods/AttributeSources/EfAddToListAttributeSource.cs +++ b/src/BlueWest.EfMethods/AttributeSources/EfAddToListAttributeSource.cs @@ -29,6 +29,15 @@ namespace BlueWest.EfMethods.EfMethods.AttributeSources .WriteLine($"public sealed class {AttributeName}Attribute : Attribute") .WriteOpeningBracket(); + builder + .WriteLine("/// ") + .WriteLine("/// Attribute for generating a function to get many entities.") + .WriteLine("/// The nameof property of the list.") + .WriteLine("/// The projection with the creation data.") + .WriteLine("/// The projection type with the return data.") + .WriteLine("/// Nameof with look up key of the entity containing the list.") + .WriteLine("/// "); + builder .WriteLine($"public {AttributeName}Attribute(string listPropertyNameof, Type createType, Type returnType, string primaryKeyNameof){"{}"}") .WriteLine(); diff --git a/src/BlueWest.EfMethods/AttributeSources/EfGetListAttributeSource.cs b/src/BlueWest.EfMethods/AttributeSources/EfGetListAttributeSource.cs index 19a3ebb..6eff97f 100644 --- a/src/BlueWest.EfMethods/AttributeSources/EfGetListAttributeSource.cs +++ b/src/BlueWest.EfMethods/AttributeSources/EfGetListAttributeSource.cs @@ -30,6 +30,14 @@ namespace BlueWest.Sources .WriteLine($"public sealed class {AttributeName}Attribute : Attribute") .WriteOpeningBracket(); + + builder + .WriteLine("/// ") + .WriteLine("/// Gets records of a list.") + .WriteLine("/// A nameof with the containing list.") + .WriteLine("/// The projection with the return data.") + .WriteLine("/// The lookup key of the main entity.") + .WriteLine("/// "); builder .WriteLine($"public {AttributeName}Attribute(string listPropertyNameof, Type returnType, string primaryKeyNameof){"{}"}") .WriteLine(); diff --git a/src/BlueWest.EfMethods/AttributeSources/EfGetManyAttributeSource.cs b/src/BlueWest.EfMethods/AttributeSources/EfGetManyAttributeSource.cs index 9a6ca5b..ff20982 100644 --- a/src/BlueWest.EfMethods/AttributeSources/EfGetManyAttributeSource.cs +++ b/src/BlueWest.EfMethods/AttributeSources/EfGetManyAttributeSource.cs @@ -30,7 +30,11 @@ namespace BlueWest.EfMethods.Sources .WriteLine("[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]") .WriteLine($"public sealed class {AttributeName}Attribute : Attribute") .WriteOpeningBracket(); - + builder + .WriteLine("/// ") + .WriteLine("/// Gets many records of a entity.") + .WriteLine("/// The projection with the return data.") + .WriteLine("/// "); builder .WriteLine($"public {AttributeName}Attribute(Type returnType){"{}"}") .WriteLine(); diff --git a/src/BlueWest.EfMethods/AttributeSources/EfGetOneByAttributeSource.cs b/src/BlueWest.EfMethods/AttributeSources/EfGetOneByAttributeSource.cs index d1c1cc9..56b8e7b 100644 --- a/src/BlueWest.EfMethods/AttributeSources/EfGetOneByAttributeSource.cs +++ b/src/BlueWest.EfMethods/AttributeSources/EfGetOneByAttributeSource.cs @@ -29,6 +29,13 @@ namespace BlueWest.EfMethods.Sources .WriteLine($"public sealed class {AttributeName}Attribute : Attribute") .WriteOpeningBracket(); + builder + .WriteLine("/// ") + .WriteLine("/// Gets one record of a entity by a look up key.") + .WriteLine("/// The lookup key.") + .WriteLine("/// The projection with the return data.") + .WriteLine("/// "); + builder .WriteLine($"public {AttributeName}Attribute(string bynameof, Type returnType)") .WriteOpeningBracket() diff --git a/src/BlueWest.EfMethods/AttributeSources/EfGetOneFromListAttributeSource.cs b/src/BlueWest.EfMethods/AttributeSources/EfGetOneFromListAttributeSource.cs index 3892e2a..cf51f28 100644 --- a/src/BlueWest.EfMethods/AttributeSources/EfGetOneFromListAttributeSource.cs +++ b/src/BlueWest.EfMethods/AttributeSources/EfGetOneFromListAttributeSource.cs @@ -33,6 +33,15 @@ namespace BlueWest.Sources .WriteLine($"public sealed class {AttributeName}Attribute : Attribute") .WriteOpeningBracket(); + builder + .WriteLine("/// ") + .WriteLine("/// Gets one record of a list in a entity by the specified look up keys.") + .WriteLine("/// The lookup key of this entity.") + .WriteLine("/// A nameof with the list in the main type.") + .WriteLine("/// A nameof with the lookup key of the the list type.") + .WriteLine("/// The projection with the returning data.") + .WriteLine("/// "); + builder .WriteLine($"public {AttributeName}Attribute(string entityPrimaryKeyNameof, string listMemberNameof, string listPrimaryKeyNameof, Type returnType){"{}"}") .WriteLine(); diff --git a/src/BlueWest.EfMethods/AttributeSources/EfUpdateMethodsAttributeSource.cs b/src/BlueWest.EfMethods/AttributeSources/EfUpdateMethodsAttributeSource.cs index f81cbfa..9039c12 100644 --- a/src/BlueWest.EfMethods/AttributeSources/EfUpdateMethodsAttributeSource.cs +++ b/src/BlueWest.EfMethods/AttributeSources/EfUpdateMethodsAttributeSource.cs @@ -27,6 +27,15 @@ namespace BlueWest.EfMethods.Sources .WriteLine($"public sealed class {AttributeName}Attribute : Attribute") .WriteOpeningBracket(); + + builder + .WriteLine("/// ") + .WriteLine("/// Updates a entity with projection data.") + .WriteLine("/// A projection type with the update data.") + .WriteLine("/// Type of the projection with the return data.") + .WriteLine("/// A nameof with the lookup key of this entity.") + .WriteLine("/// "); + builder .WriteLine($"public {AttributeName}Attribute(Type updateType, Type returnType, string keyPropertyMemberName){"{}"}") .WriteLine(); diff --git a/src/BlueWest.EfMethods/EfGeneratorContext.cs b/src/BlueWest.EfMethods/EfGeneratorContext.cs index 148ca74..7c6ce0a 100644 --- a/src/BlueWest.EfMethods/EfGeneratorContext.cs +++ b/src/BlueWest.EfMethods/EfGeneratorContext.cs @@ -25,7 +25,6 @@ namespace BlueWest.EfMethods public ImmutableArray 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(); - - // 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; + var byParamFullTypeName = keyMemberType?.Type.ToDisplayString(); + 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; } diff --git a/src/BlueWest.EfMethods/EfMethodsSource.cs b/src/BlueWest.EfMethods/EfMethodsSource.cs index dd2c6b1..382962b 100644 --- a/src/BlueWest.EfMethods/EfMethodsSource.cs +++ b/src/BlueWest.EfMethods/EfMethodsSource.cs @@ -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();