-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split validator interface, implement name duplication validator
- Loading branch information
Showing
6 changed files
with
285 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/org/neo4j/importer/v1/validation/NoDuplicatedNameValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.neo4j.importer.v1.validation; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.neo4j.importer.v1.actions.Action; | ||
import org.neo4j.importer.v1.sources.Source; | ||
import org.neo4j.importer.v1.targets.CustomQueryTarget; | ||
import org.neo4j.importer.v1.targets.NodeTarget; | ||
import org.neo4j.importer.v1.targets.RelationshipTarget; | ||
import org.neo4j.importer.v1.validation.SpecificationValidationResult.Builder; | ||
|
||
public class NoDuplicatedNameValidator implements SpecificationValidator { | ||
|
||
private final NameCounter nameCounter; | ||
|
||
public NoDuplicatedNameValidator() { | ||
nameCounter = new NameCounter(); | ||
} | ||
|
||
@Override | ||
public void visitSource(int index, Source source) { | ||
nameCounter.track(source.getName(), String.format("$.sources[%d]", index)); | ||
} | ||
|
||
@Override | ||
public void visitNodeTarget(int index, NodeTarget target) { | ||
nameCounter.track(target.getName(), String.format("$.targets.nodes[%d]", index)); | ||
} | ||
|
||
@Override | ||
public void visitRelationshipTarget(int index, RelationshipTarget target) { | ||
nameCounter.track(target.getName(), String.format("$.targets.relationships[%d]", index)); | ||
} | ||
|
||
@Override | ||
public void visitCustomQueryTarget(int index, CustomQueryTarget target) { | ||
nameCounter.track(target.getName(), String.format("$.targets.queries[%d]", index)); | ||
} | ||
|
||
@Override | ||
public void visitAction(int index, Action action) { | ||
nameCounter.track(action.getName(), String.format("$.actions[%d]", index)); | ||
} | ||
|
||
@Override | ||
public void accept(Builder builder) { | ||
nameCounter.reportErrorsIfAny(builder); | ||
} | ||
} | ||
|
||
class NameCounter { | ||
|
||
private final Map<String, List<String>> pathsUsingName = new LinkedHashMap<>(); | ||
|
||
public void track(String name, String path) { | ||
pathsUsingName.computeIfAbsent(name, (ignored) -> new ArrayList<>()).add(String.format("%s.name", path)); | ||
} | ||
|
||
public void reportErrorsIfAny(Builder builder) { | ||
pathsUsingName.entrySet().stream() | ||
.filter(entry -> entry.getValue().size() > 1) | ||
.forEach(entry -> { | ||
List<String> paths = entry.getValue(); | ||
builder.addError( | ||
paths.get(0), | ||
"DUPN", | ||
String.format( | ||
"Name \"%s\" is duplicated across the following paths: %s", | ||
entry.getKey(), String.join(", ", paths))); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.neo4j.importer.v1.validation.NoDuplicatedNameValidator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters