Skip to content

fix #3116 instances of gethashcode in equals implementation #3117

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

Merged
merged 2 commits into from
Mar 6, 2018
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Nest/CommonAbstractions/Infer/IndexName/IndexName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ private bool EqualsString(string other)

private bool EqualsMarker(IndexName other)
{
if (!this.Name.IsNullOrEmpty() && other != null && !other.Name.IsNullOrEmpty())
if (other == null) return false;
if (!this.Name.IsNullOrEmpty() && !other.Name.IsNullOrEmpty())
return EqualsString(PrefixClusterName(other,other.Name));

if (this.Type != null && other != null && other.Type != null)
return this.GetHashCode() == other.GetHashCode();
return false;
if ((!this.Cluster.IsNullOrEmpty() || !other.Cluster.IsNullOrEmpty()) && this.Cluster != other.Cluster) return false;

return this.Type != null && other?.Type != null && this.Type == other.Type;
}

public string GetString(IConnectionConfigurationValues settings)
Expand Down
19 changes: 14 additions & 5 deletions src/Nest/CommonAbstractions/Infer/Indices/Indices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ string IUrlParameter.GetString(IConnectionConfigurationValues settings)
all => "_all",
many =>
{
var nestSettings = settings as IConnectionSettingsValues;
if (nestSettings == null)
if (!(settings is IConnectionSettingsValues nestSettings))
throw new Exception("Tried to pass index names on querysting but it could not be resolved because no nest settings are available");

var infer = nestSettings.Inferrer;
Expand All @@ -85,19 +84,29 @@ string IUrlParameter.GetString(IConnectionConfigurationValues settings)
);
}

public static bool operator ==(Indices left, Indices right) => Equals(left, right);

public static bool operator !=(Indices left, Indices right) => !Equals(left, right);

public override bool Equals(object obj)
{
var other = obj as Indices;
if (other == null) return false;
if (!(obj is Indices other)) return false;
return this.Match(
all => other.Match(a => true, m => false),
many => other.Match(
a => false,
m => this.GetHashCode().Equals(other.GetHashCode())
m => EqualsAllIndices(m.Indices, many.Indices)
)
);
}

private static bool EqualsAllIndices(IReadOnlyList<IndexName> thisIndices, IReadOnlyList<IndexName> otherIndices)
{
if (thisIndices == null && otherIndices == null) return true;
if (thisIndices == null || otherIndices == null) return false;
return thisIndices.Count == otherIndices.Count && !thisIndices.Except(otherIndices).Any();
}

public override int GetHashCode()
{
return this.Match(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ public override bool Equals(object obj)
{
var s = obj as string;
if (!s.IsNullOrEmpty()) return this.EqualsString(s);
var pp = obj as RelationName;
if (pp != null) return this.EqualsMarker(pp);

if (obj is RelationName pp) return this.EqualsMarker(pp);
return base.Equals(obj);
}

Expand All @@ -70,8 +68,8 @@ public bool EqualsMarker(RelationName other)
{
if (!this.Name.IsNullOrEmpty() && other != null && !other.Name.IsNullOrEmpty())
return EqualsString(other.Name);
if (this.Type != null && other != null && other.Type != null)
return this.GetHashCode() == other.GetHashCode();
if (this.Type != null && other?.Type != null)
return this.Type == other.Type;
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Nest/CommonAbstractions/Infer/TypeName/TypeName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ private bool EqualsMarker(TypeName other)
{
if (!this.Name.IsNullOrEmpty() && other != null && !other.Name.IsNullOrEmpty())
return EqualsString(other.Name);
if (this.Type != null && other != null && other.Type != null)
return this.GetHashCode() == other.GetHashCode();
if (this.Type != null && other?.Type != null)
return this.Type == other.Type;
return false;
}

Expand Down
16 changes: 13 additions & 3 deletions src/Nest/CommonAbstractions/Infer/Types/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,30 @@ string IUrlParameter.GetString(IConnectionConfigurationValues settings)
);

}
public static bool operator ==(Types left, Types right) => Equals(left, right);

public static bool operator !=(Types left, Types right) => !Equals(left, right);

public override bool Equals(object obj)
{
var other = obj as Types;
if (other == null) return false;
if (!(obj is Types other)) return false;
return this.Match(
all => other.Match(a => true, m => false),
many => other.Match(
a => false,
m => this.GetHashCode().Equals(other.GetHashCode())
m => EqualsAllTypes(m.Types, many.Types)
)
);
}

private static bool EqualsAllTypes(IReadOnlyList<TypeName> thisTypes, IReadOnlyList<TypeName> otherTypes)
{
if (thisTypes == null && otherTypes == null) return true;
if (thisTypes == null || otherTypes == null) return false;
if (thisTypes.Count != otherTypes.Count) return false;
return thisTypes.Count == otherTypes.Count && !thisTypes.Except(otherTypes).Any();
}

public override int GetHashCode()
{
return this.Match(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,22 @@ [U] public void EqualsValidation()
{
var clusterIndex = (IndexName)"cluster_one:p";
var index = (IndexName)"p";
Index<Project>("cluster_one").Should().NotBe(Index<Project>("cluster_two"));

clusterIndex.Should().NotBe(index);
clusterIndex.Should().Be("cluster_one:p");
clusterIndex.Should().Be((IndexName)"cluster_one:p");

Index<Project>().Should().Be(Index<Project>());
Index<Project>().Should().NotBe(Index<Project>("cluster_two"));
Index<Project>("cluster_one").Should().NotBe(Index<Project>("cluster_two"));
Index<Project>("cluster_one").Should().NotBe("cluster_one:project");
Index<Project>().Should().NotBe(Index<Developer>());
Index<Project>("cluster_one").Should().NotBe(Index<Developer>("cluster_one"));

Nest.Indices indices1 = "foo,bar";
Nest.Indices indices2 = "bar,foo";
indices1.Should().Be(indices2);
(indices1 == indices2).Should().BeTrue();
}

//hide
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ [U] public void EqualsValidation()

Relation<Project>().Should().Be(Relation<Project>());
Relation<Project>().Should().NotBe(Relation<Developer>());


Nest.Types types1 = "foo,bar";
Nest.Types types2 = "bar,foo";
types1.Should().Be(types2);
(types1 == types2).Should().BeTrue();
}

//hide
Expand Down
7 changes: 1 addition & 6 deletions src/Tests/Framework/Configuration/TestConfigurationBase.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tests.Framework.Versions;
using Tests.Framework.Versions;

namespace Tests.Framework.Configuration
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Text.RegularExpressions;
using Nest;
using Version = SemVer.Version;

namespace Tests.Framework.Versions
Expand Down
7 changes: 1 addition & 6 deletions src/Tests/Framework/ElasticsearchVersionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ namespace Tests.Framework
{
public class MockElasticsearchVersionResolver : ElasticsearchVersionResolver
{
public MockElasticsearchVersionResolver()
{

}
public MockElasticsearchVersionResolver() { }

public override string LatestSnapshot => "7.3.1-SNAPSHOT";
public override string LatestVersion => "7.3.1";
Expand All @@ -18,8 +15,6 @@ public override string SnapshotZipFilename(string version)
}
}



public class ElasticsearchVersionTests
{
public ElasticsearchVersion Create(string version) => ElasticsearchVersion.Create(version, new MockElasticsearchVersionResolver());
Expand Down