Skip to content

Commit

Permalink
feat: Support IsLimitSet and similar members (smithy-lang#303)
Browse files Browse the repository at this point in the history
* feat: Support IsLimitSet and similar members
  • Loading branch information
ajewellamz authored and ShubhamChaturvedi7 committed Feb 24, 2024
1 parent 9f1a475 commit 420dac3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,13 @@ public String isSetMethodForStructureMember(final MemberShape memberShape) {
return "IsSet%s".formatted(classPropertyForStructureMember(memberShape));
}

/**
* Returns the name of the given member shape's IsSet member
*/
public String isSetMemberForStructureMember(final MemberShape memberShape) {
return "Is%sSet".formatted(classPropertyForStructureMember(memberShape));
}

/**
* Returns the name of the class property fur use as a variable name, i.e. the first letter is lower case
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,24 @@ private String generateConstructorArg(final MemberShape memberShape) {
nameResolver.classPropertyForStructureMember(memberShape));
}

// return true if this struct/member is one of the special ones with a IsXxxSet member
public boolean memberSupportsIsSet(final MemberShape memberShape)
{
String parent = memberShape.getId().getName();
String member = nameResolver.classPropertyForStructureMember(memberShape);
if (parent.equals("ScanInput")) {
if (member.equals("TotalSegments") ||
member.equals("Segment") ||
member.equals("Limit")) {
return true;
}
}
if (parent.equals("QueryInput") && member.equals("Limit")) {
return true;
}
return false;
}

/**
* Returns:
* "type varName = value.IsSetPropertyName() ? value.PropertyName : (type) null;"
Expand All @@ -488,14 +506,25 @@ public TokenTree generateExtractOptionalMember(final MemberShape memberShape) {
final String varName = nameResolver.variableNameForClassProperty(memberShape);
final String propertyName = nameResolver.classPropertyForStructureMember(memberShape);
if (AwsSdkNameResolverHelpers.isInAwsSdkNamespace(memberShape.getId())) {
return TokenTree.of(
type,
varName,
"= value.%s != null".formatted(propertyName),
"? value.%s :".formatted(propertyName),
"(%s) null;".formatted(type)
);
} else {
if (memberSupportsIsSet(memberShape)) {
final String isSetMember = nameResolver.isSetMemberForStructureMember(memberShape);
return TokenTree.of(
type,
varName,
"= value.%s".formatted(isSetMember),
"? value.%s :".formatted(propertyName),
"(%s) null;".formatted(type)
);
} else {
return TokenTree.of(
type,
varName,
"= value.%s != null".formatted(propertyName),
"? value.%s :".formatted(propertyName),
"(%s) null;".formatted(type)
);
}
} else {
final String isSetMethod = nameResolver.isSetMethodForStructureMember(memberShape);
return TokenTree.of(
type,
Expand Down

0 comments on commit 420dac3

Please sign in to comment.