• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include "test/cpp/util/service_describer.h"
20 
21 #include <iostream>
22 #include <sstream>
23 #include <string>
24 #include <vector>
25 
26 namespace grpc {
27 namespace testing {
28 
DescribeServiceList(std::vector<grpc::string> service_list,grpc::protobuf::DescriptorPool & desc_pool)29 grpc::string DescribeServiceList(std::vector<grpc::string> service_list,
30                                  grpc::protobuf::DescriptorPool& desc_pool) {
31   std::stringstream result;
32   for (auto it = service_list.begin(); it != service_list.end(); it++) {
33     auto const& service = *it;
34     const grpc::protobuf::ServiceDescriptor* service_desc =
35         desc_pool.FindServiceByName(service);
36     if (service_desc != nullptr) {
37       result << DescribeService(service_desc);
38     }
39   }
40   return result.str();
41 }
42 
DescribeService(const grpc::protobuf::ServiceDescriptor * service)43 grpc::string DescribeService(const grpc::protobuf::ServiceDescriptor* service) {
44   grpc::string result;
45   if (service->options().deprecated()) {
46     result.append("DEPRECATED\n");
47   }
48   result.append("filename: " + service->file()->name() + "\n");
49 
50   grpc::string package = service->full_name();
51   size_t pos = package.rfind("." + service->name());
52   if (pos != grpc::string::npos) {
53     package.erase(pos);
54     result.append("package: " + package + ";\n");
55   }
56   result.append("service " + service->name() + " {\n");
57   for (int i = 0; i < service->method_count(); ++i) {
58     result.append(DescribeMethod(service->method(i)));
59   }
60   result.append("}\n\n");
61   return result;
62 }
63 
DescribeMethod(const grpc::protobuf::MethodDescriptor * method)64 grpc::string DescribeMethod(const grpc::protobuf::MethodDescriptor* method) {
65   std::stringstream result;
66   result << "  rpc " << method->name()
67          << (method->client_streaming() ? "(stream " : "(")
68          << method->input_type()->full_name() << ") returns "
69          << (method->server_streaming() ? "(stream " : "(")
70          << method->output_type()->full_name() << ") {}\n";
71   if (method->options().deprecated()) {
72     result << " DEPRECATED";
73   }
74   return result.str();
75 }
76 
SummarizeService(const grpc::protobuf::ServiceDescriptor * service)77 grpc::string SummarizeService(
78     const grpc::protobuf::ServiceDescriptor* service) {
79   grpc::string result;
80   for (int i = 0; i < service->method_count(); ++i) {
81     result.append(SummarizeMethod(service->method(i)));
82   }
83   return result;
84 }
85 
SummarizeMethod(const grpc::protobuf::MethodDescriptor * method)86 grpc::string SummarizeMethod(const grpc::protobuf::MethodDescriptor* method) {
87   grpc::string result = method->name();
88   result.append("\n");
89   return result;
90 }
91 
92 }  // namespace testing
93 }  // namespace grpc
94