-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Items can be added to the cache dynamically at any point in time. Also the method can be invoked multiple times. Fixed a bug which would occur if a child library would also use this library. Also updated BidirectionalDict.
- Loading branch information
1 parent
4075eb3
commit fc17a05
Showing
12 changed files
with
186 additions
and
156 deletions.
There are no files selected for viewing
4 changes: 1 addition & 3 deletions
4
...ringyEnums/StringyEnums.Benchmarks/Benchmarks/CommonEnumStringValuesExtensionBenchmark.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
5 changes: 1 addition & 4 deletions
5
src/StringyEnums/StringyEnums.Benchmarks/NoOptimizationConfig.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
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,10 @@ | ||
using System.Reflection; | ||
|
||
namespace StringyEnums.Tests | ||
{ | ||
public static class EnumCoreExtensions | ||
{ | ||
public static void ClearCache() | ||
=> ((dynamic)typeof(EnumCore).GetProperty("RepresentationCache", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null)).Clear(); | ||
} | ||
} |
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,36 @@ | ||
using StringyEnums.Shared.Models; | ||
using Xunit; | ||
|
||
namespace StringyEnums.Tests | ||
{ | ||
public class EnumCoreTests | ||
{ | ||
[Fact] | ||
public void IsEnumToRepresenationEqual() | ||
{ | ||
EnumCoreExtensions.ClearCache(); | ||
|
||
EnumCore.Init(cache => cache.InitWith<TestEnum>()); | ||
|
||
Assert.Equal("Enum 4", TestEnum.EnumFour.GetRepresentation()); | ||
|
||
EnumCore.Init(cache => cache.InitWith<TestUShortEnum>()); | ||
|
||
Assert.Equal("Enum 4", TestUShortEnum.EnumFour.GetRepresentation()); | ||
} | ||
|
||
[Fact] | ||
public void IsRepresenationToEnumEqual() | ||
{ | ||
EnumCoreExtensions.ClearCache(); | ||
|
||
EnumCore.Init(cache => cache.InitWith<TestEnum>()); | ||
|
||
Assert.Equal(TestEnum.EnumFour, "Enum 4".GetEnumFromRepresentation<TestEnum>()); | ||
|
||
EnumCore.Init(cache => cache.InitWith<TestUShortEnum>()); | ||
|
||
Assert.Equal(TestUShortEnum.EnumFour, "Enum 4".GetEnumFromRepresentation<TestUShortEnum>()); | ||
} | ||
} | ||
} |
5 changes: 2 additions & 3 deletions
5
...s/StringyEnums.Test/EnumExtensionTests.cs → .../StringyEnums.Tests/EnumExtensionTests.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
File renamed without changes.
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 |
---|---|---|
@@ -1,94 +1,98 @@ | ||
using BidirectionalDict; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace StringyEnums | ||
{ | ||
/// <summary> | ||
/// Handles the initializing of a Cache | ||
/// </summary> | ||
public class CacheInitializer | ||
{ | ||
private readonly Dictionary<Type, IReadOnlyBiDictionary<uint, string>> _tempCache; | ||
|
||
internal CacheInitializer() | ||
{ | ||
_tempCache = new Dictionary<Type, IReadOnlyBiDictionary<uint, string>>(); | ||
} | ||
|
||
/// <summary> | ||
/// Initializes the cache a given set of assemblies. | ||
/// </summary> | ||
/// <param name="assemblies">Assemblies which contain enums which get added to the cache.</param> | ||
public CacheInitializer InitWith(params Assembly[] assemblies) | ||
{ | ||
foreach (var assembly in assemblies) | ||
{ | ||
var enumTypes = assembly.GetTypes().Where(x => x.IsEnum); | ||
|
||
foreach (var enumType in enumTypes) | ||
{ | ||
InitWith(enumType); | ||
} | ||
} | ||
|
||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes the cache with a given enum. | ||
/// </summary> | ||
/// <typeparam name="TEnum">The enum which should be added to the cache.</typeparam> | ||
public CacheInitializer InitWith<TEnum>() where TEnum : struct, Enum | ||
{ | ||
InitWith(typeof(TEnum)); | ||
|
||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes the cache with a set of given enums. | ||
/// </summary> | ||
/// <param name="enums">The enums which should be added to the cache.</param> | ||
public CacheInitializer InitWith(params Enum[] enums) | ||
{ | ||
foreach (var enumVal in enums) | ||
{ | ||
InitWith(enumVal); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
private void InitWith(Type enumType) | ||
{ | ||
if (!enumType.IsEnum) | ||
throw new ArgumentException("The type provided is not of type enum", nameof(enumType)); | ||
|
||
var fields = enumType.GetFields(); | ||
|
||
var temptDict = new BiDictionary<uint, string>(); | ||
|
||
foreach (var field in fields.Where(x => x.IsStatic)) | ||
{ | ||
var attr = field.GetCustomAttribute<StringRepresentationAttribute>(); | ||
|
||
if (attr is { }) | ||
{ | ||
temptDict.TryAdd(Convert.ToUInt32(field.GetValue(enumType))!, attr.StringRepresentation); | ||
} | ||
} | ||
|
||
if (temptDict.Count > 0) | ||
{ | ||
_tempCache.Add(enumType, temptDict); | ||
} | ||
} | ||
|
||
internal IReadOnlyDictionary<Type, IReadOnlyBiDictionary<uint, string>> CustructCache() | ||
=> new ReadOnlyDictionary<Type, IReadOnlyBiDictionary<uint, string>>(_tempCache.ToDictionary(k => k.Key, v => (IReadOnlyBiDictionary<uint, string>)new ReadOnlyBiDictionary<uint, string>(v.Value))); | ||
} | ||
/// <summary> | ||
/// Handles the initializing of a Cache | ||
/// </summary> | ||
public class CacheInitializer | ||
{ | ||
private readonly Dictionary<Type, IReadOnlyBiDictionary<uint, string>> _tempCache; | ||
|
||
internal CacheInitializer() | ||
{ | ||
_tempCache = new Dictionary<Type, IReadOnlyBiDictionary<uint, string>>(); | ||
} | ||
|
||
/// <summary> | ||
/// Initializes the cache a given set of assemblies. | ||
/// </summary> | ||
/// <param name="assemblies">Assemblies which contain enums which get added to the cache.</param> | ||
public CacheInitializer InitWith(params Assembly[] assemblies) | ||
{ | ||
foreach (var assembly in assemblies) | ||
{ | ||
var enumTypes = assembly.GetTypes().Where(x => x.IsEnum); | ||
|
||
foreach (var enumType in enumTypes) | ||
{ | ||
InitWith(enumType); | ||
} | ||
} | ||
|
||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes the cache with a given enum. | ||
/// </summary> | ||
/// <typeparam name="TEnum">The enum which should be added to the cache.</typeparam> | ||
public CacheInitializer InitWith<TEnum>() where TEnum : struct, Enum | ||
{ | ||
InitWith(typeof(TEnum)); | ||
|
||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes the cache with a set of given enums. | ||
/// </summary> | ||
/// <param name="enums">The enums which should be added to the cache.</param> | ||
public CacheInitializer InitWith(params Enum[] enums) | ||
{ | ||
foreach (var enumVal in enums) | ||
{ | ||
InitWith(enumVal); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
private void InitWith(Type enumType) | ||
{ | ||
if (!enumType.IsEnum) | ||
throw new ArgumentException("The type provided is not of type enum", nameof(enumType)); | ||
|
||
var fields = enumType.GetFields(); | ||
|
||
var temptDict = new BiDictionary<uint, string>(); | ||
|
||
foreach (var field in fields.Where(x => x.IsStatic)) | ||
{ | ||
var attr = field.GetCustomAttribute<StringRepresentationAttribute>(); | ||
|
||
if (attr is { }) | ||
{ | ||
temptDict.TryAdd(Convert.ToUInt32(field.GetValue(enumType))!, attr.StringRepresentation); | ||
} | ||
} | ||
|
||
if (temptDict.Count > 0) | ||
{ | ||
_tempCache.Add(enumType, temptDict); | ||
} | ||
} | ||
|
||
internal IEnumerable<KeyValuePair<Type, IReadOnlyBiDictionary<uint, string>>> CustructCache() | ||
{ | ||
foreach (var cacheItem in _tempCache) | ||
{ | ||
yield return new KeyValuePair<Type, IReadOnlyBiDictionary<uint, string>>(cacheItem.Key, new ReadOnlyBiDictionary<uint, string>(cacheItem.Value)); | ||
} | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.