json schema: simplify match result tree traversing, IDEA-CR-25347

This commit is contained in:
Irina.Chernushina
2017-11-06 09:13:59 +01:00
parent 1e970cc023
commit deebe9eb88
3 changed files with 21 additions and 17 deletions
@@ -87,8 +87,8 @@ public class JsonSchemaResolver {
}
// actually we pass any schema here
final JsonLikePsiWalker walker = JsonLikePsiWalker.getWalker(element, firstSchema);
final JsonValueAdapter adapter = walker.createValueAdapter(element);
if (adapter == null) return null;
JsonValueAdapter adapter;
if (walker == null || (adapter = walker.createValueAdapter(element)) == null) return null;
final JsonValueAdapter parentAdapter;
if (topLevelSchema) {
@@ -100,10 +100,12 @@ public class JsonSchemaResolver {
}
final Ref<JsonSchemaObject> schemaRef = new Ref<>();
MatchResult.iterateTree(resolveRoot, (parent, node) -> {
if (node.getSchema() == null || parentAdapter != null && parent.isNothing()) return true;
MatchResult.iterateTree(resolveRoot, node -> {
final JsonSchemaTreeNode parent = node.getParent();
if (node.getSchema() == null || parentAdapter != null && parent != null && parent.isNothing()) return true;
if (!isCorrect(adapter, node.getSchema())) return true;
if (parentAdapter == null ||
parent == null ||
parent.getSchema() == null ||
parent.isAny() ||
isCorrect(parentAdapter, parent.getSchema())) {
@@ -35,11 +35,13 @@ public class JsonSchemaTreeNode {
@Nullable private final JsonSchemaObject mySchema;
@NotNull private final List<JsonSchemaVariantsTreeBuilder.Step> mySteps = new SmartList<>();
@Nullable private final JsonSchemaTreeNode myParent;
@NotNull private final List<JsonSchemaTreeNode> myChildren = new ArrayList<>();
public JsonSchemaTreeNode(@Nullable JsonSchemaTreeNode parent,
@Nullable JsonSchemaObject schema) {
assert schema != null || parent != null;
myParent = parent;
mySchema = schema;
if (parent != null && !parent.getSteps().isEmpty()) {
mySteps.addAll(parent.getSteps().subList(1, parent.getSteps().size()));
@@ -111,6 +113,11 @@ public class JsonSchemaTreeNode {
return mySteps;
}
@Nullable
public JsonSchemaTreeNode getParent() {
return myParent;
}
@NotNull
public List<JsonSchemaTreeNode> getChildren() {
return myChildren;
@@ -136,6 +143,7 @@ public class JsonSchemaTreeNode {
if (myNothing != node.myNothing) return false;
if (myResolveState != node.myResolveState) return false;
if (mySchema != null ? !mySchema.equals(node.mySchema) : node.mySchema != null) return false;
//noinspection RedundantIfStatement
if (!mySteps.equals(node.mySteps)) return false;
return true;
@@ -15,9 +15,7 @@
*/
package com.jetbrains.jsonSchema.impl;
import com.intellij.openapi.util.Pair;
import com.intellij.util.Consumer;
import com.intellij.util.PairProcessor;
import com.intellij.util.Processor;
import org.jetbrains.annotations.NotNull;
import java.util.*;
@@ -37,7 +35,7 @@ public class MatchResult {
public static MatchResult create(@NotNull JsonSchemaTreeNode root) {
List<JsonSchemaObject> schemas = new ArrayList<>();
Map<Integer, Set<JsonSchemaObject>> oneOfGroups = new HashMap<>();
iterateTree(root, (parent, node) -> {
iterateTree(root, node -> {
if (node.isAny()) return true;
int groupNumber = node.getExcludingGroupNumber();
if (groupNumber < 0) {
@@ -54,20 +52,16 @@ public class MatchResult {
}
public static void iterateTree(@NotNull JsonSchemaTreeNode root,
@NotNull final PairProcessor<JsonSchemaTreeNode, JsonSchemaTreeNode> parentChildConsumer) {
final ArrayDeque<Pair<JsonSchemaTreeNode, JsonSchemaTreeNode>> queue = new ArrayDeque<>();
final Consumer<JsonSchemaTreeNode> queueChildren = node -> node.getChildren().forEach(child -> queue.add(Pair.create(node, child)));
queueChildren.consume(root);
@NotNull final Processor<JsonSchemaTreeNode> processor) {
final ArrayDeque<JsonSchemaTreeNode> queue = new ArrayDeque<>(root.getChildren());
while (!queue.isEmpty()) {
final Pair<JsonSchemaTreeNode, JsonSchemaTreeNode> pair = queue.removeFirst();
final JsonSchemaTreeNode node = pair.getSecond();
final JsonSchemaTreeNode node = queue.removeFirst();
if (node.getChildren().isEmpty()) {
if (!node.isNothing() && SchemaResolveState.normal.equals(node.getResolveState()) &&
!parentChildConsumer.process(pair.getFirst(), node)) {
if (!node.isNothing() && SchemaResolveState.normal.equals(node.getResolveState()) && !processor.process(node)) {
break;
}
} else {
queueChildren.consume(node);
queue.addAll(node.getChildren());
}
}
}