static ImmutableList<ServiceElement> toElements(ImmutableList<Service> services) { ImmutableList.Builder<ServiceElement> elements = new ImmutableList.Builder<>(); for (Service service : services) { elements.add(ServiceElement.builder(service.location) .documentation(service.documentation) .name(service.name) .rpcs(Rpc.toElements(service.rpcs)) .options(service.options.toElements()) .build()); } return elements.build(); } }
/** Reads a service declaration and returns it. */ private ServiceElement readService(Location location, String documentation) { String name = reader.readName(); ServiceElement.Builder builder = ServiceElement.builder(location) .name(name) .documentation(documentation); reader.require('{'); ImmutableList.Builder<RpcElement> rpcs = ImmutableList.builder(); ImmutableList.Builder<OptionElement> options = ImmutableList.builder(); while (true) { String rpcDocumentation = reader.readDocumentation(); if (reader.peekChar('}')) break; Object declared = readDeclaration(rpcDocumentation, Context.SERVICE); if (declared instanceof RpcElement) { rpcs.add((RpcElement) declared); } else if (declared instanceof OptionElement) { options.add((OptionElement) declared); } } return builder.options(options.build()) .rpcs(rpcs.build()) .build(); }
@Test public void emptyToSchema() { ServiceElement service = ServiceElement.builder(location).name("Service").build(); String expected = "service Service {}\n"; assertThat(service.toSchema()).isEqualTo(expected); }
@Test public void addMultipleServices() { ServiceElement service1 = ServiceElement.builder(location).name("Service1").build(); ServiceElement service2 = ServiceElement.builder(location).name("Service2").build(); ProtoFileElement file = ProtoFileElement.builder(location) .services(ImmutableList.of(service1, service2)) .build(); assertThat(file.services()).hasSize(2); }
@Test public void simpleWithServicesToSchema() { TypeElement element = MessageElement.builder(location).name("Message").build(); ServiceElement service = ServiceElement.builder(location).name("Service").build(); ProtoFileElement file = ProtoFileElement.builder(location) .types(ImmutableList.of(element)) .services(ImmutableList.of(service)) .build(); String expected = "" + "// file.proto\n" + "\n" + "message Message {}\n" + "\n" + "service Service {}\n"; assertThat(file.toSchema()).isEqualTo(expected); }
@Test public void singleToSchema() { ServiceElement service = ServiceElement.builder(location) .name("Service") .rpcs(ImmutableList.of( RpcElement.builder(location) .name("Name") .requestType("RequestType") .responseType("ResponseType") .build())) .build(); String expected = "" + "service Service {\n" + " rpc Name (RequestType) returns (ResponseType);\n" + "}\n"; assertThat(service.toSchema()).isEqualTo(expected); }
@Test public void addMultipleOptions() { OptionElement kitKat = OptionElement.create("kit", Kind.STRING, "kat"); OptionElement fooBar = OptionElement.create("foo", Kind.STRING, "bar"); ServiceElement service = ServiceElement.builder(location) .name("Service") .options(ImmutableList.of(kitKat, fooBar)) .rpcs(ImmutableList.of( RpcElement.builder(location) .name("Name") .requestType("RequestType") .responseType("ResponseType") .build())) .build(); assertThat(service.options()).hasSize(2); }
@Test public void multipleToSchema() { RpcElement rpc = RpcElement.builder(location) .name("Name") .requestType("RequestType") .responseType("ResponseType") .build(); ServiceElement service = ServiceElement.builder(location) .name("Service") .rpcs(ImmutableList.of(rpc, rpc)) .build(); String expected = "" + "service Service {\n" + " rpc Name (RequestType) returns (ResponseType);\n" + " rpc Name (RequestType) returns (ResponseType);\n" + "}\n"; assertThat(service.toSchema()).isEqualTo(expected); }
@Test public void singleWithOptionsToSchema() { ServiceElement service = ServiceElement.builder(location) .name("Service") .options(ImmutableList.of( OptionElement.create("foo", Kind.STRING, "bar"))) .rpcs(ImmutableList.of( RpcElement.builder(location) .name("Name") .requestType("RequestType") .responseType("ResponseType") .build())) .build(); String expected = "" + "service Service {\n" + " option foo = \"bar\";\n" + "\n" + " rpc Name (RequestType) returns (ResponseType);\n" + "}\n"; assertThat(service.toSchema()).isEqualTo(expected); }
@Test public void singleWithDocumentationToSchema() { ServiceElement service = ServiceElement.builder(location) .name("Service") .documentation("Hello") .rpcs(ImmutableList.of( RpcElement.builder(location) .name("Name") .requestType("RequestType") .responseType("ResponseType") .build())) .build(); String expected = "" + "// Hello\n" + "service Service {\n" + " rpc Name (RequestType) returns (ResponseType);\n" + "}\n"; assertThat(service.toSchema()).isEqualTo(expected); }
@Test public void addMultipleRpcs() { RpcElement firstName = RpcElement.builder(location) .name("FirstName") .requestType("RequestType") .responseType("ResponseType") .build(); RpcElement lastName = RpcElement.builder(location) .name("LastName") .requestType("RequestType") .responseType("ResponseType") .build(); ServiceElement service = ServiceElement.builder(location) .name("Service") .rpcs(ImmutableList.of(firstName, lastName)) .build(); assertThat(service.rpcs()).hasSize(2); }
ProtoFileElement expected = ProtoFileElement.builder(location) .services(ImmutableList.of( ServiceElement.builder(location.at(1, 1)) .name("SearchService") .options(ImmutableList.of(
OptionElement option1 = OptionElement.create("kit", Kind.STRING, "kat"); OptionElement option2 = OptionElement.create("foo", Kind.STRING, "bar"); ServiceElement service1 = ServiceElement.builder(location.at(16, 1)) .name("Service1") .build(); ServiceElement service2 = ServiceElement.builder(location.at(17, 1)) .name("Service2") .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)) .build())) .build(); ServiceElement service2 = ServiceElement.builder(location.at(24, 1)) .name("Service2") .build();
ProtoFileElement expected = ProtoFileElement.builder(location) .services(ImmutableList.of( ServiceElement.builder(location.at(1, 1)) .name("RouteGuide") .rpcs(ImmutableList.of(
static ImmutableList<ServiceElement> toElements(ImmutableList<Service> services) { ImmutableList.Builder<ServiceElement> elements = new ImmutableList.Builder<>(); for (Service service : services) { elements.add(ServiceElement.builder(service.location) .documentation(service.documentation) .name(service.name) .rpcs(Rpc.toElements(service.rpcs)) .options(service.options.toElements()) .build()); } return elements.build(); } }
static ImmutableList<ServiceElement> toElements(ImmutableList<Service> services) { ImmutableList.Builder<ServiceElement> elements = new ImmutableList.Builder<>(); for (Service service : services) { elements.add(ServiceElement.builder(service.location) .documentation(service.documentation) .name(service.name) .rpcs(Rpc.toElements(service.rpcs)) .options(service.options.toElements()) .build()); } return elements.build(); } }
/** Reads a service declaration and returns it. */ private ServiceElement readService(Location location, String documentation) { String name = reader.readName(); ServiceElement.Builder builder = ServiceElement.builder(location) .name(name) .documentation(documentation); reader.require('{'); ImmutableList.Builder<RpcElement> rpcs = ImmutableList.builder(); ImmutableList.Builder<OptionElement> options = ImmutableList.builder(); while (true) { String rpcDocumentation = reader.readDocumentation(); if (reader.peekChar('}')) break; Object declared = readDeclaration(rpcDocumentation, Context.SERVICE); if (declared instanceof RpcElement) { rpcs.add((RpcElement) declared); } else if (declared instanceof OptionElement) { options.add((OptionElement) declared); } } return builder.options(options.build()) .rpcs(rpcs.build()) .build(); }
/** Reads a service declaration and returns it. */ private ServiceElement readService(Location location, String documentation) { String name = readName(); ServiceElement.Builder builder = ServiceElement.builder(location) .name(name) .documentation(documentation); if (readChar() != '{') throw unexpected("expected '{'"); ImmutableList.Builder<RpcElement> rpcs = ImmutableList.builder(); ImmutableList.Builder<OptionElement> options = ImmutableList.builder(); while (true) { String rpcDocumentation = readDocumentation(); if (peekChar() == '}') { pos++; break; } Object declared = readDeclaration(rpcDocumentation, Context.SERVICE); if (declared instanceof RpcElement) { rpcs.add((RpcElement) declared); } else if (declared instanceof OptionElement) { options.add((OptionElement) declared); } } return builder.options(options.build()) .rpcs(rpcs.build()) .build(); }