Skip to content

Commit

Permalink
Merge pull request #11 from calebjenkins/develop
Browse files Browse the repository at this point in the history
Version bump - New IList AddRange extension method
  • Loading branch information
calebjenkins committed May 28, 2023
2 parents 54fc697 + c640227 commit 5f59b00
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 69 deletions.
70 changes: 70 additions & 0 deletions .github/workflows/ci.yml
@@ -0,0 +1,70 @@
on:
push:
branches:
- '!develop'
- '!main'
paths-ignore:
- '**.md'
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 15

steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '7.0.x'

- name: Package Restore
run: dotnet restore

- name: Build
run: dotnet build --configuration Release

- name: Test
#run: dotnet test --configuration Release --no-build
run: dotnet test --configuration Release --no-build --filter 'FullyQualifiedName!~BDD_Specs'

#- name: Run SpecFlow Tests
# uses: cryptic-wizard/run-specflow-tests@v1.3.1
# with:
# test-assembly-path: src/BDD/BDD_Specs/BDD_Specs/bin/Release/net6.0
# test-assembly-dll: BDD_Specs.dll
# output-html: BDD_SpecResults.html
# no-build: true
# upload-artifact: true

- name: Echo Branch Name
run: echo "Branch name is $GITHUB_REF_NAME "

- name: Get version
id: get_version
uses: greenygh0st/net-proj-release-version@v1
with:
# Filepath of the *.csproj file, relative to root of repository
PROJ_FILE: src/Extensions/Extensions.csproj

# env:
# RELEASE_VERSION: ${{ steps.get_version.outputs.RELEASE_VERSION }}


- name: Show Release Version
run: echo "Release Version is ${{steps.get_version.outputs.RELEASE_VERSION}}"

- name: Set Release Version
run: echo "getting version"

env:
RELEASE_VERSION: steps.get_version.outputs.RELEASE_VERSION

- name: Print new build number
run: echo "Build number is $GITHUB_RUN_NUMBER "

