Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"rollForward": false
},
"csharpier": {
"version": "1.3.0",
"version": "1.4.0",
"commands": [
"csharpier"
],
Expand Down
3 changes: 1 addition & 2 deletions src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ internal static TData Access<TAttribute, TData>(AttributeData attrData, SymbolAc
var dataType = typeof(TData);

var syntax = (AttributeSyntax?)attrData.ApplicationSyntaxReference?.GetSyntax();
var syntaxArguments =
(IReadOnlyList<AttributeArgumentSyntax>?)syntax?.ArgumentList?.Arguments
var syntaxArguments = (IReadOnlyList<AttributeArgumentSyntax>?)syntax?.ArgumentList?.Arguments
?? new AttributeArgumentSyntax[attrData.ConstructorArguments.Length + attrData.NamedArguments.Length];
var typeArguments = (IReadOnlyCollection<ITypeSymbol>?)attrData.AttributeClass?.TypeArguments ?? [];
var attr = Create<TData>(typeArguments, attrData.ConstructorArguments, syntaxArguments, symbolAccessor);
Expand Down
81 changes: 41 additions & 40 deletions src/Riok.Mapperly/Configuration/MapperConfigurationMerger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public static MapperConfiguration Merge(MapperConfiguration highPriority, Mapper
EnumMappingStrategy = highPriority.EnumMappingStrategy ?? lowPriority.EnumMappingStrategy,
EnumMappingIgnoreCase = highPriority.EnumMappingIgnoreCase ?? lowPriority.EnumMappingIgnoreCase,
ThrowOnMappingNullMismatch = highPriority.ThrowOnMappingNullMismatch ?? lowPriority.ThrowOnMappingNullMismatch,
ThrowOnPropertyMappingNullMismatch =
highPriority.ThrowOnPropertyMappingNullMismatch ?? lowPriority.ThrowOnPropertyMappingNullMismatch,
ThrowOnPropertyMappingNullMismatch = highPriority.ThrowOnPropertyMappingNullMismatch
?? lowPriority.ThrowOnPropertyMappingNullMismatch,
AllowNullPropertyAssignment = highPriority.AllowNullPropertyAssignment ?? lowPriority.AllowNullPropertyAssignment,
UseDeepCloning = highPriority.UseDeepCloning ?? lowPriority.UseDeepCloning,
StackCloningStrategy = highPriority.StackCloningStrategy ?? lowPriority.StackCloningStrategy,
Expand All @@ -37,84 +37,85 @@ public static MapperConfiguration Merge(MapperConfiguration highPriority, Mapper
public static MapperAttribute MergeToAttribute(MapperConfiguration mapperConfiguration, MapperConfiguration defaultMapperConfiguration)
{
var mapper = new MapperAttribute();
mapper.PropertyNameMappingStrategy =
mapperConfiguration.PropertyNameMappingStrategy
mapper.PropertyNameMappingStrategy = mapperConfiguration.PropertyNameMappingStrategy
?? defaultMapperConfiguration.PropertyNameMappingStrategy
?? mapper.PropertyNameMappingStrategy;

mapper.EnumMappingStrategy =
mapperConfiguration.EnumMappingStrategy ?? defaultMapperConfiguration.EnumMappingStrategy ?? mapper.EnumMappingStrategy;
mapper.EnumMappingStrategy = mapperConfiguration.EnumMappingStrategy
?? defaultMapperConfiguration.EnumMappingStrategy
?? mapper.EnumMappingStrategy;

mapper.EnumMappingIgnoreCase =
mapperConfiguration.EnumMappingIgnoreCase ?? defaultMapperConfiguration.EnumMappingIgnoreCase ?? mapper.EnumMappingIgnoreCase;
mapper.EnumMappingIgnoreCase = mapperConfiguration.EnumMappingIgnoreCase
?? defaultMapperConfiguration.EnumMappingIgnoreCase
?? mapper.EnumMappingIgnoreCase;

mapper.ThrowOnMappingNullMismatch =
mapperConfiguration.ThrowOnMappingNullMismatch
mapper.ThrowOnMappingNullMismatch = mapperConfiguration.ThrowOnMappingNullMismatch
?? defaultMapperConfiguration.ThrowOnMappingNullMismatch
?? mapper.ThrowOnMappingNullMismatch;

mapper.ThrowOnPropertyMappingNullMismatch =
mapperConfiguration.ThrowOnPropertyMappingNullMismatch
mapper.ThrowOnPropertyMappingNullMismatch = mapperConfiguration.ThrowOnPropertyMappingNullMismatch
?? defaultMapperConfiguration.ThrowOnPropertyMappingNullMismatch
?? mapper.ThrowOnPropertyMappingNullMismatch;

mapper.AllowNullPropertyAssignment =
mapperConfiguration.AllowNullPropertyAssignment
mapper.AllowNullPropertyAssignment = mapperConfiguration.AllowNullPropertyAssignment
?? defaultMapperConfiguration.AllowNullPropertyAssignment
?? mapper.AllowNullPropertyAssignment;

mapper.UseDeepCloning = mapperConfiguration.UseDeepCloning ?? defaultMapperConfiguration.UseDeepCloning ?? mapper.UseDeepCloning;

mapper.StackCloningStrategy =
mapperConfiguration.StackCloningStrategy ?? defaultMapperConfiguration.StackCloningStrategy ?? mapper.StackCloningStrategy;
mapper.StackCloningStrategy = mapperConfiguration.StackCloningStrategy
?? defaultMapperConfiguration.StackCloningStrategy
?? mapper.StackCloningStrategy;

mapper.EnabledConversions =
mapperConfiguration.EnabledConversions ?? defaultMapperConfiguration.EnabledConversions ?? mapper.EnabledConversions;
mapper.EnabledConversions = mapperConfiguration.EnabledConversions
?? defaultMapperConfiguration.EnabledConversions
?? mapper.EnabledConversions;

mapper.UseReferenceHandling =
mapperConfiguration.UseReferenceHandling ?? defaultMapperConfiguration.UseReferenceHandling ?? mapper.UseReferenceHandling;
mapper.UseReferenceHandling = mapperConfiguration.UseReferenceHandling
?? defaultMapperConfiguration.UseReferenceHandling
?? mapper.UseReferenceHandling;

mapper.IgnoreObsoleteMembersStrategy =
mapperConfiguration.IgnoreObsoleteMembersStrategy
mapper.IgnoreObsoleteMembersStrategy = mapperConfiguration.IgnoreObsoleteMembersStrategy
?? defaultMapperConfiguration.IgnoreObsoleteMembersStrategy
?? mapper.IgnoreObsoleteMembersStrategy;

mapper.RequiredMappingStrategy =
mapperConfiguration.RequiredMappingStrategy
mapper.RequiredMappingStrategy = mapperConfiguration.RequiredMappingStrategy
?? defaultMapperConfiguration.RequiredMappingStrategy
?? mapper.RequiredMappingStrategy;

mapper.RequiredEnumMappingStrategy =
mapperConfiguration.RequiredEnumMappingStrategy
mapper.RequiredEnumMappingStrategy = mapperConfiguration.RequiredEnumMappingStrategy
?? defaultMapperConfiguration.RequiredEnumMappingStrategy
?? mapperConfiguration.RequiredMappingStrategy
?? defaultMapperConfiguration.RequiredMappingStrategy
?? mapper.RequiredMappingStrategy;

mapper.IncludedMembers =
mapperConfiguration.IncludedMembers ?? defaultMapperConfiguration.IncludedMembers ?? mapper.IncludedMembers;
mapper.IncludedMembers = mapperConfiguration.IncludedMembers
?? defaultMapperConfiguration.IncludedMembers
?? mapper.IncludedMembers;

mapper.IncludedConstructors =
mapperConfiguration.IncludedConstructors ?? defaultMapperConfiguration.IncludedConstructors ?? mapper.IncludedConstructors;
mapper.IncludedConstructors = mapperConfiguration.IncludedConstructors
?? defaultMapperConfiguration.IncludedConstructors
?? mapper.IncludedConstructors;

mapper.PreferParameterlessConstructors =
mapperConfiguration.PreferParameterlessConstructors
mapper.PreferParameterlessConstructors = mapperConfiguration.PreferParameterlessConstructors
?? defaultMapperConfiguration.PreferParameterlessConstructors
?? mapper.PreferParameterlessConstructors;

mapper.AutoUserMappings =
mapperConfiguration.AutoUserMappings ?? defaultMapperConfiguration.AutoUserMappings ?? mapper.AutoUserMappings;
mapper.AutoUserMappings = mapperConfiguration.AutoUserMappings
?? defaultMapperConfiguration.AutoUserMappings
?? mapper.AutoUserMappings;

mapper.EnumNamingStrategy =
mapperConfiguration.EnumNamingStrategy ?? defaultMapperConfiguration.EnumNamingStrategy ?? mapper.EnumNamingStrategy;
mapper.EnumNamingStrategy = mapperConfiguration.EnumNamingStrategy
?? defaultMapperConfiguration.EnumNamingStrategy
?? mapper.EnumNamingStrategy;

mapper.QueryableProjectionNullHandling =
mapperConfiguration.QueryableProjectionNullHandling
mapper.QueryableProjectionNullHandling = mapperConfiguration.QueryableProjectionNullHandling
?? defaultMapperConfiguration.QueryableProjectionNullHandling
?? mapper.QueryableProjectionNullHandling;

mapper.NoExpressionInlining =
mapperConfiguration.NoExpressionInlining ?? defaultMapperConfiguration.NoExpressionInlining ?? mapper.NoExpressionInlining;
mapper.NoExpressionInlining = mapperConfiguration.NoExpressionInlining
?? defaultMapperConfiguration.NoExpressionInlining
?? mapper.NoExpressionInlining;

return mapper;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Riok.Mapperly/Configuration/MapperConfigurationReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ bool supportsDeepCloning
bool supportsDeepCloning
)
{
var typeMapping =
(ITypeMapping?)_mappings.FindNamedNewInstanceMapping(name.FullName, out var ambiguousName)
var typeMapping = (ITypeMapping?)_mappings.FindNamedNewInstanceMapping(name.FullName, out var ambiguousName)
?? _mappings.FindExistingInstanceNamedMapping(name.FullName, out ambiguousName);
var methodSymbol = (typeMapping as IUserMapping)?.Method;
if (!ValidateIncludedConfig(ambiguousName, configRef, name.FullName, typeMapping, methodSymbol))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ public static class SpanMappingBuilder

// if the source is Span/ReadOnlySpan or Array and target is Span/ReadOnlySpan
// and element type is the same, then direct cast
(CollectionType.Span or CollectionType.ReadOnlySpan or CollectionType.Array, CollectionType.Span or CollectionType.ReadOnlySpan)
when elementMapping.IsSynthetic && !ctx.Configuration.UseDeepCloning => new CastMapping(ctx.Source, ctx.Target),
(
CollectionType.Span
or CollectionType.ReadOnlySpan
or CollectionType.Array,
CollectionType.Span
or CollectionType.ReadOnlySpan
) when elementMapping.IsSynthetic && !ctx.Configuration.UseDeepCloning => new CastMapping(ctx.Source, ctx.Target),

// otherwise map each value into an Array
_ => BuildToArrayOrMap(ctx, elementMapping),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
// source is A x ? MapToA(x) : <other cases>
var typeExpressions = typeMappings
.Reverse()
.Aggregate<INewInstanceMapping, ExpressionSyntax>(
DefaultLiteral(),
(aggregate, current) => BuildConditional(ctx, aggregate, current)
.Aggregate<INewInstanceMapping, ExpressionSyntax>(DefaultLiteral(), (aggregate, current) =>
BuildConditional(ctx, aggregate, current)
);

// cast to target type, to ensure the compiler picks the correct type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,29 @@ public class UnsafeAccessorTypeContext(UniqueNameBuilder nameBuilder, INamedType

public UnsafeSetPropertyAccessor GetOrBuildPropertySetter(PropertyMember member)
{
return GetOrBuild(
UnsafeAccessorType.SetProperty,
member.Symbol,
static (m, className, methodName) => new UnsafeSetPropertyAccessor(m, className, methodName)
return GetOrBuild(UnsafeAccessorType.SetProperty, member.Symbol, static (m, className, methodName) =>
new UnsafeSetPropertyAccessor(m, className, methodName)
);
}

public UnsafeGetPropertyAccessor GetOrBuildPropertyGetter(PropertyMember member)
{
return GetOrBuild(
UnsafeAccessorType.GetProperty,
member.Symbol,
static (m, className, methodName) => new UnsafeGetPropertyAccessor(m, className, methodName)
return GetOrBuild(UnsafeAccessorType.GetProperty, member.Symbol, static (m, className, methodName) =>
new UnsafeGetPropertyAccessor(m, className, methodName)
);
}

public UnsafeFieldAccessor GetOrBuildFieldGetter(FieldMember member)
{
return GetOrBuild(
UnsafeAccessorType.GetField,
member.Symbol,
static (m, className, methodName) => new UnsafeFieldAccessor(m, className, methodName)
return GetOrBuild(UnsafeAccessorType.GetField, member.Symbol, static (m, className, methodName) =>
new UnsafeFieldAccessor(m, className, methodName)
);
}

public UnsafeConstructorAccessor GetOrBuildConstructor(IMethodSymbol ctorSymbol)
{
return GetOrBuild(
UnsafeAccessorType.Constructor,
ctorSymbol,
static (s, className, methodName) => new UnsafeConstructorAccessor(s, className, methodName)
return GetOrBuild(UnsafeAccessorType.Constructor, ctorSymbol, static (s, className, methodName) =>
new UnsafeConstructorAccessor(s, className, methodName)
);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Riok.Mapperly/Descriptors/UserMethodMappingExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ internal static IEnumerable<IUserMapping> ExtractUserMappings(SimpleMappingBuild
var methods = mapperSymbol.GetMembers().OfType<IMethodSymbol>().Where(method => IsMappingMethodCandidate(ctx, method));
foreach (var method in methods)
{
var mapping =
BuildUserDefinedMapping(ctx, method)
var mapping = BuildUserDefinedMapping(ctx, method)
?? BuildUserImplementedMapping(
ctx,
method,
Expand Down
23 changes: 10 additions & 13 deletions src/Riok.Mapperly/Emit/Syntax/SyntaxIndentationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,18 @@ public static SyntaxToken AddTrailingLineFeed(this SyntaxToken token, int indent

public static SyntaxTriviaList AddLineFeedAndIndentation(this SyntaxTriviaList trivia, int indentation)
{
var triviaToInsert = _indentationLineFeedSyntaxTriviaCache.GetOrAdd(
indentation,
static indentationLevel =>
var triviaToInsert = _indentationLineFeedSyntaxTriviaCache.GetOrAdd(indentation, static indentationLevel =>
{
// +1 for the new line at the beginning of the indentation
var toInsert = new SyntaxTrivia[indentationLevel + 1];
toInsert[0] = ElasticCarriageReturnLineFeed;
for (var i = 1; i < toInsert.Length; i++)
{
// +1 for the new line at the beginning of the indentation
var toInsert = new SyntaxTrivia[indentationLevel + 1];
toInsert[0] = ElasticCarriageReturnLineFeed;
for (var i = 1; i < toInsert.Length; i++)
{
toInsert[i] = _indentation;
}

return toInsert;
toInsert[i] = _indentation;
}
);

return toInsert;
});

return trivia.InsertRange(0, triviaToInsert);
}
Expand Down
26 changes: 10 additions & 16 deletions src/Riok.Mapperly/Helpers/IncrementalValuesProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,13 @@ public static void ReportDiagnostics(
IncrementalValuesProvider<ImmutableEquatableArray<Diagnostic>> diagnostics
)
{
context.RegisterSourceOutput(
diagnostics,
static (context, diagnostics) =>
context.RegisterSourceOutput(diagnostics, static (context, diagnostics) =>
{
foreach (var diagnostic in diagnostics)
{
foreach (var diagnostic in diagnostics)
{
context.ReportDiagnostic(diagnostic);
}
context.ReportDiagnostic(diagnostic);
}
);
});
}

/// <summary>
Expand All @@ -72,13 +69,10 @@ public static void EmitMapperSource(
IncrementalValuesProvider<MapperNode> mappers
)
{
context.RegisterImplementationSourceOutput(
mappers,
static (spc, mapper) =>
{
var mapperText = mapper.Body.GetText(Encoding.UTF8);
spc.AddSource(mapper.FileName, mapperText);
}
);
context.RegisterImplementationSourceOutput(mappers, static (spc, mapper) =>
{
var mapperText = mapper.Body.GetText(Encoding.UTF8);
spc.AddSource(mapper.FileName, mapperText);
});
}
}
17 changes: 6 additions & 11 deletions src/Riok.Mapperly/MapperGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,10 @@ CancellationToken cancellationToken
var compilation = compilationContext.Compilation.WithOptions(advancedOptions);

// map all symbols to the new compilation
compilationContext = compilationContext with
{
Compilation = compilation,
Types = new WellKnownTypes(compilation),
};
compilationContext = compilationContext with { Compilation = compilation, Types = new WellKnownTypes(compilation) };
assemblyScopedStaticMappers = MapStaticMappers(assemblyScopedStaticMappers, compilation);
var mapperDeclarationSymbolFqn = mapperDeclaration.Symbol.FullyQualifiedMetadataName();
var mappedMapperSymbol =
compilationContext.Compilation.GetBestTypeByMetadataName(mapperDeclarationSymbolFqn)
var mappedMapperSymbol = compilationContext.Compilation.GetBestTypeByMetadataName(mapperDeclarationSymbolFqn)
?? throw new InvalidOperationException($"Could not get type {mapperDeclarationSymbolFqn}");

mapperDeclaration = mapperDeclaration with { Symbol = mappedMapperSymbol };
Expand Down Expand Up @@ -167,8 +162,7 @@ CSharpCompilation compilation
foreach (var config in staticMappers)
{
var mapperTypeFqn = config.MapperType.FullyQualifiedMetadataName();
var mappedMapperType =
compilation.GetBestTypeByMetadataName(mapperTypeFqn)
var mappedMapperType = compilation.GetBestTypeByMetadataName(mapperTypeFqn)
?? throw new InvalidOperationException($"Could not get type {mapperTypeFqn}");
staticMappersBuilder.Add(
new UseStaticMapperConfiguration(mappedMapperType) { NoExpressionInlining = config.NoExpressionInlining }
Expand Down Expand Up @@ -259,8 +253,9 @@ MapperConfiguration mapperDefaults
{
var includedMembers = mapperConfiguration.IncludedMembers ?? mapperDefaults.IncludedMembers ?? MemberVisibility.AllAccessible;

var includedConstructors =
mapperConfiguration.IncludedConstructors ?? mapperDefaults.IncludedConstructors ?? MemberVisibility.AllAccessible;
var includedConstructors = mapperConfiguration.IncludedConstructors
?? mapperDefaults.IncludedConstructors
?? MemberVisibility.AllAccessible;

if (includedMembers.HasFlag(MemberVisibility.Accessible) && includedConstructors.HasFlag(MemberVisibility.Accessible))
{
Expand Down
Loading
Loading