Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/loresoft/Equatable.Generator
Source generator for Equals and GetHashCode with attribute based control of equality implementation
https://github.com/loresoft/Equatable.Generator
equals equatable gethashcode source-generator
Last synced: about 1 month ago
JSON representation
Source generator for Equals and GetHashCode with attribute based control of equality implementation
- Host: GitHub
- URL: https://github.com/loresoft/Equatable.Generator
- Owner: loresoft
- License: mit
- Created: 2024-08-29T22:21:55.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-12-17T08:21:49.000Z (about 1 month ago)
- Last Synced: 2024-12-18T19:09:52.289Z (about 1 month ago)
- Topics: equals, equatable, gethashcode, source-generator
- Language: C#
- Homepage:
- Size: 158 KB
- Stars: 22
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- RSCG_Examples - https://github.com/loresoft/Equatable.Generator
README
# Equatable.Generator
Source generator for `Equals` and `GetHashCode` with attribute based control of equality implementation
[![Build Project](https://github.com/loresoft/Equatable.Generator/actions/workflows/dotnet.yml/badge.svg)](https://github.com/loresoft/Equatable.Generator/actions/workflows/dotnet.yml)
[![Coverage Status](https://coveralls.io/repos/github/loresoft/Equatable.Generator/badge.svg?branch=main)](https://coveralls.io/github/loresoft/Equatable.Generator?branch=main)
[![Equatable.Generator](https://img.shields.io/nuget/v/Equatable.Generator.svg)](https://www.nuget.org/packages/Equatable.Generator/)
## Features
- Override `Equals` and `GetHashCode`
- Implement `IEquatable`
- Support `class`, `record` and `struct` types
- Support `EqualityComparer` per property via attribute
- Attribute based control of equality implementation.
- Attribute comparers supported: String, Sequence, Dictionary, HashSet, Reference, and Custom
- No runtime dependencies. Library is compile time dependence only.### Usage
#### Add package
Add the nuget package to your projects.
`dotnet add package Equatable.Generator`
Prevent including Equatable.Generator as a dependency
```xml
```
### Requirements
This library requires:
- Target framework .NET Standard 2.0 or greater
- Project C# `LangVersion` 8.0 or higher### Equatable Attributes
Place `[Equatable]` attribute on a `class`, `record` or `struct`. The source generator will create a partial with overrides for `Equals` and `GetHashCode` for all public properties.
- `[Equatable]` Marks the class to generate overrides for `Equals` and `GetHashCode`
The default comparer used in the implementation of `Equals` and `GetHashCode` is `EqualityComparer.Default`. Customize the comparer used with the following attributes.
- `[IgnoreEquality]` Ignore property in `Equals` and `GetHashCode` implementations
- `[StringEquality]` Use specified `StringComparer` when comparing strings
- `[SequenceEquality]` Use `Enumerable.SequenceEqual` to determine whether enumerables are equal
- `[DictionaryEquality]` Use to determine if dictionaries are equal
- `[HashSetEquality]` Use `ISet.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? Roles { get; set; }[DictionaryEquality]
public Dictionary? Permissions { get; set; }[SequenceEquality]
public List? History { get; set; }
}
```