Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking API change to support base type comparison #51

Merged
merged 15 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
14 changes: 5 additions & 9 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ name: Release

env:
NUGET_API_KEY: ${{secrets.NUGET_API_KEY}}



on:
push:
tags:
- '*'
branches:
- '*'
pull_request:
- '*'

jobs:
build:
Expand All @@ -29,7 +25,7 @@ jobs:
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-restore --verbosity normal
run: dotnet test --no-restore --verbosity normal -f net6.0
- name: Set Version
if: startsWith(github.ref, 'refs/tags')
run: |
Expand All @@ -39,8 +35,8 @@ jobs:
- name: Pack
if: startsWith(github.ref, 'refs/tags')
run: |
dotnet pack Generator.Equals/Generator.Equals.csproj /p:PackageVersion=$VERSION /p:Version=$VERSION -c Release -o ./publish
dotnet pack Generator.Equals.Runtime/Generator.Equals.Runtime.csproj /p:PackageVersion=$VERSION /p:Version=$VERSION -c Release -o ./publish
dotnet pack Generator.Equals/Generator.Equals.csproj /p:PackageVersion=$VERSION /p:Version=$VERSION -c Release -o ./publish
- name: Publish
if: startsWith(github.ref, 'refs/tags')
run: dotnet nuget push './publish/**/*.nupkg' -s https://api.nuget.org/v3/index.json -k $NUGET_API_KEY --skip-duplicate
run: dotnet nuget push './publish/**/*.nupkg' -s https://api.nuget.org/v3/index.json -k $NUGET_API_KEY --skip-duplicate
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on:
push:
branches:
- '*'
pull_request:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-restore --verbosity normal -f net6.0
13 changes: 11 additions & 2 deletions Generator.Equals.Runtime/Attributes.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.CodeDom.Compiler;
using System.Diagnostics;

namespace Generator.Equals
{
[GeneratedCode("Generator.Equals", "1.0.0.0")]
[Conditional("GENERATOR_EQUALS")]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class EquatableAttribute : Attribute
Expand All @@ -18,45 +20,52 @@ public class EquatableAttribute : Attribute
public bool IgnoreInheritedMembers { get; set; }
}

[GeneratedCode("Generator.Equals", "1.0.0.0")]
[Conditional("GENERATOR_EQUALS")]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class DefaultEqualityAttribute : Attribute
{
}

[GeneratedCode("Generator.Equals", "1.0.0.0")]
[Conditional("GENERATOR_EQUALS")]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class OrderedEqualityAttribute : Attribute
{
}

[GeneratedCode("Generator.Equals", "1.0.0.0")]
[Conditional("GENERATOR_EQUALS")]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class IgnoreEqualityAttribute : Attribute
{
}

[GeneratedCode("Generator.Equals", "1.0.0.0")]
[Conditional("GENERATOR_EQUALS")]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class UnorderedEqualityAttribute : Attribute
{
}

[GeneratedCode("Generator.Equals", "1.0.0.0")]
[Conditional("GENERATOR_EQUALS")]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class ReferenceEqualityAttribute : Attribute
{
}

[GeneratedCode("Generator.Equals", "1.0.0.0")]
[Conditional("GENERATOR_EQUALS")]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class SetEqualityAttribute : Attribute
{
}

[GeneratedCode("Generator.Equals", "1.0.0.0")]
[Conditional("GENERATOR_EQUALS")]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class CustomEqualityAttribute: Attribute
public class CustomEqualityAttribute : Attribute
{
public Type EqualityType { get; }
public string FieldOrPropertyName { get; }
Expand All @@ -67,4 +76,4 @@ public CustomEqualityAttribute(Type equalityType, string fieldOrPropertyName = "
FieldOrPropertyName = fieldOrPropertyName;
}
}
}
}
19 changes: 16 additions & 3 deletions Generator.Equals.Runtime/DefaultEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#nullable enable
using System.CodeDom.Compiler;
using System.Collections.Generic;

