Adapt data to new MapTo feature
This commit is contained in:
parent
3a2d3a1b41
commit
e41fd2d815
|
@ -1,3 +1,4 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using BlueWest.Data;
|
||||
using BlueWest.WebApi.MySQL;
|
||||
|
@ -6,6 +7,9 @@ using Microsoft.AspNetCore.Mvc;
|
|||
|
||||
namespace BlueWest.WebApi.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// Controller responsible to get country data
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class CountriesController : ControllerBase
|
||||
|
@ -25,7 +29,7 @@ public class CountriesController : ControllerBase
|
|||
/// <summary>
|
||||
/// Add Country
|
||||
/// </summary>
|
||||
/// <param name="country"></param>
|
||||
/// <param name="countryToCreate">The country data to create</param>
|
||||
/// <returns>The newly created country</returns>
|
||||
/// /// <summary>
|
||||
/// Creates a Country.
|
||||
|
@ -44,11 +48,11 @@ public class CountriesController : ControllerBase
|
|||
/// <response code="201">Returns the newly created country</response>
|
||||
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||
[HttpPost]
|
||||
public ActionResult AddCountry(Country country)
|
||||
public ActionResult AddCountry(CountryCreate countryToCreate)
|
||||
{
|
||||
_dbContext.Countries.Add(country);
|
||||
_dbContext.Countries.Add(new Country(countryToCreate, new List<Currency>()));
|
||||
_dbContext.SaveChanges();
|
||||
return CreatedAtRoute(nameof(GetCountryById), new {countryId = country.Code}, country);
|
||||
return CreatedAtRoute(nameof(GetCountryById), new {countryId = countryToCreate.Code}, countryToCreate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -7,6 +7,9 @@ using Microsoft.AspNetCore.Mvc;
|
|||
|
||||
namespace BlueWest.WebApi.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// The controller responsible to fetch currency data
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class CurrenciesController : ControllerBase
|
||||
|
@ -19,16 +22,27 @@ namespace BlueWest.WebApi.Controllers
|
|||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Add Currency to the table of currencies
|
||||
/// </summary>
|
||||
/// <param name="currencyToCreate">Currency data to create</param>
|
||||
/// <returns></returns>
|
||||
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||
[HttpPost]
|
||||
public ActionResult AddCurrency(CurrencyCreate currency)
|
||||
public ActionResult AddCurrency(CurrencyCreate currencyToCreate)
|
||||
{
|
||||
var newCurrency = new Currency();
|
||||
var newCurrency = new Currency(currencyToCreate, new List<Country>());
|
||||
_dbContext.Currencies.Add(newCurrency);
|
||||
_dbContext.SaveChanges();
|
||||
return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = currency.Code}, currency);
|
||||
return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = currencyToCreate.Code}, currencyToCreate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update a currency data from the Currency table in the database
|
||||
/// </summary>
|
||||
/// <param name="currencyNumber">The currency number we want to update</param>
|
||||
/// <param name="currencyToUpdate">Currency Data to update</param>
|
||||
/// <returns></returns>
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[HttpPut("{currencyNumber}")]
|
||||
|
@ -48,6 +62,11 @@ namespace BlueWest.WebApi.Controllers
|
|||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets a currency by the currency number (id)
|
||||
/// </summary>
|
||||
/// <param name="currencyId">The id of the currency to get</param>
|
||||
/// <returns></returns>
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[HttpGet("{currencyId}", Name = nameof(GetCurrencyById))]
|
||||
|
|
|
@ -4,12 +4,14 @@ using MapTo;
|
|||
|
||||
namespace BlueWest.Data
|
||||
{
|
||||
[MapFrom(typeof(CurrencyUpdate))]
|
||||
[MapFrom(new []{
|
||||
typeof(CurrencyUpdate),
|
||||
typeof(CurrencyCreate)})]
|
||||
|
||||
public partial class Currency
|
||||
{
|
||||
[MaxLength(3)]public int Num { get; set; } // Primary key
|
||||
[MaxLength(3)]public string Code { get; set; }
|
||||
[MaxLength(3)] public int Num { get; set; } // Primary key
|
||||
[MaxLength(3)] public string Code { get; set; }
|
||||
public List<Country> Countries { get; set; }
|
||||
|
||||
public Currency()
|
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using MapTo;
|
||||
|
||||
namespace BlueWest.Data
|
||||
|
@ -8,7 +9,7 @@ namespace BlueWest.Data
|
|||
public partial class CurrencyUpdate
|
||||
{
|
||||
// ISO 4217 Code
|
||||
public string Code { get; set; }
|
||||
[MaxLength(3)] public string Code { get; set; }
|
||||
|
||||
|
||||
}
|
|
@ -30,18 +30,22 @@ namespace MapTo.Extensions
|
|||
|
||||
internal static SourceBuilder WriteModelInfo(this SourceBuilder builder, MappingModel model)
|
||||
{
|
||||
var targetSourceType = model.MappedSourceTypes[0];
|
||||
return builder
|
||||
.WriteLine()
|
||||
.WriteComment($" IsTypeUpdatable {model.IsTypeUpdatable}")
|
||||
.WriteComment($" HasMappedBaseClass {model.HasMappedBaseClass.ToString()}")
|
||||
.WriteComment($" Namespace {model.Namespace}")
|
||||
.WriteComment($" Options {model.Options.ToString()}")
|
||||
.WriteComment($" Type {model.Type}")
|
||||
.WriteComment($" TypeIdentifierName {model.TypeIdentifierName}")
|
||||
.WriteComment($" SourceNamespace {targetSourceType.SourceNamespace}")
|
||||
.WriteComment($" SourceTypeFullName {targetSourceType.SourceTypeFullName}")
|
||||
.WriteComment($" SourceTypeIdentifierName {targetSourceType.SourceTypeIdentifierName}");
|
||||
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||
{
|
||||
builder
|
||||
.WriteLine()
|
||||
.WriteComment($" IsTypeUpdatable {model.IsTypeUpdatable}")
|
||||
.WriteComment($" HasMappedBaseClass {model.HasMappedBaseClass.ToString()}")
|
||||
.WriteComment($" Namespace {model.Namespace}")
|
||||
.WriteComment($" Options {model.Options.ToString()}")
|
||||
.WriteComment($" Type {model.Type}")
|
||||
.WriteComment($" TypeIdentifierName {model.TypeIdentifierName}")
|
||||
.WriteComment($" SourceNamespace {targetSourceType.SourceNamespace}")
|
||||
.WriteComment($" SourceTypeFullName {targetSourceType.SourceTypeFullName}")
|
||||
.WriteComment($" SourceTypeIdentifierName {targetSourceType.SourceTypeIdentifierName}");
|
||||
}
|
||||
|
||||
return builder;
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,7 +15,6 @@ namespace MapTo.Extensions
|
|||
{
|
||||
const bool writeDebugInfo = true;
|
||||
|
||||
var targetSourceType = model.MappedSourceTypes[0];
|
||||
|
||||
using var builder = new SourceBuilder()
|
||||
.WriteLine(GeneratedFilesHeader)
|
||||
|
@ -27,23 +26,30 @@ namespace MapTo.Extensions
|
|||
.WriteLine($"namespace {model.Namespace}")
|
||||
.WriteOpeningBracket();
|
||||
|
||||
if (writeDebugInfo)
|
||||
builder
|
||||
.WriteModelInfo(model)
|
||||
.WriteLine()
|
||||
.WriteComment("Type properties")
|
||||
.WriteComment()
|
||||
.WriteMappedProperties(targetSourceType.TypeProperties)
|
||||
.WriteLine()
|
||||
.WriteComment("Source properties")
|
||||
.WriteLine()
|
||||
.WriteComment("Type fields")
|
||||
.WriteComment()
|
||||
.WriteMappedProperties(targetSourceType.TypeFields)
|
||||
.WriteLine()
|
||||
.WriteComment("Source fields")
|
||||
.WriteMappedProperties(targetSourceType.SourceFields)
|
||||
.WriteLine();
|
||||
|
||||
|
||||
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||
{
|
||||
if (writeDebugInfo)
|
||||
builder
|
||||
.WriteModelInfo(model)
|
||||
.WriteLine()
|
||||
.WriteComment("Type properties")
|
||||
.WriteComment()
|
||||
.WriteMappedProperties(targetSourceType.TypeProperties)
|
||||
.WriteLine()
|
||||
.WriteComment("Source properties")
|
||||
.WriteLine()
|
||||
.WriteComment("Type fields")
|
||||
.WriteComment()
|
||||
.WriteMappedProperties(targetSourceType.TypeFields)
|
||||
.WriteLine()
|
||||
.WriteComment("Source fields")
|
||||
.WriteMappedProperties(targetSourceType.SourceFields)
|
||||
.WriteLine();
|
||||
|
||||
}
|
||||
|
||||
|
||||
builder
|
||||
// Class declaration
|
||||
|
@ -52,10 +58,14 @@ namespace MapTo.Extensions
|
|||
.WriteLine()
|
||||
// Class body
|
||||
.GeneratePublicConstructor(model);
|
||||
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||
{
|
||||
if (model.IsTypeUpdatable && targetSourceType.TypeProperties.GetWritableMappedProperties().Length > 0) builder.GenerateUpdateMethod(model);
|
||||
if (model.IsTypeUpdatable && targetSourceType.TypeFields.GetWritableMappedProperties().Length > 0) builder.GenerateUpdateMethod(model);
|
||||
}
|
||||
|
||||
if (model.IsJsonExtension) builder.WriteToJsonMethod(model);
|
||||
if (model.IsTypeUpdatable && targetSourceType.TypeProperties.GetWritableMappedProperties().Length > 0) builder.GenerateUpdateMethod(model);
|
||||
if (model.IsTypeUpdatable && targetSourceType.TypeFields.GetWritableMappedProperties().Length > 0) builder.GenerateUpdateMethod(model);
|
||||
|
||||
|
||||
builder
|
||||
.WriteLine()
|
||||
|
@ -70,46 +80,47 @@ namespace MapTo.Extensions
|
|||
|
||||
private static SourceBuilder GeneratePublicConstructor(this SourceBuilder builder, MappingModel model)
|
||||
{
|
||||
var targetSourceType = model.MappedSourceTypes[0];
|
||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||
const string mappingContextParameterName = "context";
|
||||
|
||||
var baseConstructor = /*model.HasMappedBaseClass ? $" : base({mappingContextParameterName}, {sourceClassParameterName})" :*/ string.Empty;
|
||||
|
||||
var stringBuilder = new StringBuilder();
|
||||
|
||||
var otherProperties = new List<MappedMember>();
|
||||
|
||||
foreach (var property in targetSourceType.TypeProperties)
|
||||
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||
{
|
||||
if (!targetSourceType.SourceProperties.IsMappedProperty(property))
|
||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||
var baseConstructor = /*model.HasMappedBaseClass ? $" : base({mappingContextParameterName}, {sourceClassParameterName})" :*/ string.Empty;
|
||||
var stringBuilder = new StringBuilder();
|
||||
var otherProperties = new List<MappedMember>();
|
||||
|
||||
foreach (var property in targetSourceType.TypeProperties)
|
||||
{
|
||||
stringBuilder.Append(", ");
|
||||
stringBuilder.Append($"{property.FullyQualifiedType} {property.SourcePropertyName.ToCamelCase()}");
|
||||
otherProperties.Add(property);
|
||||
}
|
||||
}
|
||||
if (!targetSourceType.SourceProperties.IsMappedProperty(property))
|
||||
{
|
||||
stringBuilder.Append(", ");
|
||||
stringBuilder.Append($"{property.FullyQualifiedType} {property.SourcePropertyName.ToCamelCase()}");
|
||||
otherProperties.Add(property);
|
||||
}
|
||||
|
||||
foreach (var property in targetSourceType.TypeFields)
|
||||
{
|
||||
if (!targetSourceType.SourceFields.IsMappedProperty(property))
|
||||
}
|
||||
foreach (var property in targetSourceType.TypeFields)
|
||||
{
|
||||
stringBuilder.Append(", ");
|
||||
stringBuilder.Append($"{property.FullyQualifiedType} {property.SourcePropertyName.ToCamelCase()}");
|
||||
otherProperties.Add(property);
|
||||
if (!targetSourceType.SourceFields.IsMappedProperty(property))
|
||||
{
|
||||
stringBuilder.Append(", ");
|
||||
stringBuilder.Append($"{property.FullyQualifiedType} {property.SourcePropertyName.ToCamelCase()}");
|
||||
otherProperties.Add(property);
|
||||
}
|
||||
}
|
||||
|
||||
var readOnlyPropertiesArguments = stringBuilder.ToString();
|
||||
|
||||
builder
|
||||
.WriteLine($"public {model.TypeIdentifierName}({targetSourceType.SourceType} {sourceClassParameterName}{readOnlyPropertiesArguments}){baseConstructor}")
|
||||
.WriteOpeningBracket()
|
||||
.WriteAssignmentMethod(model, otherProperties.ToArray().ToImmutableArray(), sourceClassParameterName, mappingContextParameterName, false);
|
||||
|
||||
builder.WriteClosingBracket();
|
||||
}
|
||||
|
||||
|
||||
var readOnlyPropertiesArguments = stringBuilder.ToString();
|
||||
|
||||
builder
|
||||
.WriteLine($"public {model.TypeIdentifierName}({targetSourceType.SourceType} {sourceClassParameterName}{readOnlyPropertiesArguments}){baseConstructor}")
|
||||
.WriteOpeningBracket()
|
||||
.WriteAssignmentMethod(model, otherProperties.ToArray().ToImmutableArray(), sourceClassParameterName, mappingContextParameterName, false);
|
||||
|
||||
// End constructor declaration
|
||||
return builder.WriteClosingBracket();
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static bool IsMappedProperty(this System.Collections.Immutable.ImmutableArray<MappedMember> properties, MappedMember property)
|
||||
|
@ -131,29 +142,33 @@ namespace MapTo.Extensions
|
|||
.WriteLine("var stringBuilder = new System.Text.StringBuilder();")
|
||||
.WriteLine(GetStringBuilderAppendNoInterpolation("{"));
|
||||
|
||||
var targetSourceType = model.MappedSourceTypes[0];
|
||||
foreach (var property in targetSourceType.TypeProperties)
|
||||
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||
{
|
||||
if (!property.isEnumerable)
|
||||
HandlePropertyEnumerable(builder, property);
|
||||
else
|
||||
foreach (var property in targetSourceType.TypeProperties)
|
||||
{
|
||||
builder = WriteJsonField(builder, property);
|
||||
if (!property.isEnumerable)
|
||||
HandlePropertyEnumerable(builder, property);
|
||||
else
|
||||
{
|
||||
builder = WriteJsonField(builder, property);
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (var property in targetSourceType.TypeFields)
|
||||
{
|
||||
if (!property.isEnumerable)
|
||||
HandleFieldEnumerable(builder, property);
|
||||
else
|
||||
foreach (var property in targetSourceType.TypeFields)
|
||||
{
|
||||
builder.WriteLine(GetStringBuilderAppend($"\\\"{property.Name.ToCamelCase()}\\\" : [{GetJsonArrayValue(property, ref builder)}],"));
|
||||
if (!property.isEnumerable)
|
||||
HandleFieldEnumerable(builder, property);
|
||||
else
|
||||
{
|
||||
builder.WriteLine(GetStringBuilderAppend($"\\\"{property.Name.ToCamelCase()}\\\" : [{GetJsonArrayValue(property, ref builder)}],"));
|
||||
}
|
||||
}
|
||||
|
||||
builder.WriteLine(GetStringBuilderAppendNoInterpolation("}"));
|
||||
builder.WriteLine("return stringBuilder.ToString();");
|
||||
builder.WriteClosingBracket();
|
||||
}
|
||||
|
||||
builder.WriteLine(GetStringBuilderAppendNoInterpolation("}"));
|
||||
builder.WriteLine("return stringBuilder.ToString();");
|
||||
builder.WriteClosingBracket();
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
|
@ -231,33 +246,48 @@ namespace MapTo.Extensions
|
|||
private static SourceBuilder WriteAssignmentMethod(this SourceBuilder builder, MappingModel model, System.Collections.Immutable.ImmutableArray<MappedMember>? otherProperties,
|
||||
string? sourceClassParameterName, string mappingContextParameterName, bool fromUpdate)
|
||||
{
|
||||
var targetSourceType = model.MappedSourceTypes[0];
|
||||
|
||||
foreach (var property in targetSourceType.SourceProperties)
|
||||
List<MappedMember> _addedMembers = new List<MappedMember>();
|
||||
|
||||
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||
{
|
||||
if (property.isReadOnly && fromUpdate) continue;
|
||||
foreach (var property in targetSourceType.SourceProperties)
|
||||
{
|
||||
if (property.isReadOnly && fromUpdate) continue;
|
||||
if(_addedMembers.Contains(property)) continue;
|
||||
|
||||
builder.WriteLine($"{property.Name} = {sourceClassParameterName}.{property.SourcePropertyName};");
|
||||
|
||||
builder.WriteLine($"{property.Name} = {sourceClassParameterName}.{property.SourcePropertyName};");
|
||||
_addedMembers.Add(property);
|
||||
|
||||
}
|
||||
|
||||
foreach (var property in targetSourceType.SourceFields)
|
||||
{
|
||||
if (property.isReadOnly && fromUpdate) continue;
|
||||
if(_addedMembers.Contains(property)) continue;
|
||||
|
||||
builder.WriteLine($"{property.Name} = {sourceClassParameterName}.{property.SourcePropertyName};");
|
||||
_addedMembers.Add(property);
|
||||
|
||||
}
|
||||
|
||||
if (otherProperties == null) return builder;
|
||||
|
||||
foreach (var property in otherProperties)
|
||||
{
|
||||
if(_addedMembers.Contains(property)) continue;
|
||||
|
||||
builder.WriteLine(property.MappedSourcePropertyTypeName is null
|
||||
? $"{property.Name} = {property.SourcePropertyName.ToCamelCase()};"
|
||||
: "");
|
||||
_addedMembers.Add(property);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var property in targetSourceType.SourceFields)
|
||||
{
|
||||
if (property.isReadOnly && fromUpdate) continue;
|
||||
|
||||
builder.WriteLine($"{property.Name} = {sourceClassParameterName}.{property.SourcePropertyName};");
|
||||
|
||||
}
|
||||
|
||||
if (otherProperties == null) return builder;
|
||||
|
||||
foreach (var property in otherProperties)
|
||||
{
|
||||
builder.WriteLine(property.MappedSourcePropertyTypeName is null
|
||||
? $"{property.Name} = {property.SourcePropertyName.ToCamelCase()};"
|
||||
: "");
|
||||
|
||||
}
|
||||
|
||||
return builder;
|
||||
|
||||
|
@ -266,15 +296,19 @@ namespace MapTo.Extensions
|
|||
|
||||
private static SourceBuilder GenerateUpdateMethod(this SourceBuilder builder, MappingModel model)
|
||||
{
|
||||
var targetSourceType = model.MappedSourceTypes[0];
|
||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||
|
||||
builder
|
||||
.GenerateUpdaterMethodsXmlDocs(model, sourceClassParameterName)
|
||||
.WriteLine($"public void Update({targetSourceType.SourceType} {sourceClassParameterName})")
|
||||
.WriteOpeningBracket()
|
||||
.WriteAssignmentMethod(model, null, sourceClassParameterName, "context", true)
|
||||
.WriteClosingBracket();
|
||||
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||
{
|
||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||
|
||||
builder
|
||||
.GenerateUpdaterMethodsXmlDocs(model, sourceClassParameterName)
|
||||
.WriteLine($"public void Update({targetSourceType.SourceType} {sourceClassParameterName})")
|
||||
.WriteOpeningBracket()
|
||||
.WriteAssignmentMethod(model, null, sourceClassParameterName, "context", true)
|
||||
.WriteClosingBracket();
|
||||
}
|
||||
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
@ -286,28 +320,37 @@ namespace MapTo.Extensions
|
|||
return builder;
|
||||
}
|
||||
|
||||
var targetSourceType = model.MappedSourceTypes[0];
|
||||
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||
{
|
||||
builder
|
||||
.WriteLine("/// <summary>")
|
||||
.WriteLine($"/// Updates <see cref=\"{model.TypeIdentifierName}\"/> and sets its participating properties")
|
||||
.WriteLine($"/// using the property values from <paramref name=\"{sourceClassParameterName}\"/>.")
|
||||
.WriteLine("/// </summary>")
|
||||
.WriteLine($"/// <param name=\"{sourceClassParameterName}\">The instance of <see cref=\"{targetSourceType.SourceType}\"/> to use as source.</param>");
|
||||
|
||||
return builder
|
||||
.WriteLine("/// <summary>")
|
||||
.WriteLine($"/// Updates <see cref=\"{model.TypeIdentifierName}\"/> and sets its participating properties")
|
||||
.WriteLine($"/// using the property values from <paramref name=\"{sourceClassParameterName}\"/>.")
|
||||
.WriteLine("/// </summary>")
|
||||
.WriteLine($"/// <param name=\"{sourceClassParameterName}\">The instance of <see cref=\"{targetSourceType.SourceType}\"/> to use as source.</param>");
|
||||
}
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static SourceBuilder GenerateEnumerableJsonSourceTypeExtensionMethod(this SourceBuilder builder, MappingModel model)
|
||||
{
|
||||
var targetSourceType = model.MappedSourceTypes[0];
|
||||
|
||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||
{
|
||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||
|
||||
return builder
|
||||
.WriteLineIf(model.Options.SupportNullableStaticAnalysis, $"[return: NotNullIfNotNull(\"{sourceClassParameterName}\")]")
|
||||
.WriteLine($"{model.Options.GeneratedMethodsAccessModifier.ToLowercaseString()} static string ToJson(this IEnumerable<{targetSourceType.SourceType}{model.Options.NullableReferenceSyntax}> {sourceClassParameterName}List)")
|
||||
.WriteOpeningBracket()
|
||||
.WriteLine($"return {sourceClassParameterName} == null ? null : new {model.TypeIdentifierName}({sourceClassParameterName});")
|
||||
.WriteClosingBracket();
|
||||
builder
|
||||
.WriteLineIf(model.Options.SupportNullableStaticAnalysis, $"[return: NotNullIfNotNull(\"{sourceClassParameterName}\")]")
|
||||
.WriteLine($"{model.Options.GeneratedMethodsAccessModifier.ToLowercaseString()} static string ToJson(this IEnumerable<{targetSourceType.SourceType}{model.Options.NullableReferenceSyntax}> {sourceClassParameterName}List)")
|
||||
.WriteOpeningBracket()
|
||||
.WriteLine($"return {sourceClassParameterName} == null ? null : new {model.TypeIdentifierName}({sourceClassParameterName});")
|
||||
.WriteClosingBracket();
|
||||
}
|
||||
|
||||
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,15 +22,18 @@ namespace MapTo.Sources
|
|||
.WriteLine($"partial record {model.TypeIdentifierName}")
|
||||
.WriteOpeningBracket();
|
||||
|
||||
var targetSourceType = model.MappedSourceTypes[0];
|
||||
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||
{
|
||||
if (targetSourceType.GenerateSecondaryConstructor)
|
||||
{
|
||||
builder
|
||||
.GenerateSecondaryConstructor(model)
|
||||
.WriteLine();
|
||||
}
|
||||
}
|
||||
|
||||
// Class body
|
||||
if (targetSourceType.GenerateSecondaryConstructor)
|
||||
{
|
||||
builder
|
||||
.GenerateSecondaryConstructor(model)
|
||||
.WriteLine();
|
||||
}
|
||||
|
||||
|
||||
builder
|
||||
.GeneratePrivateConstructor(model)
|
||||
|
@ -55,105 +58,118 @@ namespace MapTo.Sources
|
|||
private static SourceBuilder GenerateSecondaryConstructor(this SourceBuilder builder, MappingModel model)
|
||||
{
|
||||
// grab first data from array
|
||||
var targetSourceType = model.MappedSourceTypes[0];
|
||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||
|
||||
if (model.Options.GenerateXmlDocument)
|
||||
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||
{
|
||||
builder
|
||||
.WriteLine("/// <summary>")
|
||||
.WriteLine($"/// Initializes a new instance of the <see cref=\"{model.TypeIdentifierName}\"/> class")
|
||||
.WriteLine($"/// using the property values from the specified <paramref name=\"{sourceClassParameterName}\"/>.")
|
||||
.WriteLine("/// </summary>")
|
||||
.WriteLine($"/// <exception cref=\"ArgumentNullException\">{sourceClassParameterName} is null</exception>");
|
||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||
if (model.Options.GenerateXmlDocument)
|
||||
{
|
||||
builder
|
||||
.WriteLine("/// <summary>")
|
||||
.WriteLine($"/// Initializes a new instance of the <see cref=\"{model.TypeIdentifierName}\"/> class")
|
||||
.WriteLine($"/// using the property values from the specified <paramref name=\"{sourceClassParameterName}\"/>.")
|
||||
.WriteLine("/// </summary>")
|
||||
.WriteLine($"/// <exception cref=\"ArgumentNullException\">{sourceClassParameterName} is null</exception>");
|
||||
}
|
||||
builder .WriteLine($"{model.Options.ConstructorAccessModifier.ToLowercaseString()} {model.TypeIdentifierName}({targetSourceType.SourceType} {sourceClassParameterName})")
|
||||
.WriteLine($" : this(new {MappingContextSource.ClassName}(), {sourceClassParameterName}) {{ }}");
|
||||
}
|
||||
|
||||
return builder
|
||||
.WriteLine($"{model.Options.ConstructorAccessModifier.ToLowercaseString()} {model.TypeIdentifierName}({targetSourceType.SourceType} {sourceClassParameterName})")
|
||||
.WriteLine($" : this(new {MappingContextSource.ClassName}(), {sourceClassParameterName}) {{ }}");
|
||||
|
||||
|
||||
return builder;
|
||||
|
||||
}
|
||||
|
||||
private static SourceBuilder GeneratePrivateConstructor(this SourceBuilder builder, MappingModel model)
|
||||
{
|
||||
var targetSourceType = model.MappedSourceTypes[0];
|
||||
|
||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||
const string mappingContextParameterName = "context";
|
||||
|
||||
builder
|
||||
.WriteLine($"private protected {model.TypeIdentifierName}({MappingContextSource.ClassName} {mappingContextParameterName}, {targetSourceType.SourceType} {sourceClassParameterName})")
|
||||
.Indent()
|
||||
.Write(": this(").
|
||||
|
||||
WriteProperties(model, sourceClassParameterName, mappingContextParameterName)
|
||||
|
||||
.WriteLine(")")
|
||||
.Unindent()
|
||||
.WriteOpeningBracket()
|
||||
.WriteLine($"if ({mappingContextParameterName} == null) throw new ArgumentNullException(nameof({mappingContextParameterName}));")
|
||||
.WriteLine($"if ({sourceClassParameterName} == null) throw new ArgumentNullException(nameof({sourceClassParameterName}));")
|
||||
.WriteLine()
|
||||
.WriteLine($"{mappingContextParameterName}.{MappingContextSource.RegisterMethodName}({sourceClassParameterName}, this);");
|
||||
|
||||
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||
{
|
||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||
builder
|
||||
.WriteLine(
|
||||
$"private protected {model.TypeIdentifierName}({MappingContextSource.ClassName} {mappingContextParameterName}, {targetSourceType.SourceType} {sourceClassParameterName})")
|
||||
.Indent()
|
||||
.Write(": this(").WriteProperties(model, sourceClassParameterName, mappingContextParameterName)
|
||||
.WriteLine(")")
|
||||
.Unindent()
|
||||
.WriteOpeningBracket()
|
||||
.WriteLine($"if ({mappingContextParameterName} == null) throw new ArgumentNullException(nameof({mappingContextParameterName}));")
|
||||
.WriteLine($"if ({sourceClassParameterName} == null) throw new ArgumentNullException(nameof({sourceClassParameterName}));")
|
||||
.WriteLine()
|
||||
.WriteLine($"{mappingContextParameterName}.{MappingContextSource.RegisterMethodName}({sourceClassParameterName}, this);")
|
||||
.WriteClosingBracket();
|
||||
}
|
||||
// End constructor declaration
|
||||
return builder.WriteClosingBracket();
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static SourceBuilder WriteProperties(this SourceBuilder builder, MappingModel model, string sourceClassParameterName,
|
||||
string mappingContextParameterName)
|
||||
{
|
||||
var targetSourceType = model.MappedSourceTypes[0];
|
||||
|
||||
for (var i = 0; i < targetSourceType.SourceProperties.Length; i++)
|
||||
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||
{
|
||||
var property = targetSourceType.SourceProperties[i];
|
||||
if (property.TypeConverter is null)
|
||||
for (var i = 0; i < targetSourceType.SourceProperties.Length; i++)
|
||||
{
|
||||
if (property.IsEnumerable)
|
||||
var property = targetSourceType.SourceProperties[i];
|
||||
if (property.TypeConverter is null)
|
||||
{
|
||||
builder.Write(
|
||||
$"{property.Name}: {sourceClassParameterName}.{property.SourcePropertyName}.Select({mappingContextParameterName}.{MappingContextSource.MapMethodName}<{property.MappedSourcePropertyTypeName}, {property.EnumerableTypeArgument}>).ToList()");
|
||||
if (property.IsEnumerable)
|
||||
{
|
||||
builder.Write(
|
||||
$"{property.Name}: {sourceClassParameterName}.{property.SourcePropertyName}.Select({mappingContextParameterName}.{MappingContextSource.MapMethodName}<{property.MappedSourcePropertyTypeName}, {property.EnumerableTypeArgument}>).ToList()");
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Write(property.MappedSourcePropertyTypeName is null
|
||||
? $"{property.Name}: {sourceClassParameterName}.{property.SourcePropertyName}"
|
||||
: $"{property.Name}: {mappingContextParameterName}.{MappingContextSource.MapMethodName}<{property.MappedSourcePropertyTypeName}, {property.Type}>({sourceClassParameterName}.{property.SourcePropertyName})");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Write(property.MappedSourcePropertyTypeName is null
|
||||
? $"{property.Name}: {sourceClassParameterName}.{property.SourcePropertyName}"
|
||||
: $"{property.Name}: {mappingContextParameterName}.{MappingContextSource.MapMethodName}<{property.MappedSourcePropertyTypeName}, {property.Type}>({sourceClassParameterName}.{property.SourcePropertyName})");
|
||||
var parameters = property.TypeConverterParameters.IsEmpty
|
||||
? "null"
|
||||
: $"new object[] {{ {string.Join(", ", property.TypeConverterParameters)} }}";
|
||||
|
||||
builder.Write(
|
||||
$"{property.Name}: new {property.TypeConverter}().Convert({sourceClassParameterName}.{property.SourcePropertyName}, {parameters})");
|
||||
}
|
||||
|
||||
if (i < targetSourceType.SourceProperties.Length - 1)
|
||||
{
|
||||
builder.Write(", ");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var parameters = property.TypeConverterParameters.IsEmpty
|
||||
? "null"
|
||||
: $"new object[] {{ {string.Join(", ", property.TypeConverterParameters)} }}";
|
||||
|
||||
builder.Write(
|
||||
$"{property.Name}: new {property.TypeConverter}().Convert({sourceClassParameterName}.{property.SourcePropertyName}, {parameters})");
|
||||
}
|
||||
|
||||
if (i < targetSourceType.SourceProperties.Length - 1)
|
||||
{
|
||||
builder.Write(", ");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static SourceBuilder GenerateFactoryMethod(this SourceBuilder builder, MappingModel model)
|
||||
{
|
||||
var targetSourceType = model.MappedSourceTypes[0];
|
||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||
{
|
||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||
|
||||
return builder
|
||||
.GenerateConvertorMethodsXmlDocs(model, sourceClassParameterName)
|
||||
.WriteLineIf(model.Options.SupportNullableStaticAnalysis, $"[return: NotNullIfNotNull(\"{sourceClassParameterName}\")]")
|
||||
.WriteLine(
|
||||
$"{model.Options.GeneratedMethodsAccessModifier.ToLowercaseString()} static {model.TypeIdentifierName}{model.Options.NullableReferenceSyntax} From({targetSourceType.SourceType}{model.Options.NullableReferenceSyntax} {sourceClassParameterName})")
|
||||
.WriteOpeningBracket()
|
||||
.WriteLine(
|
||||
$"return {sourceClassParameterName} == null ? null : {MappingContextSource.ClassName}.{MappingContextSource.FactoryMethodName}<{targetSourceType.SourceType}, {model.TypeIdentifierName}>({sourceClassParameterName});")
|
||||
.WriteClosingBracket();
|
||||
builder
|
||||
.GenerateConvertorMethodsXmlDocs(model, sourceClassParameterName)
|
||||
.WriteLineIf(model.Options.SupportNullableStaticAnalysis, $"[return: NotNullIfNotNull(\"{sourceClassParameterName}\")]")
|
||||
.WriteLine(
|
||||
$"{model.Options.GeneratedMethodsAccessModifier.ToLowercaseString()} static {model.TypeIdentifierName}{model.Options.NullableReferenceSyntax} From({targetSourceType.SourceType}{model.Options.NullableReferenceSyntax} {sourceClassParameterName})")
|
||||
.WriteOpeningBracket()
|
||||
.WriteLine(
|
||||
$"return {sourceClassParameterName} == null ? null : {MappingContextSource.ClassName}.{MappingContextSource.FactoryMethodName}<{targetSourceType.SourceType}, {model.TypeIdentifierName}>({sourceClassParameterName});")
|
||||
.WriteClosingBracket();
|
||||
}
|
||||
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static SourceBuilder GenerateConvertorMethodsXmlDocs(this SourceBuilder builder, MappingModel model, string sourceClassParameterName)
|
||||
|
@ -162,44 +178,58 @@ namespace MapTo.Sources
|
|||
{
|
||||
return builder;
|
||||
}
|
||||
var targetSourceType = model.MappedSourceTypes[0];
|
||||
|
||||
return builder
|
||||
.WriteLine("/// <summary>")
|
||||
.WriteLine($"/// Creates a new instance of <see cref=\"{model.TypeIdentifierName}\"/> and sets its participating properties")
|
||||
.WriteLine($"/// using the property values from <paramref name=\"{sourceClassParameterName}\"/>.")
|
||||
.WriteLine("/// </summary>")
|
||||
.WriteLine($"/// <param name=\"{sourceClassParameterName}\">The instance of <see cref=\"{targetSourceType.SourceType}\"/> to use as source.</param>")
|
||||
.WriteLine(
|
||||
$"/// <returns>A new instance of <see cred=\"{model.TypeIdentifierName}\"/> -or- <c>null</c> if <paramref name=\"{sourceClassParameterName}\"/> is <c>null</c>.</returns>");
|
||||
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||
{
|
||||
builder
|
||||
.WriteLine("/// <summary>")
|
||||
.WriteLine($"/// Creates a new instance of <see cref=\"{model.TypeIdentifierName}\"/> and sets its participating properties")
|
||||
.WriteLine($"/// using the property values from <paramref name=\"{sourceClassParameterName}\"/>.")
|
||||
.WriteLine("/// </summary>")
|
||||
.WriteLine($"/// <param name=\"{sourceClassParameterName}\">The instance of <see cref=\"{targetSourceType.SourceType}\"/> to use as source.</param>")
|
||||
.WriteLine(
|
||||
$"/// <returns>A new instance of <see cred=\"{model.TypeIdentifierName}\"/> -or- <c>null</c> if <paramref name=\"{sourceClassParameterName}\"/> is <c>null</c>.</returns>");
|
||||
}
|
||||
|
||||
return builder;
|
||||
|
||||
}
|
||||
|
||||
private static SourceBuilder GenerateSourceTypeExtensionClass(this SourceBuilder builder, MappingModel model)
|
||||
{
|
||||
var targetSourceType = model.MappedSourceTypes[0];
|
||||
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||
{
|
||||
builder
|
||||
.WriteLine(
|
||||
$"{model.Options.GeneratedMethodsAccessModifier.ToLowercaseString()} static partial class {targetSourceType.SourceTypeIdentifierName}To{model.TypeIdentifierName}Extensions")
|
||||
.WriteOpeningBracket()
|
||||
.GenerateSourceTypeExtensionMethod(model)
|
||||
.WriteClosingBracket();
|
||||
}
|
||||
|
||||
return builder
|
||||
.WriteLine(
|
||||
$"{model.Options.GeneratedMethodsAccessModifier.ToLowercaseString()} static partial class {targetSourceType.SourceTypeIdentifierName}To{model.TypeIdentifierName}Extensions")
|
||||
.WriteOpeningBracket()
|
||||
.GenerateSourceTypeExtensionMethod(model)
|
||||
.WriteClosingBracket();
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static SourceBuilder GenerateSourceTypeExtensionMethod(this SourceBuilder builder, MappingModel model)
|
||||
{
|
||||
var targetSourceType = model.MappedSourceTypes[0];
|
||||
|
||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||
{
|
||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||
|
||||
return builder
|
||||
.GenerateConvertorMethodsXmlDocs(model, sourceClassParameterName)
|
||||
.WriteLineIf(model.Options.SupportNullableStaticAnalysis, $"[return: NotNullIfNotNull(\"{sourceClassParameterName}\")]")
|
||||
.WriteLine(
|
||||
$"{model.Options.GeneratedMethodsAccessModifier.ToLowercaseString()} static {model.TypeIdentifierName}{model.Options.NullableReferenceSyntax} To{model.TypeIdentifierName}(this {targetSourceType.SourceType}{model.Options.NullableReferenceSyntax} {sourceClassParameterName})")
|
||||
.WriteOpeningBracket()
|
||||
.WriteLine($"return {sourceClassParameterName} == null ? null : new {model.TypeIdentifierName}({sourceClassParameterName});")
|
||||
.WriteClosingBracket();
|
||||
builder
|
||||
.GenerateConvertorMethodsXmlDocs(model, sourceClassParameterName)
|
||||
.WriteLineIf(model.Options.SupportNullableStaticAnalysis, $"[return: NotNullIfNotNull(\"{sourceClassParameterName}\")]")
|
||||
.WriteLine(
|
||||
$"{model.Options.GeneratedMethodsAccessModifier.ToLowercaseString()} static {model.TypeIdentifierName}{model.Options.NullableReferenceSyntax} To{model.TypeIdentifierName}(this {targetSourceType.SourceType}{model.Options.NullableReferenceSyntax} {sourceClassParameterName})")
|
||||
.WriteOpeningBracket()
|
||||
.WriteLine($"return {sourceClassParameterName} == null ? null : new {model.TypeIdentifierName}({sourceClassParameterName});")
|
||||
.WriteClosingBracket();
|
||||
|
||||
}
|
||||
|
||||
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue