Skip to content

Commit e44fd7f

Browse files
authored
Add a way to take a consumer of a writer in the FileManifest (#2599)
* Add a way to take a consumer of a writer in the FileManifest * Expand introduced start import * Move the default implementation to the interface * Apply spotless
1 parent 6f02795 commit e44fd7f

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

smithy-build/src/main/java/software/amazon/smithy/build/FileManifest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
import java.io.InputStream;
99
import java.io.Reader;
1010
import java.io.StringReader;
11+
import java.io.Writer;
12+
import java.nio.file.Files;
1113
import java.nio.file.Path;
1214
import java.nio.file.Paths;
1315
import java.util.List;
1416
import java.util.Set;
17+
import java.util.function.Consumer;
1518
import java.util.stream.Collectors;
1619
import software.amazon.smithy.model.node.Node;
1720

@@ -212,7 +215,8 @@ default Path writeFile(String path, InputStream fileContentsInputStream) {
212215
*/
213216
@SuppressWarnings("unused")
214217
default Path writeJson(Path path, Node node) {
215-
return writeFile(path, Node.prettyPrintJson(node).trim() + "\n");
218+
writeUsing(path, (writer) -> Node.prettyPrintJsonToWriter(node, writer));
219+
return path;
216220
}
217221

218222
/**
@@ -227,6 +231,23 @@ default Path writeJson(String path, Node node) {
227231
return writeJson(Paths.get(path), node);
228232
}
229233

234+
/**
235+
* Adds a file to the result generated by passing a writer to the consumer.
236+
*
237+
* @param path Relative path to write to.
238+
* @param consumer Node data to write to JSON.
239+
*/
240+
default void writeUsing(Path path, Consumer<Writer> consumer) {
241+
path = addFile(path);
242+
243+
try (Writer writer = Files.newBufferedWriter(path)) {
244+
consumer.accept(writer);
245+
writer.write('\n');
246+
} catch (IOException e) {
247+
throw new SmithyBuildException("Unable to create a write to file `" + path + "`: " + e.getMessage(), e);
248+
}
249+
}
250+
230251
/**
231252
* Checks if the given file is stored in the manifest.
232253
*

smithy-build/src/main/java/software/amazon/smithy/build/MockManifest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.io.IOException;
99
import java.io.InputStream;
1010
import java.io.Reader;
11+
import java.io.StringWriter;
12+
import java.io.Writer;
1113
import java.nio.charset.Charset;
1214
import java.nio.charset.StandardCharsets;
1315
import java.nio.file.InvalidPathException;
@@ -19,6 +21,7 @@
1921
import java.util.Optional;
2022
import java.util.Set;
2123
import java.util.concurrent.ConcurrentSkipListMap;
24+
import java.util.function.Consumer;
2225

2326
/**
2427
* A {@link FileManifest} that doesn't actually store files on disk.
@@ -128,6 +131,13 @@ public Path writeFile(Path path, InputStream fileContentsInputStream) {
128131
}
129132
}
130133

134+
@Override
135+
public void writeUsing(Path path, Consumer<Writer> consumer) {
136+
Writer writer = new StringWriter();
137+
consumer.accept(writer);
138+
writeFile(path, writer.toString());
139+
}
140+
131141
/**
132142
* Gets the contents of a stored file as a String.
133143
*

smithy-model/src/main/java/software/amazon/smithy/model/node/Node.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static java.lang.String.format;
88

99
import java.io.InputStream;
10+
import java.io.Writer;
1011
import java.util.Arrays;
1112
import java.util.Collection;
1213
import java.util.Comparator;
@@ -144,6 +145,27 @@ public static String prettyPrintJson(Node node, String indentString) {
144145
return NodeHandler.prettyPrint(node, indentString);
145146
}
146147

148+
/**
149+
* Writes the contents of a pretty-printed Node to the given writer.
150+
*
151+
* @param node Node to write.
152+
* @param writer Writer to write to.
153+
*/
154+
public static void prettyPrintJsonToWriter(Node node, Writer writer) {
155+
NodeHandler.prettyPrintToWriter(node, " ", writer);
156+
}
157+
158+
/**
159+
* Writes the contents of a pretty-printed Node to the given writer.
160+
*
161+
* @param node Node to write.
162+
* @param indentString String to use for indention.
163+
* @param writer Writer to write to.
164+
*/
165+
public static void prettyPrintJsonToWriter(Node node, String indentString, Writer writer) {
166+
NodeHandler.prettyPrintToWriter(node, indentString, writer);
167+
}
168+
147169
/**
148170
* Writes the contents of a Node to a non-pretty-printed JSON string.
149171
*

smithy-model/src/main/java/software/amazon/smithy/model/node/internal/NodeHandler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package software.amazon.smithy.model.node.internal;
66

77
import java.io.StringWriter;
8+
import java.io.Writer;
89
import java.math.BigDecimal;
910
import java.math.BigInteger;
1011
import software.amazon.smithy.model.SourceLocation;
@@ -45,6 +46,12 @@ public static String prettyPrint(Node node, String indentString) {
4546
return writer.toString();
4647
}
4748

49+
@SmithyInternalApi
50+
public static void prettyPrintToWriter(Node node, String indentString, Writer writer) {
51+
JsonWriter jsonWriter = new PrettyPrintWriter(writer, indentString);
52+
node.accept(new NodeWriter(jsonWriter));
53+
}
54+
4855
@Override
4956
void endNull(SourceLocation location) {
5057
value = new NullNode(location);

0 commit comments

Comments
 (0)