ProtoFileElement readProtoFile() { while (true) { String documentation = reader.readDocumentation(); if (reader.exhausted()) { return fileBuilder.syntax(syntax) .publicImports(publicImports.build()) .imports(imports.build()) .types(nestedTypes.build()) .services(services.build()) .extendDeclarations(extendsList.build()) .options(options.build()) .build(); } Object declaration = readDeclaration(documentation, Context.FILE); if (declaration instanceof TypeElement) { nestedTypes.add((TypeElement) declaration); } else if (declaration instanceof ServiceElement) { services.add((ServiceElement) declaration); } else if (declaration instanceof OptionElement) { options.add((OptionElement) declaration); } else if (declaration instanceof ExtendElement) { extendsList.add((ExtendElement) declaration); } } }
@Test public void syntaxToSchema() { TypeElement element = MessageElement.builder(location).name("Message").build(); ProtoFileElement file = ProtoFileElement.builder(location) .syntax(PROTO_2) .types(ImmutableList.of(element)) .build(); String expected = "" + "// file.proto\n" + "syntax = \"proto2\";\n" + "\n" + "message Message {}\n"; assertThat(file.toSchema()).isEqualTo(expected); } }
@Test public void syntaxSpecified() throws Exception { String proto = "" + "syntax = \"proto3\";\n" + "message Foo {}"; ProtoFileElement expected = ProtoFileElement.builder(location) .syntax(ProtoFile.Syntax.PROTO_3) .types(ImmutableList.<TypeElement>of( MessageElement.builder(location.at(2, 1)) .name("Foo") .build())) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
ProtoFileElement readProtoFile() { while (true) { String documentation = readDocumentation(); if (pos == data.length) { return fileBuilder.syntax(syntax) .publicImports(publicImports.build()) .imports(imports.build()) .types(nestedTypes.build()) .services(services.build()) .extendDeclarations(extendsList.build()) .options(options.build()) .build(); } Object declaration = readDeclaration(documentation, Context.FILE); if (declaration instanceof TypeElement) { nestedTypes.add((TypeElement) declaration); } else if (declaration instanceof ServiceElement) { services.add((ServiceElement) declaration); } else if (declaration instanceof OptionElement) { options.add((OptionElement) declaration); } else if (declaration instanceof ExtendElement) { extendsList.add((ExtendElement) declaration); } } }
ProtoFileElement readProtoFile() { while (true) { String documentation = reader.readDocumentation(); if (reader.exhausted()) { return fileBuilder.syntax(syntax) .publicImports(publicImports.build()) .imports(imports.build()) .types(nestedTypes.build()) .services(services.build()) .extendDeclarations(extendsList.build()) .options(options.build()) .build(); } Object declaration = readDeclaration(documentation, Context.FILE); if (declaration instanceof TypeElement) { nestedTypes.add((TypeElement) declaration); } else if (declaration instanceof ServiceElement) { services.add((ServiceElement) declaration); } else if (declaration instanceof OptionElement) { options.add((OptionElement) declaration); } else if (declaration instanceof ExtendElement) { extendsList.add((ExtendElement) declaration); } } }
ProtoFileElement toElement() { return ProtoFileElement.builder(location) .imports(imports) .publicImports(publicImports) .packageName(packageName) .types(Type.toElements(types)) .services(Service.toElements(services)) .extendDeclarations(Extend.toElements(extendList)) .options(options.toElements()) .syntax(syntax) .build(); }
ProtoFileElement toElement() { return ProtoFileElement.builder(location) .imports(imports) .publicImports(publicImports) .packageName(packageName) .types(Type.toElements(types)) .services(Service.toElements(services)) .extendDeclarations(Extend.toElements(extendList)) .options(options.toElements()) .syntax(syntax) .build(); }
+ "}"; ProtoFileElement expected = ProtoFileElement.builder(location) .syntax(ProtoFile.Syntax.PROTO_3) .types(ImmutableList.<TypeElement>of( MessageElement.builder(location.at(2, 1))
@Test public void proto3ExtensionFieldsPermitRepeated() throws Exception { String proto = "" + "syntax = \"proto3\";\n" + "message Message {\n" + "}\n" + "extend Message {\n" + " repeated string a = 1;\n" + "}"; ProtoFileElement expected = ProtoFileElement.builder(location) .syntax(ProtoFile.Syntax.PROTO_3) .types(ImmutableList.<TypeElement>of( MessageElement.builder(location.at(2, 1)) .name("Message") .build())) .extendDeclarations(ImmutableList.of( ExtendElement.builder(location.at(4, 1)) .name("Message") .fields(ImmutableList.of( FieldElement.builder(location.at(5, 3)) .label(REPEATED) .type("string") .name("a") .tag(1) .build())) .build())) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
@Test public void proto3MessageFieldsPermitRepeated() throws Exception { String proto = "" + "syntax = \"proto3\";\n" + "message Message {\n" + " repeated string a = 1;\n" + "}"; ProtoFileElement expected = ProtoFileElement.builder(location) .syntax(ProtoFile.Syntax.PROTO_3) .types(ImmutableList.<TypeElement>of( MessageElement.builder(location.at(2, 1)) .name("Message") .fields(ImmutableList.of( FieldElement.builder(location.at(3, 3)) .label(REPEATED) .type("string") .name("a") .tag(1) .build())) .build())) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
@Test public void proto3MessageFieldsDoNotRequireLabels() throws Exception { String proto = "" + "syntax = \"proto3\";\n" + "message Message {\n" + " string a = 1;\n" + " int32 b = 2;\n" + "}"; ProtoFileElement expected = ProtoFileElement.builder(location) .syntax(ProtoFile.Syntax.PROTO_3) .types(ImmutableList.<TypeElement>of( MessageElement.builder(location.at(2, 1)) .name("Message") .fields(ImmutableList.of( FieldElement.builder(location.at(3, 3)) .type("string") .name("a") .tag(1) .build(), FieldElement.builder(location.at(4, 3)) .type("int32") .name("b") .tag(2) .build())) .build())) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
@Test public void syntaxMayFollowCommentsAndEmptyLines() throws Exception { String proto = "" + "/* comment 1 */\n" + "// comment 2\n" + "\n" + "syntax = \"proto3\";\n" + "message Foo {}"; ProtoFileElement expected = ProtoFileElement.builder(location) .syntax(ProtoFile.Syntax.PROTO_3) .types(ImmutableList.<TypeElement>of( MessageElement.builder(location.at(5, 1)) .name("Foo") .build())) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
ProtoFileElement toElement() { return ProtoFileElement.builder(location) .imports(imports) .publicImports(publicImports) .packageName(packageName) .types(Type.toElements(types)) .services(Service.toElements(services)) .extendDeclarations(Extend.toElements(extendList)) .options(options.toElements()) .syntax(syntax) .build(); }