blob: b475e3633a45b9e25528e7f7e37b17fff99edaa8 [file] [log] [blame]
Yuchen Zeng387afd72016-08-26 14:25:21 -07001/*
2 *
3 * Copyright 2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "service_describer.h"
35
36#include <google/protobuf/descriptor.pb.h>
37#include <iostream>
38#include <sstream>
39#include <string>
40#include <vector>
41
42namespace grpc {
43namespace testing {
44
45grpc::string DescribeServiceList(std::vector<grpc::string> service_list,
46 google::protobuf::DescriptorPool& desc_pool) {
47 std::stringstream result;
48 for (auto const& service : service_list) {
49 const google::protobuf::ServiceDescriptor* service_desc =
50 desc_pool.FindServiceByName(service);
51 if (service_desc != nullptr) {
52 result << DescribeService(service_desc);
53 }
54 }
55 return result.str();
56}
57
58grpc::string DescribeService(
59 const google::protobuf::ServiceDescriptor* service) {
60 grpc::string result;
61 if (service->options().deprecated()) {
62 result.append("DEPRECATED\n");
63 }
64 result.append("filename: " + service->file()->name() + "\n");
65
66 grpc::string package = service->full_name();
67 size_t pos = package.rfind("." + service->name());
68 if (pos != grpc::string::npos) {
69 package.erase(pos);
70 result.append("package: " + package + ";\n");
71 }
72 result.append("service " + service->name() + " {\n");
73 for (int i = 0; i < service->method_count(); ++i) {
74 result.append(DescribeMethod(service->method(i)));
75 }
76 result.append("}\n\n");
77 return result;
78}
79
80grpc::string DescribeMethod(const google::protobuf::MethodDescriptor* method) {
81 std::stringstream result;
82 result << " rpc " << method->name()
83 << (method->client_streaming() ? "(stream " : "(")
84 << method->input_type()->full_name() << ") returns "
85 << (method->server_streaming() ? "(stream " : "(")
86 << method->output_type()->full_name() << ") {}\n";
87 if (method->options().deprecated()) {
88 result << " DEPRECATED";
89 }
90 return result.str();
91}
92
93grpc::string SummarizeService(
94 const google::protobuf::ServiceDescriptor* service) {
95 grpc::string result;
96 for (int i = 0; i < service->method_count(); ++i) {
97 result.append(SummarizeMethod(service->method(i)));
98 }
99 return result;
100}
101
102grpc::string SummarizeMethod(const google::protobuf::MethodDescriptor* method) {
103 grpc::string result = method->name();
104 result.append("\n");
105 return result;
106}
107
108} // namespace testing
109} // namespace grpc