fix AMP for null with new projectors

This commit is contained in:
honzapatCZ 2025-01-11 19:01:04 +01:00
parent 0be24c0990
commit ead01089be
3 changed files with 49 additions and 16 deletions

View File

@ -213,10 +213,10 @@ namespace AutoMapProperty
try try
{ {
var newList = GetModifiedAttributeList(compilation, propSyntax.AttributeLists); var newList = GetModifiedAttributeList(compilation, propSyntax.AttributeLists);
/*
context.ReportDiagnostic(Diagnostic.Create( 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) 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 //Modify attributes to not include our generation attribute
mem = propSyntax.WithAttributeLists(newList); mem = propSyntax.WithAttributeLists(newList);
@ -402,11 +402,11 @@ namespace AutoMapProperty
ourType); ourType);
}); });
//c.MappableProperties = c.MappableProperties.ToDictionary(x => x.Item1, x => x.Item2).Select(x => (x.Key, x.Value)).ToList(); //c.MappableProperties = c.MappableProperties.ToDictionary(x => x.Item1, x => x.Item2).Select(x => (x.Key, x.Value)).ToList();
context.ReportDiagnostic( /*context.ReportDiagnostic(
Diagnostic.Create( Diagnostic.Create(
new DiagnosticDescriptor( new DiagnosticDescriptor(
"NEJ01", "Report candidates vs real", "{0} had: {1} possible candidates and:{2} source values, but in the end there were: {3} values", "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); sb.Append("(").Append(ToType).Append(@")source").Append(prefix).Append(".").Append(Key);
else else
{ {
sb.Append("source").Append(prefix).Append(".").Append(Key).Append(" == null ? null : ");
var classToGenerate = otherClasses.First(x => x.Name == ToType.Name); var classToGenerate = otherClasses.First(x => x.Name == ToType.Name);
sb.Append(@"new ").Append(classToGenerate.Name).Append(@"() sb.Append(@"new ").Append(classToGenerate.Name).Append(@"()
{"); {");
@ -747,7 +748,7 @@ namespace ").Append(classToGenerate.Namespace).Append(@"
[System.CodeDom.Compiler.GeneratedCode(""AutoMapProperty"", ""1.0.0"")] [System.CodeDom.Compiler.GeneratedCode(""AutoMapProperty"", ""1.0.0"")]
public partial ").Append(classToGenerate.SourceContainer is ClassDeclarationSyntax ? "class " : "interface ").Append(classToGenerate.Name); 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(": IAutomappedAttribute<").Append(classToGenerate.SourceContainer.Identifier).Append(",").Append(classToGenerate.Name).Append(">");
sb.Append(", System.Collections.Generic.IEqualityComparer<").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); return dat.ApplyFrom(null, source);
}"); }");
sb.Append(@" 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) public new ").Append(classToGenerate.Name).Append(@" ApplyFrom(System.IServiceProvider? providers, ").Append(classToGenerate.SourceContainer.Identifier).Append(@" source)
{ {
if(source == null) if(source == null)
@ -893,14 +900,16 @@ namespace ").Append(classToGenerate.Namespace).Append(@"
public override int GetHashCode() public override int GetHashCode()
{ {
return 7; return 7;
}"); }
var abstr = cds.Modifiers.Any(x => x.IsKind(SyntaxKind.AbstractKeyword)); object IAutomappedAttribute.ApplyTo(System.IServiceProvider? providers, object source)
if (!abstr)
{ {
if(source == null)
sb.Append(@" 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) public new ").Append(classToGenerate.SourceContainer.Identifier).Append(@" ApplyTo(System.IServiceProvider? providers, ").Append(classToGenerate.SourceContainer.Identifier).Append(@" source)
{ {
if(source == null) if(source == null)
@ -917,9 +926,23 @@ namespace ").Append(classToGenerate.Namespace).Append(@"
sb.Append(@" sb.Append(@"
} }
return source; return source;
}"); }
Type IAutomappedAttribute.GetSourceType() => typeof(").Append(classToGenerate.SourceContainer.Identifier).Append(@");
");
sb.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) public static explicit operator ").Append(classToGenerate.SourceContainer.Identifier).Append(@"(").Append(classToGenerate.Name).Append(@" source)
{ {
var dat = new ").Append(classToGenerate.SourceContainer.Identifier).Append(@"(); var dat = new ").Append(classToGenerate.SourceContainer.Identifier).Append(@"();

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>

View File

@ -6,12 +6,22 @@ using Microsoft.Extensions.DependencyInjection;
namespace AutoMapPropertyHelper namespace AutoMapPropertyHelper
{ {
public interface IAutomappedAttribute<TSource, TSelf> 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<TSource, TSelf> : 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 TSource ApplyTo(IServiceProvider? providers, TSource value);
public TSelf ApplyFrom(IServiceProvider? providers, TSource source); public TSelf ApplyFrom(IServiceProvider? providers, TSource source);
public Expression<Func<TSource, TSelf>> GetProjectorFrom(IServiceProvider providers); public Expression<Func<TSource, TSelf>> GetProjectorFrom(IServiceProvider providers) => throw new NotImplementedException();
} }
} }