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

fix: properly generate code and pass tests when protobuf message definition is empty #131

Merged
merged 2 commits into from
Dec 1, 2023
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 @@ -522,13 +522,8 @@ public static final class Builder {
* Create an empty builder
*/
public Builder() {}

/**
* Create a pre-populated builder
* $constructorParamDocs
*/
public Builder($constructorParams) {
$constructorCode }

$prePopulatedBuilder

/**
* Build a new model record with data set on builder
Expand All @@ -544,19 +539,34 @@ public Builder($constructorParams) {
"private " + field.javaFieldType() + " " + field.nameCamelFirstLower() +
" = " + getDefaultValue(field, msgDef, lookupHelper)
).collect(Collectors.joining(";\n ")))
.replace("$prePopulatedBuilder", generatePrePopulatedBuilder(fields))
.replace("$javaRecordName",javaRecordName)
.replace("$recordParams",fields.stream().map(Field::nameCamelFirstLower).collect(Collectors.joining(", ")))
.replace("$builderMethods", String.join("\n", builderMethods))
.indent(DEFAULT_INDENT);
}

private static String generatePrePopulatedBuilder(List<Field> fields) {
if (fields.isEmpty()) {
return "";
}
return """
/**
* Create a pre-populated builder
* $constructorParamDocs
*/
public Builder($constructorParams) {
$constructorCode }"""
.replace("$constructorParamDocs",fields.stream().map(field ->
"\n * @param "+field.nameCamelFirstLower()+" "+
field.comment().replaceAll("\n", "\n * "+" ".repeat(field.nameCamelFirstLower().length()))
).collect(Collectors.joining(", ")))
).collect(Collectors.joining(", ")))
.replace("$constructorParams",fields.stream().map(field ->
field.javaFieldType() + " " + field.nameCamelFirstLower()
).collect(Collectors.joining(", ")))
).collect(Collectors.joining(", ")))
.replace("$constructorCode",fields.stream().map(field ->
"this.$name = $name;".replace("$name", field.nameCamelFirstLower())
).collect(Collectors.joining("\n")).indent(DEFAULT_INDENT * 2))
.replace("$javaRecordName",javaRecordName)
.replace("$recordParams",fields.stream().map(Field::nameCamelFirstLower).collect(Collectors.joining(", ")))
.replace("$builderMethods", String.join("\n", builderMethods))
).collect(Collectors.joining("\n")).indent(DEFAULT_INDENT * 2))
.indent(DEFAULT_INDENT);
}

Expand Down Expand Up @@ -592,4 +602,4 @@ private static String generateConstructorCode(final Field f) {
}
return sb.toString().indent(DEFAULT_INDENT);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private static String generateModelTestArgumentsMethod(final String modelClassNa
%s
).max().getAsInt();
// create new stream of model objects using lists above as constructor params
ARGUMENTS = IntStream.range(0,maxValues)
ARGUMENTS = (maxValues > 0 ? IntStream.range(0, maxValues) : IntStream.of(0))
.mapToObj(i -> new %s(
%s
)).toList();
Expand All @@ -143,6 +143,10 @@ private static String generateModelTestArgumentsMethod(final String modelClassNa
.collect(Collectors.joining("\n")).indent(DEFAULT_INDENT),
fields.stream()
.map(f -> f.nameCamelFirstLower()+"List.size()")
.collect(Collectors.collectingAndThen(
Collectors.toList(),
list -> list.isEmpty() ? Stream.of("0") : list.stream()
))
.collect(Collectors.joining(",\n")).indent(DEFAULT_INDENT * 2),
modelClassName,
fields.stream().map(field -> "%sList.get(Math.min(i, %sList.size()-1))".formatted(
Expand Down
Loading