-
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathAnyExpression.cs
More file actions
96 lines (78 loc) · 2.68 KB
/
AnyExpression.cs
File metadata and controls
96 lines (78 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System.Collections.Immutable;
using System.Text;
using JetBrains.Annotations;
using JsonApiDotNetCore.Queries.Parsing;
namespace JsonApiDotNetCore.Queries.Expressions;
/// <summary>
/// This expression allows to test if an attribute value equals any of the specified constants. It represents the "any" filter function, resulting from
/// text such as:
/// <c>
/// any(owner.name,'Jack','Joe','John')
/// </c>
/// .
/// </summary>
[PublicAPI]
public class AnyExpression : FilterExpression
{
/// <summary>
/// The function or attribute whose value to compare. Attribute chain format: an optional list of to-one relationships, followed by an attribute.
/// </summary>
public QueryExpression MatchTarget { get; }
/// <summary>
/// One or more constants to compare the attribute's value against.
/// </summary>
public IImmutableSet<LiteralConstantExpression> Constants { get; }
public AnyExpression(QueryExpression matchTarget, IImmutableSet<LiteralConstantExpression> constants)
{
ArgumentNullException.ThrowIfNull(matchTarget);
ArgumentGuard.NotNullNorEmpty(constants);
MatchTarget = matchTarget;
Constants = constants;
}
public override TResult Accept<TArgument, TResult>(QueryExpressionVisitor<TArgument, TResult> visitor, TArgument argument)
{
return visitor.VisitAny(this, argument);
}
public override string ToString()
{
return InnerToString(false);
}
public override string ToFullString()
{
return InnerToString(true);
}
private string InnerToString(bool toFullString)
{
var builder = new StringBuilder();
builder.Append(Keywords.Any);
builder.Append('(');
builder.Append(toFullString ? MatchTarget.ToFullString() : MatchTarget.ToString());
builder.Append(',');
builder.Append(string.Join(',', Constants.Select(constant => toFullString ? constant.ToFullString() : constant.ToString()).Order()));
builder.Append(')');
return builder.ToString();
}
public override bool Equals(object? obj)
{
if (ReferenceEquals(this, obj))
{
return true;
}
if (obj is null || GetType() != obj.GetType())
{
return false;
}
var other = (AnyExpression)obj;
return MatchTarget.Equals(other.MatchTarget) && Constants.SetEquals(other.Constants);
}
public override int GetHashCode()
{
var hashCode = new HashCode();
hashCode.Add(MatchTarget);
foreach (LiteralConstantExpression constant in Constants)
{
hashCode.Add(constant);
}
return hashCode.ToHashCode();
}
}