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(); } }
@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); }
type = reader.readDataType(word); builder.responseType(type); reader.require(')');
type = readDataType(word); builder.responseType(type); if (readChar() != ')') throw unexpected("expected ')'");
type = reader.readDataType(word); builder.responseType(type); reader.require(')');
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(); } }
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(); } }
.name("MethodA") .requestType("Message2") .responseType("Message1") .options(ImmutableList.<OptionElement>of( OptionElement.create("methodoption", OptionElement.Kind.NUMBER, 1)))
.name("Search") .requestType("SearchRequest") .responseType("SearchResponse") .build(), RpcElement.builder(location.at(5, 3)) .name("Purchase") .requestType("PurchaseRequest") .responseType("PurchaseResponse") .options(ImmutableList.of( OptionElement.create("squareup.sake.timeout", Kind.NUMBER, "15", true),
.name("GetFeature") .requestType("Point") .responseType("Feature") .build(), RpcElement.builder(location.at(3, 3)) .name("ListFeatures") .requestType("Rectangle") .responseType("Feature") .responseStreaming(true) .build(), .name("RecordRoute") .requestType("Point") .responseType("RouteSummary") .requestStreaming(true) .build(), .name("RouteChat") .requestType("RouteNote") .responseType("RouteNote") .requestStreaming(true) .responseStreaming(true)
@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 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 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); }
@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 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 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); } }
@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 rpcToSchema() { RpcElement rpc = RpcElement.builder(location) .name("Name") .requestType("RequestType") .responseType("ResponseType") .build(); String expected = "rpc Name (RequestType) returns (ResponseType);\n"; assertThat(rpc.toSchema()).isEqualTo(expected); }
@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); }