Skip to content

Add DefaultVisitorContext and NullVisitorContext #336

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
Jul 22, 2023
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
8 changes: 8 additions & 0 deletions src/GraphQLParser.ApiTests/GraphQLParser.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,10 @@ namespace GraphQLParser.Visitors
public System.Threading.CancellationToken CancellationToken { get; init; }
public System.Collections.Generic.Stack<GraphQLParser.AST.ASTNode> Parents { get; init; }
}
public struct DefaultVisitorContext : GraphQLParser.Visitors.IASTVisitorContext
{
public System.Threading.CancellationToken CancellationToken { get; set; }
}
public interface IASTVisitorContext
{
System.Threading.CancellationToken CancellationToken { get; }
Expand Down Expand Up @@ -960,6 +964,10 @@ namespace GraphQLParser.Visitors
public MaxDepthVisitor() { }
public override System.Threading.Tasks.ValueTask VisitAsync(GraphQLParser.AST.ASTNode? node, TContext context) { }
}
public struct NullVisitorContext : GraphQLParser.Visitors.IASTVisitorContext
{
public System.Threading.CancellationToken CancellationToken { get; }
}
public static class PrintContextExtensions
{
public static System.Threading.Tasks.ValueTask WriteAsync<TContext>(this TContext context, GraphQLParser.ROM value)
Expand Down
11 changes: 11 additions & 0 deletions src/GraphQLParser/Visitors/DefaultVisitorContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace GraphQLParser.Visitors;

/// <summary>
/// An implementation of <see cref="IASTVisitorContext"/> that only contains a <see cref="CancellationToken"/>.
/// Ideal for use in cases where there is no context variables.
/// </summary>
public struct DefaultVisitorContext : IASTVisitorContext
{
/// <inheritdoc/>
public CancellationToken CancellationToken { get; set; }
}
12 changes: 12 additions & 0 deletions src/GraphQLParser/Visitors/NullVisitorContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace GraphQLParser.Visitors;

/// <summary>
/// An implementation of <see cref="IASTVisitorContext"/> that does nothing.
/// Ideal for use in cases where the visitor runs synchronously, there is no context
/// variables, and cancellation is not required.
/// </summary>
public struct NullVisitorContext : IASTVisitorContext
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly struct?

{
/// <inheritdoc/>
public readonly CancellationToken CancellationToken => default;
}