Update model property names to better reflect the source type.

This commit is contained in:
Mohammadreza Taikandi 2020-12-19 09:07:35 +00:00
parent 450de3dedc
commit 0327ab41a6
3 changed files with 17 additions and 20 deletions

View File

@ -66,9 +66,9 @@ namespace MapTo
classModifiers: classSyntax.GetClassModifier(), classModifiers: classSyntax.GetClassModifier(),
className: classSyntax.GetClassName(), className: classSyntax.GetClassName(),
properties: classSymbol.GetAllMembersOfType<IPropertySymbol>(), properties: classSymbol.GetAllMembersOfType<IPropertySymbol>(),
destinationNamespace: destinationTypeSymbol.ContainingNamespace.Name, sourceNamespace: destinationTypeSymbol.ContainingNamespace.Name,
destinationClassName: destinationTypeSymbol.Name, sourceClassName: destinationTypeSymbol.Name,
destinationTypeProperties: destinationTypeSymbol.GetAllMembersOfType<IPropertySymbol>()); sourceTypeProperties: destinationTypeSymbol.GetAllMembersOfType<IPropertySymbol>());
} }
private static ITypeSymbol? GetDestinationTypeSymbol(ClassDeclarationSyntax classSyntax, SemanticModel model) private static ITypeSymbol? GetDestinationTypeSymbol(ClassDeclarationSyntax classSyntax, SemanticModel model)

View File

@ -1,20 +1,19 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
namespace MapTo.Models namespace MapTo.Models
{ {
public class MapModel public class MapModel
{ {
public MapModel(string? ns, string classModifiers, string className, IEnumerable<IPropertySymbol> properties, string destinationNamespace, string destinationClassName, IEnumerable<IPropertySymbol> destinationTypeProperties) public MapModel(string? ns, string classModifiers, string className, IEnumerable<IPropertySymbol> properties, string sourceNamespace, string sourceClassName, IEnumerable<IPropertySymbol> sourceTypeProperties)
{ {
Namespace = ns; Namespace = ns;
ClassModifiers = classModifiers; ClassModifiers = classModifiers;
ClassName = className; ClassName = className;
Properties = properties; Properties = properties;
DestinationNamespace = destinationNamespace; SourceNamespace = sourceNamespace;
DestinationClassName = destinationClassName; SourceClassName = sourceClassName;
DestinationTypeProperties = destinationTypeProperties; SourceTypeProperties = sourceTypeProperties;
} }
public string? Namespace { get; } public string? Namespace { get; }
@ -25,12 +24,10 @@ namespace MapTo.Models
public IEnumerable<IPropertySymbol> Properties { get; } public IEnumerable<IPropertySymbol> Properties { get; }
public string DestinationNamespace { get; } public string SourceNamespace { get; }
public string DestinationClassName { get; } public string SourceClassName { get; }
public IEnumerable<IPropertySymbol> DestinationTypeProperties { get; } public IEnumerable<IPropertySymbol> SourceTypeProperties { get; }
public bool IsEmpty => !Properties.Any() || !DestinationTypeProperties.Any();
} }
} }

View File

@ -79,9 +79,9 @@ namespace MapTo
{ {
builder.AppendLine("using System;"); builder.AppendLine("using System;");
if (!string.IsNullOrWhiteSpace(model.DestinationNamespace) && model.Namespace != model.DestinationNamespace) if (!string.IsNullOrWhiteSpace(model.SourceNamespace) && model.Namespace != model.SourceNamespace)
{ {
builder.AppendFormat("using {0};", model.DestinationNamespace).AppendLine(); builder.AppendFormat("using {0};", model.SourceNamespace).AppendLine();
} }
return builder.AppendLine(); return builder.AppendLine();
@ -89,18 +89,18 @@ namespace MapTo
private static StringBuilder GenerateConstructor(this StringBuilder builder, MapModel model, out List<IPropertySymbol> mappedProperties) private static StringBuilder GenerateConstructor(this StringBuilder builder, MapModel model, out List<IPropertySymbol> mappedProperties)
{ {
var destinationClassParameterName = model.DestinationClassName.ToCamelCase(); var destinationClassParameterName = model.SourceClassName.ToCamelCase();
builder builder
.PadLeft(Indent2) .PadLeft(Indent2)
.AppendFormat("public {0}({1} {2})", model.ClassName, model.DestinationClassName, destinationClassParameterName) .AppendFormat("public {0}({1} {2})", model.ClassName, model.SourceClassName, destinationClassParameterName)
.AppendOpeningBracket(Indent2) .AppendOpeningBracket(Indent2)
.PadLeft(Indent3) .PadLeft(Indent3)
.AppendFormat("if ({0} == null) throw new ArgumentNullException(nameof({0}));", destinationClassParameterName) .AppendFormat("if ({0} == null) throw new ArgumentNullException(nameof({0}));", destinationClassParameterName)
.AppendLine(); .AppendLine();
mappedProperties = new List<IPropertySymbol>(); mappedProperties = new List<IPropertySymbol>();
foreach (var propertySymbol in model.DestinationTypeProperties) foreach (var propertySymbol in model.SourceTypeProperties)
{ {
if (model.Properties.Any(p => p.Name == propertySymbol.Name)) if (model.Properties.Any(p => p.Name == propertySymbol.Name))
{ {
@ -117,12 +117,12 @@ namespace MapTo
private static StringBuilder GenerateFactoryMethod(this StringBuilder builder, MapModel model) private static StringBuilder GenerateFactoryMethod(this StringBuilder builder, MapModel model)
{ {
var destinationClassParameterName = model.DestinationClassName.ToCamelCase(); var destinationClassParameterName = model.SourceClassName.ToCamelCase();
return builder return builder
.AppendLine() .AppendLine()
.PadLeft(Indent2) .PadLeft(Indent2)
.AppendFormat("public static {0} From({1} {2})", model.ClassName, model.DestinationClassName, destinationClassParameterName) .AppendFormat("public static {0} From({1} {2})", model.ClassName, model.SourceClassName, destinationClassParameterName)
.AppendOpeningBracket(Indent2) .AppendOpeningBracket(Indent2)
.PadLeft(Indent3) .PadLeft(Indent3)
.AppendFormat("return {0} == null ? null : new {1}({0});", destinationClassParameterName, model.ClassName) .AppendFormat("return {0} == null ? null : new {1}({0});", destinationClassParameterName, model.ClassName)