Skip to content

Commit

Permalink
Use array to return required members
Browse files Browse the repository at this point in the history
  • Loading branch information
milesziemer committed May 24, 2023
1 parent f741863 commit a5efba9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public abstract class CollectionShape extends Shape {

CollectionShape(Builder<?, ?> builder) {
super(builder, false);
Map<String, MemberShape> members = getRequiredMembers(builder, "member");
member = members.get("member");
MemberShape[] members = getRequiredMembers(builder, "member");
member = members[0];
validateMemberShapeIds();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public final class MapShape extends Shape implements ToSmithyBuilder<MapShape> {

private MapShape(Builder builder) {
super(builder, false);
Map<String, MemberShape> members = getRequiredMembers(builder, "key", "value");
key = members.get("key");
value = members.get("value");
MemberShape[] members = getRequiredMembers(builder, "key", "value");
key = members[0];
value = members[1];
validateMemberShapeIds();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,18 @@ protected void validateMixins(Map<ShapeId, Shape> mixins, Map<ShapeId, Trait> in
}
}

protected Map<String, MemberShape> getRequiredMembers(
AbstractShapeBuilder<?, ?> builder,
String... requiredMembersNames
) {
protected MemberShape[] getRequiredMembers(AbstractShapeBuilder<?, ?> builder, String... requiredMembersNames) {
// Only lists/sets and maps have required members, the maximum number being 2.
List<String> missingMembersNames = new ArrayList<>(2);
Map<String, MemberShape> members = new HashMap<>(2);

for (String requiredMemberName : requiredMembersNames) {
Optional<MemberShape> member = getRequiredMixinMember(builder, requiredMemberName);
if (member.isPresent()) {
members.put(requiredMemberName, member.get());
// Caller knows the order of provided member names, so we don't need a dynamic data structure.
MemberShape[] members = new MemberShape[requiredMembersNames.length];

for (int memberNameIndex = 0; memberNameIndex < requiredMembersNames.length; memberNameIndex++) {
String requiredMemberName = requiredMembersNames[memberNameIndex];
MemberShape member = getRequiredMixinMember(builder, requiredMemberName);
if (member != null) {
members[memberNameIndex] = member;
} else {
missingMembersNames.add(requiredMemberName);
}
Expand All @@ -133,13 +133,10 @@ protected Map<String, MemberShape> getRequiredMembers(
return members;
}

protected Optional<MemberShape> getRequiredMixinMember(
AbstractShapeBuilder<?, ?> builder,
String requiredMemberName
) {
private MemberShape getRequiredMixinMember(AbstractShapeBuilder<?, ?> builder, String requiredMemberName) {
Optional<MemberShape> memberOnBuilder = builder.getMember(requiredMemberName);
if (memberOnBuilder.isPresent()) {
return memberOnBuilder;
return memberOnBuilder.get();
}

// Get the most recently introduced mixin member with the given name.
Expand All @@ -155,18 +152,18 @@ protected Optional<MemberShape> getRequiredMixinMember(
}

if (mostRecentMember == null) {
return Optional.empty();
return null;
}

return Optional.of(MemberShape.builder()
return MemberShape.builder()
.id(getId().withMember(requiredMemberName))
.target(mostRecentMember.getTarget())
.source(getSourceLocation())
.addMixin(mostRecentMember)
.build());
.build();
}

protected SourceException missingRequiredMembersException(List<String> missingMembersNames) {
private SourceException missingRequiredMembersException(List<String> missingMembersNames) {
String missingRequired = missingMembersNames.size() > 1 ? "members" : "member";
String missingMembers = String.join(", ", missingMembersNames);
String message = String.format("Missing required %s of shape `%s`: %s",
Expand Down

0 comments on commit a5efba9

Please sign in to comment.