Skip to content

Commit 2d190b1

Browse files
committed
Changes in Workflows.
Changed implementation based on SonarCloud recommendation.
1 parent f9b3c24 commit 2d190b1

File tree

9 files changed

+93
-33
lines changed

9 files changed

+93
-33
lines changed

.github/workflows/dotnet.yml

+40-4
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,58 @@ on:
99
jobs:
1010
build:
1111

12-
runs-on: ubuntu-latest
12+
runs-on: windows-latest
1313

1414
strategy:
1515
matrix:
1616
dotnet: [ '8.0.x' ]
1717
name: .NET ${{ matrix.dotnet }}
1818

1919
steps:
20-
- uses: actions/checkout@v3
20+
- name: Set up JDK 17
21+
uses: actions/setup-java@v4
22+
with:
23+
java-version: 17
24+
distribution: 'zulu' # Alternative distribution options are available.
25+
- uses: actions/checkout@v4
26+
with:
27+
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
2128
- name: Setup .NET
22-
uses: actions/setup-dotnet@v2
29+
uses: actions/setup-dotnet@v3
2330
with:
2431
dotnet-version: ${{ matrix.dotnet }}
32+
- name: Cache SonarQube Cloud packages
33+
uses: actions/cache@v4
34+
with:
35+
path: ~\sonar\cache
36+
key: ${{ runner.os }}-sonar
37+
restore-keys: ${{ runner.os }}-sonar
38+
- name: Cache SonarQube Cloud scanner
39+
id: cache-sonar-scanner
40+
uses: actions/cache@v4
41+
with:
42+
path: .\.sonar\scanner
43+
key: ${{ runner.os }}-sonar-scanner
44+
restore-keys: ${{ runner.os }}-sonar-scanner
45+
- name: Install SonarQube Cloud scanner
46+
if: steps.cache-sonar-scanner.outputs.cache-hit != 'true'
47+
shell: powershell
48+
run: |
49+
New-Item -Path .\.sonar\scanner -ItemType Directory
50+
dotnet tool update dotnet-sonarscanner --tool-path .\.sonar\scanner
2551
- name: Restore dependencies
2652
run: dotnet restore
53+
- name: SonarCloudPrepare
54+
env:
55+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
56+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
57+
run: .\.sonar\scanner\dotnet-sonarscanner begin /k:"TensionDev_UUIDUtil" /o:"tensiondev" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.scanner.scanAll=false /d:sonar.cs.opencover.reportsPaths=**/coverage.opencover.xml
2758
- name: Build
2859
run: dotnet build --no-restore
2960
- name: Test
30-
run: dotnet test --no-build --verbosity normal
61+
run: dotnet test --no-build --verbosity normal --collect "XPlat Code Coverage;Format=opencover"
62+
- name: SonarCloudAnalyze
63+
env:
64+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
65+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
66+
run: .\.sonar\scanner\dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"

UUIDUtil/UUIDUtil.csproj

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
88
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
99
<PackageId>TensionDev.UUID</PackageId>
10-
<Version>2.1.0</Version>
10+
<Version>2.2.0</Version>
1111
<Authors>TensionDev amsga</Authors>
1212
<Company>TensionDev</Company>
1313
<Product>TensionDev.UUID</Product>
@@ -21,8 +21,6 @@
2121
<PackageReleaseNotes>Change in license to Apache License 2.0.
2222
Release with UUID / GUID Version 1, Version 3, Version 4 and Version 5, and Draft Versions 6 and 7.</PackageReleaseNotes>
2323
<NeutralLanguage>en-SG</NeutralLanguage>
24-
<AssemblyVersion>2.1.0.0</AssemblyVersion>
25-
<FileVersion>2.1.0.0</FileVersion>
2624
<IncludeSymbols>true</IncludeSymbols>
2725
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2826
<PackageReadmeFile>README.md</PackageReadmeFile>

UUIDUtil/UUIDv1.cs

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public class UUIDv1
3030
protected internal static readonly Object s_initLock = new Object();
3131
protected internal static readonly Object s_clockLock = new Object();
3232

33+
protected UUIDv1()
34+
{
35+
}
36+
3337
/// <summary>
3438
/// Initialises a new GUID/UUID based on Version 1 (date-time and MAC address)
3539
/// </summary>

