Skip to content

Commit

Permalink
chore: semi fix
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Mar 28, 2024
1 parent 8a9d3d4 commit c7ccc75
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 56 deletions.
10 changes: 3 additions & 7 deletions src/Riok.Mapperly/Configuration/MapperConfigurationReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,7 @@ private MembersMappingConfiguration BuildMembersConfig(MappingConfigurationRefer
.AccessFirstOrDefault<MapperIgnoreObsoleteMembersAttribute>(configRef.Method)
?.IgnoreObsoleteStrategy;
var requiredMapping = _dataAccessor.AccessFirstOrDefault<MapperRequiredMappingAttribute>(configRef.Method)?.RequiredMappingStrategy;
var maxRecursionDepth = _dataAccessor.Access<MapperMaxRecursionDepthAttribute>(method).FirstOrDefault()
is not { } methodMaxRecursionDepth
? _defaultConfiguration.Properties.MaxRecursionDepth
: methodMaxRecursionDepth.MaxRecursionDepth;


var maxRecursionDepth = _dataAccessor.AccessFirstOrDefault<MapperMaxRecursionDepthAttribute>(configRef.Method)?.MaxRecursionDepth;

// ignore the required mapping / ignore obsolete as the same attribute is used for other mapping types
// e.g. enum to enum
Expand Down Expand Up @@ -123,7 +118,8 @@ is not { } methodMaxRecursionDepth
ignoredTargetMembers,
memberConfigurations,
ignoreObsolete ?? MapperConfiguration.Members.IgnoreObsoleteMembersStrategy,
requiredMapping ?? MapperConfiguration.Members.RequiredMappingStrategy
requiredMapping ?? MapperConfiguration.Members.RequiredMappingStrategy,
maxRecursionDepth ?? MapperConfiguration.Members.MaxRecursionDepth
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ conversionType is not MappingConversionType.EnumToString and not MappingConversi
/// <returns>The <see cref="INewInstanceMapping"/> if a mapping was found or <c>null</c> if none was found.</returns>
public override INewInstanceMapping? FindMapping(TypeMappingKey mappingKey)
{
// check for recursion loop returning null to prevent a loop or default when recursion limit is reached.
var count = _parentTypes.GetDepth(mappingKey);
if (count >= 1)
{
return count >= Configuration.Members.MaxRecursionDepth + 2
? new DefaultMemberMapping(mappingKey.Source, mappingKey.Target)
: null;
}

var mapping = InlinedMappings.Find(mappingKey, out var isInlined);
if (mapping == null)
return null;
Expand All @@ -91,14 +100,14 @@ conversionType is not MappingConversionType.EnumToString and not MappingConversi

public INewInstanceMapping? FindNewInstanceMapping(IMethodSymbol method)
{
// check for recursion loop returning null to prevent a loop or default when recursion limit is reached.
var count = _parentTypes.GetDepth(mappingKey);
if (count >= 1)
{
return count >= Configuration.Properties.MaxRecursionDepth + 2
? new DefaultMemberMapping(mappingKey.Source, mappingKey.Target)
: null;
}
// // check for recursion loop returning null to prevent a loop or default when recursion limit is reached.
// var count = _parentTypes.GetDepth(mappingKey);
// if (count >= 1)
// {
// return count >= Configuration.Properties.MaxRecursionDepth + 2
// ? new DefaultMemberMapping(mappingKey.Source, mappingKey.Target)
// : null;
// }

INewInstanceMapping? mapping = InlinedMappings.FindNewInstanceUserMapping(method, out var isInlined);
if (mapping == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ private MapperConfiguration NewMapperConfiguration()
if (type == typeof(bool))
return !modifiedValue;

if (type == typeof(int))
return !modifiedValue;

if (type.IsEnum)
return type.GetEnumValues().GetValue(modifiedValue ? 1 : 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Riok.Mapperly.Tests.Mapping;

[UsesVerify]
public class QueryableProjectionLoopTest
{
[Fact]
Expand Down
55 changes: 15 additions & 40 deletions test/Riok.Mapperly.Tests/Mapping/QueryableProjectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,46 +116,21 @@ public Task RecordToRecordManualListMapping()
return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task ClassToClassWithUserImplemented()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"""
private partial System.Linq.IQueryable<B> Map(System.Linq.IQueryable<A> source);
private D MapToD(C v) => new D { Value = v.Value + "-mapped" };
""",
"class A { public string StringValue { get; set; } public C NestedValue { get; set; } }",
"class B { public string StringValue { get; set; } public D NestedValue { get; set; } }",
"class C { public string Value { get; set; } }",
"class D { public string Value { get; set; } }"
);

[Fact]
public Task ReferenceLoopInitProperty()
{
var source = TestSourceBuilder.Mapping(
"System.Linq.IQueryable<A>",
"System.Linq.IQueryable<B>",
"class A { public A? Parent { get; set; } public int IntValue { get; set; } }",
"class B { public B? Parent { get; set; } public int IntValue { get; set; } }"
);

return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task ReferenceLoopCtor()
{
var source = TestSourceBuilder.Mapping(
"System.Linq.IQueryable<A>",
"System.Linq.IQueryable<B>",
"class A { public A? Parent { get; set; } public int IntValue { get; set; } }",
"class B { public B(B? parent) {} public int IntValue { get; set; } }"
);

return TestHelper.VerifyGenerator(source);
}
// [Fact]
// public Task ClassToClassWithUserImplemented()
// {
// var source = TestSourceBuilder.MapperWithBodyAndTypes(
// """
// private partial System.Linq.IQueryable<B> Map(System.Linq.IQueryable<A> source);
//
// private D MapToD(C v) => new D { Value = v.Value + "-mapped" };
// """,
// "class A { public string StringValue { get; set; } public C NestedValue { get; set; } }",
// "class B { public string StringValue { get; set; } public D NestedValue { get; set; } }",
// "class C { public string Value { get; set; } }",
// "class D { public string Value { get; set; } }"
// );
// }

[Fact]
public Task CtorShouldSkipUnmatchedOptionalParameters()
Expand Down

0 comments on commit c7ccc75

Please sign in to comment.