static ImmutableList<OneOfElement> toElements(ImmutableList<OneOf> oneOfs) { ImmutableList.Builder<OneOfElement> elements = new ImmutableList.Builder<>(); for (OneOf oneOf : oneOfs) { elements.add(OneOfElement.builder() .documentation(oneOf.documentation) .name(oneOf.name) .fields(Field.toElements(oneOf.fields)) .build()); } return elements.build(); } }
static ImmutableList<OneOf> fromElements(String packageName, ImmutableList<OneOfElement> elements, boolean extension) { ImmutableList.Builder<OneOf> oneOfs = ImmutableList.builder(); for (OneOfElement oneOf : elements) { if (!oneOf.groups().isEmpty()) { GroupElement group = oneOf.groups().get(0); throw new IllegalStateException(group.location() + ": 'group' is not supported"); } oneOfs.add(new OneOf(oneOf.name(), oneOf.documentation(), Field.fromElements(packageName, oneOf.fields(), extension))); } return oneOfs.build(); }
builder.append('\n'); for (OneOfElement oneOf : oneOfs()) { appendIndented(builder, oneOf.toSchema());
public final String toSchema() { StringBuilder builder = new StringBuilder(); appendDocumentation(builder, documentation()); builder.append("oneof ").append(name()).append(" {"); if (!fields().isEmpty()) { builder.append('\n'); for (FieldElement field : fields()) { appendIndented(builder, field.toSchema()); } } if (!groups().isEmpty()) { builder.append('\n'); for (GroupElement group : groups()) { appendIndented(builder, group.toSchema()); } } return builder.append("}\n").toString(); }
private OneOfElement readOneOf(String documentation) { OneOfElement.Builder builder = OneOfElement.builder() .name(reader.readName()) .documentation(documentation); ImmutableList.Builder<FieldElement> fields = ImmutableList.builder(); ImmutableList.Builder<GroupElement> groups = ImmutableList.builder(); reader.require('{'); while (true) { String nestedDocumentation = reader.readDocumentation(); if (reader.peekChar('}')) break; Location location = reader.location(); String type = reader.readDataType(); if (type.equals("group")) { groups.add(readGroup(location, nestedDocumentation, null)); } else { fields.add(readField(location, nestedDocumentation, null, type)); } } return builder.fields(fields.build()) .groups(groups.build()) .build(); }
builder.append('\n'); for (OneOfElement oneOf : oneOfs()) { appendIndented(builder, oneOf.toSchema());
Builder(OneOfElement source) { this.name = source.name(); this.documentation = source.documentation(); this.fields = source.fields(); this.groups = source.groups(); } @Override
@Test public void oneOfToSchema() { TypeElement element = MessageElement.builder(location) .name("Message") .oneOfs(ImmutableList.of( OneOfElement.builder() .name("hi") .fields(ImmutableList.of( FieldElement.builder(location) .type("string") .name("name") .tag(1) .build())) .build())) .build(); String expected = "" + "message Message {\n" + " oneof hi {\n" + " string name = 1;\n" + " }\n" + "}\n"; assertThat(element.toSchema()).isEqualTo(expected); }
builder.append('\n'); for (OneOfElement oneOf : oneOfs()) { appendIndented(builder, oneOf.toSchema());
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OneOfElement) { OneOfElement that = (OneOfElement) o; return (this.name.equals(that.name())) && (this.documentation.equals(that.documentation())) && (this.fields.equals(that.fields())) && (this.groups.equals(that.groups())); } return false; }
@Test public void addMultipleOneOfs() { OneOfElement hi = OneOfElement.builder() .name("hi") .fields(ImmutableList.of( FieldElement.builder(location) .type("string") .name("name") .tag(1) .build())) .build(); OneOfElement hey = OneOfElement.builder() .name("hey") .fields(ImmutableList.of( FieldElement.builder(location) .type("string") .name("city") .tag(2) .build())) .build(); MessageElement element = MessageElement.builder(location) .name("Message") .oneOfs(ImmutableList.of(hi, hey)) .build(); assertThat(element.oneOfs()).hasSize(2); }
static ImmutableList<OneOf> fromElements(String packageName, ImmutableList<OneOfElement> elements, boolean extension) { ImmutableList.Builder<OneOf> oneOfs = ImmutableList.builder(); for (OneOfElement oneOf : elements) { if (!oneOf.groups().isEmpty()) { throw new IllegalStateException("'group' is not supported"); } oneOfs.add(new OneOf(oneOf.name(), oneOf.documentation(), Field.fromElements(packageName, oneOf.fields(), extension))); } return oneOfs.build(); }
static ImmutableList<OneOf> fromElements(String packageName, ImmutableList<OneOfElement> elements, boolean extension) { ImmutableList.Builder<OneOf> oneOfs = ImmutableList.builder(); for (OneOfElement oneOf : elements) { if (!oneOf.groups().isEmpty()) { throw new IllegalStateException("'group' is not supported"); } oneOfs.add(new OneOf(oneOf.name(), oneOf.documentation(), Field.fromElements(packageName, oneOf.fields(), extension))); } return oneOfs.build(); }
.build())) .oneOfs(ImmutableList.of( OneOfElement.builder() .name("page_info") .fields(ImmutableList.of(FieldElement.builder(location.at(4, 5))
public final String toSchema() { StringBuilder builder = new StringBuilder(); appendDocumentation(builder, documentation()); builder.append("oneof ").append(name()).append(" {"); if (!fields().isEmpty()) { builder.append('\n'); for (FieldElement field : fields()) { appendIndented(builder, field.toSchema()); } } if (!groups().isEmpty()) { builder.append('\n'); for (GroupElement group : groups()) { appendIndented(builder, group.toSchema()); } } return builder.append("}\n").toString(); }
.name("Message") .oneOfs(ImmutableList.of( OneOfElement.builder() .name("hi") .fields(ImmutableList.of(
public final String toSchema() { StringBuilder builder = new StringBuilder(); appendDocumentation(builder, documentation()); builder.append("oneof ").append(name()).append(" {"); if (!fields().isEmpty()) { builder.append('\n'); for (FieldElement field : fields()) { appendIndented(builder, field.toSchema()); } } if (!groups().isEmpty()) { builder.append('\n'); for (GroupElement group : groups()) { appendIndented(builder, group.toSchema()); } } return builder.append("}\n").toString(); }
.build())) .oneOfs(ImmutableList.of( OneOfElement.builder() .name("page_info") .fields(ImmutableList.of(FieldElement.builder(location.at(4, 5))
static ImmutableList<OneOfElement> toElements(ImmutableList<OneOf> oneOfs) { ImmutableList.Builder<OneOfElement> elements = new ImmutableList.Builder<>(); for (OneOf oneOf : oneOfs) { elements.add(OneOfElement.builder() .documentation(oneOf.documentation) .name(oneOf.name) .fields(Field.toElements(oneOf.fields)) .build()); } return elements.build(); } }