static Service fromElement(ProtoType protoType, ServiceElement element) { ImmutableList<Rpc> rpcs = Rpc.fromElements(element.rpcs()); Options options = new Options(Options.SERVICE_OPTIONS, element.options()); return new Service(protoType, element.location(), element.documentation(), element.name(), rpcs, options); }
@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); }
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(); } }
@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); }
public List<RpcMethodDefinition> getRpcMethods() throws IOException { List<RpcMethodDefinition> retval = new ArrayList<>(); ProtoParser parser = new ProtoParser(Location.get(input.getCanonicalPath()), gulpFile(input)); ProtoFileElement element = parser.readProtoFile(); if (protoFileMatchesPackage(element)) { List<ServiceElement> services = element.services(); if (services != null) { for (ServiceElement service : services) { String name = service.name(); List<RpcElement> rpcs = service.rpcs(); if (rpcs != null) { for (RpcElement rpc : rpcs) { RpcMethodDefinition def = new RpcMethodDefinition(name + "." + rpc.name(), rpc.requestType(), rpc.responseType(), element.packageName(), fileName ); retval.add(def); } } } } } return retval; }
static ImmutableList<Service> fromElements(String packageName, ImmutableList<ServiceElement> elements) { ImmutableList.Builder<Service> services = ImmutableList.builder(); for (ServiceElement service : elements) { ProtoType protoType = ProtoType.get(packageName, service.name()); services.add(Service.fromElement(protoType, service)); } return services.build(); }
builder.append('\n'); for (ServiceElement service : services()) { builder.append(service.toSchema());
/** 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(); }
static ImmutableList<Service> fromElements(String packageName, ImmutableList<ServiceElement> elements) { ImmutableList.Builder<Service> services = ImmutableList.builder(); for (ServiceElement service : elements) { ProtoType protoType = ProtoType.get(packageName, service.name()); services.add(Service.fromElement(protoType, service)); } return services.build(); }
builder.append('\n'); for (ServiceElement service : services()) { builder.append(service.toSchema());
public final String toSchema() { StringBuilder builder = new StringBuilder(); appendDocumentation(builder, documentation()); builder.append("service ") .append(name()) .append(" {"); if (!options().isEmpty()) { builder.append('\n'); for (OptionElement option : options()) { appendIndented(builder, option.toSchemaDeclaration()); } } if (!rpcs().isEmpty()) { builder.append('\n'); for (RpcElement rpc : rpcs()) { appendIndented(builder, rpc.toSchema()); } } return builder.append("}\n").toString(); }
@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 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); }
static ImmutableList<Service> fromElements(String packageName, ImmutableList<ServiceElement> elements) { ImmutableList.Builder<Service> services = ImmutableList.builder(); for (ServiceElement service : elements) { ProtoType protoType = ProtoType.get(packageName, service.name()); services.add(Service.fromElement(protoType, service)); } return services.build(); }
builder.append('\n'); for (ServiceElement service : services()) { builder.append(service.toSchema());
Builder(ServiceElement source) { this.location = source.location(); this.name = source.name(); this.documentation = source.documentation(); this.rpcs = source.rpcs(); this.options = source.options(); } @Override
@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 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); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof ServiceElement) { ServiceElement that = (ServiceElement) o; return (this.location.equals(that.location())) && (this.name.equals(that.name())) && (this.documentation.equals(that.documentation())) && (this.rpcs.equals(that.rpcs())) && (this.options.equals(that.options())); } return false; }