/** Reads an extend declaration. */ private ExtendElement readExtend(Location location, String documentation) { String name = reader.readName(); ExtendElement.Builder builder = ExtendElement.builder(location) .name(name) .documentation(documentation); reader.require('{'); ImmutableList.Builder<FieldElement> fields = ImmutableList.builder(); while (true) { String nestedDocumentation = reader.readDocumentation(); if (reader.peekChar('}')) break; Object declared = readDeclaration(nestedDocumentation, Context.EXTEND); if (declared instanceof FieldElement) { fields.add((FieldElement) declared); } } return builder.fields(fields.build()) .build(); }
/** Reads an extend declaration. */ private ExtendElement readExtend(Location location, String documentation) { String name = reader.readName(); ExtendElement.Builder builder = ExtendElement.builder(location) .name(name) .documentation(documentation); reader.require('{'); ImmutableList.Builder<FieldElement> fields = ImmutableList.builder(); while (true) { String nestedDocumentation = reader.readDocumentation(); if (reader.peekChar('}')) break; Object declared = readDeclaration(nestedDocumentation, Context.EXTEND); if (declared instanceof FieldElement) { fields.add((FieldElement) declared); } } return builder.fields(fields.build()) .build(); }
/** Reads an extend declaration. */ private ExtendElement readExtend(Location location, String documentation) { String name = readName(); ExtendElement.Builder builder = ExtendElement.builder(location) .name(name) .documentation(documentation); if (readChar() != '{') throw unexpected("expected '{'"); ImmutableList.Builder<FieldElement> fields = ImmutableList.builder(); while (true) { String nestedDocumentation = readDocumentation(); if (peekChar() == '}') { pos++; break; } Object declared = readDeclaration(nestedDocumentation, Context.EXTEND); if (declared instanceof FieldElement) { fields.add((FieldElement) declared); } } return builder.fields(fields.build()) .build(); }
static ImmutableList<ExtendElement> toElements(ImmutableList<Extend> extendList) { ImmutableList.Builder<ExtendElement> elements = new ImmutableList.Builder<>(); for (Extend extend : extendList) { elements.add(ExtendElement.builder(extend.location) .documentation(extend.documentation) .name(extend.name) .fields(Field.toElements(extend.fields)) .build()); } return elements.build(); }
static ImmutableList<ExtendElement> toElements(ImmutableList<Extend> extendList) { ImmutableList.Builder<ExtendElement> elements = new ImmutableList.Builder<>(); for (Extend extend : extendList) { elements.add(ExtendElement.builder(extend.location) .documentation(extend.documentation) .name(extend.name) .fields(Field.toElements(extend.fields)) .build()); } return elements.build(); }
.build(); ExtendElement extend1 = ExtendElement.builder(location.at(16, 1)) .name("Extend1") .build(); ExtendElement extend2 = ExtendElement.builder(location.at(17, 1)) .name("Extend2") .build(); OptionElement option1 = OptionElement.create("kit", OptionElement.Kind.STRING, "kat"); OptionElement option2 = OptionElement.create("foo", OptionElement.Kind.STRING, "bar");
.build(); ExtendElement extend1 = ExtendElement.builder(location.at(13, 1)) .name("Extend1") .build(); ExtendElement extend2 = ExtendElement.builder(location.at(14, 1)) .name("Extend2") .build(); OptionElement option1 = OptionElement.create("kit", Kind.STRING, "kat"); OptionElement option2 = OptionElement.create("foo", Kind.STRING, "bar");
@Test public void extendInMessageWithPackage() throws Exception { String proto = "" + "package kit.kat;\n" + "\n" + "message Bar {\n" + " extend Foo {\n" + " optional Bar bar = 126;\n" + " }\n" + "}"; ProtoFileElement expected = ProtoFileElement.builder(location) .packageName("kit.kat") .types(ImmutableList.<TypeElement>of( MessageElement.builder(location.at(3, 1)) .name("Bar") .build())) .extendDeclarations(ImmutableList.of( ExtendElement.builder(location.at(4, 3)) .name("Foo") .fields(ImmutableList.of( FieldElement.builder(location.at(5, 5)) .label(OPTIONAL) .type("Bar") .name("bar") .tag(126) .build())) .build())) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
@Test public void fqcnExtendInMessageWithPackage() throws Exception { String proto = "" + "package kit.kat;\n" + "\n" + "message Bar {\n" + " extend example.Foo {\n" + " optional Bar bar = 126;\n" + " }\n" + "}"; ProtoFileElement expected = ProtoFileElement.builder(location) .packageName("kit.kat") .types(ImmutableList.<TypeElement>of(MessageElement.builder(location.at(3, 1)) .name("Bar") .build())) .extendDeclarations(ImmutableList.of( ExtendElement.builder(location.at(4, 3)) .name("example.Foo") .fields(ImmutableList.of( FieldElement.builder(location.at(5, 5)) .label(OPTIONAL) .type("Bar") .name("bar") .tag(126) .build())) .build())) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
@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 fqcnExtendInMessage() throws Exception { String proto = "" + "message Bar {\n" + " extend example.Foo {\n" + " optional Bar bar = 126;\n" + " }\n" + "}"; ProtoFileElement expected = ProtoFileElement.builder(location) .types(ImmutableList.<TypeElement>of( MessageElement.builder(location.at(1, 1)).name("Bar").build())) .extendDeclarations(ImmutableList.of( ExtendElement.builder(location.at(2, 3)) .name("example.Foo") .fields(ImmutableList.of( FieldElement.builder(location.at(3, 5)) .label(OPTIONAL) .type("Bar") .name("bar") .tag(126) .build())) .build())) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
@Test public void extendInMessage() throws Exception { String proto = "" + "message Bar {\n" + " extend Foo {\n" + " optional Bar bar = 126;\n" + " }\n" + "}"; ProtoFileElement expected = ProtoFileElement.builder(location) .types(ImmutableList.<TypeElement>of( MessageElement.builder(location.at(1, 1)).name("Bar").build())) .extendDeclarations(ImmutableList.of( ExtendElement.builder(location.at(2, 3)) .name("Foo") .fields(ImmutableList.of( FieldElement.builder(location.at(3, 5)) .label(OPTIONAL) .type("Bar") .name("bar") .tag(126) .build())) .build())) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
@Test public void extend() throws Exception { String proto = "" + "// Extends Foo\n" + "extend Foo {\n" + " optional int32 bar = 126;\n" + "}"; ProtoFileElement expected = ProtoFileElement.builder(location) .extendDeclarations(ImmutableList.of( ExtendElement.builder(location.at(2, 1)) .name("Foo") .documentation("Extends Foo") .fields(ImmutableList.of( FieldElement.builder(location.at(3, 3)) .label(OPTIONAL) .type("int32") .name("bar") .tag(126) .build())) .build())) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
@Test public void simpleWithDocumentationToSchema() { ExtendElement extend = ExtendElement.builder(location) .name("Name") .documentation("Hello") .fields(ImmutableList.of( FieldElement.builder(location) .label(REQUIRED) .type("string") .name("name") .tag(1) .build())) .build(); String expected = "" + "// Hello\n" + "extend Name {\n" + " required string name = 1;\n" + "}\n"; assertThat(extend.toSchema()).isEqualTo(expected); } }
@Test public void addMultipleFields() { FieldElement firstName = FieldElement.builder(location) .label(REQUIRED) .type("string") .name("first_name") .tag(1) .build(); FieldElement lastName = FieldElement.builder(location) .label(REQUIRED) .type("string") .name("last_name") .tag(2) .build(); ExtendElement extend = ExtendElement.builder(location) .name("Name") .fields(ImmutableList.of(firstName, lastName)) .build(); assertThat(extend.fields()).hasSize(2); }
@Test public void simpleWithExtendsToSchema() { ProtoFileElement file = ProtoFileElement.builder(location) .extendDeclarations(ImmutableList.of( ExtendElement.builder(location.at(5, 1)).name("Extend").build())) .types(ImmutableList.<TypeElement>of( MessageElement.builder(location) .name("Message") .build())) .build(); String expected = "" + "// file.proto\n" + "\n" + "message Message {}\n" + "\n" + "extend Extend {}\n"; assertThat(file.toSchema()).isEqualTo(expected); }