Skip to content

Commit 3829739

Browse files
committed
ShapeIndex is not adding any value as a separate abstraction from
Model. Separating ShapeIndex from Model made building models much more verbose, interacting with shapes in a model more verbose, updating models verbose, and caused an awkward API if you need to access things like knowledge indexes but only have access to a ShapeIndex (knowledge indexes are only on models). This commit first deprecates all ShapeIndex APIs and provides alternatives that are to be used instead. In some cases, the alternative, Model based APIs, still call into the deprecated ShapeIndex APIs. This is because a Model always has a ShapeIndex but a ShapeIndex is not a model, and it cuts down on code duplication. In the next version bump (likely 0.10.0), we will remove the ShapeIndex APIs altogether.
1 parent ddb6ccf commit 3829739

File tree

184 files changed

+1202
-1126
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

184 files changed

+1202
-1126
lines changed

aws/smithy-aws-apigateway-openapi/src/main/java/software/amazon/smithy/aws/apigateway/openapi/AddBinaryTypes.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
import java.util.logging.Logger;
2222
import java.util.stream.Collectors;
2323
import java.util.stream.Stream;
24+
import software.amazon.smithy.model.Model;
2425
import software.amazon.smithy.model.knowledge.HttpBinding;
2526
import software.amazon.smithy.model.knowledge.HttpBindingIndex;
2627
import software.amazon.smithy.model.knowledge.TopDownIndex;
2728
import software.amazon.smithy.model.node.ArrayNode;
2829
import software.amazon.smithy.model.node.Node;
29-
import software.amazon.smithy.model.shapes.ShapeIndex;
3030
import software.amazon.smithy.model.traits.MediaTypeTrait;
3131
import software.amazon.smithy.openapi.fromsmithy.Context;
3232
import software.amazon.smithy.openapi.fromsmithy.OpenApiMapper;
@@ -63,24 +63,24 @@ public OpenApi after(Context context, OpenApi openApi) {
6363
}
6464

6565
private Stream<String> supportedMediaTypes(Context context) {
66-
ShapeIndex shapeIndex = context.getModel().getShapeIndex();
66+
Model model = context.getModel();
6767
HttpBindingIndex httpBindingIndex = context.getModel().getKnowledge(HttpBindingIndex.class);
6868
TopDownIndex topDownIndex = context.getModel().getKnowledge(TopDownIndex.class);
6969

7070
// Find the media types defined on all request and response bindings.
7171
return topDownIndex.getContainedOperations(context.getService()).stream()
7272
.flatMap(operation -> Stream.concat(
7373
OptionalUtils.stream(
74-
binaryMediaType(shapeIndex, httpBindingIndex.getRequestBindings(operation))),
74+
binaryMediaType(model, httpBindingIndex.getRequestBindings(operation))),
7575
OptionalUtils.stream(
76-
binaryMediaType(shapeIndex, httpBindingIndex.getResponseBindings(operation)))));
76+
binaryMediaType(model, httpBindingIndex.getResponseBindings(operation)))));
7777
}
7878

