From 2ef7ded4506d89060cd17c438828d7e7d712345d Mon Sep 17 00:00:00 2001 From: Mohammadreza Taikandi Date: Thu, 21 Jan 2021 16:43:58 +0000 Subject: [PATCH] Cleanup. --- src/MapTo/DiagnosticProvider.cs | 2 +- src/MapTo/Sources/IgnorePropertyAttributeSource.cs | 7 ++++--- src/MapTo/Sources/MapFromAttributeSource.cs | 2 ++ src/MapTo/Sources/MapTypeConverterAttributeSource.cs | 6 +++--- test/MapTo.Tests/Tests.cs | 2 +- test/TestConsoleApp/ViewModels/UserViewModel.cs | 2 +- 6 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/MapTo/DiagnosticProvider.cs b/src/MapTo/DiagnosticProvider.cs index 7085bcd..65e6281 100644 --- a/src/MapTo/DiagnosticProvider.cs +++ b/src/MapTo/DiagnosticProvider.cs @@ -23,7 +23,7 @@ namespace MapTo Create($"{ErrorId}030", location, "Type Mismatch", $"No matching properties found between '{classType.ToDisplayString()}' and '{sourceType.ToDisplayString()}' types."); internal static Diagnostic NoMatchingPropertyTypeFoundError(IPropertySymbol property) => - Create($"{ErrorId}031", property.Locations.FirstOrDefault(), "Type Mismatch", $"Cannot create a map for '{property.ToDisplayString()}' property because source and destination types are not implicitly convertible. Consider using '{MapTypeConverterAttributeSource.FullyQualifiedName}' to provide a type converter or ignore the property using '{RootNamespace}.{IgnorePropertyAttributeSource.AttributeName}Attribute'."); + Create($"{ErrorId}031", property.Locations.FirstOrDefault(), "Type Mismatch", $"Cannot create a map for '{property.ToDisplayString()}' property because source and destination types are not implicitly convertible. Consider using '{MapTypeConverterAttributeSource.FullyQualifiedName}' to provide a type converter or ignore the property using '{IgnorePropertyAttributeSource.FullyQualifiedName}'."); internal static Diagnostic InvalidTypeConverterGenericTypesError(IPropertySymbol property, IPropertySymbol sourceProperty) => Create($"{ErrorId}032", property.Locations.FirstOrDefault(), "Type Mismatch", $"Cannot map '{property.ToDisplayString()}' property because the annotated converter does not implement '{RootNamespace}.{TypeConverterSource.InterfaceName}<{sourceProperty.Type.ToDisplayString()}, {property.Type.ToDisplayString()}>'."); diff --git a/src/MapTo/Sources/IgnorePropertyAttributeSource.cs b/src/MapTo/Sources/IgnorePropertyAttributeSource.cs index 18eb5ed..03a8b79 100644 --- a/src/MapTo/Sources/IgnorePropertyAttributeSource.cs +++ b/src/MapTo/Sources/IgnorePropertyAttributeSource.cs @@ -5,7 +5,8 @@ namespace MapTo.Sources internal static class IgnorePropertyAttributeSource { internal const string AttributeName = "IgnoreProperty"; - internal const string FullyQualifiedName = RootNamespace + "." + AttributeName + "Attribute"; + internal const string AttributeClassName = AttributeName + "Attribute"; + internal const string FullyQualifiedName = RootNamespace + "." + AttributeClassName; internal static SourceCode Generate(SourceGenerationOptions options) { @@ -26,10 +27,10 @@ namespace MapTo.Sources builder .WriteLine("[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]") - .WriteLine($"public sealed class {AttributeName}Attribute : Attribute {{ }}") + .WriteLine($"public sealed class {AttributeClassName} : Attribute {{ }}") .WriteClosingBracket(); - return new(builder.ToString(), $"{AttributeName}Attribute.g.cs"); + return new(builder.ToString(), $"{AttributeClassName}.g.cs"); } } } \ No newline at end of file diff --git a/src/MapTo/Sources/MapFromAttributeSource.cs b/src/MapTo/Sources/MapFromAttributeSource.cs index c483391..f4a54d3 100644 --- a/src/MapTo/Sources/MapFromAttributeSource.cs +++ b/src/MapTo/Sources/MapFromAttributeSource.cs @@ -5,6 +5,8 @@ namespace MapTo.Sources internal static class MapFromAttributeSource { internal const string AttributeName = "MapFrom"; + internal const string AttributeClassName = AttributeName + "Attribute"; + internal const string FullyQualifiedName = RootNamespace + "." + AttributeClassName; internal static SourceCode Generate(SourceGenerationOptions options) { diff --git a/src/MapTo/Sources/MapTypeConverterAttributeSource.cs b/src/MapTo/Sources/MapTypeConverterAttributeSource.cs index 7bac936..648d29b 100644 --- a/src/MapTo/Sources/MapTypeConverterAttributeSource.cs +++ b/src/MapTo/Sources/MapTypeConverterAttributeSource.cs @@ -23,12 +23,12 @@ namespace MapTo.Sources { builder .WriteLine("/// ") - .WriteLine("/// Maps a property to property of another object.") + .WriteLine("/// Specifies what type to use as a converter for the property this attribute is bound to.") .WriteLine("/// "); } builder - .WriteLine("[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]") + .WriteLine("[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]") .WriteLine($"public sealed class {AttributeClassName} : Attribute") .WriteOpeningBracket(); @@ -51,7 +51,7 @@ namespace MapTo.Sources { builder .WriteLine("/// ") - .WriteLine("/// Gets or sets the to be used to convert the source type.") + .WriteLine($"/// Gets or sets the to be used to convert the source type.") .WriteLine("/// "); } diff --git a/test/MapTo.Tests/Tests.cs b/test/MapTo.Tests/Tests.cs index 9e6ccbb..24f581f 100644 --- a/test/MapTo.Tests/Tests.cs +++ b/test/MapTo.Tests/Tests.cs @@ -527,7 +527,7 @@ using System; namespace MapTo {{ - [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)] + [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public sealed class MapTypeConverterAttribute : Attribute {{ public MapTypeConverterAttribute(Type converter) diff --git a/test/TestConsoleApp/ViewModels/UserViewModel.cs b/test/TestConsoleApp/ViewModels/UserViewModel.cs index 6da6987..0a75f43 100644 --- a/test/TestConsoleApp/ViewModels/UserViewModel.cs +++ b/test/TestConsoleApp/ViewModels/UserViewModel.cs @@ -10,7 +10,7 @@ namespace TestConsoleApp.ViewModels [IgnoreProperty] public string LastName { get; } - [MapProperty(Converter = typeof(LastNameConverter))] + [MapTypeConverter(typeof(LastNameConverter))] public string Key { get; } private class LastNameConverter : ITypeConverter