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 System.Linq;
|
||||||
using BlueWest.Data;
|
using BlueWest.Data;
|
||||||
using BlueWest.WebApi.MySQL;
|
using BlueWest.WebApi.MySQL;
|
||||||
|
@ -6,6 +7,9 @@ using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace BlueWest.WebApi.Controllers
|
namespace BlueWest.WebApi.Controllers
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Controller responsible to get country data
|
||||||
|
/// </summary>
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("[controller]")]
|
[Route("[controller]")]
|
||||||
public class CountriesController : ControllerBase
|
public class CountriesController : ControllerBase
|
||||||
|
@ -25,7 +29,7 @@ public class CountriesController : ControllerBase
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Add Country
|
/// Add Country
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="country"></param>
|
/// <param name="countryToCreate">The country data to create</param>
|
||||||
/// <returns>The newly created country</returns>
|
/// <returns>The newly created country</returns>
|
||||||
/// /// <summary>
|
/// /// <summary>
|
||||||
/// Creates a Country.
|
/// Creates a Country.
|
||||||
|
@ -44,11 +48,11 @@ public class CountriesController : ControllerBase
|
||||||
/// <response code="201">Returns the newly created country</response>
|
/// <response code="201">Returns the newly created country</response>
|
||||||
[ProducesResponseType(StatusCodes.Status201Created)]
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||||
[HttpPost]
|
[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();
|
_dbContext.SaveChanges();
|
||||||
return CreatedAtRoute(nameof(GetCountryById), new {countryId = country.Code}, country);
|
return CreatedAtRoute(nameof(GetCountryById), new {countryId = countryToCreate.Code}, countryToCreate);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -7,6 +7,9 @@ using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace BlueWest.WebApi.Controllers
|
namespace BlueWest.WebApi.Controllers
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The controller responsible to fetch currency data
|
||||||
|
/// </summary>
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("[controller]")]
|
[Route("[controller]")]
|
||||||
public class CurrenciesController : ControllerBase
|
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)]
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||||
[HttpPost]
|
[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.Currencies.Add(newCurrency);
|
||||||
_dbContext.SaveChanges();
|
_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.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
[HttpPut("{currencyNumber}")]
|
[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.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
[HttpGet("{currencyId}", Name = nameof(GetCurrencyById))]
|
[HttpGet("{currencyId}", Name = nameof(GetCurrencyById))]
|
||||||
|
|
|
@ -4,7 +4,9 @@ using MapTo;
|
||||||
|
|
||||||
namespace BlueWest.Data
|
namespace BlueWest.Data
|
||||||
{
|
{
|
||||||
[MapFrom(typeof(CurrencyUpdate))]
|
[MapFrom(new []{
|
||||||
|
typeof(CurrencyUpdate),
|
||||||
|
typeof(CurrencyCreate)})]
|
||||||
|
|
||||||
public partial class Currency
|
public partial class Currency
|
||||||
{
|
{
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
using MapTo;
|
using MapTo;
|
||||||
|
|
||||||
namespace BlueWest.Data
|
namespace BlueWest.Data
|
||||||
|
@ -8,7 +9,7 @@ namespace BlueWest.Data
|
||||||
public partial class CurrencyUpdate
|
public partial class CurrencyUpdate
|
||||||
{
|
{
|
||||||
// ISO 4217 Code
|
// ISO 4217 Code
|
||||||
public string Code { get; set; }
|
[MaxLength(3)] public string Code { get; set; }
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -30,8 +30,9 @@ namespace MapTo.Extensions
|
||||||
|
|
||||||
internal static SourceBuilder WriteModelInfo(this SourceBuilder builder, MappingModel model)
|
internal static SourceBuilder WriteModelInfo(this SourceBuilder builder, MappingModel model)
|
||||||
{
|
{
|
||||||
var targetSourceType = model.MappedSourceTypes[0];
|
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||||
return builder
|
{
|
||||||
|
builder
|
||||||
.WriteLine()
|
.WriteLine()
|
||||||
.WriteComment($" IsTypeUpdatable {model.IsTypeUpdatable}")
|
.WriteComment($" IsTypeUpdatable {model.IsTypeUpdatable}")
|
||||||
.WriteComment($" HasMappedBaseClass {model.HasMappedBaseClass.ToString()}")
|
.WriteComment($" HasMappedBaseClass {model.HasMappedBaseClass.ToString()}")
|
||||||
|
@ -42,6 +43,9 @@ namespace MapTo.Extensions
|
||||||
.WriteComment($" SourceNamespace {targetSourceType.SourceNamespace}")
|
.WriteComment($" SourceNamespace {targetSourceType.SourceNamespace}")
|
||||||
.WriteComment($" SourceTypeFullName {targetSourceType.SourceTypeFullName}")
|
.WriteComment($" SourceTypeFullName {targetSourceType.SourceTypeFullName}")
|
||||||
.WriteComment($" SourceTypeIdentifierName {targetSourceType.SourceTypeIdentifierName}");
|
.WriteComment($" SourceTypeIdentifierName {targetSourceType.SourceTypeIdentifierName}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,6 @@ namespace MapTo.Extensions
|
||||||
{
|
{
|
||||||
const bool writeDebugInfo = true;
|
const bool writeDebugInfo = true;
|
||||||
|
|
||||||
var targetSourceType = model.MappedSourceTypes[0];
|
|
||||||
|
|
||||||
using var builder = new SourceBuilder()
|
using var builder = new SourceBuilder()
|
||||||
.WriteLine(GeneratedFilesHeader)
|
.WriteLine(GeneratedFilesHeader)
|
||||||
|
@ -27,6 +26,10 @@ namespace MapTo.Extensions
|
||||||
.WriteLine($"namespace {model.Namespace}")
|
.WriteLine($"namespace {model.Namespace}")
|
||||||
.WriteOpeningBracket();
|
.WriteOpeningBracket();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||||
|
{
|
||||||
if (writeDebugInfo)
|
if (writeDebugInfo)
|
||||||
builder
|
builder
|
||||||
.WriteModelInfo(model)
|
.WriteModelInfo(model)
|
||||||
|
@ -45,6 +48,9 @@ namespace MapTo.Extensions
|
||||||
.WriteMappedProperties(targetSourceType.SourceFields)
|
.WriteMappedProperties(targetSourceType.SourceFields)
|
||||||
.WriteLine();
|
.WriteLine();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
builder
|
builder
|
||||||
// Class declaration
|
// Class declaration
|
||||||
.WriteLine($"partial {structOrClass} {model.TypeIdentifierName}")
|
.WriteLine($"partial {structOrClass} {model.TypeIdentifierName}")
|
||||||
|
@ -52,10 +58,14 @@ namespace MapTo.Extensions
|
||||||
.WriteLine()
|
.WriteLine()
|
||||||
// Class body
|
// Class body
|
||||||
.GeneratePublicConstructor(model);
|
.GeneratePublicConstructor(model);
|
||||||
|
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||||
if (model.IsJsonExtension) builder.WriteToJsonMethod(model);
|
{
|
||||||
if (model.IsTypeUpdatable && targetSourceType.TypeProperties.GetWritableMappedProperties().Length > 0) builder.GenerateUpdateMethod(model);
|
if (model.IsTypeUpdatable && targetSourceType.TypeProperties.GetWritableMappedProperties().Length > 0) builder.GenerateUpdateMethod(model);
|
||||||
if (model.IsTypeUpdatable && targetSourceType.TypeFields.GetWritableMappedProperties().Length > 0) builder.GenerateUpdateMethod(model);
|
if (model.IsTypeUpdatable && targetSourceType.TypeFields.GetWritableMappedProperties().Length > 0) builder.GenerateUpdateMethod(model);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (model.IsJsonExtension) builder.WriteToJsonMethod(model);
|
||||||
|
|
||||||
|
|
||||||
builder
|
builder
|
||||||
.WriteLine()
|
.WriteLine()
|
||||||
|
@ -70,14 +80,13 @@ namespace MapTo.Extensions
|
||||||
|
|
||||||
private static SourceBuilder GeneratePublicConstructor(this SourceBuilder builder, MappingModel model)
|
private static SourceBuilder GeneratePublicConstructor(this SourceBuilder builder, MappingModel model)
|
||||||
{
|
{
|
||||||
var targetSourceType = model.MappedSourceTypes[0];
|
|
||||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
|
||||||
const string mappingContextParameterName = "context";
|
const string mappingContextParameterName = "context";
|
||||||
|
|
||||||
|
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||||
|
{
|
||||||
|
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||||
var baseConstructor = /*model.HasMappedBaseClass ? $" : base({mappingContextParameterName}, {sourceClassParameterName})" :*/ string.Empty;
|
var baseConstructor = /*model.HasMappedBaseClass ? $" : base({mappingContextParameterName}, {sourceClassParameterName})" :*/ string.Empty;
|
||||||
|
|
||||||
var stringBuilder = new StringBuilder();
|
var stringBuilder = new StringBuilder();
|
||||||
|
|
||||||
var otherProperties = new List<MappedMember>();
|
var otherProperties = new List<MappedMember>();
|
||||||
|
|
||||||
foreach (var property in targetSourceType.TypeProperties)
|
foreach (var property in targetSourceType.TypeProperties)
|
||||||
|
@ -88,8 +97,8 @@ namespace MapTo.Extensions
|
||||||
stringBuilder.Append($"{property.FullyQualifiedType} {property.SourcePropertyName.ToCamelCase()}");
|
stringBuilder.Append($"{property.FullyQualifiedType} {property.SourcePropertyName.ToCamelCase()}");
|
||||||
otherProperties.Add(property);
|
otherProperties.Add(property);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
foreach (var property in targetSourceType.TypeFields)
|
foreach (var property in targetSourceType.TypeFields)
|
||||||
{
|
{
|
||||||
if (!targetSourceType.SourceFields.IsMappedProperty(property))
|
if (!targetSourceType.SourceFields.IsMappedProperty(property))
|
||||||
|
@ -100,7 +109,6 @@ namespace MapTo.Extensions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var readOnlyPropertiesArguments = stringBuilder.ToString();
|
var readOnlyPropertiesArguments = stringBuilder.ToString();
|
||||||
|
|
||||||
builder
|
builder
|
||||||
|
@ -108,8 +116,11 @@ namespace MapTo.Extensions
|
||||||
.WriteOpeningBracket()
|
.WriteOpeningBracket()
|
||||||
.WriteAssignmentMethod(model, otherProperties.ToArray().ToImmutableArray(), sourceClassParameterName, mappingContextParameterName, false);
|
.WriteAssignmentMethod(model, otherProperties.ToArray().ToImmutableArray(), sourceClassParameterName, mappingContextParameterName, false);
|
||||||
|
|
||||||
|
builder.WriteClosingBracket();
|
||||||
|
}
|
||||||
|
|
||||||
// End constructor declaration
|
// End constructor declaration
|
||||||
return builder.WriteClosingBracket();
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool IsMappedProperty(this System.Collections.Immutable.ImmutableArray<MappedMember> properties, MappedMember property)
|
private static bool IsMappedProperty(this System.Collections.Immutable.ImmutableArray<MappedMember> properties, MappedMember property)
|
||||||
|
@ -131,7 +142,8 @@ namespace MapTo.Extensions
|
||||||
.WriteLine("var stringBuilder = new System.Text.StringBuilder();")
|
.WriteLine("var stringBuilder = new System.Text.StringBuilder();")
|
||||||
.WriteLine(GetStringBuilderAppendNoInterpolation("{"));
|
.WriteLine(GetStringBuilderAppendNoInterpolation("{"));
|
||||||
|
|
||||||
var targetSourceType = model.MappedSourceTypes[0];
|
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||||
|
{
|
||||||
foreach (var property in targetSourceType.TypeProperties)
|
foreach (var property in targetSourceType.TypeProperties)
|
||||||
{
|
{
|
||||||
if (!property.isEnumerable)
|
if (!property.isEnumerable)
|
||||||
|
@ -154,6 +166,9 @@ namespace MapTo.Extensions
|
||||||
builder.WriteLine(GetStringBuilderAppendNoInterpolation("}"));
|
builder.WriteLine(GetStringBuilderAppendNoInterpolation("}"));
|
||||||
builder.WriteLine("return stringBuilder.ToString();");
|
builder.WriteLine("return stringBuilder.ToString();");
|
||||||
builder.WriteClosingBracket();
|
builder.WriteClosingBracket();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -231,21 +246,29 @@ namespace MapTo.Extensions
|
||||||
private static SourceBuilder WriteAssignmentMethod(this SourceBuilder builder, MappingModel model, System.Collections.Immutable.ImmutableArray<MappedMember>? otherProperties,
|
private static SourceBuilder WriteAssignmentMethod(this SourceBuilder builder, MappingModel model, System.Collections.Immutable.ImmutableArray<MappedMember>? otherProperties,
|
||||||
string? sourceClassParameterName, string mappingContextParameterName, bool fromUpdate)
|
string? sourceClassParameterName, string mappingContextParameterName, bool fromUpdate)
|
||||||
{
|
{
|
||||||
var targetSourceType = model.MappedSourceTypes[0];
|
|
||||||
|
|
||||||
|
List<MappedMember> _addedMembers = new List<MappedMember>();
|
||||||
|
|
||||||
|
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||||
|
{
|
||||||
foreach (var property in targetSourceType.SourceProperties)
|
foreach (var property in targetSourceType.SourceProperties)
|
||||||
{
|
{
|
||||||
if (property.isReadOnly && fromUpdate) continue;
|
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)
|
foreach (var property in targetSourceType.SourceFields)
|
||||||
{
|
{
|
||||||
if (property.isReadOnly && fromUpdate) continue;
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -253,11 +276,18 @@ namespace MapTo.Extensions
|
||||||
|
|
||||||
foreach (var property in otherProperties)
|
foreach (var property in otherProperties)
|
||||||
{
|
{
|
||||||
|
if(_addedMembers.Contains(property)) continue;
|
||||||
|
|
||||||
builder.WriteLine(property.MappedSourcePropertyTypeName is null
|
builder.WriteLine(property.MappedSourcePropertyTypeName is null
|
||||||
? $"{property.Name} = {property.SourcePropertyName.ToCamelCase()};"
|
? $"{property.Name} = {property.SourcePropertyName.ToCamelCase()};"
|
||||||
: "");
|
: "");
|
||||||
|
_addedMembers.Add(property);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
|
|
||||||
|
@ -266,7 +296,9 @@ namespace MapTo.Extensions
|
||||||
|
|
||||||
private static SourceBuilder GenerateUpdateMethod(this SourceBuilder builder, MappingModel model)
|
private static SourceBuilder GenerateUpdateMethod(this SourceBuilder builder, MappingModel model)
|
||||||
{
|
{
|
||||||
var targetSourceType = model.MappedSourceTypes[0];
|
|
||||||
|
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||||
|
{
|
||||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||||
|
|
||||||
builder
|
builder
|
||||||
|
@ -275,6 +307,8 @@ namespace MapTo.Extensions
|
||||||
.WriteOpeningBracket()
|
.WriteOpeningBracket()
|
||||||
.WriteAssignmentMethod(model, null, sourceClassParameterName, "context", true)
|
.WriteAssignmentMethod(model, null, sourceClassParameterName, "context", true)
|
||||||
.WriteClosingBracket();
|
.WriteClosingBracket();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
@ -286,28 +320,37 @@ namespace MapTo.Extensions
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
var targetSourceType = model.MappedSourceTypes[0];
|
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||||
|
{
|
||||||
return builder
|
builder
|
||||||
.WriteLine("/// <summary>")
|
.WriteLine("/// <summary>")
|
||||||
.WriteLine($"/// Updates <see cref=\"{model.TypeIdentifierName}\"/> and sets its participating properties")
|
.WriteLine($"/// Updates <see cref=\"{model.TypeIdentifierName}\"/> and sets its participating properties")
|
||||||
.WriteLine($"/// using the property values from <paramref name=\"{sourceClassParameterName}\"/>.")
|
.WriteLine($"/// using the property values from <paramref name=\"{sourceClassParameterName}\"/>.")
|
||||||
.WriteLine("/// </summary>")
|
.WriteLine("/// </summary>")
|
||||||
.WriteLine($"/// <param name=\"{sourceClassParameterName}\">The instance of <see cref=\"{targetSourceType.SourceType}\"/> to use as source.</param>");
|
.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)
|
private static SourceBuilder GenerateEnumerableJsonSourceTypeExtensionMethod(this SourceBuilder builder, MappingModel model)
|
||||||
{
|
{
|
||||||
var targetSourceType = model.MappedSourceTypes[0];
|
|
||||||
|
|
||||||
|
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||||
|
{
|
||||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||||
|
|
||||||
return builder
|
builder
|
||||||
.WriteLineIf(model.Options.SupportNullableStaticAnalysis, $"[return: NotNullIfNotNull(\"{sourceClassParameterName}\")]")
|
.WriteLineIf(model.Options.SupportNullableStaticAnalysis, $"[return: NotNullIfNotNull(\"{sourceClassParameterName}\")]")
|
||||||
.WriteLine($"{model.Options.GeneratedMethodsAccessModifier.ToLowercaseString()} static string ToJson(this IEnumerable<{targetSourceType.SourceType}{model.Options.NullableReferenceSyntax}> {sourceClassParameterName}List)")
|
.WriteLine($"{model.Options.GeneratedMethodsAccessModifier.ToLowercaseString()} static string ToJson(this IEnumerable<{targetSourceType.SourceType}{model.Options.NullableReferenceSyntax}> {sourceClassParameterName}List)")
|
||||||
.WriteOpeningBracket()
|
.WriteOpeningBracket()
|
||||||
.WriteLine($"return {sourceClassParameterName} == null ? null : new {model.TypeIdentifierName}({sourceClassParameterName});")
|
.WriteLine($"return {sourceClassParameterName} == null ? null : new {model.TypeIdentifierName}({sourceClassParameterName});")
|
||||||
.WriteClosingBracket();
|
.WriteClosingBracket();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,15 +22,18 @@ namespace MapTo.Sources
|
||||||
.WriteLine($"partial record {model.TypeIdentifierName}")
|
.WriteLine($"partial record {model.TypeIdentifierName}")
|
||||||
.WriteOpeningBracket();
|
.WriteOpeningBracket();
|
||||||
|
|
||||||
var targetSourceType = model.MappedSourceTypes[0];
|
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||||
|
{
|
||||||
// Class body
|
|
||||||
if (targetSourceType.GenerateSecondaryConstructor)
|
if (targetSourceType.GenerateSecondaryConstructor)
|
||||||
{
|
{
|
||||||
builder
|
builder
|
||||||
.GenerateSecondaryConstructor(model)
|
.GenerateSecondaryConstructor(model)
|
||||||
.WriteLine();
|
.WriteLine();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Class body
|
||||||
|
|
||||||
|
|
||||||
builder
|
builder
|
||||||
.GeneratePrivateConstructor(model)
|
.GeneratePrivateConstructor(model)
|
||||||
|
@ -55,9 +58,10 @@ namespace MapTo.Sources
|
||||||
private static SourceBuilder GenerateSecondaryConstructor(this SourceBuilder builder, MappingModel model)
|
private static SourceBuilder GenerateSecondaryConstructor(this SourceBuilder builder, MappingModel model)
|
||||||
{
|
{
|
||||||
// grab first data from array
|
// grab first data from array
|
||||||
var targetSourceType = model.MappedSourceTypes[0];
|
|
||||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
|
||||||
|
|
||||||
|
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||||
|
{
|
||||||
|
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||||
if (model.Options.GenerateXmlDocument)
|
if (model.Options.GenerateXmlDocument)
|
||||||
{
|
{
|
||||||
builder
|
builder
|
||||||
|
@ -67,43 +71,47 @@ namespace MapTo.Sources
|
||||||
.WriteLine("/// </summary>")
|
.WriteLine("/// </summary>")
|
||||||
.WriteLine($"/// <exception cref=\"ArgumentNullException\">{sourceClassParameterName} is null</exception>");
|
.WriteLine($"/// <exception cref=\"ArgumentNullException\">{sourceClassParameterName} is null</exception>");
|
||||||
}
|
}
|
||||||
|
builder .WriteLine($"{model.Options.ConstructorAccessModifier.ToLowercaseString()} {model.TypeIdentifierName}({targetSourceType.SourceType} {sourceClassParameterName})")
|
||||||
return builder
|
|
||||||
.WriteLine($"{model.Options.ConstructorAccessModifier.ToLowercaseString()} {model.TypeIdentifierName}({targetSourceType.SourceType} {sourceClassParameterName})")
|
|
||||||
.WriteLine($" : this(new {MappingContextSource.ClassName}(), {sourceClassParameterName}) {{ }}");
|
.WriteLine($" : this(new {MappingContextSource.ClassName}(), {sourceClassParameterName}) {{ }}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return builder;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private static SourceBuilder GeneratePrivateConstructor(this SourceBuilder builder, MappingModel model)
|
private static SourceBuilder GeneratePrivateConstructor(this SourceBuilder builder, MappingModel model)
|
||||||
{
|
{
|
||||||
var targetSourceType = model.MappedSourceTypes[0];
|
|
||||||
|
|
||||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
|
||||||
const string mappingContextParameterName = "context";
|
const string mappingContextParameterName = "context";
|
||||||
|
|
||||||
|
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||||
|
{
|
||||||
|
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||||
builder
|
builder
|
||||||
.WriteLine($"private protected {model.TypeIdentifierName}({MappingContextSource.ClassName} {mappingContextParameterName}, {targetSourceType.SourceType} {sourceClassParameterName})")
|
.WriteLine(
|
||||||
|
$"private protected {model.TypeIdentifierName}({MappingContextSource.ClassName} {mappingContextParameterName}, {targetSourceType.SourceType} {sourceClassParameterName})")
|
||||||
.Indent()
|
.Indent()
|
||||||
.Write(": this(").
|
.Write(": this(").WriteProperties(model, sourceClassParameterName, mappingContextParameterName)
|
||||||
|
|
||||||
WriteProperties(model, sourceClassParameterName, mappingContextParameterName)
|
|
||||||
|
|
||||||
.WriteLine(")")
|
.WriteLine(")")
|
||||||
.Unindent()
|
.Unindent()
|
||||||
.WriteOpeningBracket()
|
.WriteOpeningBracket()
|
||||||
.WriteLine($"if ({mappingContextParameterName} == null) throw new ArgumentNullException(nameof({mappingContextParameterName}));")
|
.WriteLine($"if ({mappingContextParameterName} == null) throw new ArgumentNullException(nameof({mappingContextParameterName}));")
|
||||||
.WriteLine($"if ({sourceClassParameterName} == null) throw new ArgumentNullException(nameof({sourceClassParameterName}));")
|
.WriteLine($"if ({sourceClassParameterName} == null) throw new ArgumentNullException(nameof({sourceClassParameterName}));")
|
||||||
.WriteLine()
|
.WriteLine()
|
||||||
.WriteLine($"{mappingContextParameterName}.{MappingContextSource.RegisterMethodName}({sourceClassParameterName}, this);");
|
.WriteLine($"{mappingContextParameterName}.{MappingContextSource.RegisterMethodName}({sourceClassParameterName}, this);")
|
||||||
|
.WriteClosingBracket();
|
||||||
|
}
|
||||||
// End constructor declaration
|
// End constructor declaration
|
||||||
return builder.WriteClosingBracket();
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SourceBuilder WriteProperties(this SourceBuilder builder, MappingModel model, string sourceClassParameterName,
|
private static SourceBuilder WriteProperties(this SourceBuilder builder, MappingModel model, string sourceClassParameterName,
|
||||||
string mappingContextParameterName)
|
string mappingContextParameterName)
|
||||||
{
|
{
|
||||||
var targetSourceType = model.MappedSourceTypes[0];
|
|
||||||
|
|
||||||
|
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||||
|
{
|
||||||
for (var i = 0; i < targetSourceType.SourceProperties.Length; i++)
|
for (var i = 0; i < targetSourceType.SourceProperties.Length; i++)
|
||||||
{
|
{
|
||||||
var property = targetSourceType.SourceProperties[i];
|
var property = targetSourceType.SourceProperties[i];
|
||||||
|
@ -136,16 +144,20 @@ namespace MapTo.Sources
|
||||||
builder.Write(", ");
|
builder.Write(", ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SourceBuilder GenerateFactoryMethod(this SourceBuilder builder, MappingModel model)
|
private static SourceBuilder GenerateFactoryMethod(this SourceBuilder builder, MappingModel model)
|
||||||
{
|
{
|
||||||
var targetSourceType = model.MappedSourceTypes[0];
|
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||||
|
{
|
||||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||||
|
|
||||||
return builder
|
builder
|
||||||
.GenerateConvertorMethodsXmlDocs(model, sourceClassParameterName)
|
.GenerateConvertorMethodsXmlDocs(model, sourceClassParameterName)
|
||||||
.WriteLineIf(model.Options.SupportNullableStaticAnalysis, $"[return: NotNullIfNotNull(\"{sourceClassParameterName}\")]")
|
.WriteLineIf(model.Options.SupportNullableStaticAnalysis, $"[return: NotNullIfNotNull(\"{sourceClassParameterName}\")]")
|
||||||
.WriteLine(
|
.WriteLine(
|
||||||
|
@ -156,15 +168,20 @@ namespace MapTo.Sources
|
||||||
.WriteClosingBracket();
|
.WriteClosingBracket();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
|
||||||
private static SourceBuilder GenerateConvertorMethodsXmlDocs(this SourceBuilder builder, MappingModel model, string sourceClassParameterName)
|
private static SourceBuilder GenerateConvertorMethodsXmlDocs(this SourceBuilder builder, MappingModel model, string sourceClassParameterName)
|
||||||
{
|
{
|
||||||
if (!model.Options.GenerateXmlDocument)
|
if (!model.Options.GenerateXmlDocument)
|
||||||
{
|
{
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
var targetSourceType = model.MappedSourceTypes[0];
|
|
||||||
|
|
||||||
return builder
|
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||||
|
{
|
||||||
|
builder
|
||||||
.WriteLine("/// <summary>")
|
.WriteLine("/// <summary>")
|
||||||
.WriteLine($"/// Creates a new instance of <see cref=\"{model.TypeIdentifierName}\"/> and sets its participating properties")
|
.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($"/// using the property values from <paramref name=\"{sourceClassParameterName}\"/>.")
|
||||||
|
@ -174,11 +191,15 @@ namespace MapTo.Sources
|
||||||
$"/// <returns>A new instance of <see cred=\"{model.TypeIdentifierName}\"/> -or- <c>null</c> if <paramref name=\"{sourceClassParameterName}\"/> is <c>null</c>.</returns>");
|
$"/// <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)
|
private static SourceBuilder GenerateSourceTypeExtensionClass(this SourceBuilder builder, MappingModel model)
|
||||||
{
|
{
|
||||||
var targetSourceType = model.MappedSourceTypes[0];
|
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||||
|
{
|
||||||
return builder
|
builder
|
||||||
.WriteLine(
|
.WriteLine(
|
||||||
$"{model.Options.GeneratedMethodsAccessModifier.ToLowercaseString()} static partial class {targetSourceType.SourceTypeIdentifierName}To{model.TypeIdentifierName}Extensions")
|
$"{model.Options.GeneratedMethodsAccessModifier.ToLowercaseString()} static partial class {targetSourceType.SourceTypeIdentifierName}To{model.TypeIdentifierName}Extensions")
|
||||||
.WriteOpeningBracket()
|
.WriteOpeningBracket()
|
||||||
|
@ -186,13 +207,17 @@ namespace MapTo.Sources
|
||||||
.WriteClosingBracket();
|
.WriteClosingBracket();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
|
||||||
private static SourceBuilder GenerateSourceTypeExtensionMethod(this SourceBuilder builder, MappingModel model)
|
private static SourceBuilder GenerateSourceTypeExtensionMethod(this SourceBuilder builder, MappingModel model)
|
||||||
{
|
{
|
||||||
var targetSourceType = model.MappedSourceTypes[0];
|
|
||||||
|
|
||||||
|
foreach (var targetSourceType in model.MappedSourceTypes)
|
||||||
|
{
|
||||||
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
|
||||||
|
|
||||||
return builder
|
builder
|
||||||
.GenerateConvertorMethodsXmlDocs(model, sourceClassParameterName)
|
.GenerateConvertorMethodsXmlDocs(model, sourceClassParameterName)
|
||||||
.WriteLineIf(model.Options.SupportNullableStaticAnalysis, $"[return: NotNullIfNotNull(\"{sourceClassParameterName}\")]")
|
.WriteLineIf(model.Options.SupportNullableStaticAnalysis, $"[return: NotNullIfNotNull(\"{sourceClassParameterName}\")]")
|
||||||
.WriteLine(
|
.WriteLine(
|
||||||
|
@ -200,6 +225,11 @@ namespace MapTo.Sources
|
||||||
.WriteOpeningBracket()
|
.WriteOpeningBracket()
|
||||||
.WriteLine($"return {sourceClassParameterName} == null ? null : new {model.TypeIdentifierName}({sourceClassParameterName});")
|
.WriteLine($"return {sourceClassParameterName} == null ? null : new {model.TypeIdentifierName}({sourceClassParameterName});")
|
||||||
.WriteClosingBracket();
|
.WriteClosingBracket();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return builder;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue