Skip to content

Commit 938985f

Browse files
author
Bart Koelman
authored
QueryExpression rewriter (#820)
* Added Equals/GetHashCode to QueryExpression concrete types * Added expression rewriter
1 parent 54727c9 commit 938985f

25 files changed

+865
-17
lines changed

src/JsonApiDotNetCore/Queries/Expressions/CollectionNotEmptyExpression.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,27 @@ public override string ToString()
2424
{
2525
return $"{Keywords.Has}({TargetCollection})";
2626
}
27+
28+
public override bool Equals(object obj)
29+
{
30+
if (ReferenceEquals(this, obj))
31+
{
32+
return true;
33+
}
34+
35+
if (obj is null || GetType() != obj.GetType())
36+
{
37+
return false;
38+
}
39+
40+
var other = (CollectionNotEmptyExpression) obj;
41+
42+
return TargetCollection.Equals(other.TargetCollection);
43+
}
44+
45+
public override int GetHashCode()
46+
{
47+
return TargetCollection.GetHashCode();
48+
}
2749
}
2850
}

src/JsonApiDotNetCore/Queries/Expressions/ComparisonExpression.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,27 @@ public override string ToString()
2828
{
2929
return $"{Operator.ToString().Camelize()}({Left},{Right})";
3030
}
31+
32+
public override bool Equals(object obj)
33+
{
34+
if (ReferenceEquals(this, obj))
35+
{
36+
return true;
37+
}
38+
39+
if (obj is null || GetType() != obj.GetType())
40+
{
41+
return false;
42+
}
43+
44+
var other = (ComparisonExpression) obj;
45+
46+
return Operator == other.Operator && Left.Equals(other.Left) && Right.Equals(other.Right);
47+
}
48+
49+
public override int GetHashCode()
50+
{
51+
return HashCode.Combine(Operator, Left, Right);
52+
}
3153
}
3254
}

src/JsonApiDotNetCore/Queries/Expressions/CountExpression.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,27 @@ public override string ToString()
2424
{
2525
return $"{Keywords.Count}({TargetCollection})";
2626
}
27+
28+
public override bool Equals(object obj)
29+
{
30+
if (ReferenceEquals(this, obj))
31+
{
32+
return true;
33+
}
34+
35+
if (obj is null || GetType() != obj.GetType())
36+
{
37+
return false;
38+
}
39+
40+
var other = (CountExpression) obj;
41+
42+
return TargetCollection.Equals(other.TargetCollection);
43+
}
44+
45+
public override int GetHashCode()
46+
{
47+
return TargetCollection.GetHashCode();
48+
}
2749
}
2850
}

src/JsonApiDotNetCore/Queries/Expressions/EqualsAnyOfExpression.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,35 @@ public override string ToString()
3939

4040
return builder.ToString();
4141
}
42+
43+
public override bool Equals(object obj)
44+
{
45+
if (ReferenceEquals(this, obj))
46+
{
47+
return true;
48+
}
49+
50+
if (obj is null || GetType() != obj.GetType())
51+
{
52+
return false;
53+
}
54+
55+
var other = (EqualsAnyOfExpression) obj;
56+
57+
return TargetAttribute.Equals(other.TargetAttribute) && Constants.SequenceEqual(other.Constants);
58+
}
59+
60+
public override int GetHashCode()
61+
{
62+
var hashCode = new HashCode();
63+
hashCode.Add(TargetAttribute);
64+
65+
foreach (var constant in Constants)
66+
{
67+
hashCode.Add(constant);
68+
}
69+
70+
return hashCode.ToHashCode();
71+
}
4272
}
4373
}

src/JsonApiDotNetCore/Queries/Expressions/IncludeElementExpression.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,35 @@ public override string ToString()
4444

4545
return builder.ToString();
4646
}
47+
48+
public override bool Equals(object obj)
49+
{
50+
if (ReferenceEquals(this, obj))
51+
{
52+
return true;
53+
}
54+
55+
if (obj is null || GetType() != obj.GetType())
56+
{
57+
return false;
58+
}
59+
60+
var other = (IncludeElementExpression) obj;
61+
62+
return Relationship.Equals(other.Relationship) == Children.SequenceEqual(other.Children);
63+
}
64+
65+
public override int GetHashCode()
66+
{
67+
var hashCode = new HashCode();
68+
hashCode.Add(Relationship);
69+
70+
foreach (var child in Children)
71+
{
72+
hashCode.Add(child);
73+
}
74+
75+
return hashCode.ToHashCode();
76+
}
4777
}
4878
}

src/JsonApiDotNetCore/Queries/Expressions/IncludeExpression.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,34 @@ public override string ToString()
3838
var chains = IncludeChainConverter.GetRelationshipChains(this);
3939
return string.Join(",", chains.Select(child => child.ToString()));
4040
}
41+
42+
public override bool Equals(object obj)
43+
{
44+
if (ReferenceEquals(this, obj))
45+
{
46+
return true;
47+
}
48+
49+
if (obj is null || GetType() != obj.GetType())
50+
{
51+
return false;
52+
}
53+
54+
var other = (IncludeExpression) obj;
55+
56+
return Elements.SequenceEqual(other.Elements);
57+
}
58+
59+
public override int GetHashCode()
60+
{
61+
var hashCode = new HashCode();
62+
63+
foreach (var element in Elements)
64+
{
65+
hashCode.Add(element);
66+
}
67+
68+
return hashCode.ToHashCode();
69+
}
4170
}
4271
}

src/JsonApiDotNetCore/Queries/Expressions/LiteralConstantExpression.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,27 @@ public override string ToString()
2424
string value = Value.Replace("\'", "\'\'");
2525
return $"'{value}'";
2626
}
27+
28+
public override bool Equals(object obj)
29+
{
30+
if (ReferenceEquals(this, obj))
31+
{
32+
return true;
33+
}
34+
35+
if (obj is null || GetType() != obj.GetType())
36+
{
37+
return false;
38+
}
39+
40+
var other = (LiteralConstantExpression) obj;
41+
42+
return Value == other.Value;
43+
}
44+
45+
public override int GetHashCode()
46+
{
47+
return Value.GetHashCode();
48+
}
2749
}
2850
}

src/JsonApiDotNetCore/Queries/Expressions/LogicalExpression.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,35 @@ public override string ToString()
4646

4747
return builder.ToString();
4848
}
49+
50+
public override bool Equals(object obj)
51+
{
52+
if (ReferenceEquals(this, obj))
53+
{
54+
return true;
55+
}
56+
57+
if (obj is null || GetType() != obj.GetType())
58+
{
59+
return false;
60+
}
61+
62+
var other = (LogicalExpression) obj;
63+
64+
return Operator == other.Operator && Terms.SequenceEqual(other.Terms);
65+
}
66+
67+
public override int GetHashCode()
68+
{
69+
var hashCode = new HashCode();
70+
hashCode.Add(Operator);
71+
72+
foreach (var term in Terms)
73+
{
74+
hashCode.Add(term);
75+
}
76+
77+
return hashCode.ToHashCode();
78+
}
4979
}
5080
}

src/JsonApiDotNetCore/Queries/Expressions/MatchTextExpression.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,27 @@ public override string ToString()
3737

3838
return builder.ToString();
3939
}
40+
41+
public override bool Equals(object obj)
42+
{
43+
if (ReferenceEquals(this, obj))
44+
{
45+
return true;
46+
}
47+
48+
if (obj is null || GetType() != obj.GetType())
49+
{
50+
return false;
51+
}
52+
53+
var other = (MatchTextExpression) obj;
54+
55+
return TargetAttribute.Equals(other.TargetAttribute) && TextValue.Equals(other.TextValue) && MatchKind == other.MatchKind;
56+
}
57+
58+
public override int GetHashCode()
59+
{
60+
return HashCode.Combine(TargetAttribute, TextValue, MatchKind);
61+
}
4062
}
4163
}

src/JsonApiDotNetCore/Queries/Expressions/NotExpression.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,27 @@ public override string ToString()
2424
{
2525
return $"{Keywords.Not}({Child})";
2626
}
27+
28+
public override bool Equals(object obj)
29+
{
30+
if (ReferenceEquals(this, obj))
31+
{
32+
return true;
33+
}
34+
35+
if (obj is null || GetType() != obj.GetType())
36+
{
37+
return false;
38+
}
39+
40+
var other = (NotExpression) obj;
41+
42+
return Child.Equals(other.Child);
43+
}
44+
45+
public override int GetHashCode()
46+
{
47+
return Child.GetHashCode();
48+
}
2749
}
2850
}

src/JsonApiDotNetCore/Queries/Expressions/NullConstantExpression.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using JsonApiDotNetCore.Queries.Internal.Parsing;
23

34
namespace JsonApiDotNetCore.Queries.Expressions
@@ -16,5 +17,25 @@ public override string ToString()
1617
{
1718
return Keywords.Null;
1819
}
20+
21+
public override bool Equals(object obj)
22+
{
23+
if (ReferenceEquals(this, obj))
24+
{
25+
return true;
26+
}
27+
28+
if (obj is null || GetType() != obj.GetType())
29+
{
30+
return false;
31+
}
32+
33+
return true;
34+
}
35+
36+
public override int GetHashCode()
37+
{
38+
return new HashCode().ToHashCode();
39+
}
1940
}
2041
}

src/JsonApiDotNetCore/Queries/Expressions/PaginationElementQueryStringValueExpression.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
13
namespace JsonApiDotNetCore.Queries.Expressions
24
{
35
/// <summary>
@@ -23,5 +25,27 @@ public override string ToString()
2325
{
2426
return Scope == null ? Value.ToString() : $"{Scope}: {Value}";
2527
}
28+
29+
public override bool Equals(object obj)
30+
{
31+
if (ReferenceEquals(this, obj))
32+
{
33+
return true;
34+
}
35+
36+
if (obj is null || GetType() != obj.GetType())
37+
{
38+
return false;
39+
}
40+
41+
var other = (PaginationElementQueryStringValueExpression) obj;
42+
43+
return Equals(Scope, other.Scope) && Value == other.Value;
44+
}
45+
46+
public override int GetHashCode()
47+
{
48+
return HashCode.Combine(Scope, Value);
49+
}
2650
}
2751
}

0 commit comments

Comments
 (0)