private void markRpc(ProtoType declaringType, Rpc rpc) { if (marks.contains(ProtoMember.get(declaringType, rpc.name()))) { markOptions(rpc.options()); mark(rpc.requestType()); mark(rpc.responseType()); } } }
Rpc retainAll(Schema schema, MarkSet markSet) { if (!markSet.contains(requestType) || !markSet.contains(responseType)) return null; Rpc result = new Rpc(location, name, documentation, requestTypeElement, responseTypeElement, requestStreaming, responseStreaming, options.retainAll(schema, markSet)); result.requestType = requestType; result.responseType = responseType; return result; }
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); }
Service retainAll(Schema schema, MarkSet markSet) { // If this service is not retained, prune it. if (!markSet.contains(protoType)) { return null; } ImmutableList.Builder<Rpc> retainedRpcs = ImmutableList.builder(); for (Rpc rpc : rpcs) { Rpc retainedRpc = rpc.retainAll(schema, markSet); if (retainedRpc != null && markSet.contains(ProtoMember.get(protoType, rpc.name()))) { retainedRpcs.add(retainedRpc); } } return new Service(protoType, location, documentation, name, retainedRpcs.build(), options.retainAll(schema, markSet)); }
ProtoType requestType = rpc.requestType(); TypeName requestJavaType = javaGenerator.typeName(requestType); ProtoType responseType = rpc.responseType(); TypeName responseJavaType = javaGenerator.typeName(responseType); MethodSpec.Builder rpcBuilder = MethodSpec.methodBuilder(rpc.name()); rpcBuilder.addModifiers(PUBLIC, ABSTRACT); rpcBuilder.returns(responseJavaType); rpcBuilder.addParameter(requestJavaType, "request"); if (!rpc.documentation().isEmpty()) { rpcBuilder.addJavadoc("$L\n", rpc.documentation());
@Test public void rpcTypeImported() throws Exception { Schema schema = new RepoBuilder() .add("a.proto", "" + "package pa;\n" + "import \"b.proto\";\n" + "service Service {\n" + " rpc Call (pb.B) returns (pb.B);\n" + "}\n") .add("b.proto", "" + "package pb;\n" + "message B {\n" + "}\n") .schema(); Service service = schema.getService("pa.Service"); MessageType b = (MessageType) schema.getType("pb.B"); assertThat(service.rpcs().get(0).requestType()).isEqualTo(b.type()); assertThat(service.rpcs().get(0).responseType()).isEqualTo(b.type()); }
/** Returns the RPC named {@code name}, or null if this service has no such method. */ public Rpc rpc(String name) { for (Rpc rpc : rpcs) { if (rpc.name().equals(name)) { return rpc; } } return null; }
/** * https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#parameter-object * * @param rpc * @return */ protected List<Parameter> getParameters(Rpc rpc) { ArrayList<Parameter> parameters = Lists.newArrayList(); ProtoType protoType = rpc.requestType(); Type type = schmea.getType(protoType); return parameters; }
/** * https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#responseObject * * @return */ protected ApiResponse getSuccessApiResponse(Rpc rpc) { ApiResponse apiResponse = new ApiResponse(); apiResponse.content(getContent(rpc.responseType())); Type type = this.schmea.getType(rpc.responseType()); apiResponse.description(type.documentation()); return apiResponse; }
public static MethodMetaInfo readFrom(Rpc rpc) { Options options = rpc.options(); String path = OptionUtil.readStringOption(options, PATH); String method = OptionUtil.readStringOption(options, METHOD); String summary = OptionUtil.readSummary(rpc.documentation()); List<String> paramTypesStr = OptionUtil.readStringList(options, REQUEST_PARAMS); List<Param> pathParams = buildParams(paramTypesStr); List<String> requestParamsStr = OptionUtil.readStringList(options, PATH_PARAMS); List<Param> requestParams = buildParams(requestParamsStr); List<String> headerParamsStr = OptionUtil.readStringList(options, HEAD_PARAMS); List<Param> headerParams = buildParams(headerParamsStr); return MethodMetaInfo.builder() .path(path) .method(Method.get(method)) .pathParams(pathParams) .requestParams(requestParams) .headerParams(headerParams) .summary(summary) .build(); }
void link(Linker linker) { linker = linker.withContext(this); for (Rpc rpc : rpcs) { rpc.link(linker); } }
void linkOptions(Linker linker) { linker = linker.withContext(this); for (Rpc rpc : rpcs) { rpc.linkOptions(linker); } options.link(linker); }
public TypeSpec generateService(ProtoFile protoFile, Service service) { ClassName apiName = (ClassName) typeName(service.type()); TypeSpec.Builder typeBuilder = TypeSpec.interfaceBuilder(apiName.simpleName()); typeBuilder.addModifiers(PUBLIC); InterfaceMetaInfo interfaceMetaInfo = InterfaceMetaInfo.readFrom(protoFile, service); typeBuilder.addAnnotations(interfaceMetaInfo.generateAnnotations()); if (!service.documentation().isEmpty()) { typeBuilder.addJavadoc("$L\n", service.documentation()); } for (Rpc rpc : service.rpcs()) { ProtoType requestType = rpc.requestType(); TypeName requestJavaType = typeName(requestType); ProtoType responseType = rpc.responseType(); TypeName responseJavaType = typeName(responseType); MethodSpec.Builder rpcBuilder = MethodSpec.methodBuilder(rpc.name()); MethodMetaInfo methodMetaInfo = MethodMetaInfo.readFrom(rpc); rpcBuilder.addAnnotation(serviceAnnotation(rpc, apiName, interfaceMetaInfo)); rpcBuilder.addAnnotation(methodMetaInfo.generateRaptorMethod()); rpcBuilder.addModifiers(PUBLIC, ABSTRACT); rpcBuilder.returns(responseJavaType); ParameterSpec request = ParameterSpec.builder(requestJavaType, "request").build(); rpcBuilder.addParameter(request); if (!rpc.documentation().isEmpty()) { rpcBuilder.addJavadoc("$L\n", rpc.documentation()); } typeBuilder.addMethod(rpcBuilder.build()); } return typeBuilder.build(); }
@Test public void linkService() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "import \"request.proto\";\n" + "import \"response.proto\";\n" + "service Service {\n" + " rpc Call (Request) returns (Response);\n" + "}\n") .add("request.proto", "" + "message Request {\n" + "}\n") .add("response.proto", "" + "message Response {\n" + "}\n") .schema(); Service service = schema.getService("Service"); Rpc call = service.rpc("Call"); assertThat(call.requestType()).isEqualTo(schema.getType("Request").type()); assertThat(call.responseType()).isEqualTo(schema.getType("Response").type()); }
Service retainAll(Schema schema, MarkSet markSet) { // If this service is not retained, prune it. if (!markSet.contains(protoType)) { return null; } ImmutableList.Builder<Rpc> retainedRpcs = ImmutableList.builder(); for (Rpc rpc : rpcs) { Rpc retainedRpc = rpc.retainAll(schema, markSet); if (retainedRpc != null && markSet.contains(ProtoMember.get(protoType, rpc.name()))) { retainedRpcs.add(retainedRpc); } } return new Service(protoType, location, documentation, name, retainedRpcs.build(), options.retainAll(schema, markSet)); }
private void markRoots(Service service) { ProtoType protoType = service.type(); if (identifierSet.includes(protoType)) { marks.root(protoType); queue.add(protoType); } else { for (Rpc rpc : service.rpcs()) { markRoots(ProtoMember.get(protoType, rpc.name())); } } }
/** * https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#requestBodyObject * * @param rpc * @return */ protected RequestBody getRequestBody(Rpc rpc) { RequestBody requestBody = new RequestBody(); ProtoType protoType = rpc.requestType(); Type type = schmea.getType(protoType); requestBody.description(type.documentation()); requestBody.required(true); requestBody.content(getContent(rpc.requestType())); return requestBody; }