service_describer.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include "test/cpp/util/service_describer.h"
  19. #include <iostream>
  20. #include <sstream>
  21. #include <string>
  22. #include <vector>
  23. namespace grpc {
  24. namespace testing {
  25. std::string DescribeServiceList(std::vector<std::string> service_list,
  26. grpc::protobuf::DescriptorPool& desc_pool) {
  27. std::stringstream result;
  28. for (auto it = service_list.begin(); it != service_list.end(); it++) {
  29. auto const& service = *it;
  30. const grpc::protobuf::ServiceDescriptor* service_desc =
  31. desc_pool.FindServiceByName(service);
  32. if (service_desc != nullptr) {
  33. result << DescribeService(service_desc);
  34. }
  35. }
  36. return result.str();
  37. }
  38. std::string DescribeService(const grpc::protobuf::ServiceDescriptor* service) {
  39. std::string result;
  40. if (service->options().deprecated()) {
  41. result.append("DEPRECATED\n");
  42. }
  43. result.append("filename: " + service->file()->name() + "\n");
  44. std::string package = service->full_name();
  45. size_t pos = package.rfind("." + service->name());
  46. if (pos != std::string::npos) {
  47. package.erase(pos);
  48. result.append("package: " + package + ";\n");
  49. }
  50. result.append("service " + service->name() + " {\n");
  51. for (int i = 0; i < service->method_count(); ++i) {
  52. result.append(DescribeMethod(service->method(i)));
  53. }
  54. result.append("}\n\n");
  55. return result;
  56. }
  57. std::string DescribeMethod(const grpc::protobuf::MethodDescriptor* method) {
  58. std::stringstream result;
  59. result << " rpc " << method->name()
  60. << (method->client_streaming() ? "(stream " : "(")
  61. << method->input_type()->full_name() << ") returns "
  62. << (method->server_streaming() ? "(stream " : "(")
  63. << method->output_type()->full_name() << ") {}\n";
  64. if (method->options().deprecated()) {
  65. result << " DEPRECATED";
  66. }
  67. return result.str();
  68. }
  69. std::string SummarizeService(const grpc::protobuf::ServiceDescriptor* service) {
  70. std::string result;
  71. for (int i = 0; i < service->method_count(); ++i) {
  72. result.append(SummarizeMethod(service->method(i)));
  73. }
  74. return result;
  75. }
  76. std::string SummarizeMethod(const grpc::protobuf::MethodDescriptor* method) {
  77. std::string result = method->name();
  78. result.append("\n");
  79. return result;
  80. }
  81. } // namespace testing
  82. } // namespace grpc