-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathCountVisitor.cs
More file actions
60 lines (51 loc) · 1.58 KB
/
CountVisitor.cs
File metadata and controls
60 lines (51 loc) · 1.58 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
using GraphQLParser.AST;
namespace GraphQLParser.Visitors;
/// <summary>
/// Counts AST nodes.
/// </summary>
/// <typeparam name="TContext">Type of the context object passed into all VisitXXX methods.</typeparam>
public class CountVisitor<TContext> : ASTVisitor<TContext>
where TContext : ICountContext
{
/// <inheritdoc/>
public override ValueTask VisitAsync(ASTNode? node, TContext context)
{
if (node != null && context.ShouldInclude(node))
++context.Count;
return base.VisitAsync(node, context);
}
}
/// <summary>
/// Context used by <see cref="CountVisitor{TContext}"/>.
/// </summary>
public interface ICountContext : IASTVisitorContext
{
/// <summary>
/// Number of found AST nodes.
/// </summary>
public int Count { get; set; }
/// <summary>
/// Predicate used to increment <see cref="Count"/>.
/// </summary>
public Func<ASTNode, bool> ShouldInclude { get; }
}
/// <summary>
/// Default implementation for <see cref="ICountContext"/>.
/// </summary>
public class DefaultCountContext : ICountContext
{
/// <summary>
/// Creates context with the specified delegate.
/// </summary>
/// <param name="shouldInclude">Predicate used to increment <see cref="Count"/>.</param>
public DefaultCountContext(Func<ASTNode, bool> shouldInclude)
{
ShouldInclude = shouldInclude;
}
/// <inheritdoc/>
public CancellationToken CancellationToken { get; set; }
/// <inheritdoc/>
public int Count { get; set; }
/// <inheritdoc/>
public Func<ASTNode, bool> ShouldInclude { get; }
}