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(); }
public static Builder builder(Location location) { return new AutoValue_FieldElement.Builder() .documentation("") .options(ImmutableList.<OptionElement>of()) .location(location); }
.name("has_options") .tag(3) .options(ImmutableList.of( OptionElement.create("option_map", Kind.MAP, ImmutableMap.of("nested_map", ImmutableMap.of("key", "value", "key2",
.options(options.build()) .build();
documentation = reader.tryAppendTrailingDocumentation(documentation); 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(); }
public static Builder builder(Location location) { return new AutoValue_FieldElement.Builder() .documentation("") .options(ImmutableList.<OptionElement>of()) .location(location); }
public static Builder builder(Location location) { return new AutoValue_FieldElement.Builder() .documentation("") .options(ImmutableList.<OptionElement>of()) .location(location); }
.name("default_int32") .tag(401) .options(ImmutableList.of( OptionElement.create("x", Kind.NUMBER, "2147483647"))) .build(), .name("default_uint32") .tag(402) .options(ImmutableList.of( OptionElement.create("x", Kind.NUMBER, "4294967295"))) .build(), .name("default_sint32") .tag(403) .options(ImmutableList.of( OptionElement.create("x", Kind.NUMBER, "-2147483648"))) .build(), .name("default_fixed32") .tag(404) .options(ImmutableList.of( OptionElement.create("x", Kind.NUMBER, "4294967295"))) .build(), .name("default_sfixed32") .tag(405) .options(ImmutableList.of( OptionElement.create("x", Kind.NUMBER, "-2147483648"))) .build(),
.name("koka_ko_koka_ko") .tag(1) .options(ImmutableList.of( OptionElement.create("old_default", Kind.BOOLEAN, "true"))) .build(), .name("coodle_doodle_do") .tag(2) .options(ImmutableList.of( OptionElement.create("delay", Kind.NUMBER, "100", true), OptionElement.create("old_default", Kind.BOOLEAN, "false"))) .name("coo_coo_ca_cha") .tag(3) .options(ImmutableList.of( OptionElement.create("old_default", Kind.BOOLEAN, "true"), OptionElement.create("delay", Kind.NUMBER, "200", true)))
.name("ctype") .tag(1) .options(ImmutableList.of( OptionElement.create("old_default", Kind.ENUM, "STRING"), OptionElement.create("deprecated", Kind.BOOLEAN, "true")))
.name("bar") .tag(1) .options(ImmutableList.of( OptionElement.create("validation.range", Kind.OPTION, OptionElement.create("min", Kind.NUMBER, "1"), true),
@Test public void defaultFieldWithStringEscapes() throws Exception { String proto = "" + "message Foo {\n" + " optional string name = 1 [\n" + " x = \"\\a\\b\\f\\n\\r\\t\\v\1f\01\001\11\011\111\\xe\\Xe\\xE\\xE\\x41\\X41\"\n" + " ];\n" + "}"; FieldElement field = FieldElement.builder(location.at(2, 3)) .label(OPTIONAL) .type("string") .name("name") .tag(1) .options(ImmutableList.of(OptionElement.create("x", Kind.STRING, "\u0007\b\f\n\r\t\u000b\u0001f\u0001\u0001\u0009\u0009I\u000e\u000e\u000e\u000eAA"))) .build(); assertThat(field.options()).containsOnly(OptionElement.create("x", Kind.STRING, "\u0007\b\f\n\r\t\u000b\u0001f\u0001\u0001\u0009\u0009I\u000e\u000e\u000e\u000eAA")); 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); }
@Test public void defaultFieldWithParen() throws Exception { String proto = "" + "message Foo {\n" + " optional string claim_token = 2 [(squareup.redacted) = true];\n" + "}"; FieldElement field = FieldElement.builder(location.at(2, 3)) .label(OPTIONAL) .type("string") .name("claim_token") .tag(2) .options(ImmutableList.of( OptionElement.create("squareup.redacted", Kind.BOOLEAN, "true", true))) .build(); assertThat(field.options()).containsOnly( OptionElement.create("squareup.redacted", Kind.BOOLEAN, "true", true)); 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); }
/** It looks like an option, but 'default' is special. It's missing from descriptor.proto! */ @Test public void defaultFieldOptionIsSpecial() { String proto = "" + "message Message {\n" + " required string a = 1 [default = \"b\", faulted = \"c\"];\n" + "}\n"; ProtoFileElement expected = ProtoFileElement.builder(location) .types(ImmutableList.<TypeElement>of( MessageElement.builder(location.at(1, 1)) .name("Message") .fields(ImmutableList.of( FieldElement.builder(location.at(2, 3)) .label(REQUIRED) .type("string") .name("a") .defaultValue("b") .options(ImmutableList.of( OptionElement.create("faulted", Kind.STRING, "c"))) .tag(1) .build())) .build())) .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(); }
@Test public void fieldWithOptionsToSchema() { FieldElement field = FieldElement.builder(location) .label(REQUIRED) .type("string") .name("name") .tag(1) .options(ImmutableList.of(OptionElement.create("kit", Kind.STRING, "kat"))) .build(); String expected = "required string name = 1 [\n" + " kit = \"kat\"\n" + "];\n"; assertThat(field.toSchema()).isEqualTo(expected); } }
@Test public void field() { FieldElement field = FieldElement.builder(location) .label(OPTIONAL) .type("CType") .name("ctype") .tag(1) .options(ImmutableList.of( OptionElement.create("default", Kind.ENUM, "TEST"), OptionElement.create("deprecated", Kind.BOOLEAN, "true"))) .build(); assertThat(field.options()).containsOnly( // OptionElement.create("default", Kind.ENUM, "TEST"), // OptionElement.create("deprecated", Kind.BOOLEAN, "true")); }
@Test public void addMultipleOptions() { OptionElement kitKat = OptionElement.create("kit", Kind.STRING, "kat"); OptionElement fooBar = OptionElement.create("foo", Kind.STRING, "bar"); FieldElement field = FieldElement.builder(location) .label(REQUIRED) .type("string") .name("name") .tag(1) .options(ImmutableList.of(kitKat, fooBar)) .build(); assertThat(field.options()).hasSize(2); } }