From 90da30344e1752daa434f3dc32c3e621a2c19e06 Mon Sep 17 00:00:00 2001 From: honzapatCZ Date: Sat, 11 Jan 2025 23:10:22 +0100 Subject: [PATCH] fixup abstract to generation --- AutoMapProperty/AutoMapProperty.cs | 42 ++++++++++++++++++------------ 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/AutoMapProperty/AutoMapProperty.cs b/AutoMapProperty/AutoMapProperty.cs index e61cf44..805ca14 100644 --- a/AutoMapProperty/AutoMapProperty.cs +++ b/AutoMapProperty/AutoMapProperty.cs @@ -213,10 +213,10 @@ namespace AutoMapProperty try { var newList = GetModifiedAttributeList(compilation, propSyntax.AttributeLists); -/* - context.ReportDiagnostic(Diagnostic.Create( - new DiagnosticDescriptor("NEJ23", "Error", "New list on: {1} {0}", "NEJ", DiagnosticSeverity.Warning, true), null, string.Join(", ", newList.Select(x => x.ToString())), propSymbol.Name) - );*/ + /* + context.ReportDiagnostic(Diagnostic.Create( + new DiagnosticDescriptor("NEJ23", "Error", "New list on: {1} {0}", "NEJ", DiagnosticSeverity.Warning, true), null, string.Join(", ", newList.Select(x => x.ToString())), propSymbol.Name) + );*/ //Modify attributes to not include our generation attribute mem = propSyntax.WithAttributeLists(newList); @@ -562,7 +562,7 @@ namespace AutoMapProperty sb.Append("(").Append(ToType).Append(@")source").Append(prefix).Append(".").Append(Key); else { - sb.Append("source").Append(prefix).Append(".").Append(Key).Append(" == null ? null : "); + sb.Append("source").Append(prefix).Append(".").Append(Key).Append(" == null ? null : "); var classToGenerate = otherClasses.First(x => x.Name == ToType.Name); sb.Append(@"new ").Append(classToGenerate.Name).Append(@"() {"); @@ -604,7 +604,7 @@ namespace AutoMapProperty return sb.ToString(); } } - public static string GeneratePropertyTo(List otherClasses, string Key, ITypeSymbol FromType, ITypeSymbol ToType, bool nullable = true) + public static string? GeneratePropertyTo(List otherClasses, string Key, ITypeSymbol FromType, ITypeSymbol ToType, bool nullable = true) { var sb = new StringBuilder(); sb.Append(Key).Append(@" ="); @@ -615,6 +615,9 @@ namespace AutoMapProperty bool isArray = !SymbolEqualityComparer.Default.Equals(FromType, concreteFromType); + if (concreteFromType.IsAbstract) + return null; + if (isArray) { sb.Append("(").Append(FromType).Append(@")(this.").Append(Key).Append(" != null ? "); @@ -657,7 +660,10 @@ namespace AutoMapProperty { if (Value.FromIsReadOnly) return null; - retSb.Append(GeneratePropertyTo(otherClasses, Key, Value.FromType, Value.ToType, nullable)); + var generated = GeneratePropertyTo(otherClasses, Key, Value.FromType, Value.ToType, nullable); + if (generated == null) + return null; + retSb.Append(generated); } else { @@ -748,7 +754,7 @@ namespace ").Append(classToGenerate.Namespace).Append(@" [System.CodeDom.Compiler.GeneratedCode(""AutoMapProperty"", ""1.0.0"")] public partial ").Append(classToGenerate.SourceContainer is ClassDeclarationSyntax ? "class " : "interface ").Append(classToGenerate.Name); - if (classToGenerate.SourceContainer is ClassDeclarationSyntax )//&& !classToGenerate.SourceContainer.Modifiers.Any(x => x.IsKind(SyntaxKind.AbstractKeyword))) + if (classToGenerate.SourceContainer is ClassDeclarationSyntax)//&& !classToGenerate.SourceContainer.Modifiers.Any(x => x.IsKind(SyntaxKind.AbstractKeyword))) { sb.Append(": IAutomappedAttribute<").Append(classToGenerate.SourceContainer.Identifier).Append(",").Append(classToGenerate.Name).Append(">"); sb.Append(", System.Collections.Generic.IEqualityComparer<").Append(classToGenerate.Name).Append(">"); @@ -915,21 +921,23 @@ namespace ").Append(classToGenerate.Namespace).Append(@" if(source == null) return null; {"); - foreach (var prop in classToGenerate.MappableProperties) - { - var property = GenerateCorrectToProperty(otherClasses, prop.Key, prop.Value); - if (property == null) - continue; - sb.Append(@" - ").Append(property); - } + foreach (var prop in classToGenerate.MappableProperties) + {/* + if (prop.Value.FromType.IsAbstract) + continue;*/ + var property = GenerateCorrectToProperty(otherClasses, prop.Key, prop.Value); + if (property == null) + continue; sb.Append(@" + ").Append(property); + } + sb.Append(@" } return source; } Type IAutomappedAttribute.GetSourceType() => typeof(").Append(classToGenerate.SourceContainer.Identifier).Append(@"); "); - sb.Append(@" + sb.Append(@" ");