-
-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support capacity settable members (#1526)
- Loading branch information
Showing
19 changed files
with
249 additions
and
109 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/Riok.Mapperly/Descriptors/Enumerables/Capacity/CapacityMemberSetter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Riok.Mapperly.Symbols.Members; | ||
|
||
namespace Riok.Mapperly.Descriptors.Enumerables.Capacity; | ||
|
||
internal class CapacityMemberSetter(IMappableMember targetCapacityMember, IMemberSetter setter) : ICapacityMemberSetter | ||
{ | ||
public bool SupportsCoalesceAssignment => setter.SupportsCoalesceAssignment; | ||
|
||
public IMappableMember TargetCapacity => targetCapacityMember; | ||
|
||
public ExpressionSyntax BuildAssignment( | ||
ExpressionSyntax? baseAccess, | ||
ExpressionSyntax valueToAssign, | ||
bool coalesceAssignment = false | ||
) => setter.BuildAssignment(baseAccess, valueToAssign, coalesceAssignment); | ||
|
||
public static ICapacityMemberSetter Build(MappingBuilderContext ctx, IMappableMember member) => | ||
new CapacityMemberSetter(member, member.BuildSetter(ctx.UnsafeAccessorContext)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/Riok.Mapperly/Descriptors/Enumerables/Capacity/EnsureCapacityMethodSetter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Riok.Mapperly.Symbols.Members; | ||
using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; | ||
|
||
namespace Riok.Mapperly.Descriptors.Enumerables.Capacity; | ||
|
||
/// <summary> | ||
/// Ensures the capacity of a collection by calling `EnsureCapacity(int)` | ||
/// </summary> | ||
internal class EnsureCapacityMethodSetter : ICapacityMemberSetter | ||
{ | ||
public static readonly EnsureCapacityMethodSetter Instance = new(); | ||
|
||
public const string EnsureCapacityMethodName = "EnsureCapacity"; | ||
|
||
private EnsureCapacityMethodSetter() { } | ||
|
||
public bool SupportsCoalesceAssignment => false; | ||
|
||
public IMappableMember? TargetCapacity => null; | ||
|
||
public ExpressionSyntax BuildAssignment(ExpressionSyntax? baseAccess, ExpressionSyntax valueToAssign, bool coalesceAssignment = false) | ||
{ | ||
if (baseAccess == null) | ||
throw new ArgumentNullException(nameof(baseAccess)); | ||
|
||
return InvocationWithoutIndention(MemberAccess(baseAccess, EnsureCapacityMethodName), valueToAssign); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Riok.Mapperly/Descriptors/Enumerables/Capacity/ICapacityMemberSetter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Riok.Mapperly.Symbols.Members; | ||
|
||
namespace Riok.Mapperly.Descriptors.Enumerables.Capacity; | ||
|
||
/// <summary> | ||
/// Sets the capacity of a collection to the provided count. | ||
/// </summary> | ||
public interface ICapacityMemberSetter : IMemberSetter | ||
{ | ||
IMappableMember? TargetCapacity { get; } | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Riok.Mapperly/Descriptors/Enumerables/Capacity/ICapacitySetter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Riok.Mapperly.Descriptors.Mappings; | ||
using Riok.Mapperly.Symbols.Members; | ||
|
||
namespace Riok.Mapperly.Descriptors.Enumerables.Capacity; | ||
|
||
/// <summary> | ||
/// Sets the capacity of a collection to the calculated count. | ||
/// </summary> | ||
public interface ICapacitySetter | ||
{ | ||
IMappableMember? CapacityTargetMember { get; } | ||
|
||
StatementSyntax Build(TypeMappingBuildContext ctx, ExpressionSyntax target); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/Riok.Mapperly/Descriptors/Enumerables/Capacity/SimpleCapacitySetter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Riok.Mapperly.Descriptors.Mappings; | ||
using Riok.Mapperly.Symbols.Members; | ||
using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper; | ||
|
||
namespace Riok.Mapperly.Descriptors.Enumerables.Capacity; | ||
|
||
/// <summary> | ||
/// Represents setting the capacity on a collection where both the source and targets counts are accessible. | ||
/// </summary> | ||
/// <remarks> | ||
/// <code> | ||
/// target.EnsureCapacity(source.Length + target.Count); | ||
/// // or | ||
/// target.Capacity = source.Length + target.Count; | ||
/// </code> | ||
/// </remarks> | ||
public class SimpleCapacitySetter(ICapacityMemberSetter capacitySetter, IMemberGetter? targetAccessor, IMemberGetter sourceAccessor) | ||
: ICapacitySetter | ||
{ | ||
public IMappableMember? CapacityTargetMember => capacitySetter.TargetCapacity; | ||
|
||
public StatementSyntax Build(TypeMappingBuildContext ctx, ExpressionSyntax target) | ||
{ | ||
var count = sourceAccessor.BuildAccess(ctx.Source); | ||
if (targetAccessor != null) | ||
{ | ||
count = Add(count, targetAccessor.BuildAccess(target)); | ||
} | ||
|
||
return ctx.SyntaxFactory.ExpressionStatement(capacitySetter.BuildAssignment(target, count)); | ||
} | ||
} |
29 changes: 0 additions & 29 deletions
29
src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityInfo.cs
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
src/Riok.Mapperly/Descriptors/Enumerables/EnsureCapacity/EnsureCapacityMember.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.