Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implementation of inlined operation input / output shapes #963

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ final class IdlModelParser extends SimpleParser {
private final String filename;
private TraitEntry pendingDocumentationComment;

private String operationInputSuffix = "Input";
private String operationOutputSuffix = "Output";

// A pending trait that also doesn't yet have a resolved trait shape ID.
static final class TraitEntry {
final String traitName;
Expand Down Expand Up @@ -181,6 +184,7 @@ ModelSyntaxException syntax(ShapeId shapeId, String message) {
}

private void parseControlSection() {
Set<String> definedKeys = new HashSet<>();
while (peek() == '$') {
expect('$');
ws();
Expand All @@ -189,15 +193,19 @@ private void parseControlSection() {
expect(':');
ws();

// Validation here for better error location.
if (key.equals("version") && modelFile.getVersion() != Version.UNKNOWN) {
throw syntax("Cannot define multiple versions in the same file");
if (definedKeys.contains(key)) {
throw syntax(format("Duplicate control statement `%s`", key));
}
definedKeys.add(key);

Node value = IdlNodeParser.parseNode(this);

if (key.equals("version")) {
onVersion(value);
} else if (key.equals("operationInputSuffix")) {
operationInputSuffix = value.expectStringNode().getValue();
JordonPhillips marked this conversation as resolved.
Show resolved Hide resolved
} else if (key.equals("operationOutputSuffix")) {
operationOutputSuffix = value.expectStringNode().getValue();
} else {
modelFile.events().add(ValidationEvent.builder()
.id(Validator.MODEL_ERROR)
Expand Down Expand Up @@ -519,6 +527,12 @@ private void parseMember(

ws();
expect(':');

if (peek() == '=') {
throw syntax("Defining structures inline with the `:=` syntax may only be used when "
+ "defining operation input and output shapes.");
}

ws();
ShapeId memberId = parent.withMember(memberName);
MemberShape.Builder memberBuilder = MemberShape.builder().id(memberId).source(memberLocation);
Expand Down Expand Up @@ -570,37 +584,119 @@ private void parseStructuredShape(
ws();

// Parse optional "with" statements to add mixins, but only if it's supported by the version.
if (peek() == 'w') {
expect('w');
expect('i');
expect('t');
expect('h');
parseMixins(id);
parseMembers(id, Collections.emptySet(), structureMember);
}

if (!modelFile.getVersion().supportsMixins()) {
throw syntax(id, "Mixins can only be used with Smithy version 2 or later. "
+ "Attempted to use mixins with version `" + modelFile.getVersion() + "`.");
}
private void parseMixins(ShapeId id) {
if (peek() != 'w') {
return;
}

ws();
do {
String target = ParserUtils.parseShapeId(this);
modelFile.addForwardReference(target, resolved -> modelFile.addPendingMixin(id, resolved));
ws();
} while (peek() != '{');
expect('w');
expect('i');
expect('t');
expect('h');

if (!modelFile.getVersion().supportsMixins()) {
throw syntax(id, "Mixins can only be used with Smithy version 2 or later. "
+ "Attempted to use mixins with version `" + modelFile.getVersion() + "`.");
}

parseMembers(id, Collections.emptySet(), structureMember);
ws();
do {
String target = ParserUtils.parseShapeId(this);
modelFile.addForwardReference(target, resolved -> modelFile.addPendingMixin(id, resolved));
ws();
} while (peek() != '{');
}

private void parseOperationStatement(ShapeId id, SourceLocation location) {
ws();
OperationShape.Builder builder = OperationShape.builder().id(id).source(location);
ObjectNode node = IdlNodeParser.parseObjectNode(this, id.toString());
LoaderUtils.checkForAdditionalProperties(node, id, OPERATION_PROPERTY_NAMES, modelFile.events());
parseProperties(id, propertyName -> {
switch (propertyName) {
case "input":
parseInlineableOperationMember(id, operationInputSuffix, builder::input);
break;
case "output":
parseInlineableOperationMember(id, operationOutputSuffix, builder::output);
break;
case "errors":
parseIdList(builder::addError);
break;
default:
JordonPhillips marked this conversation as resolved.
Show resolved Hide resolved
throw syntax(id, String.format("Unknown property %s for %s", propertyName, id));
}
});
modelFile.onShape(builder);
}

private void parseProperties(ShapeId id, Consumer<String> valueParser) {
ws();
expect('{');
ws();

Set<String> defined = new HashSet<>();
while (!eof() && peek() != '}') {
String key = IdlNodeParser.parseNodeObjectKey(this);
if (defined.contains(key)) {
throw syntax(id, String.format("Duplicate %s binding for %s", key, id));
}
defined.add(key);

ws();
expect(':');
valueParser.accept(key);
ws();
}

expect('}');
}

private void parseInlineableOperationMember(ShapeId id, String suffix, Consumer<ShapeId> consumer) {
if (peek() == '=') {
if (!modelFile.getVersion().supportsInlineOperationIO()) {
throw syntax(id, "Inlined operation inputs and outputs can only be used with Smithy version 2 or "
+ "later. Attempted to use inlined IO with version `" + modelFile.getVersion() + "`.");
}
expect('=');
JordonPhillips marked this conversation as resolved.
Show resolved Hide resolved
clearPendingDocs();
ws();
consumer.accept(parseInlineStructure(id.getName() + suffix));
} else {
ws();
modelFile.addForwardReference(ParserUtils.parseShapeId(this), consumer);
}
}

private ShapeId parseInlineStructure(String name) {
List<TraitEntry> traits = parseDocsAndTraits();
ShapeId id = ShapeId.fromRelative(modelFile.namespace(), name);
SourceLocation location = currentLocation();
parseMixins(id);
StructureShape.Builder builder = StructureShape.builder().id(id).source(location);

modelFile.onShape(builder);
optionalId(node, "input", builder::input);
optionalId(node, "output", builder::output);
optionalIdList(node, ERRORS_KEYS, builder::addError);
parseMembers(id, Collections.emptySet(), true);
addTraits(id, traits);
clearPendingDocs();
ws();
return id;
}

private void parseIdList(Consumer<ShapeId> consumer) {
increaseNestingLevel();
ws();
expect('[');
ws();

while (!eof() && peek() != ']') {
modelFile.addForwardReference(ParserUtils.parseShapeId(this), consumer);
ws();
}

expect(']');
decreaseNestingLevel();
}

private void parseServiceStatement(ShapeId id, SourceLocation location) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ static ObjectNode parseObjectNode(IdlModelParser parser, String parent) {
String key = parseNodeObjectKey(parser);
parser.ws();
parser.expect(':');
if (parser.peek() == '=') {
throw parser.syntax("The `:=` syntax may only be used when defining inline operation input and "
+ "output shapes.");
}
parser.ws();
Node value = parseNode(parser);
StringNode keyNode = new StringNode(key, keyLocation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ boolean supportsRequiredSugar() {
return this == VERSION_1_1 || this == VERSION_2_0;
}

/**
* Checks if this version of the IDL supports inlined operation IO shapes.
*
* @return Returns true if this version supports inlined operation IO shapes.
*/
boolean supportsInlineOperationIO() {
return this == VERSION_1_1 || this == VERSION_2_0;
}

/**
* Perform version-specific trait validation.
*
Expand Down
Loading