From c815d9849493d6c3ec72c8a2419cba284e314343 Mon Sep 17 00:00:00 2001 From: Mojtaba Shojajou Date: Tue, 3 Aug 2021 21:21:05 +0430 Subject: [PATCH] fix generation bug while there are no any matched properties but still want to map the object --- Mapalyzer.csproj | 2 +- Sources/MappedClassSource.cs | 147 +++++++++++++++++------------------ 2 files changed, 73 insertions(+), 76 deletions(-) diff --git a/Mapalyzer.csproj b/Mapalyzer.csproj index 8c4a9d7..f791e75 100644 --- a/Mapalyzer.csproj +++ b/Mapalyzer.csproj @@ -7,7 +7,7 @@ 9.0 Mapalyzer Mapalyzer - 1.2.0-alpha + 1.2.1-alpha Mojtaba Shojajou Shojajou A boilerplate library built by the C# Analyzers for mapping the DotNET domain classes to the models of the application. diff --git a/Sources/MappedClassSource.cs b/Sources/MappedClassSource.cs index a2be57b..e18bcc4 100644 --- a/Sources/MappedClassSource.cs +++ b/Sources/MappedClassSource.cs @@ -19,83 +19,80 @@ internal static class MappedClassSource private static string ProvideSource(MappingMetaData mappingMetaData, Compilation compilation) { SourceBuilder sourceBuilder = new(); + + _destinationTypeCamelCaseName = mappingMetaData.DestinationTypeName.ToCamelCase(); + _sourceTypeCamelCaseName = mappingMetaData.SourceTypeName.ToCamelCase(); - if (mappingMetaData.PropertyMapHolders.Any()) - { - _destinationTypeCamelCaseName = mappingMetaData.DestinationTypeName.ToCamelCase(); - _sourceTypeCamelCaseName = mappingMetaData.SourceTypeName.ToCamelCase(); - - string sourceNameSpace = mappingMetaData.InjectableNameSpaces.Single(ins => ins.ContainsSourceClass).NameSpace; - string classCtorArguments = string.Join(", ", ProvidePropertiesWithType(mappingMetaData.PropertyMapHolders)); - - using (sourceBuilder) - { - sourceBuilder.WriteLine(Constants.GeneratedFileHeader) - - // Writes nameSpaces - .WriteLine("using System;") - .WriteLine("using System.Collections.Generic;") - .WriteLine("using System.Linq;") - .WriteClassNameSpaces(mappingMetaData.InjectableNameSpaces.Where(ins => !ins.ContainsSourceClass).Select(ins => ins.NameSpace).ToList()) - .WriteLine() - - // Writes class namespace - .WriteLine("namespace {0}", mappingMetaData.DestinationNamespace) - .WriteOpeningBracket() - - // Writes class - .WriteLine("public partial class {0}", mappingMetaData.DestinationTypeName) - .WriteOpeningBracket() - .WriteLine("public {0}({1})", mappingMetaData.DestinationTypeName, classCtorArguments) - .WriteOpeningBracket() - .WriteConstructorPropertiesInitializations(mappingMetaData.PropertyMapHolders) - .WriteClosingBracket() - .WriteClosingBracket() // class - .WriteClosingBracket() // class namespace - .WriteLine() - - // Writes extension namespace - .WriteLine("namespace {0}", compilation.AssemblyName) - .WriteOpeningBracket() - - // Writes fromTo extension - .WriteLine("public static partial class {0}Extensions", mappingMetaData.DestinationTypeName) - .WriteOpeningBracket() - .WriteLine() - .WriteLine("public static {0}.{1} To{1}(this {2}.{3} source_{4}, Action<{2}.{3}, {0}.{1}> postMappingAction = null)", - mappingMetaData.DestinationNamespace, - mappingMetaData.DestinationTypeName, - sourceNameSpace, - mappingMetaData.SourceTypeName, - _sourceTypeCamelCaseName) - - .WriteOpeningBracket() - .WriteLine("if (source_{0} is null)", _sourceTypeCamelCaseName) - .WriteIndentedLine("throw new ArgumentNullException(nameof(source_{0}));", _sourceTypeCamelCaseName) - .WriteLine() - .WriteConverterReturnExpression(mappingMetaData, compilation) - .WriteClosingBracket() - .WriteLine() - - // Writes fromToList extension - .WriteLine("#nullable enable") - .WriteLine("public static IEnumerable<{0}.{1}>? To{2}(this IEnumerable<{3}.{4}> source_{5}, Action<{3}.{4}, {0}.{1}>? postMappingAction = null)", - mappingMetaData.DestinationNamespace, - mappingMetaData.DestinationTypeName, - mappingMetaData.DestinationTypeName.Pluralize(), - sourceNameSpace, - mappingMetaData.SourceTypeName, - _sourceTypeCamelCaseName.Pluralize()) - - .WriteOpeningBracket() - .WriteLine("return source_{0}?.Select(i => i.To{1}(postMappingAction));", _sourceTypeCamelCaseName.Pluralize(), mappingMetaData.DestinationTypeName) - .WriteClosingBracket() - .WriteLine("#nullable disable") - - .WriteClosingBracket() // extension class - .WriteClosingBracket(); // extension namespace + string sourceNameSpace = mappingMetaData.InjectableNameSpaces.Single(ins => ins.ContainsSourceClass).NameSpace; + string classCtorArguments = string.Join(", ", ProvidePropertiesWithType(mappingMetaData.PropertyMapHolders)); + + using (sourceBuilder) + { + sourceBuilder.WriteLine(Constants.GeneratedFileHeader) + + // Writes nameSpaces + .WriteLine("using System;") + .WriteLine("using System.Collections.Generic;") + .WriteLine("using System.Linq;") + .WriteClassNameSpaces(mappingMetaData.InjectableNameSpaces.Where(ins => !ins.ContainsSourceClass).Select(ins => ins.NameSpace).ToList()) + .WriteLine() + + // Writes class namespace + .WriteLine("namespace {0}", mappingMetaData.DestinationNamespace) + .WriteOpeningBracket() + + // Writes class + .WriteLine("public partial class {0}", mappingMetaData.DestinationTypeName) + .WriteOpeningBracket() + .WriteLine("public {0}({1})", mappingMetaData.DestinationTypeName, classCtorArguments) + .WriteOpeningBracket() + .WriteConstructorPropertiesInitializations(mappingMetaData.PropertyMapHolders) + .WriteClosingBracket() + .WriteClosingBracket() // class + .WriteClosingBracket() // class namespace + .WriteLine() + + // Writes extension namespace + .WriteLine("namespace {0}", compilation.AssemblyName) + .WriteOpeningBracket() + + // Writes fromTo extension + .WriteLine("public static partial class {0}Extensions", mappingMetaData.DestinationTypeName) + .WriteOpeningBracket() + .WriteLine() + .WriteLine("public static {0}.{1} To{1}(this {2}.{3} source_{4}, Action<{2}.{3}, {0}.{1}> postMappingAction = null)", + mappingMetaData.DestinationNamespace, + mappingMetaData.DestinationTypeName, + sourceNameSpace, + mappingMetaData.SourceTypeName, + _sourceTypeCamelCaseName) + + .WriteOpeningBracket() + .WriteLine("if (source_{0} is null)", _sourceTypeCamelCaseName) + .WriteIndentedLine("throw new ArgumentNullException(nameof(source_{0}));", _sourceTypeCamelCaseName) + .WriteLine() + .WriteConverterReturnExpression(mappingMetaData, compilation) + .WriteClosingBracket() + .WriteLine() + + // Writes fromToList extension + .WriteLine("#nullable enable") + .WriteLine("public static IEnumerable<{0}.{1}>? To{2}(this IEnumerable<{3}.{4}> source_{5}, Action<{3}.{4}, {0}.{1}>? postMappingAction = null)", + mappingMetaData.DestinationNamespace, + mappingMetaData.DestinationTypeName, + mappingMetaData.DestinationTypeName.Pluralize(), + sourceNameSpace, + mappingMetaData.SourceTypeName, + _sourceTypeCamelCaseName.Pluralize()) + + .WriteOpeningBracket() + .WriteLine("return source_{0}?.Select(i => i.To{1}(postMappingAction));", _sourceTypeCamelCaseName.Pluralize(), mappingMetaData.DestinationTypeName) + .WriteClosingBracket() + .WriteLine("#nullable disable") + + .WriteClosingBracket() // extension class + .WriteClosingBracket(); // extension namespace } - } return sourceBuilder.ToString(); }