namespace Generator.Equals
{
[GeneratedCode("Generator.Equals", "1.0.0.0")]
public class DefaultEqualityComparer<T> : IEqualityComparer<T>
{
public static DefaultEqualityComparer<T> Default { get; } = new DefaultEqualityComparer<T>();
Expand All @@ -16,13 +19,23 @@ static DefaultEqualityComparer()
_underlying = new ObjectEqualityComparer();
}

public bool Equals(T x, T y) => _underlying.Equals(x, y);
public bool Equals(T? x, T? y)
{
return _underlying.Equals(x!, y!);
}

public int GetHashCode(T obj) => _underlying.GetHashCode(obj);
public int GetHashCode(T obj)
{
return _underlying.GetHashCode(obj!);
}

[GeneratedCode("Generator.Equals", "1.0.0.0")]
class ObjectEqualityComparer : IEqualityComparer<T>
{
public bool Equals(T x, T y) => object.Equals(x, y);
public bool Equals(T? x, T? y)
{
return object.Equals(x!, y!);
}

public int GetHashCode(T obj) => obj?.GetHashCode() ?? 0;
}
Expand Down
24 changes: 15 additions & 9 deletions Generator.Equals.Runtime/DictionaryEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System;
#nullable enable
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;

namespace Generator.Equals
{
[GeneratedCode("Generator.Equals", "1.0.0.0")]
public class DictionaryEqualityComparer<TKey, TValue> : IEqualityComparer<IDictionary<TKey, TValue>>
{
[GeneratedCode("Generator.Equals", "1.0.0.0")]
class KeyPairEqualityComparer : IEqualityComparer<KeyValuePair<TKey, TValue>>
{
readonly IEqualityComparer<TKey> _keyEqualityComparer;
Expand All @@ -20,32 +24,34 @@ public KeyPairEqualityComparer(

public bool Equals(KeyValuePair<TKey, TValue> x, KeyValuePair<TKey, TValue> y)
{
return _keyEqualityComparer.Equals(x.Key, y.Key) &&
_valueEqualityComparer.Equals(x.Value, y.Value);
return _keyEqualityComparer.Equals(x.Key!, y.Key!) &&
_valueEqualityComparer.Equals(x.Value!, y.Value!);
}

public int GetHashCode(KeyValuePair<TKey, TValue> obj)
{
return HashCode.Combine(
_keyEqualityComparer.GetHashCode(obj.Key),
_valueEqualityComparer.GetHashCode(obj.Value)
_keyEqualityComparer.GetHashCode(obj.Key!),
_valueEqualityComparer.GetHashCode(obj.Value!)
);
}
}


public static DictionaryEqualityComparer<TKey, TValue> Default { get; } =
new DictionaryEqualityComparer<TKey, TValue>();

readonly KeyPairEqualityComparer _keyPairEqualityComparer;
public IEqualityComparer<TKey> KeyEqualityComparer { get; }
public IEqualityComparer<TValue> ValueEqualityComparer { get; }

public DictionaryEqualityComparer() : this(DefaultEqualityComparer<TKey>.Default, DefaultEqualityComparer<TValue>.Default)
public DictionaryEqualityComparer() : this(DefaultEqualityComparer<TKey>.Default,
DefaultEqualityComparer<TValue>.Default)
{
}

public DictionaryEqualityComparer(IEqualityComparer<TKey> keyEqualityComparer, IEqualityComparer<TValue> valueEqualityComparer)
public DictionaryEqualityComparer(IEqualityComparer<TKey> keyEqualityComparer,
IEqualityComparer<TValue> valueEqualityComparer)
{
_keyPairEqualityComparer = new KeyPairEqualityComparer(keyEqualityComparer, valueEqualityComparer);
KeyEqualityComparer = keyEqualityComparer;
Expand All @@ -71,7 +77,7 @@ public bool Equals(IDictionary<TKey, TValue>? x, IDictionary<TKey, TValue>? y)
if (!y.TryGetValue(pair.Key, out var yValue))
return false;

if (!ValueEqualityComparer.Equals(pair.Value, yValue))
if (!ValueEqualityComparer.Equals(pair.Value!, yValue!))
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions Generator.Equals.Runtime/Generator.Equals.Runtime.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
<RootNamespace>Generator.Equals</RootNamespace>
<IsPackable>true</IsPackable>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
5 changes: 4 additions & 1 deletion Generator.Equals.Runtime/OrderedEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
#nullable enable
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;

namespace Generator.Equals
{
[GeneratedCode("Generator.Equals", "1.0.0.0")]
public class OrderedEqualityComparer<T> : IEqualityComparer<IEnumerable<T>>
{
public static OrderedEqualityComparer<T> Default { get; } = new OrderedEqualityComparer<T>();
Expand Down
2 changes: 2 additions & 0 deletions Generator.Equals.Runtime/ReferenceEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace Generator.Equals
{
[GeneratedCode("Generator.Equals", "1.0.0.0")]
public class ReferenceEqualityComparer<T> : IEqualityComparer<T> where T : class
{
public static ReferenceEqualityComparer<T> Default { get; } = new ReferenceEqualityComparer<T>();
Expand Down
11 changes: 7 additions & 4 deletions Generator.Equals.Runtime/SetEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System.Collections.Generic;
#nullable enable
using System.CodeDom.Compiler;
using System.Collections.Generic;

namespace Generator.Equals
{
[GeneratedCode("Generator.Equals", "1.0.0.0")]
public class SetEqualityComparer<T> : IEqualityComparer<IEnumerable<T>>
{
public static SetEqualityComparer<T> Default { get; } = new SetEqualityComparer<T>();

public IEqualityComparer<T> EqualityComparer { get; }

public SetEqualityComparer() : this(DefaultEqualityComparer<T>.Default)
Expand All @@ -24,12 +27,12 @@ public bool Equals(IEnumerable<T>? x, IEnumerable<T>? y)

if (x == null || y == null)
return false;

// If either of the underlying collections is a set, then we want to respect whatever
// is the equality comparer tha was specified.
if (x is ISet<T> xSet)
return xSet.SetEquals(y);

if (y is ISet<T> ySet)
return ySet.SetEquals(x);

Expand Down
10 changes: 7 additions & 3 deletions Generator.Equals.Runtime/UnorderedEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Collections.Generic;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;

namespace Generator.Equals
{
[GeneratedCode("Generator.Equals", "1.0.0.0")]
public class UnorderedEqualityComparer<T> : IEqualityComparer<IEnumerable<T>>
{
public static IEqualityComparer<IEnumerable<T>> Default { get; } = new UnorderedEqualityComparer<T>();
Expand All @@ -12,7 +14,7 @@ public class UnorderedEqualityComparer<T> : IEqualityComparer<IEnumerable<T>>
public UnorderedEqualityComparer() : this(DefaultEqualityComparer<T>.Default)
{
}

public UnorderedEqualityComparer(IEqualityComparer<T> equalityComparer)
{
EqualityComparer = equalityComparer;
Expand All @@ -30,8 +32,10 @@ public bool Equals(IEnumerable<T>? x, IEnumerable<T>? y)
y is ICollection<T> yCollection &&
xCollection.Count != yCollection.Count) return false;

#pragma warning disable CS8714
var cnt = new Dictionary<T, int>(EqualityComparer);

#pragma warning restore CS8714

foreach (var s in x)
cnt[s] = (cnt.TryGetValue(s, out var v) ? v : 0) + 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#nullable enable
#pragma warning disable CS0612,CS0618
#pragma warning disable CS0436

namespace Generator.Equals.Tests.Classes
{
Expand Down Expand Up @@ -39,10 +40,16 @@ public override bool Equals(object? obj) =>

/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")]
public bool Equals(global::Generator.Equals.Tests.Classes.BaseEquality.Manager? other)
bool global::System.IEquatable<global::Generator.Equals.Tests.Classes.BaseEquality.Manager>.Equals(global::Generator.Equals.Tests.Classes.BaseEquality.Manager? obj) => Equals((object?) obj);

/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Generator.Equals", "1.0.0.0")]
protected bool Equals(global::Generator.Equals.Tests.Classes.BaseEquality.Manager? other)
{
return
base.Equals(other as global::Generator.Equals.Tests.Classes.BaseEquality.Person)
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;

return base.Equals(other as global::Generator.Equals.Tests.Classes.BaseEquality.Person)
&& global::Generator.Equals.DefaultEqualityComparer<global::System.String>.Default.Equals(this.Department!, other.Department!)
;
}
Expand Down
Loading