From 8741aa11dee8e0eed97d51063199b283667470b0 Mon Sep 17 00:00:00 2001 From: Simon Jefferies Date: Mon, 19 Aug 2024 21:11:03 +0100 Subject: [PATCH] Add guard class. --- .github/workflows/dotnet.yml | 44 ++++++++++++++++++++++++++++ .gitignore | 3 ++ Utility.Guard.sln | 24 ++++++++++++++++ Utility.Guard/Guard.cs | 46 ++++++++++++++++++++++++++++++ Utility.Guard/Utility.Guard.csproj | 22 ++++++++++++++ 5 files changed, 139 insertions(+) create mode 100644 .github/workflows/dotnet.yml create mode 100644 .gitignore create mode 100644 Utility.Guard.sln create mode 100644 Utility.Guard/Guard.cs create mode 100644 Utility.Guard/Utility.Guard.csproj diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml new file mode 100644 index 0000000..8434815 --- /dev/null +++ b/.github/workflows/dotnet.yml @@ -0,0 +1,44 @@ +# This workflow will build a .NET project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net + +name: .NET + +on: + push: + branches: [ "main", "develop" ] + pull_request: + branches: [ "main", "develop" ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + filter: tree:0 + + - name: Get tags + run: git fetch --tags origin + + - name: Setup .NET + uses: actions/setup-dotnet@v4 + with: + dotnet-version: 8.0.x + + - name: Restore dependencies + run: dotnet restore + + - name: Build + run: dotnet build --configuration Release --no-restore + + - name: Test + run: dotnet test --configuration Release -p:CollectCoverage=true + + - name: Publish Nuget + run: | + dotnet nuget push '**/*.nupkg' -k ${NUGET} -s https://api.nuget.org/v3/index.json --skip-duplicate --no-symbols + env: + NUGET: ${{ secrets.NUGET }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ac16142 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vs +bin +obj diff --git a/Utility.Guard.sln b/Utility.Guard.sln new file mode 100644 index 0000000..27058c0 --- /dev/null +++ b/Utility.Guard.sln @@ -0,0 +1,24 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# 17 +VisualStudioVersion = 17.10.35027.167 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utility.Guard", "Utility.Guard\Utility.Guard.csproj", "{1DF6CF00-3907-40A3-BA69-B32788F37F4C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1DF6CF00-3907-40A3-BA69-B32788F37F4C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1DF6CF00-3907-40A3-BA69-B32788F37F4C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1DF6CF00-3907-40A3-BA69-B32788F37F4C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1DF6CF00-3907-40A3-BA69-B32788F37F4C}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7D25D4C6-E7A0-4490-8BDE-9076CA666008} + EndGlobalSection +EndGlobal diff --git a/Utility.Guard/Guard.cs b/Utility.Guard/Guard.cs new file mode 100644 index 0000000..e317744 --- /dev/null +++ b/Utility.Guard/Guard.cs @@ -0,0 +1,46 @@ +using System.Linq.Expressions; + +namespace Utility; + +public static class Guard +{ + public static T ThrowIfNull(T item) + { + if (item is null) + { + throw new InvalidOperationException(); + } + + return item; + } + + public static T ThrowIfNotNull(T item) + { + if (item is not null) + { + throw new InvalidOperationException(); + } + + return item; + } + + public static int ThrowIfIntegerNotInRange(int item, int start, int end) + { + if (item >= start && item <= end) + { + throw new InvalidOperationException(); + } + + return item; + } + + public static string ThrowIfStringNullOrEmpty(string value) + { + if (string.IsNullOrEmpty(value)) + { + throw new InvalidOperationException(); + } + + return value; + } +} diff --git a/Utility.Guard/Utility.Guard.csproj b/Utility.Guard/Utility.Guard.csproj new file mode 100644 index 0000000..51b7176 --- /dev/null +++ b/Utility.Guard/Utility.Guard.csproj @@ -0,0 +1,22 @@ + + + + netstandard20 + enable + enable + True + Simon Jefferies + Simon Jefferies + + MIT + latest + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + +