-
-
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: Remove runtime dependency on Riok.Mapperly.Abstractions
Creates a new Riok.Mapperly.Templates project with templates. Some Mapperly features may require such a template (currently only the reference handling). If such a template is required by a used Mapperly feature, Mapperly emites the template in a namespace which includes the name of the assembly of the target project. This is done to prevent collisions when using InternalsVisibleTo (see #685). Currently the only themplate is the PreserveReferenceHandler which is emitted if reference handling is enabled for any mapper and the mapper does have mapper method definitions without a reference handling parameter and Mapperly needs to instantiate a IReferenceHandler.
- Loading branch information
Showing
73 changed files
with
1,450 additions
and
307 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
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
63 changes: 0 additions & 63 deletions
63
src/Riok.Mapperly.Abstractions/ReferenceHandling/Internal/PreserveReferenceHandler.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
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,3 @@ | ||
# keep namespaces block scoped, because .NET Framework language version does not support this | ||
[*.cs] | ||
csharp_style_namespace_declarations = block_scoped:warning |
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,83 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.CompilerServices; | ||
using Riok.Mapperly.Abstractions.ReferenceHandling; | ||
|
||
#nullable enable | ||
|
||
namespace Riok.Mapperly.Internal.AssemblyName | ||
{ | ||
/// <summary> | ||
/// A <see cref="IReferenceHandler"/> implementation | ||
/// which returns the same target object instance if encountered the same source object instance. | ||
/// Do not use directly. Should only be used by Mapperly generated code. | ||
/// API surface is not subject to semantic releases and may break in any release. | ||
/// </summary> | ||
internal sealed class PreserveReferenceHandler : IReferenceHandler | ||
{ | ||
private readonly Dictionary<(Type, Type), ReferenceHolder> _referenceHolders = new(); | ||
|
||
/// <inheritdoc cref="IReferenceHandler.TryGetReference{TSource,TTarget}"/> | ||
public bool TryGetReference<TSource, TTarget>(TSource source, [NotNullWhen(true)] out TTarget? target) | ||
where TSource : notnull | ||
where TTarget : notnull | ||
{ | ||
var refHolder = GetReferenceHolder<TSource, TTarget>(); | ||
return refHolder.TryGetRef(source, out target); | ||
} | ||
|
||
/// <inheritdoc cref="IReferenceHandler.SetReference{TSource,TTarget}"/> | ||
public void SetReference<TSource, TTarget>(TSource source, TTarget target) | ||
where TSource : notnull | ||
where TTarget : notnull => GetReferenceHolder<TSource, TTarget>().SetRef(source, target); | ||
|
||
private ReferenceHolder GetReferenceHolder<TSource, TTarget>() | ||
{ | ||
var mapping = (typeof(TSource), typeof(TTarget)); | ||
if (_referenceHolders.TryGetValue(mapping, out var refHolder)) | ||
return refHolder; | ||
|
||
return _referenceHolders[mapping] = new(); | ||
} | ||
|
||
private sealed class ReferenceEqualityComparer<T> : IEqualityComparer<T> | ||
{ | ||
// cannot use System.Collections.Generic.ReferenceEqualityComparer since it is not available in netstandard2.0 | ||
|
||
public static readonly IEqualityComparer<T> Instance = new ReferenceEqualityComparer<T>(); | ||
|
||
private ReferenceEqualityComparer() { } | ||
|
||
bool IEqualityComparer<T>.Equals(T? x, T? y) => ReferenceEquals(x, y); | ||
|
||
int IEqualityComparer<T>.GetHashCode(T obj) => RuntimeHelpers.GetHashCode(obj); | ||
} | ||
|
||
private class ReferenceHolder | ||
{ | ||
private readonly Dictionary<object, object> _references = new(ReferenceEqualityComparer<object>.Instance); | ||
|
||
public bool TryGetRef<TSource, TTarget>(TSource source, [NotNullWhen(true)] out TTarget? target) | ||
where TSource : notnull | ||
where TTarget : notnull | ||
{ | ||
if (_references.TryGetValue(source, out var targetObj)) | ||
{ | ||
target = (TTarget)targetObj; | ||
return true; | ||
} | ||
|
||
target = default; | ||
return false; | ||
} | ||
|
||
public void SetRef<TSource, TTarget>(TSource source, TTarget target) | ||
where TSource : notnull | ||
where TTarget : notnull | ||
{ | ||
_references[source] = target; | ||
} | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Riok.Mapperly.Templates/Riok.Mapperly.Templates.csproj
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 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<RootNamespace>Riok.Mapperly.Internal</RootNamespace> | ||
|
||
<!-- disable nullable and enable it for each file individually, | ||
as the target application may not have nullables enabled --> | ||
<Nullable>disable</Nullable> | ||
|
||
<!-- disable global usings as the target application may not have them enabled --> | ||
<ImplicitUsings>disable</ImplicitUsings> | ||
|
||
<!-- do not use polyfills for the templates, | ||
since we only copy the cs files in the directory and we do not know | ||
whether the polyfills would be required in the target application --> | ||
<MeziantouPolyfill_ExcludedPolyfills>*</MeziantouPolyfill_ExcludedPolyfills> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute"> | ||
<_Parameter1>Riok.Mapperly.Templates.Tests</_Parameter1> | ||
</AssemblyAttribute> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Riok.Mapperly.Abstractions\Riok.Mapperly.Abstractions.csproj"/> | ||
</ItemGroup> | ||
|
||
</Project> |
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.