forked from riok/mapperly
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 riok#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
81 changed files
with
1,601 additions
and
328 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
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,92 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
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(); | ||
|
||
// disable nullability since older target frameworks | ||
// may not support the NotNullWhenAttribute | ||
#nullable disable | ||
/// <inheritdoc cref="IReferenceHandler.TryGetReference{TSource,TTarget}"/> | ||
public bool TryGetReference<TSource, TTarget>(TSource source, out TTarget target) | ||
where TSource : notnull | ||
where TTarget : notnull | ||
{ | ||
var refHolder = GetReferenceHolder<TSource, TTarget>(); | ||
return refHolder.TryGetRef(source, out target); | ||
} | ||
|
||
#nullable enable | ||
|
||
/// <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); | ||
|
||
// disable nullability since older target frameworks | ||
// may not support the NotNullWhenAttribute | ||
#nullable disable | ||
public bool TryGetRef<TSource, TTarget>(TSource source, 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; | ||
} | ||
|
||
#nullable enable | ||
|
||
public void SetRef<TSource, TTarget>(TSource source, TTarget target) | ||
where TSource : notnull | ||
where TTarget : notnull | ||
{ | ||
_references[source] = target; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.