Skip to content

Commit

Permalink
Add EquatableImmutableDictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
JKamsker committed Oct 10, 2024
1 parent 5797558 commit cdff4c9
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
8 changes: 8 additions & 0 deletions Generator.Equals.DynamicGenerationTests/Base_Assertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public void EqualityTypeModel_Eqatable()
var b = CreateEqualityTypeModelMock();
Assert.Equal(a, b);
}

[Fact]
public void AttributesMetadata_Equatable()
{
var a = AttributesMetadata.CreateDefault();
var b = AttributesMetadata.CreateDefault();
Assert.True(a.Equals(b));
}

internal static EqualityTypeModel CreateEqualityTypeModelMock()
{
Expand Down
4 changes: 2 additions & 2 deletions Generator.Equals/Models/AttributesMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ internal sealed record AttributesMetadata(
AttributeMetadata SetEquality,
AttributeMetadata StringEquality,
AttributeMetadata CustomEquality,
ImmutableDictionary<long, string> StringComparisonLookup)
EquatableImmutableDictionary<long, string> StringComparisonLookup)
{
public static AttributesMetadata Instance { get; } = CreateDefault();

static AttributesMetadata CreateDefault()
internal static AttributesMetadata CreateDefault()
{
var attributesMetadata = new AttributesMetadata(
Equatable: AttributeMetadata.FromFullName("Generator.Equals.EquatableAttribute")!,
Expand Down
78 changes: 78 additions & 0 deletions Generator.Equals/Models/EquatableImmutableDictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Immutable;

namespace Generator.Equals.Models;

internal class EquatableImmutableDictionary<TKey, TValue>(ImmutableDictionary<TKey, TValue> Items)
: IEquatable<EquatableImmutableDictionary<TKey, TValue>>
where TKey : notnull
{
public ImmutableDictionary<TKey, TValue> Items { get; set; } = Items;

public EquatableImmutableDictionary() : this(ImmutableDictionary<TKey, TValue>.Empty)
{
}

//TryGetValue
public bool TryGetValue(TKey key, out TValue value) => Items.TryGetValue(key, out value);

// Key access
public TValue this[TKey key] => Items[key];

public bool Equals(EquatableImmutableDictionary<TKey, TValue>? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;

foreach (var item in Items)
{
if (!other.Items.TryGetValue(item.Key, out var otherValue))
{
return false;
}

if (!Equals(item.Value, otherValue))
{
return false;
}
}

return Items.Count == other.Items.Count;
}

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((EquatableImmutableDictionary<TKey, TValue>)obj);
}

public override int GetHashCode()
{
return Items.GetHashCode();
}

public static bool operator ==(
EquatableImmutableDictionary<TKey, TValue>? left,
EquatableImmutableDictionary<TKey, TValue>? right
)
{
return Equals(left, right);
}

public static bool operator !=(
EquatableImmutableDictionary<TKey, TValue>? left,
EquatableImmutableDictionary<TKey, TValue>? right
)
{
return !Equals(left, right);
}

// Implicit to and from ImmutableDictionary<TKey, TValue>
public static implicit operator EquatableImmutableDictionary<TKey, TValue>(ImmutableDictionary<TKey, TValue> items) =>
new(items);

public static implicit operator ImmutableDictionary<TKey, TValue>(EquatableImmutableDictionary<TKey, TValue> items) =>
items.Items;
}

0 comments on commit cdff4c9

Please sign in to comment.