Skip to content

Commit 93c0d97

Browse files
authored
Add GetValueOrDefault extension method for Guid (#279)
2 parents d0c771c + b42cd53 commit 93c0d97

File tree

7 files changed

+47
-8
lines changed

7 files changed

+47
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Tiny .NET Helpers
22

33
[![Lint Code Base](https://github.com/marcominerva/TinyHelpers/actions/workflows/linter.yml/badge.svg)](https://github.com/marcominerva/TinyHelpers/actions/workflows/linter.yml)
4-
[![CodeQL](https://github.com/marcominerva/TinyHelpers/actions/workflows/codeql.yml/badge.svg)](https://github.com/marcominerva/TinyHelpers/actions/workflows/codeql.yml)
4+
[![CodeQL](https://github.com/marcominerva/TinyHelpers/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/marcominerva/TinyHelpers/actions/workflows/github-code-scanning/codeql)
55
[![NuGet](https://img.shields.io/nuget/v/TinyHelpers.svg?style=flat-square)](https://www.nuget.org/packages/TinyHelpers)
66
[![Nuget](https://img.shields.io/nuget/dt/TinyHelpers)](https://www.nuget.org/packages/TinyHelpers)
77
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/marcominerva/TinyHelpers/blob/master/LICENSE)
@@ -23,4 +23,4 @@ Check out the [documentation](https://github.com/marcominerva/TinyHelpers/tree/m
2323
The project is constantly evolving. Contributions are welcome. Feel free to file issues and pull requests on the repo and we'll address them as we can.
2424

2525
> **Warning**
26-
Remember to work on the **develop** branch, don't use the **master** branch directly. Create Pull Requests targeting **develop**.
26+
Remember to work on the **develop** branch, don't use the **master** branch directly. Create Pull Requests targeting **develop**.

src/TinyHelpers.AspNetCore.Swashbuckle/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Tiny Helpers for Swashbuckle ASP.NET Core
22

33
[![Lint Code Base](https://github.com/marcominerva/TinyHelpers/actions/workflows/linter.yml/badge.svg)](https://github.com/marcominerva/TinyHelpers/actions/workflows/linter.yml)
4-
[![CodeQL](https://github.com/marcominerva/TinyHelpers/actions/workflows/codeql.yml/badge.svg)](https://github.com/marcominerva/TinyHelpers/actions/workflows/codeql.yml)
4+
[![CodeQL](https://github.com/marcominerva/TinyHelpers/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/marcominerva/TinyHelpers/actions/workflows/github-code-scanning/codeql)
55
[![NuGet](https://img.shields.io/nuget/v/TinyHelpers.AspNetCore.Swashbuckle.svg?style=flat-square)](https://www.nuget.org/packages/TinyHelpers.AspNetCore.Swashbuckle)
66
[![Nuget](https://img.shields.io/nuget/dt/TinyHelpers.AspNetCore.Swashbuckle)](https://www.nuget.org/packages/TinyHelpers.AspNetCore.Swashbuckle)
77
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/marcominerva/TinyHelpers/blob/master/LICENSE)

src/TinyHelpers.AspNetCore/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Tiny Helpers for ASP.NET Core
22

33
[![Lint Code Base](https://github.com/marcominerva/TinyHelpers/actions/workflows/linter.yml/badge.svg)](https://github.com/marcominerva/TinyHelpers/actions/workflows/linter.yml)
4-
[![CodeQL](https://github.com/marcominerva/TinyHelpers/actions/workflows/codeql.yml/badge.svg)](https://github.com/marcominerva/TinyHelpers/actions/workflows/codeql.yml)
4+
[![CodeQL](https://github.com/marcominerva/TinyHelpers/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/marcominerva/TinyHelpers/actions/workflows/github-code-scanning/codeql)
55
[![NuGet](https://img.shields.io/nuget/v/TinyHelpers.AspNetCore.svg?style=flat-square)](https://www.nuget.org/packages/TinyHelpers.AspNetCore)
66
[![Nuget](https://img.shields.io/nuget/dt/TinyHelpers.AspNetCore)](https://www.nuget.org/packages/TinyHelpers.AspNetCore)
77
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/marcominerva/TinyHelpers/blob/master/LICENSE)
@@ -78,4 +78,4 @@ builder.Services.AddOpenApi(options =>
7878
The project is constantly evolving. Contributions are welcome. Feel free to file issues and pull requests on the repo and we'll address them as we can.
7979

8080
> **Warning**
81-
Remember to work on the **develop** branch, don't use the **master** branch directly. Create Pull Requests targeting **develop**.
81+
Remember to work on the **develop** branch, don't use the **master** branch directly. Create Pull Requests targeting **develop**.

src/TinyHelpers.Dapper/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Tiny Helpers for Dapper
22

33
[![Lint Code Base](https://github.com/marcominerva/TinyHelpers/actions/workflows/linter.yml/badge.svg)](https://github.com/marcominerva/TinyHelpers/actions/workflows/linter.yml)
4-
[![CodeQL](https://github.com/marcominerva/TinyHelpers/actions/workflows/codeql.yml/badge.svg)](https://github.com/marcominerva/TinyHelpers/actions/workflows/codeql.yml)
4+
[![CodeQL](https://github.com/marcominerva/TinyHelpers/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/marcominerva/TinyHelpers/actions/workflows/github-code-scanning/codeql)
55
[![NuGet](https://img.shields.io/nuget/v/TinyHelpers.Dapper.svg?style=flat-square)](https://www.nuget.org/packages/TinyHelpers.Dapper)
66
[![Nuget](https://img.shields.io/nuget/dt/TinyHelpers.Dapper)](https://www.nuget.org/packages/TinyHelpers.Dapper)
77
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/marcominerva/TinyHelpers/blob/master/LICENSE)
@@ -41,4 +41,4 @@ TimeOnlyTypeHandler.Configure();
4141
The project is constantly evolving. Contributions are welcome. Feel free to file issues and pull requests on the repo and we'll address them as we can.
4242

4343
> **Warning**
44-
Remember to work on the **develop** branch, don't use the **master** branch directly. Create Pull Requests targeting **develop**.
44+
Remember to work on the **develop** branch, don't use the **master** branch directly. Create Pull Requests targeting **develop**.

src/TinyHelpers.EntityFrameworkCore/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Tiny Helpers for Entity Framework Core
22

33
[![Lint Code Base](https://github.com/marcominerva/TinyHelpers/actions/workflows/linter.yml/badge.svg)](https://github.com/marcominerva/TinyHelpers/actions/workflows/linter.yml)
4-
[![CodeQL](https://github.com/marcominerva/TinyHelpers/actions/workflows/codeql.yml/badge.svg)](https://github.com/marcominerva/TinyHelpers/actions/workflows/codeql.yml)
4+
[![CodeQL](https://github.com/marcominerva/TinyHelpers/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/marcominerva/TinyHelpers/actions/workflows/github-code-scanning/codeql)
55
[![NuGet](https://img.shields.io/nuget/v/TinyHelpers.EntityFrameworkCore.svg?style=flat-square)](https://www.nuget.org/packages/TinyHelpers.EntityFrameworkCore)
66
[![Nuget](https://img.shields.io/nuget/dt/TinyHelpers.EntityFrameworkCore)](https://www.nuget.org/packages/TinyHelpers.EntityFrameworkCore)
77
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/marcominerva/TinyHelpers/blob/master/LICENSE)

src/TinyHelpers/Extensions/GuidExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ public static bool HasValue([NotNullWhen(true)] this Guid? input)
7070
public static Guid GetValueOrCreateNew(this Guid input)
7171
=> input.IsEmpty() ? Guid.NewGuid() : input;
7272

73+
/// <summary>
74+
/// Gets the actual value of this <see cref="Guid"/> instance, if it is different from <c>Guid.Empty</c>; otherwise, returns the specified default value.
75+
/// </summary>
76+
/// <param name="input">The <see cref="Guid"/> to test.</param>
77+
/// <param name="defaultValue">The default <see cref="Guid"/> to return if the input is <c>Guid.Empty</c>.</param>
78+
/// <returns>The actual value of this <see cref="Guid"/> instance, if it is different from <c>Guid.Empty</c>; otherwise, the specified default value.</returns>
79+
public static Guid GetValueOrDefault(this Guid input, Guid defaultValue)
80+
=> input.IsEmpty() ? defaultValue : input;
81+
7382
/// <summary>
7483
/// Gets the actual value of this <see cref="Guid"/> instance, if it is different from <see langword="null"/> and <c>Guid.Empty</c>; otherwise, creates a new <see cref="Guid"/> using <see cref="Guid.NewGuid()"/>.
7584
/// </summary>

tests/TinyHelpers.Tests/Extensions/GuidExtensionsTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,34 @@ public void NullableGuidIsNotEmpty_GetValueOrCreateNew_Should_Return_TheSameGuid
212212
// Assert
213213
Assert.Equal(input, value);
214214
}
215+
216+
[Fact]
217+
public void GuidIsEmpty_GetValueOrDefault_Should_Return_DefaultValue()
218+
{
219+
// Arrange
220+
var input = Guid.Empty;
221+
var defaultValue = Guid.NewGuid();
222+
223+
// Act
224+
var value = input.GetValueOrDefault(defaultValue);
225+
226+
// Assert
227+
Assert.Equal(defaultValue, value);
228+
}
229+
230+
[Fact]
231+
public void GuidIsNotEmpty_GetValueOrDefault_Should_Return_TheSameGuid()
232+
{
233+
// Arrange
234+
var input = Guid.NewGuid();
235+
var defaultValue = Guid.NewGuid();
236+
237+
// Act
238+
var value = input.GetValueOrDefault(defaultValue);
239+
240+
// Assert
241+
Assert.Equal(input, value);
242+
}
243+
244+
215245
}

0 commit comments

Comments
 (0)