Skip to content

Commit

Permalink
Use NameAllocator in Message.Builder.build()
Browse files Browse the repository at this point in the history
Use the NameAllocator when inserting string literals, don't use the field
name directly.
  • Loading branch information
Ryan Case committed Jul 4, 2016
1 parent 7b2d875 commit 68f5cc1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1355,9 +1355,10 @@ private MethodSpec builderBuild(
for (int i = 0; i < requiredFields.size(); i++) {
Field requiredField = requiredFields.get(i);
if (i > 0) conditionals.add("\n|| ");
conditionals.add("$L == null", requiredField.name());
conditionals.add("$L == null", nameAllocator.get(requiredField));
if (i > 0) missingArgs.add(",\n");
missingArgs.add("$1L, $1S", requiredField.name());
missingArgs.add("$1L, $2S", nameAllocator.get(requiredField),
requiredField.name());
}

result.beginControlFlow("if ($L)", conditionals.add("$]").build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ public final class JavaGeneratorTest {
assertThat(JavaGenerator.sanitizeJavadoc(input)).isEqualTo(expected);
}

@Test public void generateTypeUsesNameAllocatorInMessageBuilderBuild() {
Schema schema = new SchemaBuilder()
.add("message.proto", ""
+ "message Message {\n"
+ " required float long = 1;\n"
+ "}\n")
.build();
MessageType message = (MessageType) schema.getType("Message");
JavaGenerator javaGenerator = JavaGenerator.get(schema);
TypeSpec typeSpec = javaGenerator.generateType(message);
assertThat(JavaFile.builder("com.squareup.message", typeSpec).build().toString()).contains(""
+ " @Override\n"
+ " public Message build() {\n"
+ " if (long_ == null) {\n"
+ " throw Internal.missingRequiredFields(long_, \"long\");\n"
+ " }\n"
+ " return new Message(long_, super.buildUnknownFields());\n"
+ " }\n");
}

@Test public void generateProtoFields() {
Schema schema = new SchemaBuilder()
.add("message.proto", ""
Expand Down

0 comments on commit 68f5cc1

Please sign in to comment.