Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: minor refactor for GetAttributes and UserMethods #561

Merged
merged 1 commit into from
Jul 14, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ public class UserDefinedExistingTargetMethodMapping : MethodMapping, IUserMappin
{
private const string NoMappingComment = "// Could not generate mapping";

private const string ReferenceHandlerTypeName =
"global::Riok.Mapperly.Abstractions.ReferenceHandling.Internal.PreserveReferenceHandler";

private readonly bool _enableReferenceHandling;
private readonly INamedTypeSymbol _referenceHandlerType;
private IExistingTargetMapping? _delegateMapping;

public UserDefinedExistingTargetMethodMapping(
IMethodSymbol method,
MethodParameter sourceParameter,
MethodParameter targetParameter,
MethodParameter? referenceHandlerParameter,
bool enableReferenceHandling,
INamedTypeSymbol referenceHandlerType
bool enableReferenceHandling
)
: base(method, sourceParameter, referenceHandlerParameter, targetParameter.Type)
{
_enableReferenceHandling = enableReferenceHandling;
_referenceHandlerType = referenceHandlerType;
Method = method;
TargetParameter = targetParameter;
}
Expand Down Expand Up @@ -68,7 +68,7 @@ public override IEnumerable<StatementSyntax> BuildBody(TypeMappingBuildContext c
{
// var refHandler = new RefHandler();
var referenceHandlerName = ctx.NameBuilder.New(DefaultReferenceHandlerParameterName);
var createRefHandler = CreateInstance(_referenceHandlerType);
var createRefHandler = CreateInstance(ReferenceHandlerTypeName);
yield return DeclareLocalVariable(referenceHandlerName, createRefHandler);
ctx = ctx.WithRefHandler(referenceHandlerName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ public UserDefinedNewInstanceGenericTypeMapping(
GenericMappingTypeParameters typeParameters,
MappingMethodParameters parameters,
bool enableReferenceHandling,
INamedTypeSymbol referenceHandlerType,
NullFallbackValue nullArm,
ITypeSymbol objectType
)
: base(method, parameters.Source, parameters.ReferenceHandler, enableReferenceHandling, referenceHandlerType, nullArm, objectType)
: base(method, parameters.Source, parameters.ReferenceHandler, enableReferenceHandling, nullArm, objectType)
{
TypeParameters = typeParameters;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@ namespace Riok.Mapperly.Descriptors.Mappings.UserMappings;
public class UserDefinedNewInstanceMethodMapping : MethodMapping, IDelegateUserMapping
{
private const string NoMappingComment = "// Could not generate mapping";
private const string ReferenceHandlerTypeName =
"global::Riok.Mapperly.Abstractions.ReferenceHandling.Internal.PreserveReferenceHandler";

private readonly bool _enableReferenceHandling;
private readonly INamedTypeSymbol _referenceHandlerType;

public UserDefinedNewInstanceMethodMapping(
IMethodSymbol method,
MethodParameter sourceParameter,
MethodParameter? referenceHandlerParameter,
bool enableReferenceHandling,
INamedTypeSymbol referenceHandlerType
bool enableReferenceHandling
)
: base(method, sourceParameter, referenceHandlerParameter, method.ReturnType.UpgradeNullable())
{
_enableReferenceHandling = enableReferenceHandling;
_referenceHandlerType = referenceHandlerType;
Method = method;
}

Expand All @@ -50,7 +49,7 @@ public override IEnumerable<StatementSyntax> BuildBody(TypeMappingBuildContext c
if (_enableReferenceHandling && ReferenceHandlerParameter == null)
{
// new RefHandler();
var createRefHandler = CreateInstance(_referenceHandlerType);
var createRefHandler = CreateInstance(ReferenceHandlerTypeName);
ctx = ctx.WithRefHandler(createRefHandler);
return new[] { ReturnStatement(DelegateMapping.Build(ctx)) };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ public abstract class UserDefinedNewInstanceRuntimeTargetTypeMapping : MethodMap
{
private const string IsAssignableFromMethodName = nameof(Type.IsAssignableFrom);
private const string GetTypeMethodName = nameof(GetType);
private const string ReferenceHandlerTypeName =
"global::Riok.Mapperly.Abstractions.ReferenceHandling.Internal.PreserveReferenceHandler";

private readonly List<RuntimeTargetTypeMapping> _mappings = new();
private readonly bool _enableReferenceHandling;
private readonly INamedTypeSymbol _referenceHandlerType;
private readonly NullFallbackValue _nullArm;
private readonly ITypeSymbol _objectType;

Expand All @@ -27,15 +28,13 @@ protected UserDefinedNewInstanceRuntimeTargetTypeMapping(
MethodParameter sourceParameter,
MethodParameter? referenceHandlerParameter,
bool enableReferenceHandling,
INamedTypeSymbol referenceHandlerType,
NullFallbackValue nullArm,
ITypeSymbol objectType
)
: base(method, sourceParameter, referenceHandlerParameter, method.ReturnType)
{
Method = method;
_enableReferenceHandling = enableReferenceHandling;
_referenceHandlerType = referenceHandlerType;
_nullArm = nullArm;
_objectType = objectType;
}
Expand All @@ -61,7 +60,7 @@ public override IEnumerable<StatementSyntax> BuildBody(TypeMappingBuildContext c
{
// var refHandler = new RefHandler();
var referenceHandlerName = ctx.NameBuilder.New(DefaultReferenceHandlerParameterName);
var createRefHandler = CreateInstance(_referenceHandlerType);
var createRefHandler = CreateInstance(ReferenceHandlerTypeName);
yield return DeclareLocalVariable(referenceHandlerName, createRefHandler);

ctx = ctx.WithRefHandler(referenceHandlerName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ public UserDefinedNewInstanceRuntimeTargetTypeParameterMapping(
IMethodSymbol method,
RuntimeTargetTypeMappingMethodParameters parameters,
bool enableReferenceHandling,
INamedTypeSymbol referenceHandlerType,
NullFallbackValue nullArm,
ITypeSymbol objectType
)
: base(method, parameters.Source, parameters.ReferenceHandler, enableReferenceHandling, referenceHandlerType, nullArm, objectType)
: base(method, parameters.Source, parameters.ReferenceHandler, enableReferenceHandling, nullArm, objectType)
{
_targetTypeParameter = parameters.TargetType;
}
Expand Down
8 changes: 7 additions & 1 deletion src/Riok.Mapperly/Descriptors/SymbolAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@
internal IEnumerable<AttributeData> GetAttributes<T>(ISymbol symbol)
where T : Attribute
{
var attributes = GetAttributesCore(symbol);
if (attributes.IsEmpty)
{
yield break;
}

var attributeSymbol = _types.Get<T>();
foreach (var attr in GetAttributesCore(symbol))
foreach (var attr in attributes)
{
if (SymbolEqualityComparer.Default.Equals(attr.AttributeClass?.ConstructedFrom ?? attr.AttributeClass, attributeSymbol))
{
Expand Down Expand Up @@ -77,7 +83,7 @@
if (comparer.Equals(name, member.Name))
{
yield return member;
}

Check warning on line 86 in src/Riok.Mapperly/Descriptors/SymbolAccessor.cs

View check run for this annotation

Codecov / codecov/patch

src/Riok.Mapperly/Descriptors/SymbolAccessor.cs#L86

Added line #L86 was not covered by tests
}
}

Expand Down
9 changes: 2 additions & 7 deletions src/Riok.Mapperly/Descriptors/UserMethodMappingExtractor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis;
using Riok.Mapperly.Abstractions.ReferenceHandling;
using Riok.Mapperly.Abstractions.ReferenceHandling.Internal;
using Riok.Mapperly.Descriptors.Mappings;
using Riok.Mapperly.Descriptors.Mappings.UserMappings;
using Riok.Mapperly.Diagnostics;
Expand Down Expand Up @@ -104,7 +103,6 @@ bool isStatic
methodSymbol,
runtimeTargetTypeParams,
ctx.MapperConfiguration.UseReferenceHandling,
ctx.Types.Get<PreserveReferenceHandler>(),
GetTypeSwitchNullArm(methodSymbol, runtimeTargetTypeParams, null),
ctx.Compilation.ObjectType
);
Expand All @@ -123,7 +121,6 @@ bool isStatic
typeParameters.Value,
parameters,
ctx.MapperConfiguration.UseReferenceHandling,
ctx.Types.Get<PreserveReferenceHandler>(),
GetTypeSwitchNullArm(methodSymbol, parameters, typeParameters),
ctx.Compilation.ObjectType
);
Expand All @@ -142,17 +139,15 @@ bool isStatic
parameters.Source,
parameters.Target.Value,
parameters.ReferenceHandler,
ctx.MapperConfiguration.UseReferenceHandling,
ctx.Types.Get<PreserveReferenceHandler>()
ctx.MapperConfiguration.UseReferenceHandling
);
}

return new UserDefinedNewInstanceMethodMapping(
methodSymbol,
parameters.Source,
parameters.ReferenceHandler,
ctx.MapperConfiguration.UseReferenceHandling,
ctx.Types.Get<PreserveReferenceHandler>()
ctx.MapperConfiguration.UseReferenceHandling
);
}

Expand Down
6 changes: 6 additions & 0 deletions src/Riok.Mapperly/Emit/SyntaxFactoryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,12 @@ public static LocalDeclarationStatementSyntax DeclareLocalVariable(string variab
public static StatementSyntax CreateInstance(string variableName, ITypeSymbol typeSymbol) =>
DeclareLocalVariable(variableName, CreateInstance(typeSymbol));

public static ObjectCreationExpressionSyntax CreateInstance(string typeName)
{
var type = IdentifierName(typeName);
return ObjectCreationExpression(type).WithArgumentList(SyntaxFactory.ArgumentList());
}

public static ObjectCreationExpressionSyntax CreateInstance(ITypeSymbol typeSymbol)
{
var type = NonNullableIdentifier(typeSymbol);
Expand Down
Loading