Skip to content

Commit

Permalink
JsonPatchMatcher: Short-circuit matching when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Nov 17, 2024
1 parent 0f8e755 commit f1f48c0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.RuleNode;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.jspecify.annotations.Nullable;
import org.openrewrite.Cursor;
Expand Down Expand Up @@ -137,6 +138,11 @@ protected Object aggregateResult(Object aggregate, Object nextResult) {
return (scope = nextResult);
}

@Override
protected boolean shouldVisitNextChild(RuleNode node, Object currentResult) {
return scope != null;
}

@Override
public Object visitJsonPath(JsonPathParser.JsonPathContext ctx) {
if (ctx.ROOT() != null || "[".equals(ctx.start.getText())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.RuleNode;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.jspecify.annotations.Nullable;
import org.openrewrite.Cursor;
Expand Down Expand Up @@ -137,6 +138,11 @@ protected Object aggregateResult(Object aggregate, Object nextResult) {
return (scope = nextResult);
}

@Override
protected boolean shouldVisitNextChild(RuleNode node, Object currentResult) {
return scope != null;
}

@Override
public Object visitJsonPath(JsonPathParser.JsonPathContext ctx) {
if (ctx.ROOT() != null || "[".equals(ctx.start.getText())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.RuleNode;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.jspecify.annotations.Nullable;
import org.openrewrite.Cursor;
Expand Down Expand Up @@ -161,6 +162,11 @@ protected Object aggregateResult(Object aggregate, Object nextResult) {
return (scope = nextResult);
}

@Override
protected boolean shouldVisitNextChild(RuleNode node, Object currentResult) {
return scope != null;
}

@Override
public Object visitJsonPath(JsonPathParser.JsonPathContext ctx) {
MATCH:
Expand Down

0 comments on commit f1f48c0

Please sign in to comment.