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

Add useSymbolWriter function #2328

Merged
merged 1 commit into from
Jun 14, 2024
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 @@ -176,8 +176,7 @@ public final void useFileWriter(String filename, String namespace, Consumer<W> 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}.
*
* <p>Any dependencies (i.e., {@link SymbolDependency}) required by the
* {@code Symbol} are automatically registered with the writer.
Expand All @@ -190,14 +189,10 @@ public final void useFileWriter(String filename, String namespace, Consumer<W> w
* the writer (either a newline or whatever value was set on
* {@link #setAutomaticSeparator}).
*
* <p>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<W> writerConsumer) {
// Checkout/create the appropriate writer for the shape.
Symbol symbol = symbolProvider.toSymbol(shape);
public final void useSymbolWriter(Symbol symbol, Consumer<W> writerConsumer) {
W writer = checkoutWriter(symbol.getDefinitionFile(), symbol.getNamespace());

// Add any needed DECLARE symbols.
Expand All @@ -209,6 +204,31 @@ public void useShapeWriter(Shape shape, Consumer<W> writerConsumer) {
writer.popState();
}

/**
* Gets or creates a writer for a {@link Shape} by converting the {@link Shape}
* to a {@code Symbol}.
*
* <p>Any dependencies (i.e., {@link SymbolDependency}) required by the
* {@code Symbol} are automatically registered with the writer.
*
* <p>Any imports required to declare the {@code Symbol} in code (i.e.,
* {@link SymbolReference.ContextOption#DECLARE}) are automatically
* registered with the writer.
*
* <p>If a writer already exists, a newline is automatically appended to
* the writer (either a newline or whatever value was set on
* {@link #setAutomaticSeparator}).
*
* <p>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<W> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MySimpleWriter> 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();
Expand Down
Loading