Skip to content

Commit

Permalink
Add methods for retrieving full paginator output and item paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
skmcgrail committed Nov 11, 2020
1 parent 9af70ba commit 13c7ac2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
Expand Down Expand Up @@ -81,22 +82,22 @@ private Optional<PaginationInfo> create(
}

MemberShape inputToken = trait.getInputToken().flatMap(input::getMember).orElse(null);
MemberShape outputToken = trait.getOutputToken()
List<MemberShape> outputTokenPath = trait.getOutputToken()
.flatMap(path -> PaginatedTrait.resolvePath(path, model, output))
.orElse(null);

if (inputToken == null || outputToken == null) {
if (inputToken == null || outputTokenPath == null) {
return Optional.empty();
}

MemberShape pageSizeMember = trait.getPageSize().flatMap(input::getMember).orElse(null);
MemberShape itemsMember = trait.getItems()
List<MemberShape> itemsMemberPath = trait.getItems()
.flatMap(path -> PaginatedTrait.resolvePath(path, model, output))
.orElse(null);

return Optional.of(new PaginationInfo(
service, operation, input, output, trait,
inputToken, outputToken, pageSizeMember, itemsMember));
inputToken, outputTokenPath, pageSizeMember, itemsMemberPath));
}

public Optional<PaginationInfo> getPaginationInfo(ToShapeId service, ToShapeId operation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package software.amazon.smithy.model.knowledge;

import java.util.List;
import java.util.Optional;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.OperationShape;
Expand All @@ -33,9 +34,9 @@ public final class PaginationInfo {
private final StructureShape output;
private final PaginatedTrait paginatedTrait;
private final MemberShape inputToken;
private final MemberShape outputToken;
private final List<MemberShape> outputToken;
private final MemberShape pageSize;
private final MemberShape items;
private final List<MemberShape> items;

PaginationInfo(
ServiceShape service,
Expand All @@ -44,9 +45,9 @@ public final class PaginationInfo {
StructureShape output,
PaginatedTrait paginatedTrait,
MemberShape inputToken,
MemberShape outputToken,
List<MemberShape> outputToken,
MemberShape pageSize,
MemberShape items
List<MemberShape> items
) {
this.service = service;
this.operation = operation;
Expand Down Expand Up @@ -88,11 +89,35 @@ public MemberShape getInputTokenMember() {
return inputToken;
}

/**
* @return the last {@link MemberShape} of the output path.
* @deprecated See getOutputTokenPath.
*/
public MemberShape getOutputTokenMember() {
int size = outputToken.size();
if (size == 0) {
return null;
}
return outputToken.get(size - 1);
}

public List<MemberShape> getOutputTokenPath() {
return outputToken;
}

/**
* @return the last {@link MemberShape} of the items path.
* @deprecated See getItemsMemberPath
*/
public Optional<MemberShape> getItemsMember() {
int size = items.size();
if (size == 0) {
return Optional.empty();
}
return Optional.ofNullable(items.get(size - 1));
}

public Optional<List<MemberShape>> getItemsMemberPath() {
return Optional.ofNullable(items);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package software.amazon.smithy.model.traits;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import software.amazon.smithy.model.Model;
Expand Down Expand Up @@ -90,23 +92,26 @@ public Optional<String> getPageSize() {
* of an operation.
* @return The optional member shape that the path resolves to.
*/
public static Optional<MemberShape> resolvePath(
public static Optional<List<MemberShape>> resolvePath(
String path,
Model model,
StructureShape shape
) {
List<MemberShape> memberShapes = new ArrayList<>();

// For each member name in the path, try to find that member in the previous structure
Optional<MemberShape> memberShape = Optional.empty();
Optional<MemberShape> memberShape;
Optional<StructureShape> container = Optional.of(shape);
for (String memberName: PATH_PATTERN.split(path)) {
memberShape = container.flatMap(structure -> structure.getMember(memberName));
if (!memberShape.isPresent()) {
return Optional.empty();
}
memberShapes.add(memberShape.get());
container = model.getShape(memberShape.get().getTarget())
.flatMap(Shape::asStructureShape);
}
return memberShape;
return Optional.of(memberShapes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ private List<ValidationEvent> validateMember(

if (memberPath == null) {
return service != null && validator.isRequiredToBePresent()
? Collections.singletonList(error(operation, trait, String.format(
? Collections.singletonList(error(operation, trait, String.format(
"%spaginated trait `%s` is not configured", prefix, validator.propertyName())))
: Collections.emptyList();
: Collections.emptyList();
}

if (!validator.pathsAllowed() && memberPath.contains(".")) {
Expand Down Expand Up @@ -184,7 +184,7 @@ private List<ValidationEvent> validateMember(
if (validator.pathsAllowed() && PATH_PATTERN.split(memberPath).length > 2) {
events.add(warning(operation, trait, String.format(
"%spaginated trait `%s` contains a path with more than two parts, which can make your API "
+ "cumbersome to use",
+ "cumbersome to use",
prefix, validator.propertyName()
)));
}
Expand Down Expand Up @@ -228,7 +228,8 @@ Optional<MemberShape> getMember(
) {
Optional<StructureShape> outputShape = opIndex.getOutput(operation);
return outputShape.flatMap(structureShape -> getMemberPath(opIndex, operation, trait)
.flatMap(path -> PaginatedTrait.resolvePath(path, model, structureShape)));
.flatMap(path -> PaginatedTrait.resolvePath(path, model, structureShape)))
.map(memberShapes -> memberShapes.get(memberShapes.size() - 1));
}
}

Expand Down

0 comments on commit 13c7ac2

Please sign in to comment.