-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add a FieldFormats static class for auto-populated fields
- Loading branch information
Showing
3 changed files
with
51 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* Use of this source code is governed by a BSD-style | ||
* license that can be found in the LICENSE file or at | ||
* https://developers.google.com/open-source/licenses/bsd | ||
*/ | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using Xunit; | ||
|
||
namespace Google.Api.Gax.Tests; | ||
|
||
public class FieldFormatsTest | ||
{ | ||
[Fact] | ||
public void GenerateUuid4() | ||
{ | ||
// We generate lots of values to check both uniqueness and formatting. | ||
int count = 10_000; | ||
var strings = new HashSet<string>(Enumerable.Range(0, count).Select(_ => FieldFormats.GenerateUuid4())); | ||
Assert.Equal(count, strings.Count); | ||
|
||
var pattern = new Regex("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"); | ||
Assert.All(strings, x => Assert.Matches(pattern, x)); | ||
} | ||
} |
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,22 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* Use of this source code is governed by a BSD-style | ||
* license that can be found in the LICENSE file or at | ||
* https://developers.google.com/open-source/licenses/bsd | ||
*/ | ||
|
||
using System; | ||
|
||
namespace Google.Api.Gax; | ||
|
||
/// <summary> | ||
/// Convenience methods for handling field formats documented in https://google.aip.dev/202. | ||
/// </summary> | ||
public static class FieldFormats | ||
{ | ||
/// <summary> | ||
/// Generates a UUID v4 value and formats it as xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, | ||
/// using lower-case letters for hex digits. | ||
/// </summary> | ||
public static string GenerateUuid4() => Guid.NewGuid().ToString(); | ||
} |