Skip to content

Commit

Permalink
Change record style (#57)
Browse files Browse the repository at this point in the history
- Use consistent code writer api.
- Add extra indentation to record parameters.
  • Loading branch information
andriy-dmytruk authored Feb 9, 2024
1 parent 11b220e commit 36b03dc
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,26 +158,29 @@ static CodeBlock makeJavadocWithParameters(CodeBlock javadoc,

static void emitParameters(CodeWriter codeWriter, Iterable<ParameterSpec> parameters,
boolean varargs, boolean isRecord) throws IOException {
codeWriter.emit(CodeBlock.of("($Z"));
Iterator<ParameterSpec> paramIter = parameters.iterator();
boolean hasParameters = paramIter.hasNext();
codeWriter.emit(CodeBlock.of("($Z"), isRecord && hasParameters);

boolean hasParameters = false;
boolean firstParameter = true;
for (Iterator<ParameterSpec> i = parameters.iterator(); i.hasNext(); ) {
if (isRecord && hasParameters) {
codeWriter.indent(2);
}
while (paramIter.hasNext()) {
hasParameters = true;
ParameterSpec parameter = i.next();
ParameterSpec parameter = paramIter.next();
if (!firstParameter) {
codeWriter.emit(",");
codeWriter.emit(CodeBlock.of(","), isRecord);
if (!isRecord)
codeWriter.emitWrappingSpace();
}
if (isRecord)
codeWriter.emit("\n ");
parameter.emit(codeWriter, !i.hasNext() && varargs);
parameter.emit(codeWriter, !paramIter.hasNext() && varargs);
firstParameter = false;
}

if (isRecord && hasParameters) {
codeWriter.emit("\n");
codeWriter.unindent(2);
// Add new line in the end
codeWriter.emit(CodeBlock.of(""), true);
}
codeWriter.emit(")");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ public void basicRecord() throws Exception {
+ "import java.lang.String;\n"
+ "\n"
+ "public record KeyValue(\n"
+ " String key,\n"
+ " String value\n"
+ " String key,\n"
+ " String value\n"
+ ") implements Cloneable {\n"
+ " public KeyValue {\n"
+ " if (key.indexOf(':') != -1) {\n"
Expand Down Expand Up @@ -421,8 +421,8 @@ public void varargsRecord() throws Exception {
+ "import java.lang.String;\n"
+ "\n"
+ "public record Vararg(\n"
+ " int val,\n"
+ " String... keys\n"
+ " int val,\n"
+ " String... keys\n"
+ ") {\n"
+ "}\n");
}
Expand Down Expand Up @@ -1327,8 +1327,8 @@ public void recordComponentJavadoc() throws Exception {
+ " * @param soft true for a soft flour tortilla, or false for a crunchy corn tortilla\n"
+ " */\n"
+ "record Taco(\n"
+ " String shell,\n"
+ " boolean soft\n"
+ " String shell,\n"
+ " boolean soft\n"
+ ") {\n"
+ " /**\n"
+ " * Makes a taco without a cat :(.\n"
Expand Down Expand Up @@ -1608,7 +1608,7 @@ public void recordComponentJavadoc() throws Exception {
+ " }\n"
+ "\n"
+ " record Veggie(\n"
+ " int name\n"
+ " int name\n"
+ " ) {\n"
+ " }\n"
+ "}\n");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.micronaut.sourcegen.javapoet.write;

import io.micronaut.sourcegen.JavaPoetSourceGenerator;
import io.micronaut.sourcegen.model.AnnotationDef;
import io.micronaut.sourcegen.model.ClassTypeDef;
import io.micronaut.sourcegen.model.PropertyDef;
import io.micronaut.sourcegen.model.RecordDef;
import org.junit.Test;

import java.io.IOException;
import java.io.StringWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class RecordWriteTest {


@Test
public void writeSimpleRecord() throws IOException {
RecordDef recordDef = RecordDef.builder("test.TestRecord")
.addProperty(PropertyDef.builder("name").ofType(String.class).build())
.addProperty(PropertyDef.builder("age").ofType(Integer.class)
.addAnnotation(AnnotationDef.builder(
ClassTypeDef.of("jakarta.validation.constraints.Min"))
.addMember("value", 1).build())
.build()
)
.addProperty(PropertyDef.builder("description").ofType(String.class)
.addAnnotation("jakarta.validation.constraints.NotBlank")
.build()
)
.build();
var result = writeRecord(recordDef);

var expected = """
record TestRecord(
String name,
@Min(1) Integer age,
@NotBlank String description
) {
}
""";
assertEquals(expected.strip(), result.strip());
}

private String writeRecord(RecordDef recordDef) throws IOException {
JavaPoetSourceGenerator generator = new JavaPoetSourceGenerator();
String result;
try (StringWriter writer = new StringWriter()) {
generator.write(recordDef, writer);
result = writer.toString();
}

// The regex will skip the imports and make sure it is a record
final Pattern RECORD_REGEX = Pattern.compile("package [^;]+;[\\s\\S]+" +
"(record \\S+[\\s\\S]+})\\s*");
Matcher matcher = RECORD_REGEX.matcher(result);
if (!matcher.matches()) {
fail("Expected record to match regex: \n" + RECORD_REGEX + "\nbut is: \n" + result);
}
return matcher.group(1);
}

}

0 comments on commit 36b03dc

Please sign in to comment.