Update method when properties are not readonly

This commit is contained in:
Wvader 2021-12-07 12:47:01 +00:00
parent 55f735006e
commit 1e266bfd3f
3 changed files with 35 additions and 13 deletions

View File

@ -137,6 +137,8 @@ namespace MapTo
return null; return null;
} }
string? converterFullyQualifiedName = null; string? converterFullyQualifiedName = null;
var converterParameters = ImmutableArray<string>.Empty; var converterParameters = ImmutableArray<string>.Empty;
ITypeSymbol? mappedSourcePropertyType = null; ITypeSymbol? mappedSourcePropertyType = null;
@ -162,7 +164,9 @@ namespace MapTo
converterParameters.ToImmutableArray(), converterParameters.ToImmutableArray(),
sourceProperty.Name, sourceProperty.Name,
ToQualifiedDisplayName(mappedSourcePropertyType), ToQualifiedDisplayName(mappedSourcePropertyType),
ToQualifiedDisplayName(enumerableTypeArgumentType)); ToQualifiedDisplayName(enumerableTypeArgumentType),
(sourceProperty as IPropertySymbol).IsReadOnly);
;
} }
protected bool TryGetMapTypeConverter(ISymbol property, IPropertySymbol sourceProperty, out string? converterFullyQualifiedName, protected bool TryGetMapTypeConverter(ISymbol property, IPropertySymbol sourceProperty, out string? converterFullyQualifiedName,

View File

@ -29,7 +29,8 @@ namespace MapTo
ImmutableArray<string> TypeConverterParameters, ImmutableArray<string> TypeConverterParameters,
string SourcePropertyName, string SourcePropertyName,
string? MappedSourcePropertyTypeName, string? MappedSourcePropertyTypeName,
string? EnumerableTypeArgument) string? EnumerableTypeArgument,
bool isReadOnly)
{ {
public bool IsEnumerable => EnumerableTypeArgument is not null; public bool IsEnumerable => EnumerableTypeArgument is not null;
} }

View File

@ -31,7 +31,14 @@ namespace MapTo.Sources
builder builder
.GeneratePrivateConstructor(model) .GeneratePrivateConstructor(model)
.WriteLine() .WriteLine();
if(PropertiesAreReadOnly(model))
{
builder.GenerateUpdateMethod(model);
}
builder
//.GenerateFactoryMethod(model) //.GenerateFactoryMethod(model)
.GenerateUpdateMethod(model) .GenerateUpdateMethod(model)
@ -105,7 +112,7 @@ namespace MapTo.Sources
{ {
builder.WriteLine(property.MappedSourcePropertyTypeName is null builder.WriteLine(property.MappedSourcePropertyTypeName is null
? $"{property.Name} = {sourceClassParameterName}.{property.SourcePropertyName};" ? $"{property.Name} = {sourceClassParameterName}.{property.SourcePropertyName};"
: $"{property.Name} = {mappingContextParameterName}.{MappingContextSource.MapMethodName}<{property.MappedSourcePropertyTypeName}, {property.Type}>({sourceClassParameterName}.{property.SourcePropertyName});"); : "");
} }
} }
else else
@ -123,6 +130,16 @@ namespace MapTo.Sources
} }
private static bool PropertiesAreReadOnly(MappingModel model)
{
foreach (var property in model.MappedProperties)
{
if (!property.isReadOnly) return false;
}
return true ;
}
private static SourceBuilder GenerateFactoryMethod(this SourceBuilder builder, MappingModel model) private static SourceBuilder GenerateFactoryMethod(this SourceBuilder builder, MappingModel model)
{ {
var sourceClassParameterName = model.SourceTypeIdentifierName.ToCamelCase(); var sourceClassParameterName = model.SourceTypeIdentifierName.ToCamelCase();