-
Notifications
You must be signed in to change notification settings - Fork 520
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip * WIP * fixes * wip * wip * wip * Added command info for custom commands * Added command count * Moved StreamProvider to common & added RespCommandsInfoProvider * Error handling, logging + bugfix * Added some tests * bugfixes * Added RespSerializableBase * wip * wip * Adding logic to CommandInfoUpdater tool * Finishing CommandInfoUpdater app * Moving LightClientRequest to common + moving CommandInfoUpdater under playground * running dotnet format * Adding some comments * some cleanup * Added comments to CommandInfoUpdater * dotnet format * Small bugfixes, added more supported commands * Some cleanup & small changes * Addressing comments + fixing some namings * Added test to verify coverage of command info, added some more missing commands info * Added WATCHOS, WATCHMS as internal commands * dotnet format * removing redis references from CommandInfoUpdater, adding "force" option * Updating RespCommandsInfo.json * Addressing comments * formatting * Update playground/CommandInfoUpdater/SupportedCommand.cs Co-authored-by: Lukas Maas <[email protected]> * Update libs/server/Resp/RespCommandInfoParser.cs Co-authored-by: Lukas Maas <[email protected]> * Clean up singleton comments in RespCommandInfoParser.cs * Fixing naming + checking output from calls to RespReadUtils * Some small fixes * Update test/Garnet.test/RespCommandTests.cs Co-authored-by: Lukas Maas <[email protected]> * Update playground/CommandInfoUpdater/CommandInfoUpdater.cs Co-authored-by: Lukas Maas <[email protected]> * Update playground/CommandInfoUpdater/CommandInfoUpdater.cs Co-authored-by: Lukas Maas <[email protected]> * Update playground/CommandInfoUpdater/CommandInfoUpdater.cs Co-authored-by: Lukas Maas <[email protected]> * Disabling Nullable in CommandInfoUpdater.csproj + Fixing thread-safety issue in RespCommandsInfo initialization * Adding documentation * Docs fix --------- Co-authored-by: Lukas Maas <[email protected]> Co-authored-by: Badrish Chandramouli <[email protected]>
- Loading branch information
1 parent
ed4b9ab
commit 7f1184d
Showing
52 changed files
with
8,435 additions
and
433 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,113 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
|
||
namespace Garnet.common | ||
{ | ||
/// <summary> | ||
/// Utilities for enums | ||
/// </summary> | ||
public static class EnumUtils | ||
{ | ||
private static readonly Dictionary<Type, IDictionary<string, string>> EnumNameToDescriptionCache = new(); | ||
private static readonly Dictionary<Type, IDictionary<string, List<string>>> EnumDescriptionToNameCache = new(); | ||
|
||
/// <summary> | ||
/// Gets a mapping between an enum's string value to its description, for each of the enum's values | ||
/// </summary> | ||
/// <typeparam name="T">Enum type</typeparam> | ||
/// <returns>A dictionary mapping between the enum's string value to its description</returns> | ||
public static IDictionary<string, string> GetEnumNameToDescription<T>() where T : Enum | ||
{ | ||
// Check if mapping is already in the cache. If not, add it to the cache. | ||
if (!EnumNameToDescriptionCache.ContainsKey(typeof(T))) | ||
AddTypeToCache<T>(); | ||
|
||
return EnumNameToDescriptionCache[typeof(T)]; | ||
} | ||
|
||
/// <summary> | ||
/// If enum does not have the 'Flags' attribute, gets an array of size 1 with the description of the enum's value. | ||
/// If no description exists, returns the ToString() value of the input value. | ||
/// If enum has the 'Flags' attribute, gets an array with all the descriptions of the flags which are turned on in the input value. | ||
/// If no description exists, returns the ToString() value of the flag. | ||
/// </summary> | ||
/// <typeparam name="T">Enum type</typeparam> | ||
/// <param name="value">Enum value</param> | ||
/// <returns>Array of descriptions</returns> | ||
public static string[] GetEnumDescriptions<T>(T value) where T : Enum | ||
{ | ||
var nameToDesc = GetEnumNameToDescription<T>(); | ||
return value.ToString().Split(',').Select(f => nameToDesc.ContainsKey(f.Trim()) ? nameToDesc[f.Trim()] : f).ToArray(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets an enum's values based on the description attribute | ||
/// </summary> | ||
/// <typeparam name="T">Enum type</typeparam> | ||
/// <param name="strVal">Enum description</param> | ||
/// <param name="vals">Enum values</param> | ||
/// <returns>True if matched more than one value successfully</returns> | ||
public static bool TryParseEnumsFromDescription<T>(string strVal, out IEnumerable<T> vals) where T : struct, Enum | ||
{ | ||
vals = new List<T>(); | ||
|
||
if (!EnumDescriptionToNameCache.ContainsKey(typeof(T))) | ||
AddTypeToCache<T>(); | ||
|
||
if (!EnumDescriptionToNameCache[typeof(T)].ContainsKey(strVal)) | ||
return false; | ||
|
||
foreach (var enumName in EnumDescriptionToNameCache[typeof(T)][strVal]) | ||
{ | ||
if (Enum.TryParse(enumName, out T enumVal)) | ||
{ | ||
((List<T>)vals).Add(enumVal); | ||
} | ||
} | ||
|
||
return ((List<T>)vals).Count > 0; | ||
} | ||
|
||
/// <summary> | ||
/// Gets an enum's value based on its description attribute | ||
/// If more than one values match the same description, returns the first one | ||
/// </summary> | ||
/// <typeparam name="T">Enum type</typeparam> | ||
/// <param name="strVal">Enum description</param> | ||
/// <param name="val">Enum value</param> | ||
/// <returns>True if successful</returns> | ||
public static bool TryParseEnumFromDescription<T>(string strVal, out T val) where T : struct, Enum | ||
{ | ||
var isSuccessful = TryParseEnumsFromDescription(strVal, out IEnumerable<T> vals); | ||
val = isSuccessful ? vals.First() : default; | ||
return isSuccessful; | ||
} | ||
|
||
|
||
private static void AddTypeToCache<T>() | ||
{ | ||
var valToDesc = new Dictionary<string, string>(); | ||
var descToVals = new Dictionary<string, List<string>>(); | ||
|
||
foreach (var flagFieldInfo in typeof(T).GetFields()) | ||
{ | ||
var descAttr = (DescriptionAttribute)flagFieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault(); | ||
if (descAttr != null) | ||
{ | ||
valToDesc.Add(flagFieldInfo.Name, descAttr.Description); | ||
if (!descToVals.ContainsKey(descAttr.Description)) | ||
descToVals.Add(descAttr.Description, new List<string>()); | ||
descToVals[descAttr.Description].Add(flagFieldInfo.Name); | ||
} | ||
} | ||
|
||
EnumNameToDescriptionCache.Add(typeof(T), valToDesc); | ||
EnumDescriptionToNameCache.Add(typeof(T), descToVals); | ||
} | ||
} | ||
} |
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.