-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for enum from/to string mapping in MapEnumValueAttribute
Added support for enum from/to string explicit mappings using MapEnumValueAttribute --------- Co-authored-by: latonz <[email protected]>
- Loading branch information
Showing
17 changed files
with
670 additions
and
148 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
88 changes: 88 additions & 0 deletions
88
....Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/EnumMappingDiagnosticReporter.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,88 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Riok.Mapperly.Abstractions; | ||
using Riok.Mapperly.Diagnostics; | ||
|
||
namespace Riok.Mapperly.Descriptors.MappingBodyBuilders.BuilderContext; | ||
|
||
internal static class EnumMappingDiagnosticReporter | ||
{ | ||
public static void AddUnmatchedSourceIgnoredMembers(MappingBuilderContext ctx, ISet<IFieldSymbol> ignoredSourceMembers) | ||
{ | ||
var sourceFields = ctx.SymbolAccessor.GetAllFields(ctx.Source).ToHashSet(); | ||
var unmatchedSourceMembers = ignoredSourceMembers.Where(m => !sourceFields.Contains(m)); | ||
|
||
foreach (var member in unmatchedSourceMembers) | ||
{ | ||
ctx.ReportDiagnostic( | ||
DiagnosticDescriptors.IgnoredEnumSourceMemberNotFound, | ||
member.Name, | ||
member.ConstantValue!, | ||
ctx.Source, | ||
ctx.Target | ||
); | ||
} | ||
} | ||
|
||
public static void AddUnmatchedTargetIgnoredMembers(MappingBuilderContext ctx, ISet<IFieldSymbol> ignoredTargetMembers) | ||
{ | ||
var targetFields = ctx.SymbolAccessor.GetAllFields(ctx.Target).ToHashSet(); | ||
var unmatchedTargetMembers = ignoredTargetMembers.Where(m => !targetFields.Contains(m)); | ||
|
||
foreach (var member in unmatchedTargetMembers) | ||
{ | ||
ctx.ReportDiagnostic( | ||
DiagnosticDescriptors.IgnoredEnumTargetMemberNotFound, | ||
member.Name, | ||
member.ConstantValue!, | ||
ctx.Source, | ||
ctx.Target | ||
); | ||
} | ||
} | ||
|
||
public static void AddUnmappedTargetMembersDiagnostics( | ||
MappingBuilderContext ctx, | ||
ISet<IFieldSymbol> mappings, | ||
IEnumerable<IFieldSymbol> targetMembers | ||
) | ||
{ | ||
if (!ctx.Configuration.Enum.RequiredMappingStrategy.HasFlag(RequiredMappingStrategy.Target)) | ||
return; | ||
|
||
var missingTargetMembers = targetMembers.Where(field => | ||
!mappings.Contains(field) && ctx.Configuration.Enum.FallbackValue?.ConstantValue?.Equals(field.ConstantValue) != true | ||
); | ||
foreach (var member in missingTargetMembers) | ||
{ | ||
ctx.ReportDiagnostic( | ||
DiagnosticDescriptors.TargetEnumValueNotMapped, | ||
member.Name, | ||
member.ConstantValue!, | ||
ctx.Target, | ||
ctx.Source | ||
); | ||
} | ||
} | ||
|
||
public static void AddUnmappedSourceMembersDiagnostics( | ||
MappingBuilderContext ctx, | ||
ISet<IFieldSymbol> mappings, | ||
IEnumerable<IFieldSymbol> sourceMembers | ||
) | ||
{ | ||
if (!ctx.Configuration.Enum.RequiredMappingStrategy.HasFlag(RequiredMappingStrategy.Source)) | ||
return; | ||
|
||
var missingSourceMembers = sourceMembers.Where(field => !mappings.Contains(field)); | ||
foreach (var member in missingSourceMembers) | ||
{ | ||
ctx.ReportDiagnostic( | ||
DiagnosticDescriptors.SourceEnumValueNotMapped, | ||
member.Name, | ||
member.ConstantValue!, | ||
ctx.Source, | ||
ctx.Target | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.