Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: smithy-lang/smithy
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2b4f78ee0fec03b8cd1ede6bb5c9bd23f6cf5958
Choose a base ref
..
head repository: smithy-lang/smithy
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 5a21517a2effe01c2ee7602a91bbbc0abdc61b38
Choose a head ref
Showing with 77 additions and 70 deletions.
  1. +5 −5 smithy-model/src/main/java/software/amazon/smithy/model/selector/AbstractNeighborSelector.java
  2. +1 −1 smithy-model/src/main/java/software/amazon/smithy/model/selector/AndSelector.java
  3. +2 −2 smithy-model/src/main/java/software/amazon/smithy/model/selector/AttributeSelector.java
  4. +2 −2 smithy-model/src/main/java/software/amazon/smithy/model/selector/Context.java
  5. +1 −1 smithy-model/src/main/java/software/amazon/smithy/model/selector/ForwardNeighborSelector.java
  6. +6 −6 smithy-model/src/main/java/software/amazon/smithy/model/selector/InSelector.java
  7. +9 −3 smithy-model/src/main/java/software/amazon/smithy/model/selector/InternalSelector.java
  8. +4 −4 smithy-model/src/main/java/software/amazon/smithy/model/selector/IsSelector.java
  9. +2 −2 smithy-model/src/main/java/software/amazon/smithy/model/selector/NotSelector.java
  10. +4 −4 smithy-model/src/main/java/software/amazon/smithy/model/selector/RecursiveNeighborSelector.java
  11. +1 −1 smithy-model/src/main/java/software/amazon/smithy/model/selector/ReverseNeighborSelector.java
  12. +4 −4 smithy-model/src/main/java/software/amazon/smithy/model/selector/RootSelector.java
  13. +2 −2 smithy-model/src/main/java/software/amazon/smithy/model/selector/ScopedAttributeSelector.java
  14. +1 −1 smithy-model/src/main/java/software/amazon/smithy/model/selector/SelectorParser.java
  15. +2 −2 smithy-model/src/main/java/software/amazon/smithy/model/selector/ShapeTypeCategorySelector.java
  16. +2 −2 smithy-model/src/main/java/software/amazon/smithy/model/selector/ShapeTypeSelector.java
  17. +3 −4 smithy-model/src/main/java/software/amazon/smithy/model/selector/TestSelector.java
  18. +9 −9 smithy-model/src/main/java/software/amazon/smithy/model/selector/TopDownSelector.java
  19. +5 −5 smithy-model/src/main/java/software/amazon/smithy/model/selector/VariableGetSelector.java
  20. +1 −1 smithy-model/src/main/java/software/amazon/smithy/model/selector/VariableStoreSelector.java
  21. +4 −4 smithy-model/src/main/java/software/amazon/smithy/model/selector/WrappedSelector.java
  22. +4 −2 smithy-model/src/test/resources/software/amazon/smithy/model/selector/cases/in-function.smithy
  23. +3 −3 smithy-model/src/test/resources/software/amazon/smithy/model/selector/cases/root-function.smithy
Original file line number Diff line number Diff line change
@@ -32,23 +32,23 @@ abstract class AbstractNeighborSelector implements InternalSelector {
}

