-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: intorduce MapValueAttribute to map constant values and method p…
…rovided values
- Loading branch information
Showing
57 changed files
with
1,796 additions
and
53 deletions.
There are no files selected for viewing
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
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,48 @@ | ||
--- | ||
sidebar_position: 4 | ||
description: Map constant and generated values | ||
--- | ||
|
||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
|
||
# Constant and generated values | ||
|
||
## Constant values | ||
|
||
To map a constant value to a member or a constructor parameter `MapValue` can be used. | ||
Make sure the value exactly matches the target type. | ||
|
||
<Tabs> | ||
<TabItem value="declaration" label="Declaration" default> | ||
```csharp [MapValue(nameof(CarDto.SourceSystem), "C1")] partial CarDto | ||
Map(Car car); ``` | ||
</TabItem> | ||
<TabItem value="generated" label="Generated code" default> | ||
```csharp target.SourceSystem = "C1"; ``` | ||
</TabItem> | ||
</Tabs> | ||
|
||
## Method generated values | ||
|
||
To map a method generated value to a member or a constructor parameter `MapValue` can be used. | ||
Make sure the return type exactly matches the target type. | ||
|
||
<Tabs> | ||
<TabItem value="declaration" label="Declaration" default> | ||
```csharp | ||
[MapValue(nameof(CarDto.SourceSystem), nameof(GetSourceSystem))] | ||
partial CarDto Map(Car car); | ||
|
||
string GetSourceSystem() => "C1"; | ||
``` | ||
</TabItem> | ||
<TabItem value="generated" label="Generated code" default> | ||
```csharp | ||
target.SourceSystem = GetSourceSystem(); | ||
``` | ||
</TabItem> | ||
|
||
</Tabs> | ||
|
||
This also works for constructor parameters. |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 14 | ||
sidebar_position: 15 | ||
description: A list of conversions supported by Mapperly | ||
--- | ||
|
||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 6 | ||
sidebar_position: 7 | ||
description: Constructor mappings | ||
--- | ||
|
||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 9 | ||
sidebar_position: 10 | ||
description: Map derived types and interfaces | ||
--- | ||
|
||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 3 | ||
sidebar_position: 4 | ||
description: Map enums | ||
--- | ||
|
||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 8 | ||
sidebar_position: 9 | ||
description: Map to an existing target object | ||
--- | ||
|
||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 2 | ||
sidebar_position: 3 | ||
description: Flatten properties and fields | ||
--- | ||
|
||
|
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 10 | ||
sidebar_position: 11 | ||
description: Create a generic mapping method | ||
--- | ||
|
||
|
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 12 | ||
sidebar_position: 13 | ||
description: Private member mapping | ||
--- | ||
|
||
|
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 11 | ||
sidebar_position: 12 | ||
description: Use reference handling to handle reference loops | ||
--- | ||
|
||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 5 | ||
sidebar_position: 6 | ||
description: Manually implement mappings | ||
--- | ||
|
||
|
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,73 @@ | ||
using System.Diagnostics; | ||
|
||
namespace Riok.Mapperly.Abstractions; | ||
|
||
/// <summary> | ||
/// Specifies a constant value mapping. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] | ||
[Conditional("MAPPERLY_ABSTRACTIONS_SCOPE_RUNTIME")] | ||
public sealed class MapValueAttribute : Attribute | ||
{ | ||
private const string PropertyAccessSeparatorStr = "."; | ||
private const char PropertyAccessSeparator = '.'; | ||
|
||
/// <summary> | ||
/// Maps a constant value to a target member. | ||
/// </summary> | ||
/// <param name="target">The target member path.</param> | ||
/// <param name="value">The value to assign to the <paramref name="target"/>, needs to be of the same type as the <paramref name="target"/>.</param> | ||
public MapValueAttribute(string target, object? value) | ||
: this(target.Split(PropertyAccessSeparator), value) { } | ||
|
||
/// <summary> | ||
/// Maps a constant value to a target member. | ||
/// </summary> | ||
/// <param name="target">The target member path.</param> | ||
/// <param name="value">The value to assign to the <paramref name="target"/>, needs to be of the same type as the <paramref name="target"/>.</param> | ||
public MapValueAttribute(string[] target, object? value) | ||
{ | ||
Target = target; | ||
Value = value; | ||
} | ||
|
||
/// <summary> | ||
/// Maps a method generated value to a target member. | ||
/// Requires the usage of the <see cref="Use"/> property. | ||
/// </summary> | ||
/// <param name="target">The target member path.</param> | ||
public MapValueAttribute(string target) | ||
: this(target, null) { } | ||
|
||
/// <summary> | ||
/// Maps a method generated value to a target member. | ||
/// Requires the usage of the <see cref="Use"/> property. | ||
/// </summary> | ||
/// <param name="target">The target member path, the usage of nameof is encouraged.</param> | ||
public MapValueAttribute(string[] target) | ||
: this(target, null) { } | ||
|
||
/// <summary> | ||
/// Gets the name of the target property. | ||
/// </summary> | ||
public IReadOnlyCollection<string> Target { get; } | ||
|
||
/// <summary> | ||
/// Gets the full name of the target property path. | ||
/// </summary> | ||
public string TargetFullName => string.Join(PropertyAccessSeparatorStr, Target); | ||
|
||
/// <summary> | ||
/// Gets the value to be assigned to <see cref="Target"/>. | ||
/// </summary> | ||
public object? Value { get; } | ||
|
||
/// <summary> | ||
/// Gets or sets the method name of the method which generates the value to be assigned to <see cref="Target"/>. | ||
/// Either this property or <see cref="Value"/> needs to be set. | ||
/// The return type of the referenced method must exactly match the type of <see cref="Target"/> | ||
/// and needs to be parameterless. | ||
/// The usage of nameof is encouraged. | ||
/// </summary> | ||
public string? Use { get; set; } | ||
} |
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
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
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
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,12 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Riok.Mapperly.Configuration; | ||
|
||
/// <summary> | ||
/// Represents an attribute value provided by the user. | ||
/// Allows access to the intepreted value as well as the source syntax. | ||
/// </summary> | ||
/// <param name="ConstantValue">The interpreted compile-time constant value.</param> | ||
/// <param name="Expression">The syntax as written by the user.</param> | ||
public readonly record struct AttributeValue(TypedConstant ConstantValue, ExpressionSyntax Expression); |
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
25 changes: 25 additions & 0 deletions
25
src/Riok.Mapperly/Configuration/MemberValueMappingConfiguration.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,25 @@ | ||
using System.Diagnostics; | ||
|
||
namespace Riok.Mapperly.Configuration; | ||
|
||
[DebuggerDisplay("{Target}: {DescribeValue()}")] | ||
public record MemberValueMappingConfiguration(StringMemberPath Target, AttributeValue? Value) : HasSyntaxReference | ||
{ | ||
/// <summary> | ||
/// Constructor used by <see cref="AttributeDataAccessor"/>. | ||
/// </summary> | ||
public MemberValueMappingConfiguration(StringMemberPath target) | ||
: this(target, null) { } | ||
|
||
public string? Use { get; set; } | ||
|
||
public bool IsValid => Use != null ^ Value != null; | ||
|
||
public string DescribeValue() | ||
{ | ||
if (Use != null) | ||
return Use + "()"; | ||
|
||
return Value?.Expression.ToFullString() ?? string.Empty; | ||
} | ||
} |
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
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
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
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
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.