Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Sep 5, 2024
1 parent b40d593 commit b89eaf6
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 6 deletions.
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ This library requires:

Place equatable attribute on a `class`, `record` or `struct`. The source generate will create a partial with overrides for `Equals` and `GetHashCode`.

- `[Equatable]` Marks the class to generate overrides for `Equals` and `GetHashCod`
- `[Equatable]` Marks the class to generate overrides for `Equals` and `GetHashCode`

The default comparer used in the implementation of `Equals` and `GetHashCode` is `EqualityComparer<T>.Default`. Customize the comparer used with the following attributes.

Expand All @@ -52,3 +52,38 @@ Place equatable attribute on a `class`, `record` or `struct`. The source genera
- `[HashSetEquality]` Use `ISet<T>.SetEquals` to determine whether enumerables are equal
- `[ReferenceEquality]` Use `Object.ReferenceEquals` to determines whether instances are the same instance
- `[EqualityComparer]` Use the specified `EqualityComparer`

### Example Usage

Example of using the attributes to customize the source generation of `Equals` and `GetHashCode`

``` c#
[Equatable]
public partial class UserImport
{
[StringEquality(StringComparison.OrdinalIgnoreCase)]
public string EmailAddress { get; set; } = null!;

public string? DisplayName { get; set; }

public string? FirstName { get; set; }

public string? LastName { get; set; }

public DateTimeOffset? LockoutEnd { get; set; }

public DateTimeOffset? LastLogin { get; set; }

[IgnoreEquality]
public string FullName => $"{FirstName} {LastName}";

[HashSetEquality]
public HashSet<string>? Roles { get; set; }

[DictionaryEquality]
public Dictionary<string, int>? Permissions { get; set; }

[SequenceEquality]
public List<DateTimeOffset>? History { get; set; }
}
```
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Authors>LoreSoft</Authors>
<NeutralLanguage>en-US</NeutralLanguage>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>dotnet roslyn source-generator generator</PackageTags>
<PackageTags>dotnet roslyn source-generator generator equality equals equatable hashcode</PackageTags>
<PackageProjectUrl>https://github.com/loresoft/Equatable.Generator</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageIcon>logo.png</PackageIcon>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Equatable.Attributes;

/// <summary>
/// Use a dictionary based comparer to determine if dictionaries are equal
/// </summary>
[Conditional("EQUATABLE_GENERATOR")]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class DictionaryEqualityAttribute : Attribute;
11 changes: 11 additions & 0 deletions src/Equatable.Generator/Attributes/EqualityComparerAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@

namespace Equatable.Attributes;

/// <summary>
/// Use the specified <see cref="IEqualityComparer{T}"/> in Equals and GetHashCode implementations
/// </summary>
/// <param name="equalityType">The <see cref="IEqualityComparer{T}"/> type to use</param>
/// <param name="instanceName">The singleton property name to get the instance to use from</param>
[Conditional("EQUATABLE_GENERATOR")]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class EqualityComparerAttribute(Type equalityType, string instanceName = "Default") : Attribute
{
/// <summary>
/// The <see cref="IEqualityComparer{T}"/> type to use
/// </summary>
public Type EqualityType { get; } = equalityType;

/// <summary>
/// The singleton property name to get the instance to use from
/// </summary>
public string InstanceName { get; } = instanceName;
}
3 changes: 3 additions & 0 deletions src/Equatable.Generator/Attributes/EquatableAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Equatable.Attributes;

/// <summary>
/// Marks the class to source generate overrides for Equals and GetHashCode.
/// </summary>
[Conditional("EQUATABLE_GENERATOR")]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class EquatableAttribute : Attribute;
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Diagnostics;
using System.Diagnostics;

namespace Equatable.Attributes;

/// <summary>
/// Use <see cref="ISet{T}.SetEquals(IEnumerable{T})"/> in Equals and GetHashCode implementations
/// </summary>
[Conditional("EQUATABLE_GENERATOR")]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class HashSetEqualityAttribute : Attribute;
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Diagnostics;
using System.Diagnostics;

namespace Equatable.Attributes;

/// <summary>
/// Ignore property in Equals and GetHashCode implementations
/// </summary>
[Conditional("EQUATABLE_GENERATOR")]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class IgnoreEqualityAttribute : Attribute;
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Diagnostics;
using System.Diagnostics;

namespace Equatable.Attributes;

/// <summary>
/// Use <see cref="Object.ReferenceEquals(object, object)"/> to determines whether instances are the same instance
/// </summary>
[Conditional("EQUATABLE_GENERATOR")]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class ReferenceEqualityAttribute : Attribute;
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Equatable.Attributes;

/// <summary>
/// Use <see cref="Enumerable.SequenceEqual{TSource}(IEnumerable{TSource}, IEnumerable{TSource})"/> in Equals and GetHashCode implementations
/// </summary>
[Conditional("EQUATABLE_GENERATOR")]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class SequenceEqualityAttribute : Attribute;
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
using System.Diagnostics;
using System.Diagnostics;

namespace Equatable.Attributes;

/// <summary>
/// Use specified <see cref="StringComparer" /> when comparing strings
/// </summary>
/// <param name="comparisonType">The <see cref="StringComparison"/> to use</param>
[Conditional("EQUATABLE_GENERATOR")]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class StringEqualityAttribute(StringComparison comparisonType) : Attribute
{
/// <summary>
/// The <see cref="StringComparison"/> to use
/// </summary>
public StringComparison ComparisonType { get; } = comparisonType;
}

0 comments on commit b89eaf6

Please sign in to comment.