diff --git a/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/WriterDelegator.java b/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/WriterDelegator.java index 0156be49ce4..31492a47386 100644 --- a/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/WriterDelegator.java +++ b/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/WriterDelegator.java @@ -176,8 +176,7 @@ public final void useFileWriter(String filename, String namespace, Consumer w } /** - * Gets or creates a writer for a {@link Shape} by converting the {@link Shape} - * to a {@code Symbol}. + * Gets or creates a writer for a {@link Symbol}. * *

Any dependencies (i.e., {@link SymbolDependency}) required by the * {@code Symbol} are automatically registered with the writer. @@ -190,14 +189,10 @@ public final void useFileWriter(String filename, String namespace, Consumer w * the writer (either a newline or whatever value was set on * {@link #setAutomaticSeparator}). * - *

This method may be overridden as needed. - * - * @param shape Shape to create the writer for. + * @param symbol Symbol to create the writer for. * @param writerConsumer Consumer that is expected to write to the {@code SymbolWriter}. */ - public void useShapeWriter(Shape shape, Consumer writerConsumer) { - // Checkout/create the appropriate writer for the shape. - Symbol symbol = symbolProvider.toSymbol(shape); + public final void useSymbolWriter(Symbol symbol, Consumer writerConsumer) { W writer = checkoutWriter(symbol.getDefinitionFile(), symbol.getNamespace()); // Add any needed DECLARE symbols. @@ -209,6 +204,31 @@ public void useShapeWriter(Shape shape, Consumer writerConsumer) { writer.popState(); } + /** + * Gets or creates a writer for a {@link Shape} by converting the {@link Shape} + * to a {@code Symbol}. + * + *

Any dependencies (i.e., {@link SymbolDependency}) required by the + * {@code Symbol} are automatically registered with the writer. + * + *

Any imports required to declare the {@code Symbol} in code (i.e., + * {@link SymbolReference.ContextOption#DECLARE}) are automatically + * registered with the writer. + * + *

If a writer already exists, a newline is automatically appended to + * the writer (either a newline or whatever value was set on + * {@link #setAutomaticSeparator}). + * + *

This method may be overridden as needed. + * + * @param shape Shape to create the writer for. + * @param writerConsumer Consumer that is expected to write to the {@code SymbolWriter}. + */ + public void useShapeWriter(Shape shape, Consumer writerConsumer) { + // Checkout/create the appropriate writer for the shape. + useSymbolWriter(symbolProvider.toSymbol(shape), writerConsumer); + } + /** * Sets the automatic separator that is written to a {@code SymbolWriter} * each time the writer is reused. diff --git a/smithy-codegen-core/src/test/java/software/amazon/smithy/codegen/core/WriterDelegatorTest.java b/smithy-codegen-core/src/test/java/software/amazon/smithy/codegen/core/WriterDelegatorTest.java index 4a5f48de078..b1ed5400aa1 100644 --- a/smithy-codegen-core/src/test/java/software/amazon/smithy/codegen/core/WriterDelegatorTest.java +++ b/smithy-codegen-core/src/test/java/software/amazon/smithy/codegen/core/WriterDelegatorTest.java @@ -47,6 +47,22 @@ public void createsSymbolsAndFilesForShapeWriters() { assertThat(delegator.getWriters(), hasKey(Paths.get("com/foo/Baz.bam").toString())); } + @Test + public void createsFilesForSymbolWriters() { + MockManifest mockManifest = new MockManifest(); + SymbolProvider provider = (shape) -> null; + WriterDelegator delegator = new WriterDelegator<>( + mockManifest, provider, (f, n) -> new MySimpleWriter(n)); + Symbol symbol = Symbol.builder() + .namespace("com.foo", ".") + .name("Baz") + .definitionFile("com/foo/Baz.bam") + .build(); + delegator.useSymbolWriter(symbol, writer -> { }); + + assertThat(delegator.getWriters(), hasKey(Paths.get("com/foo/Baz.bam").toString())); + } + @Test public void aggregatesDependencies() { MockManifest mockManifest = new MockManifest();