UUIDUtil/UUIDv3.cs

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ namespace TensionDev.UUID
2525
/// </summary>
2626
public class UUIDv3
2727
{
28+
protected UUIDv3()
29+
{
30+
}
31+
2832
/// <summary>
2933
/// Initialises a new GUID/UUID based on Version 3 (MD5 namespace name-based)
3034
/// </summary>

UUIDUtil/UUIDv4.cs

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ namespace TensionDev.UUID
2323
/// </summary>
2424
public class UUIDv4
2525
{
26+
protected UUIDv4()
27+
{
28+
}
29+
2630
/// <summary>
2731
/// Initialises a new GUID/UUID based on Version 4 (random)
2832
/// </summary>

UUIDUtil/UUIDv5.cs

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ namespace TensionDev.UUID
2525
/// </summary>
2626
public class UUIDv5
2727
{
28+
protected UUIDv5()
29+
{
30+
}
31+
2832
/// <summary>
2933
/// Initialises a new GUID/UUID based on Version 5 (SHA-1 namespace name-based)
3034
/// </summary>

UUIDUtil/UUIDv6.cs

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public class UUIDv6
2929
protected internal static readonly Object s_initLock = new Object();
3030
protected internal static readonly Object s_clockLock = new Object();
3131

32+
protected UUIDv6()
33+
{
34+
}
35+
3236
/// <summary>
3337
/// Initialises a new GUID/UUID based on Version 6 (date-time)
3438
/// </summary>

UUIDUtil/UUIDv7.cs

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public class UUIDv7
2929
protected internal static UInt16 s_counter = 0;
3030
protected internal static readonly Object s_counterLock = new Object();
3131

32+
protected UUIDv7()
33+
{
34+
}
35+
3236
public enum GenerationMethod
3337
{
3438
/// <summary>

UUIDUtil/Uuid.cs

+28-26
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ namespace TensionDev.UUID
2222
{
2323
public sealed class Uuid : IComparable<Uuid>, IEquatable<Uuid>
2424
{
25-
private uint _time_low;
26-
private ushort _time_mid;
27-
private ushort _time_hi_and_version;
25+
private const string INVALID_FORMAT_STRING = "The format of s is invalid";
26+
private readonly uint _time_low;
27+
private readonly ushort _time_mid;
28+
private readonly ushort _time_hi_and_version;
2829
private byte _clock_seq_hi_and_reserved;
29-
private byte _clock_seq_low;
30-
private byte[] _node;
30+
private readonly byte _clock_seq_low;
31+
private readonly byte[] _node;
3132

3233
/// <summary>
3334
/// A read-only instance of the Uuid object whose value is all zeros.
@@ -37,7 +38,7 @@ public sealed class Uuid : IComparable<Uuid>, IEquatable<Uuid>
3738
/// <summary>
3839
/// A read-only instance of the Uuid object whose value is all ones.
3940
/// </summary>
40-
public static readonly Uuid Max = new Uuid(uint.MaxValue, ushort.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff});
41+
public static readonly Uuid Max = new Uuid(uint.MaxValue, ushort.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });
4142

4243
public Uuid()
4344
{
@@ -84,20 +85,17 @@ public Uuid(byte[] b) : this()
8485
/// <exception cref="System.OverflowException">The format of s is invalid</exception>
8586
public Uuid(string s) : this()
8687
{
87-
if (s == null)
88-
throw new ArgumentNullException(nameof(s));
89-
9088
if (String.IsNullOrEmpty(s))
91-
throw new FormatException("The format of s is invalid");
89+
throw new ArgumentNullException(nameof(s), INVALID_FORMAT_STRING);
9290

9391
if (s.Length != 32 && s.Length != 36 && s.Length != 38)
94-
throw new FormatException("The format of s is invalid");
92+
throw new FormatException(INVALID_FORMAT_STRING);
9593

96-
if (s.Length == 38)
94+
if (s.Length == 38 &&
95+
!(s.StartsWith("{") && s.EndsWith("}")) &&
96+
!(s.StartsWith("(") && s.EndsWith(")")))
9797
{
98-
if (!(s.StartsWith("{") && s.EndsWith("}")) &&
99-
!(s.StartsWith("(") && s.EndsWith(")")))
100-
throw new FormatException("The format of s is invalid");
98+
throw new FormatException(INVALID_FORMAT_STRING);
10199
}
102100

103101
string vs = s.Replace("{", "");
@@ -107,15 +105,15 @@ public Uuid(string s) : this()
107105

108106
if (vs.Length == 36)
109107
{
110-
Regex regex = new Regex(@"\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b", RegexOptions.IgnoreCase);
108+
Regex regex = new Regex(@"\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(100));
111109
MatchCollection matches = regex.Matches(vs);
112110
if (matches.Count == 0)
113-
throw new FormatException("The format of s is invalid");
111+
throw new FormatException(INVALID_FORMAT_STRING);
114112
}
115113
vs = vs.Replace("-", "");
116114

117115
if (vs.Length != 32)
118-
throw new FormatException("The format of s is invalid");
116+
throw new FormatException(INVALID_FORMAT_STRING);
119117

120118
Byte[] b = new Byte[16];
121119
for (Int32 i = 0; i < vs.Length; i += 2)
@@ -126,11 +124,11 @@ public Uuid(string s) : this()
126124
}
127125
catch (FormatException)
128126
{
129-
throw new FormatException("The format of s is invalid");
127+
throw new FormatException(INVALID_FORMAT_STRING);
130128
}
131129
catch (OverflowException)
132130
{
133-
throw new OverflowException("The format of s is invalid");
131+
throw new OverflowException(INVALID_FORMAT_STRING);
134132
}
135133
}
136134

@@ -356,9 +354,10 @@ public Guid ToVariant2()
356354
{
357355
byte newClockSeq = (byte)(_clock_seq_hi_and_reserved & 0x1F);
358356
newClockSeq = (byte)(newClockSeq | 0xC0);
359-
Uuid variant2 = new Uuid(this.ToByteArray());
360-
361-
variant2._clock_seq_hi_and_reserved = newClockSeq;
357+
Uuid variant2 = new Uuid(this.ToByteArray())
358+
{
359+
_clock_seq_hi_and_reserved = newClockSeq
360+
};
362361

363362
return variant2.ToGuid();
364363
}
@@ -370,10 +369,13 @@ public Guid ToVariant2()
370369
/// <returns>A TensionDev.UUID.Uuid object.</returns>
371370
public static Uuid ToVariant1(Guid guid)
372371
{
373-
Uuid variant1 = new Uuid(guid.ToString());
374-
byte newClockSeq = (byte)(variant1._clock_seq_hi_and_reserved & 0x3F);
372+
Uuid variant2 = new Uuid(guid.ToString());
373+
byte newClockSeq = (byte)(variant2._clock_seq_hi_and_reserved & 0x3F);
375374
newClockSeq = (byte)(newClockSeq | 0x80);
376-
variant1._clock_seq_hi_and_reserved = newClockSeq;
375+
Uuid variant1 = new Uuid(variant2.ToByteArray())
376+
{
377+
_clock_seq_hi_and_reserved = newClockSeq
378+
};
377379

378380
return variant1;
379381
}

0 commit comments

Comments
 (0)