Thanks to visit codestin.com
Credit goes to github.com

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
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,8 @@ private static bool ValidateValueProviderMethod(IMembersBuilderContext<IMapping>
{
var methodName = memberMappingInfo.ValueConfiguration!.Use!;
var namedMethodCandidates = ctx
.BuilderContext.SymbolAccessor.GetAllMembers(ctx.BuilderContext.MapperDeclaration.Symbol)
.OfType<IMethodSymbol>()
.Where(m =>
string.Equals(m.Name, methodName, StringComparison.Ordinal)
&& m is { IsAsync: false, ReturnsVoid: false, IsGenericMethod: false, Parameters.Length: 0 }
&& ctx.BuilderContext.SymbolAccessor.IsDirectlyAccessible(m)
)
.BuilderContext.SymbolAccessor.GetAllDirectlyAccessibleMethods(ctx.BuilderContext.MapperDeclaration.Symbol, methodName)
.Where(m => m is { IsAsync: false, ReturnsVoid: false, IsGenericMethod: false, Parameters.Length: 0 })
.ToList();

if (namedMethodCandidates.Count == 0)
Expand Down
7 changes: 7 additions & 0 deletions src/Riok.Mapperly/Descriptors/SymbolAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@ internal Dictionary<object, IFieldSymbol> GetEnumFieldsByValue(ITypeSymbol symbo
internal HashSet<IFieldSymbol> GetFieldsExcept(ITypeSymbol symbol, ISet<IFieldSymbol> ignoredMembers) =>
GetAllFields(symbol).Where(x => !ignoredMembers.Remove(x)).ToHashSet(SymbolTypeEqualityComparer.FieldDefault);

internal IEnumerable<IMethodSymbol> GetAllDirectlyAccessibleMethods(ITypeSymbol symbol, string name)
{
return GetAllMembers(symbol)
.OfType<IMethodSymbol>()
.Where(m => string.Equals(m.Name, name, StringComparison.Ordinal) && IsDirectlyAccessible(m));
}

internal IReadOnlyCollection<ISymbol> GetAllMembers(ITypeSymbol symbol)
{
if (_allMembers.TryGetValue(symbol, out var members))
Expand Down
5 changes: 2 additions & 3 deletions src/Riok.Mapperly/Descriptors/UserMethodMappingExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,8 @@ private static IEnumerable<T> ExtractNamedUserImplementedMappings<T>(
string name
)
{
return mapperSymbol
.GetMembers(name)
.OfType<IMethodSymbol>()
return ctx
.SymbolAccessor.GetAllDirectlyAccessibleMethods(mapperSymbol, name)
.Where(m => IsMappingMethodCandidate(ctx, m, requireAttribute: false))
.Select(m => BuildUserImplementedMapping(ctx, m, null, allowPartial: true, isStatic: mapperSymbol.IsStatic, isExternal: false))
.OfType<T>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,4 +623,64 @@ class B
);
return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task BaseClassProtectedMethodToProperty()
{
var source = TestSourceBuilder.CSharp(
"""
using System;
using Riok.Mapperly.Abstractions;

public abstract class BaseMapper
{
protected int MapValue(int v) => v + 10;
}

[Mapper]
public partial class Mapper : BaseMapper
{
[MapProperty("Value", "Value", Use = nameof(MapValue))]
public partial B Map(A source);
}

public record A(int Value);
public record B(int Value);
"""
);

return TestHelper.VerifyGenerator(source);
}

[Fact]
public void BaseClassPrivateMethodToPropertyShouldDiagnostic()
{
var source = TestSourceBuilder.CSharp(
"""
using System;
using Riok.Mapperly.Abstractions;

public abstract class BaseMapper
{
private int MapValue(int v) => v + 10;
}

[Mapper]
public partial class Mapper : BaseMapper
{
[MapProperty("Value", "Value", Use = nameof(MapValue))]
public partial B Map(A source);
}

public record A(int Value);
public record B(int Value);
"""
);

TestHelper
.GenerateMapper(source, TestHelperOptions.AllowDiagnostics)
.Should()
.HaveDiagnostic(DiagnosticDescriptors.ReferencedMappingNotFound, "The referenced mapping named MapValue was not found")
.HaveAssertedAllDiagnostics();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//HintName: Mapper.g.cs
// <auto-generated />
#nullable enable
public partial class Mapper
{
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
public partial global::B Map(global::A source)
{
var target = new global::B(MapValue(source.Value));
return target;
}
}