Skip to content

Commit

Permalink
feat: Add a FieldFormats static class for auto-populated fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jskeet committed Feb 9, 2024
1 parent c2f7662 commit f1d44a4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
28 changes: 28 additions & 0 deletions Google.Api.Gax.Tests/FieldFormatsTest.cs
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));
}
}
2 changes: 1 addition & 1 deletion Google.Api.Gax.Tests/Google.Api.Gax.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<PropertyGroup>
<TargetFrameworks>net6.0;net462</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">net6.0</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
22 changes: 22 additions & 0 deletions Google.Api.Gax/FieldFormats.cs
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();
}

0 comments on commit f1d44a4

Please sign in to comment.