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(),
className: classSyntax.GetClassName(),
properties: classSymbol.GetAllMembersOfType<IPropertySymbol>(),
destinationNamespace: destinationTypeSymbol.ContainingNamespace.Name,
destinationClassName: destinationTypeSymbol.Name,
destinationTypeProperties: destinationTypeSymbol.GetAllMembersOfType<IPropertySymbol>());
sourceNamespace: destinationTypeSymbol.ContainingNamespace.Name,
sourceClassName: destinationTypeSymbol.Name,
sourceTypeProperties: destinationTypeSymbol.GetAllMembersOfType<IPropertySymbol>());
}
private static ITypeSymbol? GetDestinationTypeSymbol(ClassDeclarationSyntax classSyntax, SemanticModel model)

View File

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

View File

@ -79,9 +79,9 @@ namespace MapTo
{
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();
@ -89,18 +89,18 @@ namespace MapTo
private static StringBuilder GenerateConstructor(this StringBuilder builder, MapModel model, out List<IPropertySymbol> mappedProperties)
{
var destinationClassParameterName = model.DestinationClassName.ToCamelCase();
var destinationClassParameterName = model.SourceClassName.ToCamelCase();
builder
.PadLeft(Indent2)
.AppendFormat("public {0}({1} {2})", model.ClassName, model.DestinationClassName, destinationClassParameterName)
.AppendFormat("public {0}({1} {2})", model.ClassName, model.SourceClassName, destinationClassParameterName)
.AppendOpeningBracket(Indent2)
.PadLeft(Indent3)
.AppendFormat("if ({0} == null) throw new ArgumentNullException(nameof({0}));", destinationClassParameterName)
.AppendLine();
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))
{
@ -117,12 +117,12 @@ namespace MapTo
private static StringBuilder GenerateFactoryMethod(this StringBuilder builder, MapModel model)
{
var destinationClassParameterName = model.DestinationClassName.ToCamelCase();
var destinationClassParameterName = model.SourceClassName.ToCamelCase();
return builder
.AppendLine()
.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)
.PadLeft(Indent3)
.AppendFormat("return {0} == null ? null : new {1}({0});", destinationClassParameterName, model.ClassName)