-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add option to specify the format provider or culture of a prope…
…rty (#929)
- Loading branch information
Showing
32 changed files
with
646 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
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,16 @@ | ||
namespace Riok.Mapperly.Abstractions; | ||
|
||
/// <summary> | ||
/// Marks a property or field as a format provider. | ||
/// A format provider needs to be of a type which implements <see cref="IFormatProvider"/> and needs to have a getter. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] | ||
public sealed class FormatProviderAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// If set to true, this format provider acts as a default format provider | ||
/// and is used for all <see cref="IFormattable"/> conversions without an explicit <see cref="MapPropertyAttribute.FormatProvider"/> set. | ||
/// Only one <see cref="FormatProviderAttribute"/> in a Mapper can be set to <c>true</c>. | ||
/// </summary> | ||
public bool Default { 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
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
5 changes: 5 additions & 0 deletions
5
src/Riok.Mapperly/Descriptors/FormatProviders/FormatProvider.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,5 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Riok.Mapperly.Descriptors.FormatProviders; | ||
|
||
public record FormatProvider(string Name, bool Default, ISymbol Symbol); |
45 changes: 45 additions & 0 deletions
45
src/Riok.Mapperly/Descriptors/FormatProviders/FormatProviderBuilder.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,45 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Riok.Mapperly.Abstractions; | ||
using Riok.Mapperly.Diagnostics; | ||
using Riok.Mapperly.Helpers; | ||
using Riok.Mapperly.Symbols; | ||
|
||
namespace Riok.Mapperly.Descriptors.FormatProviders; | ||
|
||
public static class FormatProviderBuilder | ||
{ | ||
public static FormatProviderCollection ExtractFormatProviders(SimpleMappingBuilderContext ctx, ITypeSymbol mapperSymbol) | ||
{ | ||
var formatProviders = mapperSymbol | ||
.GetMembers() | ||
.Where(x => ctx.SymbolAccessor.HasAttribute<FormatProviderAttribute>(x)) | ||
.Select(x => BuildFormatProvider(ctx, x)) | ||
.WhereNotNull() | ||
.ToList(); | ||
|
||
var defaultFormatProviderCandidates = formatProviders.Where(x => x.Default).Take(2).ToList(); | ||
if (defaultFormatProviderCandidates.Count > 1) | ||
{ | ||
ctx.ReportDiagnostic(DiagnosticDescriptors.MultipleDefaultFormatProviders, defaultFormatProviderCandidates[1].Symbol); | ||
} | ||
|
||
var formatProvidersByName = formatProviders.GroupBy(x => x.Name).ToDictionary(x => x.Key, x => x.Single()); | ||
return new FormatProviderCollection(formatProvidersByName, defaultFormatProviderCandidates.FirstOrDefault()); | ||
} | ||
|
||
private static FormatProvider? BuildFormatProvider(SimpleMappingBuilderContext ctx, ISymbol symbol) | ||
{ | ||
var memberSymbol = MappableMember.Create(ctx.SymbolAccessor, symbol); | ||
if (memberSymbol == null) | ||
return null; | ||
|
||
if (!memberSymbol.CanGet || symbol.IsStatic != ctx.Static || !memberSymbol.Type.Implements(ctx.Types.Get<IFormatProvider>())) | ||
{ | ||
ctx.ReportDiagnostic(DiagnosticDescriptors.InvalidFormatProviderSignature, symbol, symbol.Name); | ||
return null; | ||
} | ||
|
||
var attribute = ctx.AttributeAccessor.AccessSingle<FormatProviderAttribute>(symbol); | ||
return new FormatProvider(symbol.Name, attribute.Default, symbol); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Riok.Mapperly/Descriptors/FormatProviders/FormatProviderCollection.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,12 @@ | ||
namespace Riok.Mapperly.Descriptors.FormatProviders; | ||
|
||
public class FormatProviderCollection( | ||
IReadOnlyDictionary<string, FormatProvider> formatProvidersByName, | ||
FormatProvider? defaultFormatProvider | ||
) | ||
{ | ||
public FormatProvider? Get(string? reference) | ||
{ | ||
return reference == null ? defaultFormatProvider : formatProvidersByName.GetValueOrDefault(reference); | ||
} | ||
} |
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.