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<Extend> fromElements(String packageName, ImmutableList<ExtendElement> extendElements) { ImmutableList.Builder<Extend> extendBuilder = new ImmutableList.Builder<>(); for (ExtendElement extendElement : extendElements) { extendBuilder.add(new Extend(extendElement.location(), extendElement.documentation(), extendElement.name(), Field.fromElements(packageName, extendElement.fields(), true))); } return extendBuilder.build(); }
public final String toSchema() { StringBuilder builder = new StringBuilder(); appendDocumentation(builder, documentation()); builder.append("extend ") .append(name()) .append(" {"); if (!fields().isEmpty()) { builder.append('\n'); for (FieldElement field : fields()) { appendIndented(builder, field.toSchema()); } } return builder.append("}\n").toString(); }
@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); }
builder.append('\n'); for (ExtendElement extendDeclaration : extendDeclarations()) { builder.append(extendDeclaration.toSchema());
public final String toSchema() { StringBuilder builder = new StringBuilder(); appendDocumentation(builder, documentation()); builder.append("extend ") .append(name()) .append(" {"); if (!fields().isEmpty()) { builder.append('\n'); for (FieldElement field : fields()) { appendIndented(builder, field.toSchema()); } } return builder.append("}\n").toString(); }
builder.append('\n'); for (ExtendElement extendDeclaration : extendDeclarations()) { builder.append(extendDeclaration.toSchema());
Builder(ExtendElement source) { this.location = source.location(); this.name = source.name(); this.documentation = source.documentation(); this.fields = source.fields(); } @Override
public final String toSchema() { StringBuilder builder = new StringBuilder(); appendDocumentation(builder, documentation()); builder.append("extend ") .append(name()) .append(" {"); if (!fields().isEmpty()) { builder.append('\n'); for (FieldElement field : fields()) { appendIndented(builder, field.toSchema()); } } return builder.append("}\n").toString(); }
/** 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(); }
@Test public void simpleToSchema() { ExtendElement extend = ExtendElement.builder(location) .name("Name") .fields(ImmutableList.of( FieldElement.builder(location) .label(REQUIRED) .type("string") .name("name") .tag(1) .build())) .build(); String expected = "" + "extend Name {\n" + " required string name = 1;\n" + "}\n"; assertThat(extend.toSchema()).isEqualTo(expected); }
builder.append('\n'); for (ExtendElement extendDeclaration : extendDeclarations()) { builder.append(extendDeclaration.toSchema());
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof ExtendElement) { ExtendElement that = (ExtendElement) o; return (this.location.equals(that.location())) && (this.name.equals(that.name())) && (this.documentation.equals(that.documentation())) && (this.fields.equals(that.fields())); } return false; }
@Test public void addMultipleExtends() { ExtendElement extend1 = ExtendElement.builder(location).name("Extend1").build(); ExtendElement extend2 = ExtendElement.builder(location).name("Extend2").build(); ProtoFileElement file = ProtoFileElement.builder(location) .extendDeclarations(ImmutableList.of(extend1, extend2)) .build(); assertThat(file.extendDeclarations()).hasSize(2); }
@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); } }
static ImmutableList<Extend> fromElements(String packageName, ImmutableList<ExtendElement> extendElements) { ImmutableList.Builder<Extend> extendBuilder = new ImmutableList.Builder<>(); for (ExtendElement extendElement : extendElements) { extendBuilder.add(new Extend(extendElement.location(), extendElement.documentation(), extendElement.name(), Field.fromElements(packageName, extendElement.fields(), true))); } return extendBuilder.build(); }
@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); }
static ImmutableList<Extend> fromElements(String packageName, ImmutableList<ExtendElement> extendElements) { ImmutableList.Builder<Extend> extendBuilder = new ImmutableList.Builder<>(); for (ExtendElement extendElement : extendElements) { extendBuilder.add(new Extend(extendElement.location(), extendElement.documentation(), extendElement.name(), Field.fromElements(packageName, extendElement.fields(), true))); } return extendBuilder.build(); }
@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); }