Skip to content

Commit

Permalink
Restored support for System.Guid.
Browse files Browse the repository at this point in the history
Added functions to convert to System.Guid as either variant 1 or variant 2 representations.
  • Loading branch information
amsga committed May 31, 2022
1 parent 7ae8822 commit 7d23388
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
dotnet: [ '3.1.x', '6.0.x' ]
dotnet: [ '3.1.x' ]
name: .NET ${{ matrix.dotnet }}

steps:
Expand All @@ -25,6 +25,6 @@ jobs:
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build ./UUIDUtil --no-restore
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test ./UUIDUtil --no-build --verbosity normal
run: dotnet test --no-restore --verbosity normal
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [v1.1.0] - 2022-05-31
[v1.1.0](https://github.com/TensionDev/UUIDUtil/releases/tag/v1.1.0)

### Added
- Added support to convert System.Guid to TensionDev.UUID.Uuid and vice-versa.


## [v1.0.0] - 2022-03-26
[v1.0.0](https://github.com/TensionDev/UUIDUtil/releases/tag/v1.0.0)

Expand Down
6 changes: 3 additions & 3 deletions UUIDUtil/UUIDUtil.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageId>TensionDev.UUID</PackageId>
<Version>1.0.0</Version>
<Version>1.1.0</Version>
<Authors>TensionDev amsga</Authors>
<Company>TensionDev</Company>
<Product>TensionDev.UUID</Product>
Expand All @@ -20,8 +20,8 @@
<PackageTags>UUID GUID</PackageTags>
<PackageReleaseNotes>Release with UUID / GUID Version 1, Version 3, Version 4 and Version 5.</PackageReleaseNotes>
<NeutralLanguage>en-SG</NeutralLanguage>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<FileVersion>1.1.0.0</FileVersion>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
Expand Down
39 changes: 39 additions & 0 deletions UUIDUtil/Uuid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,45 @@ public byte[] ToByteArray()
return vs;
}

/// <summary>
/// Returns the System.Guid equivalent of this instance.
/// </summary>
/// <returns>A System.Guid object.</returns>
public Guid ToGuid()
{
return new Guid(ToString());
}

/// <summary>
/// Return the Variant 2 version in System.Guid.
/// </summary>
/// <returns>A System.Guid object.</returns>
public Guid ToVariant2()
{
byte newClockSeq = (byte)(_clock_seq_hi_and_reserved & 0x1F);
newClockSeq = (byte)(newClockSeq | 0xC0);
Uuid variant2 = new Uuid(this.ToByteArray());

variant2._clock_seq_hi_and_reserved = newClockSeq;

return variant2.ToGuid();
}

/// <summary>
/// Returns the Variant 1 version in TensionDev.UUID.Uuid.
/// </summary>
/// <param name="guid">The System.Guid object to convert.</param>
/// <returns>A TensionDev.UUID.Uuid object.</returns>
public static Uuid ToVariant1(Guid guid)
{
Uuid variant1 = new Uuid(guid.ToString());
byte newClockSeq = (byte)(variant1._clock_seq_hi_and_reserved & 0x3F);
newClockSeq = (byte)(newClockSeq | 0x80);
variant1._clock_seq_hi_and_reserved = newClockSeq;

return variant1;
}

/// <summary>
/// Returns a string representation of the value of this instance as per RFC 4122 Section 3.
/// </summary>
Expand Down
30 changes: 30 additions & 0 deletions XUnitTestProjectUUID/UnitTestUuid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,36 @@ public void TestToByteArray3()
Assert.Equal(expected, actual);
}

[Fact]
public void TestToGuid()
{
Guid expected = new Guid("7d444840-9dc0-11d1-b245-5ffdce74fad2");
TensionDev.UUID.Uuid uuid = new TensionDev.UUID.Uuid("7d444840-9dc0-11d1-b245-5ffdce74fad2");

Guid actual = uuid.ToGuid();
Assert.Equal(expected, actual);
}

[Fact]
public void TestToVariant2()
{
Guid expected = new Guid("7d444840-9dc0-11d1-d245-5ffdce74fad2");
TensionDev.UUID.Uuid uuid = new TensionDev.UUID.Uuid("7d444840-9dc0-11d1-b245-5ffdce74fad2");

Guid actual = uuid.ToVariant2();
Assert.Equal(expected, actual);
}

[Fact]
public void TestToVariant1()
{
TensionDev.UUID.Uuid expected = new TensionDev.UUID.Uuid("7d444840-9dc0-11d1-9245-5ffdce74fad2");
Guid guid = new Guid("7d444840-9dc0-11d1-d245-5ffdce74fad2");

TensionDev.UUID.Uuid actual = TensionDev.UUID.Uuid.ToVariant1(guid);
Assert.Equal(expected, actual);
}

[Fact]
public void TestToString1()
{
Expand Down

0 comments on commit 7d23388

Please sign in to comment.