Skip to content

Commit

Permalink
Remove not useful abstract model text validator class. Mark TextIndex…
Browse files Browse the repository at this point in the history
… unstable.
  • Loading branch information
DavidOgunsAWS committed Mar 1, 2022
1 parent 7151285 commit 0a9d164
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 79 deletions.
2 changes: 1 addition & 1 deletion docs/source/1.0/guides/model-linters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ Example:
$version: "1.0"
metadata validators = [{
name: "InclusiveWords"
name: "NoninclusiveTerms"
configuration: {
appendDefaults: true,
noninclusiveTerms: {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@

package software.amazon.smithy.linters;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.knowledge.TextIndex;
import software.amazon.smithy.model.knowledge.TextInstance;
import software.amazon.smithy.model.node.NodeMapper;
import software.amazon.smithy.model.traits.Trait;
import software.amazon.smithy.model.validation.AbstractValidator;
import software.amazon.smithy.model.validation.Severity;
import software.amazon.smithy.model.validation.ValidationEvent;
import software.amazon.smithy.model.validation.ValidationUtils;
Expand All @@ -39,7 +43,7 @@
* *
* <p>See AbstractModelTextValidator for scan implementation details.
*/
public final class NoninclusiveTermsValidator extends AbstractModelTextValidator {
public final class NoninclusiveTermsValidator extends AbstractValidator {
static final Map<String, List<String>> BUILT_IN_NONINCLUSIVE_TERMS = MapUtils.of(
"master", ListUtils.of("primary", "parent", "main"),
"slave", ListUtils.of("secondary", "replica", "clone", "child"),
Expand Down Expand Up @@ -94,7 +98,36 @@ private NoninclusiveTermsValidator(Config config) {
}
}

/**
* Runs a full text scan on a given model and stores the resulting TextOccurrences objects.
*
* Namespaces are checked against a global set per model.
*
* @param model Model to validate.
* @return a list of ValidationEvents found by the implementer of getValidationEvents per the
* TextOccurrences provided by this traversal.
*/
@Override
public List<ValidationEvent> validate(Model model) {
TextIndex textIndex = TextIndex.of(model);
List<ValidationEvent> validationEvents = new ArrayList<>();
for (TextInstance text : textIndex.getTextInstances()) {
getValidationEvents(text, validationEvent -> {
validationEvents.add(validationEvent);
});
}
return validationEvents;
}

/**
* Sub-classes must implement this method to perform the following:
* 1) Decide if the text instance is at a relevant location to validate.
* 2) Analyze the text for whatever validation event it may or may not publish.
* 3) Produce a validation event, if necessary, and push it to the ValidationEvent consumer
*
* @param occurrence text occurrence found in the body of the model
* @param validationEventConsumer consumer to push ValidationEvents into
*/
protected void getValidationEvents(TextInstance instance,
Consumer<ValidationEvent> validationEventConsumer) {
for (Map.Entry<String, List<String>> termEntry : termsMap.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import software.amazon.smithy.model.traits.ReferencesTrait;
import software.amazon.smithy.model.traits.Trait;
import software.amazon.smithy.model.validation.validators.TraitValueValidator;
import software.amazon.smithy.utils.SmithyUnstableApi;

@SmithyUnstableApi
public final class TextIndex implements KnowledgeIndex {
private List<TextInstance> textInstanceList;

Expand Down Expand Up @@ -74,11 +76,7 @@ private static void getTextInstances(Shape shape,
TextInstance.Builder builder = TextInstance.builder()
.locationType(TextInstance.TextLocation.SHAPE)
.shape(shape);
if (shape.isMemberShape()) {
builder.text(((MemberShape) shape).getMemberName());
} else {
builder.text(shape.getId().getName());
}
builder.text(shape.getId().getMember().orElseGet(() -> shape.getId().getName()));
textInstances.add(builder.build());

for (Trait trait : shape.getAllTraits().values()) {
Expand Down

0 comments on commit 0a9d164

Please sign in to comment.