Skip to content

Commit

Permalink
Allow changing include name
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed Mar 25, 2024
1 parent 30a76f6 commit 01a935a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
14 changes: 7 additions & 7 deletions src/Foundatio.Parsers.LuceneQueries/Visitors/IncludeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ public class IncludeVisitor : ChainableMutatingQueryVisitor
{
private readonly LuceneQueryParser _parser = new();
private readonly ShouldSkipIncludeFunc _shouldSkipInclude;
private readonly string _name;
private readonly string _includeName;

public IncludeVisitor(ShouldSkipIncludeFunc shouldSkipInclude = null, string name = "include")
public IncludeVisitor(ShouldSkipIncludeFunc shouldSkipInclude = null, string includeName = "include")
{
_shouldSkipInclude = shouldSkipInclude;
_name = name;
_includeName = includeName;
}

public override async Task<IQueryNode> VisitAsync(TermNode node, IQueryVisitorContext context)
{
if (node.Field != "@" + _name || (_shouldSkipInclude != null && _shouldSkipInclude(node, context)))
if (node.Field != "@" + _includeName || (_shouldSkipInclude != null && _shouldSkipInclude(node, context)))
return node;

var includeResolver = context.GetIncludeResolver();
Expand All @@ -36,7 +36,7 @@ public override async Task<IQueryNode> VisitAsync(TermNode node, IQueryVisitorCo
var includeStack = context.GetIncludeStack();
if (includeStack.Contains(node.Term))
{
context.AddValidationError($"Recursive {_name} ({node.Term})");
context.AddValidationError($"Recursive {_includeName} ({node.Term})");
return node;
}

Expand All @@ -45,7 +45,7 @@ public override async Task<IQueryNode> VisitAsync(TermNode node, IQueryVisitorCo
string includedQuery = await includeResolver(node.Term).ConfigureAwait(false);
if (includedQuery == null)
{
context.AddValidationError($"Unresolved {_name} ({node.Term})");
context.AddValidationError($"Unresolved {_includeName} ({node.Term})");
context.GetValidationResult().UnresolvedIncludes.Add(node.Term);
}

Expand All @@ -64,7 +64,7 @@ public override async Task<IQueryNode> VisitAsync(TermNode node, IQueryVisitorCo
}
catch (Exception ex)
{
context.AddValidationError($"Error in {_name} resolver callback when resolving {_name} ({node.Term}): {ex.Message}");
context.AddValidationError($"Error in {_includeName} resolver callback when resolving {_includeName} ({node.Term}): {ex.Message}");
context.GetValidationResult().UnresolvedIncludes.Add(node.Term);

return node;
Expand Down
12 changes: 6 additions & 6 deletions src/Foundatio.Parsers.SqlQueries/SqlQueryParserConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ public SqlQueryParserConfiguration UseFieldMap(IDictionary<string, string> field
return UseFieldResolver(null);
}

public SqlQueryParserConfiguration UseIncludes(IncludeResolver includeResolver, ShouldSkipIncludeFunc shouldSkipInclude = null, int priority = 0) {
public SqlQueryParserConfiguration UseIncludes(IncludeResolver includeResolver, ShouldSkipIncludeFunc shouldSkipInclude = null, string includeName = "include", int priority = 0) {
IncludeResolver = includeResolver;

return AddVisitor(new IncludeVisitor(shouldSkipInclude), priority);
return AddVisitor(new IncludeVisitor(shouldSkipInclude, includeName), priority);
}

public SqlQueryParserConfiguration UseIncludes(Func<string, string> resolveInclude, ShouldSkipIncludeFunc shouldSkipInclude = null, int priority = 0) {
return UseIncludes(name => Task.FromResult(resolveInclude(name)), shouldSkipInclude, priority);
public SqlQueryParserConfiguration UseIncludes(Func<string, string> resolveInclude, ShouldSkipIncludeFunc shouldSkipInclude = null, string includeName = "include", int priority = 0) {
return UseIncludes(name => Task.FromResult(resolveInclude(name)), shouldSkipInclude, includeName, priority);
}

public SqlQueryParserConfiguration UseIncludes(IDictionary<string, string> includes, ShouldSkipIncludeFunc shouldSkipInclude = null, int priority = 0) {
return UseIncludes(name => includes.ContainsKey(name) ? includes[name] : null, shouldSkipInclude, priority);
public SqlQueryParserConfiguration UseIncludes(IDictionary<string, string> includes, ShouldSkipIncludeFunc shouldSkipInclude = null, string includeName = "include", int priority = 0) {
return UseIncludes(name => includes.ContainsKey(name) ? includes[name] : null, shouldSkipInclude, includeName, priority);
}

public SqlQueryParserConfiguration SetValidationOptions(QueryValidationOptions options) {
Expand Down

0 comments on commit 01a935a

Please sign in to comment.