Search in sources :

Example 1 with OptionElement

use of com.squareup.wire.schema.internal.parser.OptionElement in project wire by square.

the class ProtoFileTest method roundTripToElement.

@Test
public void roundTripToElement() {
    TypeElement element1 = MessageElement.builder(location.at(11, 1)).name("Message1").documentation("Some comments about Message1").build();
    TypeElement element2 = MessageElement.builder(location.at(12, 1)).name("Message2").fields(ImmutableList.<FieldElement>of(FieldElement.builder(location.at(13, 3)).type("string").name("field").tag(1).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");
    ServiceElement service1 = ServiceElement.builder(location.at(19, 1)).name("Service1").rpcs(ImmutableList.<RpcElement>of(RpcElement.builder(location.at(20, 3)).name("MethodA").requestType("Message2").responseType("Message1").options(ImmutableList.<OptionElement>of(OptionElement.create("methodoption", OptionElement.Kind.NUMBER, 1))).build())).build();
    ServiceElement service2 = ServiceElement.builder(location.at(24, 1)).name("Service2").build();
    ProtoFileElement fileElement = ProtoFileElement.builder(location).packageName("example.simple").imports(ImmutableList.of("example.thing")).publicImports(ImmutableList.of("example.other")).types(ImmutableList.of(element1, element2)).services(ImmutableList.of(service1, service2)).extendDeclarations(ImmutableList.of(extend1, extend2)).options(ImmutableList.of(option1, option2)).build();
    ProtoFile file = ProtoFile.get(fileElement);
    String expected = "" + "// file.proto\n" + "package example.simple;\n" + "\n" + "import \"example.thing\";\n" + "import public \"example.other\";\n" + "\n" + "option kit = \"kat\";\n" + "option foo = \"bar\";\n" + "\n" + "// Some comments about Message1\n" + "message Message1 {}\n" + "message Message2 {\n" + "  string field = 1;\n" + "}\n" + "\n" + "extend Extend1 {}\n" + "extend Extend2 {}\n" + "\n" + "service Service1 {\n" + "  rpc MethodA (Message2) returns (Message1) {\n" + "    option methodoption = 1;\n" + "  };\n" + "}\n" + "service Service2 {}\n";
    assertThat(file.toElement().toSchema()).isEqualTo(expected);
    assertThat(file.toElement()).isEqualToComparingFieldByField(fileElement);
}
Also used : RpcElement(com.squareup.wire.schema.internal.parser.RpcElement) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) ExtendElement(com.squareup.wire.schema.internal.parser.ExtendElement) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) ServiceElement(com.squareup.wire.schema.internal.parser.ServiceElement) Test(org.junit.Test)

Example 2 with OptionElement

use of com.squareup.wire.schema.internal.parser.OptionElement in project wire by square.

the class Options method link.

void link(Linker linker) {
    ImmutableMap<ProtoMember, Object> map = ImmutableMap.of();
    for (OptionElement option : optionElements) {
        Map<ProtoMember, Object> canonicalOption = canonicalizeOption(linker, optionType, option);
        if (canonicalOption != null) {
            map = union(linker, map, canonicalOption);
        }
    }
    this.map = map;
}
Also used : OptionElement(com.squareup.wire.schema.internal.parser.OptionElement)

Example 3 with OptionElement

use of com.squareup.wire.schema.internal.parser.OptionElement in project wire by square.

the class Options method canonicalizeValue.

private Object canonicalizeValue(Linker linker, Field context, Object value) {
    if (value instanceof OptionElement) {
        ImmutableMap.Builder<ProtoMember, Object> result = ImmutableMap.builder();
        OptionElement option = (OptionElement) value;
        Field field = linker.dereference(context, option.name());
        if (field == null) {
            linker.addError("unable to resolve option %s on %s", option.name(), context.type());
        } else {
            ProtoMember protoMember = ProtoMember.get(context.type(), field);
            result.put(protoMember, canonicalizeValue(linker, field, option.value()));
        }
        return coerceValueForField(context, result.build());
    }
    if (value instanceof Map) {
        ImmutableMap.Builder<ProtoMember, Object> result = ImmutableMap.builder();
        for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
            String name = (String) entry.getKey();
            Field field = linker.dereference(context, name);
            if (field == null) {
                linker.addError("unable to resolve option %s on %s", name, context.type());
            } else {
                ProtoMember protoMember = ProtoMember.get(context.type(), field);
                result.put(protoMember, canonicalizeValue(linker, field, entry.getValue()));
            }
        }
        return coerceValueForField(context, result.build());
    }
    if (value instanceof List) {
        ImmutableList.Builder<Object> result = ImmutableList.builder();
        for (Object element : (List<?>) value) {
            result.addAll((List) canonicalizeValue(linker, context, element));
        }
        return coerceValueForField(context, result.build());
    }
    if (value instanceof String) {
        return coerceValueForField(context, value);
    }
    throw new IllegalArgumentException("Unexpected option value: " + value);
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

OptionElement (com.squareup.wire.schema.internal.parser.OptionElement)3 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ExtendElement (com.squareup.wire.schema.internal.parser.ExtendElement)1 FieldElement (com.squareup.wire.schema.internal.parser.FieldElement)1 ProtoFileElement (com.squareup.wire.schema.internal.parser.ProtoFileElement)1 RpcElement (com.squareup.wire.schema.internal.parser.RpcElement)1 ServiceElement (com.squareup.wire.schema.internal.parser.ServiceElement)1 TypeElement (com.squareup.wire.schema.internal.parser.TypeElement)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Test (org.junit.Test)1