Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(enum): add ignore case option for enum mappings
- Loading branch information
Showing
16 changed files
with
276 additions
and
72 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
18 changes: 18 additions & 0 deletions
18
src/Riok.Mapperly/Descriptors/MappingBuilder/EnumToStringMappingBuilder.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,18 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Riok.Mapperly.Descriptors.TypeMappings; | ||
using Riok.Mapperly.Helpers; | ||
|
||
namespace Riok.Mapperly.Descriptors.MappingBuilder; | ||
|
||
public static class EnumToStringMappingBuilder | ||
{ | ||
public static TypeMapping? TryBuildMapping(MappingBuilderContext ctx) | ||
{ | ||
if (ctx.Target.SpecialType != SpecialType.System_String || !ctx.Source.IsEnum()) | ||
return null; | ||
|
||
// to string => use an optimized method of Enum.ToString which would use slow reflection | ||
// use Enum.ToString as fallback (for ex. for flags) | ||
return new EnumToStringMapping(ctx.Source, ctx.Target, ctx.Source.GetMembers().OfType<IFieldSymbol>()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Riok.Mapperly/Descriptors/MappingBuilder/StringToEnumMappingBuilder.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,22 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Riok.Mapperly.Abstractions; | ||
using Riok.Mapperly.Descriptors.TypeMappings; | ||
using Riok.Mapperly.Helpers; | ||
|
||
namespace Riok.Mapperly.Descriptors.MappingBuilder; | ||
|
||
public static class StringToEnumMappingBuilder | ||
{ | ||
public static TypeMapping? TryBuildMapping(MappingBuilderContext ctx) | ||
{ | ||
if (ctx.Source.SpecialType != SpecialType.System_String || !ctx.Target.IsEnum()) | ||
return null; | ||
|
||
// from string => use an optimized method of Enum.Parse which would use slow reflection | ||
// however we currently don't support all features of Enum.Parse yet (ex. flags) | ||
// therefore we use Enum.Parse as fallback. | ||
var members = ctx.Target.GetMembers().OfType<IFieldSymbol>(); | ||
var config = ctx.GetConfigurationOrDefault<MapEnumAttribute>(); | ||
return new EnumFromStringMapping(ctx.Source, ctx.Target, members, config.IgnoreCase); | ||
} | ||
} |
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.