@Override
public final boolean push(Context context, Shape shape, Receiver next) {
public final Response push(Context context, Shape shape, Receiver next) {
NeighborProvider resolvedProvider = getNeighborProvider(context, includeTraits);
for (Relationship rel : resolvedProvider.getNeighbors(shape)) {
if (matches(rel)) {
if (!emitMatchingRel(context, rel, next)) {
if (emitMatchingRel(context, rel, next) == Response.STOP) {
// Stop pushing shapes upstream and propagate the signal to stop.
return false;
return Response.STOP;
}
}
}

return true;
return Response.CONTINUE;
}

abstract NeighborProvider getNeighborProvider(Context context, boolean includeTraits);

abstract boolean emitMatchingRel(Context context, Relationship rel, Receiver next);
abstract Response emitMatchingRel(Context context, Relationship rel, Receiver next);

private boolean matches(Relationship rel) {
return rel.getNeighborShape().isPresent()
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ static final class IntermediateAndSelector implements InternalSelector {
}

@Override
public boolean push(Context ctx, Shape shape, Receiver next) {
public Response push(Context ctx, Shape shape, Receiver next) {
return leftSelector.push(ctx, shape, (c, s) -> rightSelector.push(c, s, next));
}

Original file line number Diff line number Diff line change
@@ -84,11 +84,11 @@ public Collection<? extends Shape> getStartingShapes(Model model) {
}

@Override
public boolean push(Context context, Shape shape, Receiver next) {
public Response push(Context context, Shape shape, Receiver next) {
if (matchesAttribute(shape, context)) {
return next.apply(context, shape);
} else {
return true;
return Response.CONTINUE;
}
}

Original file line number Diff line number Diff line change
@@ -63,10 +63,10 @@ private static final class Holder implements InternalSelector.Receiver {
boolean set;

@Override
public boolean apply(Context context, Shape shape) {
public InternalSelector.Response apply(Context context, Shape shape) {
set = true;
// Stop receiving shapes once the first value is seen.
return false;
return InternalSelector.Response.STOP;
}
}

Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ NeighborProvider getNeighborProvider(Context context, boolean includeTraits) {
}

@Override
boolean emitMatchingRel(Context context, Relationship rel, Receiver next) {
Response emitMatchingRel(Context context, Relationship rel, Receiver next) {
return next.apply(context, rel.getNeighborShape().get());
}
}
Original file line number Diff line number Diff line change
@@ -29,13 +29,13 @@ final class InSelector implements InternalSelector {
}

@Override
public boolean push(Context context, Shape shape, Receiver next) {
public Response push(Context context, Shape shape, Receiver next) {
// Some internal selectors provide optimizations for quickly checking if they contain a shape.
switch (selector.containsShapeOptimization(context, shape)) {
case YES:
return next.apply(context, shape);
case NO:
return true;
return Response.CONTINUE;
case MAYBE:
default:
// Unable to use the optimization, so emit each shape until a match is found.
@@ -46,7 +46,7 @@ public boolean push(Context context, Shape shape, Receiver next) {
return next.apply(context, shape);
}

return true;
return Response.CONTINUE;
}
}

@@ -59,13 +59,13 @@ private static final class FilteredHolder implements InternalSelector.Receiver {
}

@Override
public boolean apply(Context context, Shape shape) {
public Response apply(Context context, Shape shape) {
if (shape.equals(shapeToMatch)) {
matched = true;
return false;
return Response.STOP;
}

return true;
return Response.CONTINUE;
}
}
}
Original file line number Diff line number Diff line change
@@ -52,7 +52,13 @@ interface InternalSelector {
* @param next Receiver to call 0 or more times.
* @return Returns true to continue sending shapes to the selector.
*/
boolean push(Context ctx, Shape shape, Receiver next);
Response push(Context ctx, Shape shape, Receiver next);

/** Tells shape emitters whether to continue to send shapes to an InternalSelector or Receiver. */
enum Response {
CONTINUE,
STOP
}

/**
* Pushes {@code shape} through the selector and adds all results to {@code captures}.
@@ -71,7 +77,7 @@ interface InternalSelector {
default <C extends Collection<Shape>> C pushResultsToCollection(Context context, Shape shape, C captures) {
push(context, shape, (c, s) -> {
captures.add(s);
return true;
return Response.CONTINUE;
});
return captures;
}
@@ -127,6 +133,6 @@ interface Receiver {
* @param shape Shape that is received.
* @return Returns true to continue receiving shapes.
*/
boolean apply(Context context, Shape shape);
Response apply(Context context, Shape shape);
}
}
Original file line number Diff line number Diff line change
@@ -33,13 +33,13 @@ static InternalSelector of(List<InternalSelector> predicates) {
}

@Override
public boolean push(Context context, Shape shape, Receiver next) {
public Response push(Context context, Shape shape, Receiver next) {
for (InternalSelector selector : selectors) {
if (!selector.push(context, shape, next)) {
return false;
if (selector.push(context, shape, next) == Response.STOP) {
return Response.STOP;
}
}

return true;
return Response.CONTINUE;
}
}
Original file line number Diff line number Diff line change
@@ -29,11 +29,11 @@ final class NotSelector implements InternalSelector {
}

@Override
public boolean push(Context context, Shape shape, Receiver next) {
public Response push(Context context, Shape shape, Receiver next) {
if (!context.receivedShapes(shape, selector)) {
return next.apply(context, shape);
} else {
return true;
return Response.CONTINUE;
}
}
}
Original file line number Diff line number Diff line change
@@ -25,21 +25,21 @@
*/
final class RecursiveNeighborSelector implements InternalSelector {
@Override
public boolean push(Context context, Shape shape, Receiver next) {
public Response push(Context context, Shape shape, Receiver next) {
Walker walker = new Walker(context.neighborIndex.getProvider());
Iterator<Shape> shapeIterator = walker.iterateShapes(shape);

while (shapeIterator.hasNext()) {
Shape nextShape = shapeIterator.next();
// Don't include the shape being visited.
if (!nextShape.equals(shape)) {
if (!next.apply(context, nextShape)) {
if (next.apply(context, nextShape) == Response.STOP) {
// Stop sending recursive neighbors when told to stop and propagate.
return false;
return Response.STOP;
}
}
}

return true;
return Response.CONTINUE;
}
}
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ NeighborProvider getNeighborProvider(Context context, boolean includeTraits) {
}

@Override
boolean emitMatchingRel(Context context, Relationship rel, Receiver next) {
Response emitMatchingRel(Context context, Relationship rel, Receiver next) {
return next.apply(context, rel.getShape());
}
}
Original file line number Diff line number Diff line change
@@ -36,14 +36,14 @@ final class RootSelector implements InternalSelector {
}

@Override
public boolean push(Context context, Shape shape, Receiver next) {
public Response push(Context context, Shape shape, Receiver next) {
for (Shape v : context.getRootResult(id)) {
if (!next.apply(context, v)) {
return false;
if (next.apply(context, v) == Response.STOP) {
return Response.STOP;
}
}

return true;
return Response.CONTINUE;
}

@Override
Original file line number Diff line number Diff line change
@@ -64,11 +64,11 @@ interface ScopedFactory {
}

@Override
public boolean push(Context context, Shape shape, Receiver next) {
public Response push(Context context, Shape shape, Receiver next) {
if (matchesAssertions(shape, context.getVars())) {
return next.apply(context, shape);
} else {
return true;
return Response.CONTINUE;
}
}

Original file line number Diff line number Diff line change
@@ -259,7 +259,7 @@ private InternalSelector parseSelectorFunction() {
default:
LOGGER.warning(String.format("Unknown function name `%s` found in selector: %s",
name, expression()));
return (context, shape, next) -> true;
return (context, shape, next) -> InternalSelector.Response.CONTINUE;
}
}

Original file line number Diff line number Diff line change
@@ -27,12 +27,12 @@ final class ShapeTypeCategorySelector implements InternalSelector {
}

@Override
public boolean push(Context ctx, Shape shape, Receiver next) {
public Response push(Context ctx, Shape shape, Receiver next) {
if (shapeCategory.isInstance(shape)) {
return next.apply(ctx, shape);
}

return true;
return Response.CONTINUE;
}

@Override
Original file line number Diff line number Diff line change
@@ -29,12 +29,12 @@ final class ShapeTypeSelector implements InternalSelector {
}

@Override
public boolean push(Context ctx, Shape shape, Receiver next) {
public Response push(Context ctx, Shape shape, Receiver next) {
if (shape.getType().isShapeType(shapeType)) {
return next.apply(ctx, shape);
}

return true;
return Response.CONTINUE;
}

@Override
Original file line number Diff line number Diff line change
@@ -32,16 +32,15 @@ final class TestSelector implements InternalSelector {
}

@Override
public boolean push(Context context, Shape shape, Receiver next) {
public Response push(Context context, Shape shape, Receiver next) {
for (InternalSelector predicate : selectors) {
if (context.receivedShapes(shape, predicate)) {
// The instant something matches, stop testing selectors.
return next.apply(context, shape);
}
}

// Note that this does not return false when there is not a match,
// since it should to continue to receive shapes to test.
return true;
// Continue to receive shapes because other shapes could match.
return Response.CONTINUE;
}
}
Original file line number Diff line number Diff line change
@@ -33,22 +33,22 @@ final class TopDownSelector implements InternalSelector {
}

@Override
public boolean push(Context context, Shape shape, Receiver next) {
public Response push(Context context, Shape shape, Receiver next) {
if (shape.isServiceShape() || shape.isResourceShape() || shape.isOperationShape()) {
return pushMatch(false, context, shape, next, new HashSet<>());
}

return true;
return Response.CONTINUE;
}

// While a model can't contain recursive resource references, a custom
// validator might use the :topdown selector function on a model with
// recursive references. Custom validators are applied before resource
// cycles are detected, meaning this function needs to protect against
// recursion.
private boolean pushMatch(boolean qualified, Context context, Shape shape, Receiver next, Set<ShapeId> visited) {
private Response pushMatch(boolean qualified, Context context, Shape shape, Receiver next, Set<ShapeId> visited) {
if (visited.contains(shape.getId())) {
return true;
return Response.CONTINUE;
}

visited.add(shape.getId());
@@ -64,22 +64,22 @@ private boolean pushMatch(boolean qualified, Context context, Shape shape, Recei
}

// If the shape is matched, then it's sent to the next receiver.
if (qualified && !next.apply(context, shape)) {
return false; // fast-fail if the receiver fast-fails.
if (qualified && next.apply(context, shape) == Response.STOP) {
return Response.STOP; // fast-fail if the receiver fast-fails.
}

// Recursively check each nested resource/operation.
for (Relationship rel : context.neighborIndex.getProvider().getNeighbors(shape)) {
if (rel.getNeighborShape().isPresent() && !rel.getNeighborShapeId().equals(shape.getId())) {
if (rel.getRelationshipType() == RelationshipType.RESOURCE
|| rel.getRelationshipType() == RelationshipType.OPERATION) {
if (!pushMatch(qualified, context, rel.getNeighborShape().get(), next, visited)) {
return false;
if (pushMatch(qualified, context, rel.getNeighborShape().get(), next, visited) == Response.STOP) {
return Response.STOP;
}
}
}
}

return true;
return Response.CONTINUE;
}
}
Original file line number Diff line number Diff line change
@@ -30,16 +30,16 @@ final class VariableGetSelector implements InternalSelector {
}

@Override
public boolean push(Context context, Shape shape, Receiver next) {
// Do not fail on an invalid variable access.
public Response push(Context context, Shape shape, Receiver next) {
// Do not fail on invalid variable access.
for (Shape v : getShapes(context)) {
if (!next.apply(context, v)) {
if (next.apply(context, v) == Response.STOP) {
// Propagate the signal to stop upstream.
return false;
return Response.STOP;
}
}

return true;
return Response.CONTINUE;
}

private Set<Shape> getShapes(Context context) {
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ final class VariableStoreSelector implements InternalSelector {
}

@Override
public boolean push(Context context, Shape shape, Receiver next) {
public Response push(Context context, Shape shape, Receiver next) {
// Buffer the result of piping the shape through the selector
// so that it can be retrieved through context vars.
Set<Shape> captures = selector.pushResultsToCollection(context, shape, new HashSet<>());
Loading