From ead01089be319abb994e565e411b2c00794b2bde Mon Sep 17 00:00:00 2001 From: honzapatCZ Date: Sat, 11 Jan 2025 19:01:04 +0100 Subject: [PATCH] fix AMP for null with new projectors --- AutoMapProperty/AutoMapProperty.cs | 49 ++++++++++++++----- .../AutoMapPropertyHelper.csproj | 2 +- AutoMapPropertyHelper/IAutomappedAttribute.cs | 14 +++++- 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/AutoMapProperty/AutoMapProperty.cs b/AutoMapProperty/AutoMapProperty.cs index 10dcb2a..e61cf44 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) - ); + );*/ //Modify attributes to not include our generation attribute mem = propSyntax.WithAttributeLists(newList); @@ -402,11 +402,11 @@ namespace AutoMapProperty ourType); }); //c.MappableProperties = c.MappableProperties.ToDictionary(x => x.Item1, x => x.Item2).Select(x => (x.Key, x.Value)).ToList(); - context.ReportDiagnostic( + /*context.ReportDiagnostic( Diagnostic.Create( new DiagnosticDescriptor( "NEJ01", "Report candidates vs real", "{0} had: {1} possible candidates and:{2} source values, but in the end there were: {3} values", - "NEJ", DiagnosticSeverity.Warning, true), null, cls.Name, possibleCandidates.Count, sourceCandidates.Count, cls.MappableProperties.Count)); + "NEJ", DiagnosticSeverity.Warning, true), null, cls.Name, possibleCandidates.Count, sourceCandidates.Count, cls.MappableProperties.Count));*/ } @@ -562,6 +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 : "); var classToGenerate = otherClasses.First(x => x.Name == ToType.Name); sb.Append(@"new ").Append(classToGenerate.Name).Append(@"() {"); @@ -747,7 +748,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(">"); @@ -769,6 +770,12 @@ namespace ").Append(classToGenerate.Namespace).Append(@" return dat.ApplyFrom(null, source); }"); sb.Append(@" + object? IAutomappedAttribute.ApplyFrom(System.IServiceProvider? providers, object source) + { + if(source is ").Append(classToGenerate.SourceContainer.Identifier).Append(@" src) + return ApplyFrom(providers, src); + return null; + } public new ").Append(classToGenerate.Name).Append(@" ApplyFrom(System.IServiceProvider? providers, ").Append(classToGenerate.SourceContainer.Identifier).Append(@" source) { if(source == null) @@ -893,14 +900,16 @@ namespace ").Append(classToGenerate.Namespace).Append(@" public override int GetHashCode() { return 7; - }"); - - var abstr = cds.Modifiers.Any(x => x.IsKind(SyntaxKind.AbstractKeyword)); - if (!abstr) - { - - sb.Append(@" + } + object IAutomappedAttribute.ApplyTo(System.IServiceProvider? providers, object source) + { + if(source == null) + return null; + if(source is ").Append(classToGenerate.SourceContainer.Identifier).Append(@" src) + return ApplyTo(providers, src); + return null; + } public new ").Append(classToGenerate.SourceContainer.Identifier).Append(@" ApplyTo(System.IServiceProvider? providers, ").Append(classToGenerate.SourceContainer.Identifier).Append(@" source) { if(source == null) @@ -917,9 +926,23 @@ namespace ").Append(classToGenerate.Namespace).Append(@" sb.Append(@" } return source; - }"); + } + Type IAutomappedAttribute.GetSourceType() => typeof(").Append(classToGenerate.SourceContainer.Identifier).Append(@"); + "); sb.Append(@" + "); + + var abstr = cds.Modifiers.Any(x => x.IsKind(SyntaxKind.AbstractKeyword)); + if (!abstr) + { + + sb.Append(@" + public ").Append(classToGenerate.SourceContainer.Identifier).Append(@" Create(System.IServiceProvider? providers = null) + { + var dat = new ").Append(classToGenerate.SourceContainer.Identifier).Append(@"(); + return ApplyTo(providers, dat); + } public static explicit operator ").Append(classToGenerate.SourceContainer.Identifier).Append(@"(").Append(classToGenerate.Name).Append(@" source) { var dat = new ").Append(classToGenerate.SourceContainer.Identifier).Append(@"(); diff --git a/AutoMapPropertyHelper/AutoMapPropertyHelper.csproj b/AutoMapPropertyHelper/AutoMapPropertyHelper.csproj index a038f76..2bd1db9 100644 --- a/AutoMapPropertyHelper/AutoMapPropertyHelper.csproj +++ b/AutoMapPropertyHelper/AutoMapPropertyHelper.csproj @@ -1,7 +1,7 @@ - netstandard2.0 + net7.0 latest enable enable diff --git a/AutoMapPropertyHelper/IAutomappedAttribute.cs b/AutoMapPropertyHelper/IAutomappedAttribute.cs index 72e13c4..52b92b7 100644 --- a/AutoMapPropertyHelper/IAutomappedAttribute.cs +++ b/AutoMapPropertyHelper/IAutomappedAttribute.cs @@ -6,12 +6,22 @@ using Microsoft.Extensions.DependencyInjection; namespace AutoMapPropertyHelper { - public interface IAutomappedAttribute + public interface IAutomappedAttribute { + public virtual object ApplyTo(IServiceProvider? providers, object value) => throw new NotImplementedException(); + public virtual object? ApplyFrom(IServiceProvider? providers, object source) => throw new NotImplementedException(); + public virtual Type GetSourceType() => throw new NotImplementedException(); + } + public interface IAutomappedAttribute : IAutomappedAttribute + { + object IAutomappedAttribute.ApplyTo(IServiceProvider? providers, object value) => throw new NotImplementedException(); + object? IAutomappedAttribute.ApplyFrom(IServiceProvider? providers, object source) => throw new NotImplementedException(); + Type IAutomappedAttribute.GetSourceType() => throw new NotImplementedException(); + public TSource ApplyTo(IServiceProvider? providers, TSource value); public TSelf ApplyFrom(IServiceProvider? providers, TSource source); - public Expression> GetProjectorFrom(IServiceProvider providers); + public Expression> GetProjectorFrom(IServiceProvider providers) => throw new NotImplementedException(); } }