static ImmutableList<Rpc> fromElements(ImmutableList<RpcElement> elements) { ImmutableList.Builder<Rpc> rpcs = new ImmutableList.Builder<>(); for (RpcElement rpcElement : elements) { rpcs.add(new Rpc(rpcElement.location(), rpcElement.name(), rpcElement.documentation(), rpcElement.requestType(), rpcElement.responseType(), rpcElement.requestStreaming(), rpcElement.responseStreaming(), new Options(Options.METHOD_OPTIONS, rpcElement.options()))); } return rpcs.build(); }
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<RpcElement> toElements(ImmutableList<Rpc> rpcs) { ImmutableList.Builder<RpcElement> elements = new ImmutableList.Builder<>(); for (Rpc rpc : rpcs) { elements.add(RpcElement.builder(rpc.location) .documentation(rpc.documentation) .name(rpc.name) .requestType(rpc.requestTypeElement) .responseType(rpc.responseTypeElement) .requestStreaming(rpc.requestStreaming) .responseStreaming(rpc.responseStreaming) .options(rpc.options.toElements()) .build()); } return elements.build(); } }
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 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); }
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(); }
public final String toSchema() { StringBuilder builder = new StringBuilder(); appendDocumentation(builder, documentation()); builder.append("rpc ") .append(name()) .append(" ("); if (requestStreaming()) { builder.append("stream "); } builder.append(requestType()) .append(") returns ("); if (responseStreaming()) { builder.append("stream "); } builder.append(responseType()) .append(')'); if (!options().isEmpty()) { builder.append(" {\n"); for (OptionElement option : options()) { appendIndented(builder, option.toSchemaDeclaration()); } builder.append("}"); } return builder.append(";\n").toString(); }
@Test public void rpcWithDocumentationToSchema() { RpcElement rpc = RpcElement.builder(location) .name("Name") .documentation("Hello") .requestType("RequestType") .responseType("ResponseType") .build(); String expected = "" + "// Hello\n" + "rpc Name (RequestType) returns (ResponseType);\n"; assertThat(rpc.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); }
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(); }
Builder(RpcElement source) { this.location = source.location(); this.name = source.name(); this.documentation = source.documentation(); this.requestType = source.requestType(); this.responseType = source.responseType(); this.requestStreaming = source.requestStreaming(); this.responseStreaming = source.responseStreaming(); this.options = source.options(); } @Override
@Test public void rpcWithRequestStreamingToSchema() { RpcElement rpc = RpcElement.builder(location) .name("Name") .requestType("RequestType") .responseType("ResponseType") .requestStreaming(true) .build(); String expected = "rpc Name (stream RequestType) returns (ResponseType);\n"; assertThat(rpc.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); }
static ImmutableList<Rpc> fromElements(ImmutableList<RpcElement> elements) { ImmutableList.Builder<Rpc> rpcs = new ImmutableList.Builder<>(); for (RpcElement rpcElement : elements) { rpcs.add(new Rpc(rpcElement.location(), rpcElement.name(), rpcElement.documentation(), rpcElement.requestType(), rpcElement.responseType(), rpcElement.requestStreaming(), rpcElement.responseStreaming(), new Options(Options.METHOD_OPTIONS, rpcElement.options()))); } return rpcs.build(); }
@Test public void rpcWithResponseStreamingToSchema() { RpcElement rpc = RpcElement.builder(location) .name("Name") .requestType("RequestType") .responseType("ResponseType") .responseStreaming(true) .build(); String expected = "rpc Name (RequestType) returns (stream ResponseType);\n"; assertThat(rpc.toSchema()).isEqualTo(expected); } }
RpcElement.Builder builder = RpcElement.builder(location) .name(reader.readName()) .documentation(documentation);
static ImmutableList<Rpc> fromElements(ImmutableList<RpcElement> elements) { ImmutableList.Builder<Rpc> rpcs = new ImmutableList.Builder<>(); for (RpcElement rpcElement : elements) { rpcs.add(new Rpc(rpcElement.location(), rpcElement.name(), rpcElement.documentation(), rpcElement.requestType(), rpcElement.responseType(), rpcElement.requestStreaming(), rpcElement.responseStreaming(), new Options(Options.METHOD_OPTIONS, rpcElement.options()))); } return rpcs.build(); }
@Test public void rpcWithOptionsToSchema() { RpcElement rpc = RpcElement.builder(location) .name("Name") .requestType("RequestType") .responseType("ResponseType") .options(ImmutableList.of( OptionElement.create("foo", Kind.STRING, "bar"))) .build(); String expected = "" + "rpc Name (RequestType) returns (ResponseType) {\n" + " option foo = \"bar\";\n" + "};\n"; assertThat(rpc.toSchema()).isEqualTo(expected); }
@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); }