From 47e1ce789e10aa30c415e425a957a806d30c1c65 Mon Sep 17 00:00:00 2001 From: Jordon Phillips Date: Wed, 31 Jan 2024 20:23:59 +0100 Subject: [PATCH] Enable custom inline suffixes in IDL serializer This updates the IDL serializer to allow for customizing the inline input / output suffixes. --- .../shapes/SmithyIdlModelSerializer.java | 53 +++++++++++++++++-- .../shapes/SmithyIdlModelSerializerTest.java | 18 +++++++ .../idl-serialization/custom-inline-io.smithy | 16 ++++++ 3 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/shapes/idl-serialization/custom-inline-io.smithy diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializer.java b/smithy-model/src/main/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializer.java index a48cce53175..d8ce43a0a12 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializer.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializer.java @@ -61,12 +61,17 @@ * Serializes a {@link Model} into a set of Smithy IDL files. */ public final class SmithyIdlModelSerializer { + private static final String DEFAULT_INLINE_INPUT_SUFFIX = "Input"; + private static final String DEFAULT_INLINE_OUTPUT_SUFFIX = "Output"; + private final Predicate metadataFilter; private final Predicate shapeFilter; private final Predicate traitFilter; private final Function shapePlacer; private final Path basePath; private final SmithyIdlComponentOrder componentOrder; + private final String inlineInputSuffix; + private final String inlineOutputSuffix; /** * Trait serialization features. @@ -113,6 +118,8 @@ private SmithyIdlModelSerializer(Builder builder) { this.shapePlacer = builder.shapePlacer; } this.componentOrder = builder.componentOrder; + this.inlineInputSuffix = builder.inlineInputSuffix; + this.inlineOutputSuffix = builder.inlineOutputSuffix; } /** @@ -182,14 +189,14 @@ private Set getInlineableShapes(Model fullModel, Collection shap if (!operation.getInputShape().equals(UnitTypeTrait.UNIT)) { Shape inputShape = fullModel.expectShape(operation.getInputShape()); if (shapes.contains(inputShape) && inputShape.hasTrait(InputTrait.ID) - && operation.getInputShape().getName().equals(operation.getId().getName() + "Input")) { + && inputShape.getId().getName().equals(operation.getId().getName() + inlineInputSuffix)) { inlineableShapes.add(operation.getInputShape()); } } if (!operation.getOutputShape().equals(UnitTypeTrait.UNIT)) { Shape outputShape = fullModel.expectShape(operation.getOutputShape()); if (shapes.contains(outputShape) && outputShape.hasTrait(OutputTrait.ID) - && operation.getOutputShape().getName().equals(operation.getId().getName() + "Output")) { + && outputShape.getId().getName().equals(operation.getId().getName() + inlineOutputSuffix)) { inlineableShapes.add(operation.getOutputShape()); } } @@ -201,7 +208,17 @@ private String serializeHeader(Model fullModel, String namespace) { SmithyCodeWriter codeWriter = new SmithyCodeWriter(null, fullModel); NodeSerializer nodeSerializer = new NodeSerializer(codeWriter, fullModel); - codeWriter.write("$$version: \"$L\"", Model.MODEL_VERSION).write(""); + codeWriter.write("$$version: \"$L\"", Model.MODEL_VERSION); + + if (!inlineInputSuffix.equals(DEFAULT_INLINE_INPUT_SUFFIX)) { + codeWriter.write("$$operationInputSuffix: $S", inlineInputSuffix); + } + + if (!inlineOutputSuffix.equals(DEFAULT_INLINE_OUTPUT_SUFFIX)) { + codeWriter.write("$$operationOutputSuffix: $S", inlineOutputSuffix); + } + + codeWriter.write(""); Comparator> comparator = componentOrder.metadataComparator(); @@ -260,6 +277,8 @@ public static final class Builder implements SmithyBuilderThis will also set the "operationInputSuffix" control statement. + * + * @param inlineInputSuffix The suffix to use for inline operation input. + * @return Returns the builder. + */ + public Builder inlineInputSuffix(String inlineInputSuffix) { + this.inlineInputSuffix = inlineInputSuffix; + return this; + } + + /** + * Defines what suffixes are checked on operation output shapes to determine whether + * inline syntax should be used. + * + *

This will also set the "operationOutputSuffix" control statement. + * + * @param inlineOutputSuffix The suffix to use for inline operation output. + * @return Returns the builder. + */ + public Builder inlineOutputSuffix(String inlineOutputSuffix) { + this.inlineOutputSuffix = inlineOutputSuffix; + return this; + } + @Override public SmithyIdlModelSerializer build() { return new SmithyIdlModelSerializer(this); diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializerTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializerTest.java index 62dedc3a6ed..31243fde252 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializerTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializerTest.java @@ -259,4 +259,22 @@ public void handlesEnumMixins() { String serializedString = serialized.entrySet().iterator().next().getValue(); Assertions.assertEquals(expectedOutput, serializedString); } + + @Test + public void handlesCustomInlineSuffixes() { + URL resource = getClass().getResource("idl-serialization/custom-inline-io.smithy"); + Model model = Model.assembler().addImport(resource).assemble().unwrap(); + + Assertions.assertTrue(model.getShape(ShapeId.from("com.example#InlineOperationRequest")).isPresent()); + Assertions.assertTrue(model.getShape(ShapeId.from("com.example#InlineOperationResponse")).isPresent()); + + Map reserialized = SmithyIdlModelSerializer.builder() + .inlineInputSuffix("Request") + .inlineOutputSuffix("Response") + .build() + .serialize(model); + String modelResult = reserialized.values().iterator().next().replace("\r\n", "\n"); + + assertThat(modelResult, equalTo(IoUtils.readUtf8Url(resource).replace("\r\n", "\n"))); + } } diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/shapes/idl-serialization/custom-inline-io.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/shapes/idl-serialization/custom-inline-io.smithy new file mode 100644 index 00000000000..93d3fc9d97f --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/shapes/idl-serialization/custom-inline-io.smithy @@ -0,0 +1,16 @@ +$version: "2.0" +$operationInputSuffix: "Request" +$operationOutputSuffix: "Response" + +namespace com.example + +service InlineService { + operations: [ + InlineOperation + ] +} + +operation InlineOperation { + input := {} + output := {} +}