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 @@ -9,9 +9,8 @@ public static class ObjectFactoryBuilder
{
public static ObjectFactoryCollection ExtractObjectFactories(SimpleMappingBuilderContext ctx, ITypeSymbol mapperSymbol, bool isStatic)
{
var objectFactories = mapperSymbol
.GetMembers()
.OfType<IMethodSymbol>()
var objectFactories = ctx
.SymbolAccessor.GetAllDirectlyAccessibleMethods(mapperSymbol)
.Where(m => ctx.SymbolAccessor.HasAttribute<ObjectFactoryAttribute>(m))
.Select(x => BuildObjectFactory(ctx, x, isStatic))
.WhereNotNull()
Expand Down
5 changes: 5 additions & 0 deletions src/Riok.Mapperly/Descriptors/SymbolAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ 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)
{
return GetAllMembers(symbol).OfType<IMethodSymbol>().Where(IsDirectlyAccessible);
}

internal IEnumerable<IMethodSymbol> GetAllDirectlyAccessibleMethods(ITypeSymbol symbol, string name)
{
return GetAllMembers(symbol)
Expand Down
28 changes: 28 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/ObjectFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,34 @@ public void ShouldUseSimpleObjectFactory()
);
}

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

public abstract class BaseMapper
{
[ObjectFactory]
protected B CreateB() => new B();
}

[Mapper]
public partial class Mapper : BaseMapper
{
public partial B Map(A source);
}

public record A(int Value);
public record B { public int Value { get; set; } };
"""
);

return TestHelper.VerifyGenerator(source);
}

[Fact]
public void ShouldUseSimpleObjectFactoryForMultipleMaps()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//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 = CreateB();
target.Value = source.Value;
return target;
}
}