79-
private Optional<String> binaryMediaType(ShapeIndex shapes, Map<String, HttpBinding> httpBindings) {
79+
private Optional<String> binaryMediaType(Model model, Map<String, HttpBinding> httpBindings) {
8080
return httpBindings.values().stream()
8181
.filter(binding -> binding.getLocation().equals(HttpBinding.Location.PAYLOAD))
8282
.map(HttpBinding::getMember)
83-
.flatMap(member -> OptionalUtils.stream(member.getMemberTrait(shapes, MediaTypeTrait.class)))
83+
.flatMap(member -> OptionalUtils.stream(member.getMemberTrait(model, MediaTypeTrait.class)))
8484
.map(MediaTypeTrait::getValue)
8585
.findFirst();
8686
}

aws/smithy-aws-apigateway-openapi/src/main/java/software/amazon/smithy/aws/apigateway/openapi/AddRequestValidators.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public OperationObject updateOperation(Context context, OperationShape shape, Op
6666
@Override
6767
public OpenApi after(Context context, OpenApi openapi) {
6868
// Find each known request validator on operation shapes.
69-
Set<String> validators = context.getModel().getShapeIndex().shapes(OperationShape.class)
69+
Set<String> validators = context.getModel().shapes(OperationShape.class)
7070
.flatMap(shape -> OptionalUtils.stream(shape.getTrait(RequestValidatorTrait.class)))
7171
.map(RequestValidatorTrait::getValue)
7272
.filter(KNOWN_VALIDATORS::containsKey)

aws/smithy-aws-traits/src/main/java/software/amazon/smithy/aws/traits/ArnIndex.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ public final class ArnIndex implements KnowledgeIndex {
4747

4848
public ArnIndex(Model model) {
4949
// Pre-compute the ARN services.
50-
arnServices = unmodifiableMap(model.getShapeIndex().shapes(ServiceShape.class)
50+
arnServices = unmodifiableMap(model.shapes(ServiceShape.class)
5151
.flatMap(shape -> Trait.flatMapStream(shape, ServiceTrait.class))
5252
.map(pair -> Pair.of(pair.getLeft().getId(), resolveServiceArn(pair)))
5353
.collect(Collectors.toMap(Pair::getLeft, Pair::getRight)));
5454

5555
// Pre-compute all of the ArnTemplates in a service shape.
5656
TopDownIndex topDownIndex = model.getKnowledge(TopDownIndex.class);
57-
List<ServiceShape> services = model.getShapeIndex().shapes(ServiceShape.class)
57+
List<ServiceShape> services = model.shapes(ServiceShape.class)
5858
.filter(shape -> shape.hasTrait(ServiceTrait.class))
5959
.collect(Collectors.toList());
6060

aws/smithy-aws-traits/src/main/java/software/amazon/smithy/aws/traits/ArnTemplateValidator.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import software.amazon.smithy.model.shapes.ResourceShape;
3030
import software.amazon.smithy.model.shapes.ServiceShape;
3131
import software.amazon.smithy.model.shapes.Shape;
32-
import software.amazon.smithy.model.shapes.ShapeIndex;
3332
import software.amazon.smithy.model.traits.Trait;
3433
import software.amazon.smithy.model.validation.AbstractValidator;
3534
import software.amazon.smithy.model.validation.ValidationEvent;
@@ -46,16 +45,16 @@ public final class ArnTemplateValidator extends AbstractValidator {
4645
@Override
4746
public List<ValidationEvent> validate(Model model) {
4847
ArnIndex arnIndex = model.getKnowledge(ArnIndex.class);
49-
return model.getShapeIndex().shapes(ServiceShape.class)
48+
return model.shapes(ServiceShape.class)
5049
.flatMap(service -> Trait.flatMapStream(service, ServiceTrait.class))
51-
.flatMap(pair -> validateService(model.getShapeIndex(), arnIndex, pair.getLeft()))
50+
.flatMap(pair -> validateService(model, arnIndex, pair.getLeft()))
5251
.collect(toList());
5352
}
5453

55-
private Stream<ValidationEvent> validateService(ShapeIndex index, ArnIndex arnIndex, ServiceShape service) {
54+
private Stream<ValidationEvent> validateService(Model model, ArnIndex arnIndex, ServiceShape service) {
5655
// Make sure each ARN template contains relevant identifiers.
5756
return arnIndex.getServiceResourceArns(service.getId()).entrySet().stream()
58-
.flatMap(entry -> OptionalUtils.stream(index.getShape(entry.getKey())
57+
.flatMap(entry -> OptionalUtils.stream(model.getShape(entry.getKey())
5958
.flatMap(Shape::asResourceShape)
6059
.map(resource -> Pair.of(resource, entry.getValue()))))
6160
.flatMap(pair -> validateResourceArn(pair.getLeft(), pair.getRight()));

aws/smithy-aws-traits/src/main/java/software/amazon/smithy/aws/traits/PlaneIndex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private enum Plane { CONTROL, DATA }
5050
public PlaneIndex(Model model) {
5151
pathFinder = PathFinder.create(model);
5252

53-
model.getShapeIndex().shapes(ServiceShape.class).forEach(service -> {
53+
model.shapes(ServiceShape.class).forEach(service -> {
5454
Plane plane = extractPlane(service);
5555
if (plane != null) {
5656
servicePlanes.put(service.getId(), plane);

aws/smithy-aws-traits/src/main/java/software/amazon/smithy/aws/traits/SdkServiceIdValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public final class SdkServiceIdValidator extends AbstractValidator {
7070

7171
@Override
7272
public List<ValidationEvent> validate(Model model) {
73-
return model.getShapeIndex().shapes(ServiceShape.class)
73+
return model.shapes(ServiceShape.class)
7474
.flatMap(service -> Trait.flatMapStream(service, ServiceTrait.class))
7575
.flatMap(pair -> OptionalUtils.stream(validateService(pair.getLeft(), pair.getRight())))
7676
.collect(toList());

aws/smithy-aws-traits/src/main/java/software/amazon/smithy/aws/traits/apigateway/AuthorizerIndex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class AuthorizerIndex implements KnowledgeIndex {
4848
public AuthorizerIndex(Model model) {
4949
PathFinder finder = PathFinder.create(model);
5050

51-
model.getShapeIndex().shapes(ServiceShape.class).forEach(service -> {
51+
model.shapes(ServiceShape.class).forEach(service -> {
5252
service.getTrait(AuthorizersTrait.class).ifPresent(trait -> authorizerTraits.put(service.getId(), trait));
5353
Map<ShapeId, String> serviceMap = new HashMap<>();
5454
authorizers.put(service.getId(), serviceMap);

aws/smithy-aws-traits/src/main/java/software/amazon/smithy/aws/traits/apigateway/AuthorizersTraitValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
public class AuthorizersTraitValidator extends AbstractValidator {
3737
@Override
3838
public List<ValidationEvent> validate(Model model) {
39-
return model.getShapeIndex().shapes(ServiceShape.class)
39+
return model.shapes(ServiceShape.class)
4040
.flatMap(service -> OptionalUtils.stream(validateService(service)))
4141
.collect(Collectors.toList());
4242
}

aws/smithy-aws-traits/src/main/java/software/amazon/smithy/aws/traits/apigateway/IntegrationTraitIndex.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import software.amazon.smithy.model.shapes.ServiceShape;
2525
import software.amazon.smithy.model.shapes.Shape;
2626
import software.amazon.smithy.model.shapes.ShapeId;
27-
import software.amazon.smithy.model.shapes.ShapeIndex;
2827
import software.amazon.smithy.model.shapes.ToShapeId;
2928
import software.amazon.smithy.model.traits.Trait;
3029
import software.amazon.smithy.utils.MapUtils;
@@ -37,10 +36,10 @@ public class IntegrationTraitIndex implements KnowledgeIndex {
3736
private Map<ShapeId, Map<ShapeId, Trait>> traits = new HashMap<>();
3837

3938
public IntegrationTraitIndex(Model model) {
40-
model.getShapeIndex().shapes(ServiceShape.class).forEach(service -> {
39+
model.shapes(ServiceShape.class).forEach(service -> {
4140
Map<ShapeId, Trait> serviceMap = new HashMap<>();
4241
traits.put(service.getId(), serviceMap);
43-
walk(model.getShapeIndex(), service.getId(), service, null);
42+
walk(model, service.getId(), service, null);
4443
});
4544
}
4645

@@ -78,19 +77,19 @@ public <T extends Trait> Optional<T> getIntegrationTrait(ToShapeId service, ToSh
7877
return getIntegrationTrait(service, shape).filter(type::isInstance).map(type::cast);
7978
}
8079

81-
private void walk(ShapeIndex index, ShapeId service, EntityShape current, Trait trait) {
80+
private void walk(Model model, ShapeId service, EntityShape current, Trait trait) {
8281
Trait updatedTrait = extractTrait(current, trait);
8382
Map<ShapeId, Trait> serviceMapping = traits.get(service);
8483
serviceMapping.put(current.getId(), updatedTrait);
8584

8685
for (ShapeId resource : current.getResources()) {
87-
index.getShape(resource)
86+
model.getShape(resource)
8887
.flatMap(Shape::asResourceShape)
89-
.ifPresent(resourceShape -> walk(index, service, resourceShape, updatedTrait));
88+
.ifPresent(resourceShape -> walk(model, service, resourceShape, updatedTrait));
9089
}
9190

9291
for (ShapeId operation : current.getAllOperations()) {
93-
index.getShape(operation).ifPresent(op -> serviceMapping.put(operation, extractTrait(op, updatedTrait)));
92+
model.getShape(operation).ifPresent(op -> serviceMapping.put(operation, extractTrait(op, updatedTrait)));
9493
}
9594
}
9695

aws/smithy-aws-traits/src/main/java/software/amazon/smithy/aws/traits/clientendpointdiscovery/CleanClientDiscoveryTraitTransformer.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import software.amazon.smithy.model.shapes.ServiceShape;
2727
import software.amazon.smithy.model.shapes.Shape;
2828
import software.amazon.smithy.model.shapes.ShapeId;
29-
import software.amazon.smithy.model.shapes.ShapeIndex;
3029
import software.amazon.smithy.model.traits.ErrorTrait;
3130
import software.amazon.smithy.model.traits.Trait;
3231
import software.amazon.smithy.model.transform.ModelTransformer;
@@ -61,7 +60,7 @@ public Model onRemove(ModelTransformer transformer, Collection<Shape> shapes, Mo
6160
}
6261

6362
private Set<Shape> getServicesToUpdate(Model model, Set<ShapeId> removedOperations, Set<ShapeId> removedErrors) {
64-
return model.getShapeIndex().shapes(ServiceShape.class)
63+
return model.shapes(ServiceShape.class)
6564
.flatMap(service -> Trait.flatMapStream(service, ClientEndpointDiscoveryTrait.class))
6665
.filter(pair -> removedOperations.contains(pair.getRight().getOperation())
6766
|| removedErrors.contains(pair.getRight().getError()))
@@ -78,7 +77,7 @@ private Set<Shape> getOperationsToUpdate(
7877
Set<ShapeId> updatedServices
7978
) {
8079
ClientEndpointDiscoveryIndex discoveryIndex = model.getKnowledge(ClientEndpointDiscoveryIndex.class);
81-
Set<ShapeId> stillBoundOperations = model.getShapeIndex().shapes(ServiceShape.class)
80+
Set<ShapeId> stillBoundOperations = model.shapes(ServiceShape.class)
8281
// Get all endpoint discovery services
8382
.filter(service -> service.hasTrait(ClientEndpointDiscoveryTrait.class))
8483
.map(Shape::getId)
@@ -88,7 +87,7 @@ private Set<Shape> getOperationsToUpdate(
8887
.flatMap(service -> discoveryIndex.getEndpointDiscoveryOperations(service).stream())
8988
.collect(Collectors.toSet());
9089

91-
return model.getShapeIndex().shapes(OperationShape.class)
90+
return model.shapes(OperationShape.class)
9291
// Get all endpoint discovery operations
9392
.flatMap(operation -> Trait.flatMapStream(operation, ClientDiscoveredEndpointTrait.class))
9493
// Only get the ones where discovery is optional, as it is safe to remove in that case
@@ -101,22 +100,21 @@ private Set<Shape> getOperationsToUpdate(
101100
}
102101

103102
private Set<Shape> getMembersToUpdate(Model model, Set<ShapeId> updatedOperations) {
104-
ShapeIndex shapeIndex = model.getShapeIndex();
105-
Set<ShapeId> stillBoundMembers = shapeIndex.shapes(OperationShape.class)
103+
Set<ShapeId> stillBoundMembers = model.shapes(OperationShape.class)
106104
// Get all endpoint discovery operations
107105
.filter(operation -> operation.hasTrait(ClientDiscoveredEndpointTrait.class))
108106
// Filter out the ones which are having their endpoint discovery traits removed
109107
.filter(operation -> !updatedOperations.contains(operation.getId()))
110108
// Get the input shapes of those operations
111109
.filter(operation -> operation.getInput().isPresent())
112-
.map(operation -> shapeIndex.getShape(operation.getInput().get()).flatMap(Shape::asStructureShape))
110+
.map(operation -> model.getShape(operation.getInput().get()).flatMap(Shape::asStructureShape))
113111
.filter(Optional::isPresent)
114112
// Get the input members
115113
.flatMap(input -> input.get().getAllMembers().values().stream())
116114
.map(Shape::getId)
117115
.collect(Collectors.toSet());
118116

119-
return shapeIndex.shapes(MemberShape.class)
117+
return model.shapes(MemberShape.class)
120118
// Get all members which have the endpoint discovery id trait
121119
.filter(member -> member.hasTrait(ClientEndpointDiscoveryIdTrait.class))
122120
// Get those which are on structures that aren't still bound to endpoint discovery operations

0 commit comments

Comments
 (0)