3 changes: 2 additions & 1 deletion Calebs.Extensions.sln
Expand Up @@ -20,11 +20,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExtensionTests7", "src\Exte
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{C4850C2D-38FD-45CD-B50E-A9A5C73FC1D5}"
ProjectSection(SolutionItems) = preProject
.github\workflows\ci.yml = .github\workflows\ci.yml
.github\workflows\dev-ci.yml = .github\workflows\dev-ci.yml
.github\workflows\main-publish.yml = .github\workflows\main-publish.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtensionNugetTests7", "src\ExtensionsNugetTests7\ExtensionNugetTests7\ExtensionNugetTests7.csproj", "{AA463FE8-8ED4-4822-8280-B8EB9C72B2D8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExtensionNugetTests7", "src\ExtensionsNugetTests7\ExtensionNugetTests7\ExtensionNugetTests7.csproj", "{AA463FE8-8ED4-4822-8280-B8EB9C72B2D8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
7 changes: 4 additions & 3 deletions README.md
Expand Up @@ -2,7 +2,7 @@
[![.github/workflows/main-publish.yml](https://github.com/calebjenkins/Calebs.Extensions/actions/workflows/main-publish.yml/badge.svg?branch=main)](https://github.com/calebjenkins/Calebs.Extensions/actions/workflows/main-publish.yml)
[![NuGet](https://img.shields.io/nuget/dt/calebs.extensions.svg)](https://www.nuget.org/packages/calebs.extensions)
[![NuGet](https://img.shields.io/nuget/vpre/calebs.extensions.svg)](https://www.nuget.org/packages/calebs.extensions)

[![.github/workflows/ci.yml](https://github.com/calebjenkins/Calebs.Extensions/actions/workflows/ci.yml/badge.svg)](https://github.com/calebjenkins/Calebs.Extensions/actions/workflows/ci.yml)
# Calebs.Extensions
Useful extension methods and attributes for working with enums, strings and lists. The majority of these extensions were born out of working with various models while building micro services.

Expand Down Expand Up @@ -45,8 +45,9 @@ So instead of having your model directly bind to an enum - and throw a Bad Reque
- Compare

## ListExtensions
- ToDelimitedList
- ToDelimitedList
- ToUpper
- AddRange - `IList<T>.AddRange(IList<T>)`

## JsonExtensions
For both of these extension methods I'm using the `Newtonsoft.Json` library. I'm planning on migrating to `System.Text.Json` as soon as it is viable. Right now, Newtonsoft is easeir to serialize enums to thier ToString() value rather than index, and to deserialize the same. For example, by default - an enum is serialized to the index of the value. So an enum with (High, Med, Low) values would otherwise be serialiezed to a 2, instead of to "Med". Serializing to "Med" is my prefered behavior. I will continue to evailuate `System.Text.Json` against these unit tests and most likely migrate at some point.
Expand All @@ -62,5 +63,5 @@ Merges to `deveoper` automtically run all unit tests and publish a nuget package
Merges to `main` publish to nuget as a major release.

# Change Log
-
-1.1.0 - added IList.AddRange extension method
-
6 changes: 3 additions & 3 deletions src/ExtensionTests/EnumExtensionTests.cs
Expand Up @@ -103,19 +103,19 @@ public static bool IsValid(object model)
public class ExampleClass
{

[EnumStringValidatorAttribute(typeof(OptionsWithDescriptions))]
[EnumStringValidator(typeof(OptionsWithDescriptions))]
public string Size { get; set; } = string.Empty;
}

public class RequiredPropertyClass
{
[Required(), EnumStringValidatorAttribute(typeof(OptionsWithDescriptions))]
[Required(), EnumStringValidator(typeof(OptionsWithDescriptions))]
public string Size { get; set; } = string.Empty;
}

public class CaseSensitivePropertyClass
{
[Required(), EnumStringValidatorAttribute(typeof(OptionsWithDescriptions), false)]
[Required(), EnumStringValidator(typeof(OptionsWithDescriptions), false)]
public string Size { get; set; } = string.Empty;
}

Expand Down
1 change: 1 addition & 0 deletions src/ExtensionTests/JsonDeserializationToClassesTests.cs
@@ -1,4 +1,5 @@
using System.Reflection.Metadata;
using Calebs.Extensions;

namespace ExtensionTests;
public class JsonDeserializationToClassesTests
Expand Down
3 changes: 2 additions & 1 deletion src/ExtensionTests/JsonDeserializationToRecordsTests.cs
@@ -1,4 +1,5 @@
namespace ExtensionTests;
using Calebs.Extensions;

public class JsonDeserializationToRecordsTests
{
Expand Down Expand Up @@ -35,7 +36,7 @@ public void MissingEnumShouldDefault()
m.LastName.Should().Be("Jenkins");


m.Priority.Should().Be(ExampleEnum.High);
//m.Priority.Should().Be(ExampleEnum.High);

// In a Class, deserializing from Jason - missing enum, the default set value get's asigned
// In a Record, deserializing from Json - missing enum, the first value in the enum is used.
Expand Down
7 changes: 5 additions & 2 deletions src/ExtensionTests/JsonSerializationTests.cs
@@ -1,6 +1,8 @@

namespace ExtensionTests;

using Calebs.Extensions;

public class JsonSerializationTests
{
[Fact]
Expand All @@ -11,18 +13,19 @@ public void ShouldSerializeClasses()

json.Should().Contain("Jenkins");
json.Should().ContainAny($"\"FirstName\":\"Caleb\"");
json.Should().ContainAny("\"Priority\":\"Low\"");

}

[Fact]
public void ShouldSerializeRecords()
{
var m = new ExampleRecord("Caleb", "Jenkins", ExampleEnum.Low);
var m = new ExampleRecord("Caleb", "Jenkins", ExampleEnum.Med);
var json = m.ToJson();

json.Should().Contain("Jenkins");
json.Should().ContainAny($"\"FirstName\":\"Caleb\"");

json.Should().ContainAny("\"Priority\":\"Med\"");
}
}

36 changes: 36 additions & 0 deletions src/ExtensionTests/ListExtensionTests.cs
@@ -0,0 +1,36 @@

namespace ExtensionTests;

using Calebs.Extensions;

public class ListExtensionTests
{
[Fact]
public void ShouldCombineLists()
{
IList<string> l1 = new List<string>()
{
"One",
"two",
"three"
};

IList<string> l2 = new List<string>()
{
"four",
"five"
};

l1.Count.Should().Be(3);
l2.Count.Should().Be(2);

l1.AddRange<string>(l2);

l1.Count.Should().Be(5);

l2.Count.Should().Be(2);
}


}

16 changes: 9 additions & 7 deletions src/ExtensionTests/Messages.cs
@@ -1,8 +1,6 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.ComponentModel;
using System.Runtime.Serialization;
using SystemText = System.Text.Json.Serialization;
using DescriptionAttribute = System.ComponentModel.DescriptionAttribute;

namespace ExtensionTests
Expand All @@ -27,11 +25,14 @@ public enum Options2
public enum OptionWithEnumMember
{
[EnumMember(Value = "Large")]
[SystemText.JsonPropertyName("Large")]
Largish,
[EnumMember(Value = "Medium")]
Avg,
[EnumMember(Value = "Small")]
ReallyLittle
[EnumMember(Value = "Medium")]

Avg,
[EnumMember(Value = "Small")]

ReallyLittle
}

public enum OptionsWithDescriptions
Expand Down Expand Up @@ -63,6 +64,7 @@ public class MessageWithOptions2Enum
public class MessageWithOptions1EnumWithAttribute
{
[JsonConverter(typeof(StringEnumConverter))]
[SystemText.JsonConverter(typeof(SystemText.JsonStringEnumConverter))]
public Options Options { get; set; }
}

Expand Down
8 changes: 8 additions & 0 deletions src/ExtensionTests7/ExtensionTests7.csproj
Expand Up @@ -10,6 +10,14 @@
<Configurations>Debug;Release;NET7</Configurations>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\ExtensionTests\ExampleModel.cs" Link="ExampleModel.cs" />
<Compile Include="..\ExtensionTests\ExampleStrings.cs" Link="ExampleStrings.cs" />
<Compile Include="..\ExtensionTests\JsonDeserializationToClassesTests.cs" Link="JsonDeserializationToClassesTests.cs" />
<Compile Include="..\ExtensionTests\JsonDeserializationToRecordsTests.cs" Link="JsonDeserializationToRecordsTests.cs" />
<Compile Include="..\ExtensionTests\JsonSerializationTests.cs" Link="JsonSerializationTests.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
Expand Down
1 change: 1 addition & 0 deletions src/ExtensionTests7/Messages.cs
Expand Up @@ -96,6 +96,7 @@ public Options Options
}

[JsonProperty("Options")]
[System.Text.Json.Serialization.JsonPropertyName("Options")]
public string _options { get; set; } = string.Empty;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Extensions/Extensions.csproj
Expand Up @@ -10,7 +10,7 @@
<Configurations>Debug;Release;NET7</Configurations>
<RootNamespace>Calebs.Extensions</RootNamespace>
<AssemblyName>Calebs.Extensions</AssemblyName>
<Version>1.0.0</Version>
<Version>1.1.0</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Caleb Jenkins</Authors>
<Company>Caleb Jenkins</Company>
Expand Down
13 changes: 9 additions & 4 deletions src/Extensions/JsonExtensions.cs
@@ -1,18 +1,23 @@

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newt = Newtonsoft.Json;
using Conv = Newtonsoft.Json.Converters;


namespace Calebs.Extensions;

public static class JsonExtensions
{
public static T FromJson<T>(this string json)
{
return JsonConvert.DeserializeObject<T>(json);
T result = default(T);
result = Newt.JsonConvert.DeserializeObject<T>(json);
return result;
}

public static string ToJson(this object o)
{
return JsonConvert.SerializeObject(o, new StringEnumConverter());
var result = string.Empty;
result = Newt.JsonConvert.SerializeObject(o, new Conv.StringEnumConverter());
return result;
}
}
49 changes: 34 additions & 15 deletions src/Extensions/ListExtensions.cs
@@ -1,19 +1,38 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace Calebs.Extensions;

public static class ListExtensions
{
public static string ToDelimitedList(this List<string> list, string delimiter)
{
return string.Join(delimiter, list.ToArray());
}

public static void ToUpper(this List<string> list)
{
for (var i = 0; i < list.Count; i++)
{
list[i] = list[i].ToUpper();
}
}
}
{
public static string ToDelimitedList(this List<string> list, string delimiter)
{
return string.Join(delimiter, list.ToArray());
}

public static void ToUpper(this IList<string> list)
{
for (var i = 0; i < list.Count; i++)
{
list[i] = list[i].ToUpper();
}
}

public static void AddRange<T>(this IList<T> list, IEnumerable<T> items)
{
if (list == null) throw new ArgumentNullException(nameof(list));
if (items == null) throw new ArgumentNullException(nameof(items));

if (list is List<T> asList)
{
asList.AddRange(items);
}
else
{
foreach (var item in items)
{
list.Add(item);
}
}
}
}

0 comments on commit 5f59b00

Please sign in to comment.