| 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 |
|
| 29 |
std::string DescribeServiceList(std::vector<std::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 |
|
| 43 |
std::string DescribeService(const grpc::protobuf::ServiceDescriptor* service) { |
| 44 |
std::string result; |
| 45 |
if (service->options().deprecated()) { |
| 46 |
result.append("DEPRECATED\n"); |
| 47 |
} |
| 48 |
result.append("filename: " + service->file()->name() + "\n"); |
| 49 |
|
| 50 |
std::string package = service->full_name(); |
| 51 |
size_t pos = package.rfind("." + service->name()); |
| 52 |
if (pos != std::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 |
|
| 64 |
std::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 |
|
| 77 |
std::string SummarizeService(const grpc::protobuf::ServiceDescriptor* service) { |
| 78 |
std::string result; |
| 79 |
for (int i = 0; i < service->method_count(); ++i) { |
| 80 |
result.append(SummarizeMethod(service->method(i))); |
| 81 |
} |
| 82 |
return result; |
| 83 |
} |
| 84 |
|
| 85 |
std::string SummarizeMethod(const grpc::protobuf::MethodDescriptor* method) { |
| 86 |
std::string result = method->name(); |
| 87 |
result.append("\n"); |
| 88 |
return result; |
| 89 |
} |
| 90 |
|
| 91 |
} // namespace testing |
| 92 |
} // namespace grpc |