Skip to content

Commit

Permalink
Add missing call method, rename new call method
Browse files Browse the repository at this point in the history
The Smithy Kotlin generator uses the call method which was removed
during the refactor. Adding that back, and renaming the newly added call
method that just statically types a `Consumer` to a method named
`consumer`.
  • Loading branch information
mtdowling authored and Michael Dowling committed Mar 2, 2022
1 parent 9673ce0 commit 3999eea
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1432,16 +1432,29 @@ public final String format(Object content, Object... args) {
* <p>You can write:
*
* <pre>{@code
* writer.write("$C", writer.call(w -> w.write("Hi"));
* writer.write("$C", writer.consumer(w -> w.write("Hi"));
* }</pre>
*
* @param consumer The consumer to call.
* @return Returns the consumer as-is, but cast as the appropriate Java type.
*/
public Consumer<T> call(Consumer<T> consumer) {
public Consumer<T> consumer(Consumer<T> consumer) {
return consumer;
}

/**
* Allows calling out to arbitrary code for things like looping or
* conditional writes without breaking method chaining.
*
* @param task Method to invoke.
* @return Returns the CodeWriter.
*/
@SuppressWarnings("unchecked")
public final T call(Runnable task) {
task.run();
return (T) this;
}

/**
* Writes text to the CodeWriter and appends a newline.
*
Expand Down Expand Up @@ -1673,7 +1686,7 @@ String applyFormatter(char identifier, Object value) {
// A single trailing newline is stripped from the result, if present. This is because the $C formatter
// is often used on the same line as other text like:
//
// w.write("Hi, $C.", w.call(writer -> writer.write("name");
// w.write("Hi, $C.", w.consumer(writer -> writer.write("name");
// // Results in "Hi, name.\n" and not "Hi, name\n.\n"
//
// In these cases, the trailing newline introduced in the $C formatter by writer.write() is removed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ public void cFormaterAcceptsConsumersThatAreCodeWriters() {
@Test
public void cFormatterAcceptsConsumersThatAreSubtypesOfCodeWriters() {
CodeWriterSubtype w = new CodeWriterSubtype();
w.write("$C", w.call(writer -> writer.write2("Hello!")));
w.write("$C", w.consumer(writer -> writer.write2("Hello!")));

assertThat(w.toString(), equalTo("Hello!\n"));
}
Expand Down

0 comments on commit 3999eea

Please sign in to comment.