+ "}"; FieldElement field = FieldElement.builder(location.at(2, 5)) .label(OPTIONAL) .type("field.type") .name("has_options") .tag(3) .options(ImmutableList.of( OptionElement.create("option_map", Kind.MAP, ImmutableMap.of("nested_map", ImmutableMap.of("key", "value", "key2", OptionElement.create("option_string", Kind.LIST, ImmutableList.of("string1", "string2"), true))) .build();
@Test public void adjacentStringsConcatenated() { String proto = "" + "message Foo {\n" + " optional string name = 1 [\n" + " default = \"concat \"\n" + " 'these '\n" + " \"please\"\n" + " ];\n" + "}"; FieldElement field = FieldElement.builder(location.at(2, 3)) .label(OPTIONAL) .type("string") .name("name") .tag(1) .defaultValue("concat these please") .build(); TypeElement messageElement = MessageElement.builder(location.at(1, 1)) .name("Foo") .fields(ImmutableList.of(field)) .build(); ProtoFileElement expected = ProtoFileElement.builder(location) .types(ImmutableList.of(messageElement)) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
/** Reads an field declaration and returns it. */ private FieldElement readField( Location location, String documentation, @Nullable Field.Label label, String type) { String name = reader.readName(); reader.require('='); int tag = reader.readInt(); FieldElement.Builder builder = FieldElement.builder(location) .label(label) .type(type) .name(name) .tag(tag); List<OptionElement> options = new OptionReader(reader).readOptions(); reader.require(';'); options = new ArrayList<>(options); // Mutable copy for extractDefault. String defaultValue = stripDefault(options); documentation = reader.tryAppendTrailingDocumentation(documentation); return builder.documentation(documentation) .defaultValue(defaultValue) .options(ImmutableList.copyOf(options)) .build(); }
.label(label) .type(type) .name(name) .tag(tag); OptionElement option = readOption('='); if (option.name().equals("default")) { builder.defaultValue(String.valueOf(option.value())); // Defaults aren't options! } else { options.add(option); return builder.documentation(documentation) .options(options.build()) .build();
.label(label) .type(type) .name(name) .tag(tag); OptionElement option = readOption('='); if (option.name().equals("default")) { builder.defaultValue(String.valueOf(option.value())); // Defaults aren't options! } else { options.add(option); return builder.documentation(documentation) .options(options.build()) .build();
static ImmutableList<FieldElement> toElements(ImmutableList<Field> fields) { ImmutableList.Builder<FieldElement> elements = new ImmutableList.Builder<>(); for (Field field : fields) { elements.add(FieldElement.builder(field.location) .label(field.label) .name(field.name) .documentation(field.documentation) .tag(field.tag) .defaultValue(field.defaultValue) .options(field.options.toElements()) .type(field.elementType) .build()); } return elements.build(); }
static ImmutableList<FieldElement> toElements(ImmutableList<Field> fields) { ImmutableList.Builder<FieldElement> elements = new ImmutableList.Builder<>(); for (Field field : fields) { elements.add(FieldElement.builder(field.location) .label(field.label) .name(field.name) .documentation(field.documentation) .tag(field.tag) .defaultValue(field.defaultValue) .options(field.options.toElements()) .type(field.elementType) .build()); } return elements.build(); }
.fields(ImmutableList.of( FieldElement.builder(location.at(2, 3)) .label(REQUIRED) .type("any") .name("f1") .tag(1) .build(), FieldElement.builder(location.at(3, 3)) .label(REQUIRED) .type("bool") .name("f2") .tag(2) .build(), FieldElement.builder(location.at(4, 3)) .label(REQUIRED) .type("bytes") .name("f3") .tag(3) .build(), FieldElement.builder(location.at(5, 3)) .label(REQUIRED) .type("double") .name("f4") .tag(4) .build(), FieldElement.builder(location.at(6, 3)) .label(REQUIRED) .type("float") .name("f5")
.name("Test") .fields(ImmutableList.of(FieldElement.builder(location.at(2, 3)) .label(OPTIONAL) .type("int32") .name("default_int32") .tag(401) .options(ImmutableList.of( OptionElement.create("x", Kind.NUMBER, "2147483647"))) .build(), FieldElement.builder(location.at(3, 3)) .label(OPTIONAL) .type("uint32") .name("default_uint32") .tag(402) .options(ImmutableList.of( OptionElement.create("x", Kind.NUMBER, "4294967295"))) .build(), FieldElement.builder(location.at(4, 3)) .label(OPTIONAL) .type("sint32") .name("default_sint32") .tag(403) .options(ImmutableList.of( OptionElement.create("x", Kind.NUMBER, "-2147483648"))) .build(), FieldElement.builder(location.at(5, 3)) .label(OPTIONAL) .type("fixed32") .name("default_fixed32")
@Test public void multipleEverythingToSchema() { FieldElement field1 = FieldElement.builder(location) .label(REQUIRED) .type("string") .name("name") .tag(1) .build(); FieldElement field2 = FieldElement.builder(location) .label(REQUIRED) .type("bool") .name("other_name") .tag(2) .build(); FieldElement oneOf1Field = FieldElement.builder(location) .type("string") .name("namey") .tag(3) .build(); OneOfElement oneOf1 = OneOfElement.builder() .name("thingy") .build(); FieldElement oneOf2Field = FieldElement.builder(location) .type("string") .name("namer") .tag(4) .build(); OneOfElement oneOf2 = OneOfElement.builder() .name("thinger")
.fields(ImmutableList.of( FieldElement.builder(location.at(2, 3)) .label(REQUIRED) .type("string") .name("query") .tag(1) .build())) .oneOfs(ImmutableList.of( OneOfElement.builder() .name("page_info") .fields(ImmutableList.of(FieldElement.builder(location.at(4, 5)) .type("int32") .name("page_number") .tag(2) .build())) .groups(ImmutableList.of(GroupElement.builder(location.at(5, 5)) .name("Stuff") .fields(ImmutableList.of( FieldElement.builder(location.at(6, 7)) .label(OPTIONAL) .type("int32") .name("result_per_page") .tag(4) .build(), FieldElement.builder(location.at(7, 7)) .label(OPTIONAL) .type("int32") .name("page_count")
.fields(ImmutableList.of( FieldElement.builder(location.at(2, 3)) .label(OPTIONAL) .type("bool") .name("koka_ko_koka_ko") .tag(1) .options(ImmutableList.of( OptionElement.create("old_default", Kind.BOOLEAN, "true"))) .build(), FieldElement.builder(location.at(3, 3)) .label(OPTIONAL) .type("bool") .name("coodle_doodle_do") .tag(2) .options(ImmutableList.of( OptionElement.create("delay", Kind.NUMBER, "100", true), OptionElement.create("old_default", Kind.BOOLEAN, "false"))) .build(), FieldElement.builder(location.at(4, 3)) .label(OPTIONAL) .type("bool") .name("coo_coo_ca_cha") .tag(3) .options(ImmutableList.of( OptionElement.create("old_default", Kind.BOOLEAN, "true"), OptionElement.create("delay", Kind.NUMBER, "200", true))) .build(), FieldElement.builder(location.at(5, 3)) .label(OPTIONAL)
.fields(ImmutableList.of( FieldElement.builder(location) .type("string") .name("name") .tag(1) .build())) .groups(ImmutableList.of(GroupElement.builder(location.at(5, 5)) .name("Stuff") .fields(ImmutableList.of( FieldElement.builder(location.at(6, 7)) .label(OPTIONAL) .type("int32") .name("result_per_page") .tag(4) .build(), FieldElement.builder(location.at(7, 7)) .label(OPTIONAL) .type("int32") .name("page_count") .tag(5) .build() )) .build()))
.fields(ImmutableList.of( FieldElement.builder(location.at(3, 5)) .label(REQUIRED) .type("string") .name("url") .tag(2) .build(), FieldElement.builder(location.at(4, 5)) .label(OPTIONAL) .type("string") .name("title") .tag(3) .build(), FieldElement.builder(location.at(5, 5)) .label(REPEATED) .type("string") .name("snippets") .tag(4) .build() )) .build()
.fields(ImmutableList.of( FieldElement.builder(location.at(3, 5)) .label(REQUIRED) .type("string") .name("url") .tag(2) .build(), FieldElement.builder(location.at(4, 5)) .label(OPTIONAL) .type("string") .name("title") .tag(3) .build(), FieldElement.builder(location.at(5, 5)) .label(REPEATED) .type("string") .name("snippets") .tag(4) .build() )) .build()
.fields(ImmutableList.of( FieldElement.builder(location.at(2, 3)) .label(REQUIRED) .type("string") .name("query") .tag(1) .build())) .oneOfs(ImmutableList.of( OneOfElement.builder() .name("page_info") .fields(ImmutableList.of(FieldElement.builder(location.at(4, 5)) .type("int32") .name("page_number") .tag(2) .build(), FieldElement.builder(location.at(5, 5)) .type("int32") .name("result_per_page") .tag(3) .build())) .build())) .build()))