[PTRun][ValueGenerator]Add support for UUIDv7 (#35757)

* add Run support for UUIDv7 generation

* simplify comments and maybe satisfy spell check

* fix endianess

* prefer stack allocation for temporary fixed-size buffer

* perhaps the async test caused the pipeline to hang

* switch to .NET 9 BCL implementation of UUIDv7

* add UUIDv7 to input query suggestions + update exception messages to include v7

* simplify Guid description switch + update devdocs
This commit is contained in:
Frederik Höft
2024-11-28 16:52:16 +01:00
committed by GitHub
parent 3dc491339a
commit fc29fc7426
9 changed files with 98 additions and 62 deletions

View File

@@ -52,6 +52,11 @@ namespace Community.PowerToys.Run.Plugin.ValueGenerator.GUID
return V3AndV5(uuidNamespace, uuidName, 5);
}
public static Guid V7()
{
return Guid.CreateVersion7();
}
private static Guid V3AndV5(Guid uuidNamespace, string uuidName, short version)
{
byte[] namespaceBytes = uuidNamespace.ToByteArray();

View File

@@ -4,7 +4,6 @@
using System;
using System.Security.Cryptography;
using Wox.Plugin.Logger;
namespace Community.PowerToys.Run.Plugin.ValueGenerator.GUID
@@ -19,34 +18,15 @@ namespace Community.PowerToys.Run.Plugin.ValueGenerator.GUID
private int Version { get; set; }
public string Description
public string Description => Version switch
{
get
{
switch (Version)
{
case 1:
return "Version 1: Time base GUID";
case 3:
case 5:
string hashAlgorithm;
if (Version == 3)
{
hashAlgorithm = HashAlgorithmName.MD5.ToString();
}
else
{
hashAlgorithm = HashAlgorithmName.SHA1.ToString();
}
return $"Version {Version} ({hashAlgorithm}): Namespace and name based GUID.";
case 4:
return "Version 4: Randomly generated GUID";
default:
return string.Empty;
}
}
}
1 => "Version 1: Time base GUID",
3 => $"Version 3 ({HashAlgorithmName.MD5}): Namespace and name based GUID.",
4 => "Version 4: Randomly generated GUID",
5 => $"Version 5 ({HashAlgorithmName.SHA1}): Namespace and name based GUID.",
7 => "Version 7: Time-ordered randomly generated GUID",
_ => string.Empty,
};
private Guid? GuidNamespace { get; set; }
@@ -60,20 +40,19 @@ namespace Community.PowerToys.Run.Plugin.ValueGenerator.GUID
{
Version = version;
if (Version < 1 || Version > 5 || Version == 2)
if (Version is < 1 or > 7 or 2 or 6)
{
throw new ArgumentException("Unsupported GUID version. Supported versions are 1, 3, 4 and 5");
throw new ArgumentException("Unsupported GUID version. Supported versions are 1, 3, 4, 5, and 7");
}
if (version == 3 || version == 5)
if (version is 3 or 5)
{
if (guidNamespace == null)
{
throw new ArgumentNullException(null, NullNamespaceError);
}
Guid guid;
if (GUIDGenerator.PredefinedNamespaces.TryGetValue(guidNamespace.ToLowerInvariant(), out guid))
if (GUIDGenerator.PredefinedNamespaces.TryGetValue(guidNamespace.ToLowerInvariant(), out Guid guid))
{
GuidNamespace = guid;
}
@@ -108,20 +87,18 @@ namespace Community.PowerToys.Run.Plugin.ValueGenerator.GUID
IsSuccessful = true;
try
{
switch (Version)
Guid guid = Version switch
{
case 1:
GuidResult = GUIDGenerator.V1();
break;
case 3:
GuidResult = GUIDGenerator.V3(GuidNamespace.Value, GuidName);
break;
case 4:
GuidResult = GUIDGenerator.V4();
break;
case 5:
GuidResult = GUIDGenerator.V5(GuidNamespace.Value, GuidName);
break;
1 => GUIDGenerator.V1(),
3 => GUIDGenerator.V3(GuidNamespace.Value, GuidName),
4 => GUIDGenerator.V4(),
5 => GUIDGenerator.V5(GuidNamespace.Value, GuidName),
7 => GUIDGenerator.V7(),
_ => default,
};
if (guid != default)
{
GuidResult = guid;
}
Result = GuidResult.ToByteArray();