protobuf_plugin.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 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 #ifndef GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H
20 #define GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H
21 
22 #include <vector>
23 
24 #include "src/compiler/config.h"
29 
30 // Get leading or trailing comments in a string.
31 template <typename DescriptorType>
32 inline std::string GetCommentsHelper(const DescriptorType* desc, bool leading,
33  const std::string& prefix) {
35 }
36 
38  public:
40  : method_(method) {}
41 
42  std::string name() const { return method_->name(); }
43 
45  return grpc_cpp_generator::ClassName(method_->input_type(), true);
46  }
48  return grpc_cpp_generator::ClassName(method_->output_type(), true);
49  }
50 
52  return method_->input_type()->file()->name();
53  }
55  return method_->output_type()->file()->name();
56  }
57 
58  // TODO(https://github.com/grpc/grpc/issues/18800): Clean this up.
61  bool generate_in_pb2_grpc, std::string import_prefix,
62  const std::vector<std::string>& prefixes_to_filter) const final {
63  return grpc_python_generator::GetModuleAndMessagePath(
65  import_prefix, prefixes_to_filter);
66  }
67 
70  bool generate_in_pb2_grpc, std::string import_prefix,
71  const std::vector<std::string>& prefixes_to_filter) const final {
72  return grpc_python_generator::GetModuleAndMessagePath(
74  import_prefix, prefixes_to_filter);
75  }
76 
77  bool NoStreaming() const {
78  return !method_->client_streaming() && !method_->server_streaming();
79  }
80 
81  bool ClientStreaming() const { return method_->client_streaming(); }
82 
83  bool ServerStreaming() const { return method_->server_streaming(); }
84 
85  bool BidiStreaming() const {
86  return method_->client_streaming() && method_->server_streaming();
87  }
88 
90  return GetCommentsHelper(method_, true, prefix);
91  }
92 
94  return GetCommentsHelper(method_, false, prefix);
95  }
96 
97  vector<std::string> GetAllComments() const {
98  return grpc_python_generator::get_all_comments(method_);
99  }
100 
101  private:
103 };
104 
106  public:
108  : service_(service) {}
109 
110  std::string name() const { return service_->name(); }
111 
112  int method_count() const { return service_->method_count(); }
113  std::unique_ptr<const grpc_generator::Method> method(int i) const {
114  return std::unique_ptr<const grpc_generator::Method>(
115  new ProtoBufMethod(service_->method(i)));
116  }
117 
119  return GetCommentsHelper(service_, true, prefix);
120  }
121 
123  return GetCommentsHelper(service_, false, prefix);
124  }
125 
126  vector<std::string> GetAllComments() const {
127  return grpc_python_generator::get_all_comments(service_);
128  }
129 
130  private:
132 };
133 
135  public:
138 
139  void Print(const std::map<std::string, std::string>& vars,
140  const char* string_template) {
141  printer_.Print(vars, string_template);
142  }
143 
144  void Print(const char* string) { printer_.Print(string); }
145  void PrintRaw(const char* string) { printer_.PrintRaw(string); }
146  void Indent() { printer_.Indent(); }
147  void Outdent() { printer_.Outdent(); }
148 
149  private:
152 };
153 
155  public:
157 
158  std::string filename() const { return file_->name(); }
161  }
162 
163  std::string package() const { return file_->package(); }
164  std::vector<std::string> package_parts() const {
165  return grpc_generator::tokenize(package(), ".");
166  }
167 
168  std::string additional_headers() const { return ""; }
169 
170  int service_count() const { return file_->service_count(); }
171  std::unique_ptr<const grpc_generator::Service> service(int i) const {
172  return std::unique_ptr<const grpc_generator::Service>(
173  new ProtoBufService(file_->service(i)));
174  }
175 
176  std::unique_ptr<grpc_generator::Printer> CreatePrinter(
177  std::string* str) const {
178  return std::unique_ptr<grpc_generator::Printer>(new ProtoBufPrinter(str));
179  }
180 
182  return GetCommentsHelper(file_, true, prefix);
183  }
184 
186  return GetCommentsHelper(file_, false, prefix);
187  }
188 
189  vector<std::string> GetAllComments() const {
190  return grpc_python_generator::get_all_comments(file_);
191  }
192 
193  vector<std::string> GetImportNames() const {
194  vector<std::string> proto_names;
195  for (int i = 0; i < file_->dependency_count(); ++i) {
196  const auto& dep = *file_->dependency(i);
197  proto_names.push_back(dep.name());
198  }
199  return proto_names;
200  }
201 
202  private:
204 };
205 
206 #endif // GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H
ProtoBufService::service_
const grpc::protobuf::ServiceDescriptor * service_
Definition: protobuf_plugin.h:131
xds_interop_client.str
str
Definition: xds_interop_client.py:487
ProtoBufService::method_count
int method_count() const
Definition: protobuf_plugin.h:112
python_generator_helpers.h
ProtoBufPrinter::Print
void Print(const char *string)
Definition: protobuf_plugin.h:144
grpc_generator::Service
Definition: schema_interface.h:80
ProtoBufPrinter::printer_
grpc::protobuf::io::Printer printer_
Definition: protobuf_plugin.h:151
grpc::protobuf::FileDescriptor
GRPC_CUSTOM_FILEDESCRIPTOR FileDescriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:85
grpc_python_generator::generator_file_name
std::string generator_file_name
Definition: src/compiler/python_generator.cc:55
ProtoBufFile::filename
std::string filename() const
Definition: protobuf_plugin.h:158
ProtoBufFile::file_
const grpc::protobuf::FileDescriptor * file_
Definition: protobuf_plugin.h:203
ProtoBufFile::filename_without_ext
std::string filename_without_ext() const
Definition: protobuf_plugin.h:159
grpc_generator::GetPrefixedComments
std::string GetPrefixedComments(const DescriptorType *desc, bool leading, const std::string &prefix)
Definition: generator_helpers.h:257
grpc_generator::File
Definition: schema_interface.h:102
ProtoBufPrinter::Outdent
void Outdent()
Definition: protobuf_plugin.h:147
ProtoBufFile::service
std::unique_ptr< const grpc_generator::Service > service(int i) const
Definition: protobuf_plugin.h:171
config.h
ProtoBufPrinter::PrintRaw
void PrintRaw(const char *string)
Definition: protobuf_plugin.h:145
ProtoBufMethod
Definition: protobuf_plugin.h:37
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
GetCommentsHelper
std::string GetCommentsHelper(const DescriptorType *desc, bool leading, const std::string &prefix)
Definition: protobuf_plugin.h:32
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
ProtoBufMethod::BidiStreaming
bool BidiStreaming() const
Definition: protobuf_plugin.h:85
grpc::protobuf::io::Printer
GRPC_CUSTOM_PRINTER Printer
Definition: src/compiler/config.h:54
ProtoBufFile::package_parts
std::vector< std::string > package_parts() const
Definition: protobuf_plugin.h:164
ProtoBufMethod::NoStreaming
bool NoStreaming() const
Definition: protobuf_plugin.h:77
ProtoBufFile
Definition: protobuf_plugin.h:154
ProtoBufPrinter::ProtoBufPrinter
ProtoBufPrinter(std::string *str)
Definition: protobuf_plugin.h:136
grpc_generator::tokenize
std::vector< std::string > tokenize(const std::string &input, const std::string &delimiters)
Definition: generator_helpers.h:82
ProtoBufService::GetTrailingComments
std::string GetTrailingComments(const std::string prefix) const
Definition: protobuf_plugin.h:122
ProtoBufMethod::input_type_name
std::string input_type_name() const
Definition: protobuf_plugin.h:44
grpc_generator::Method
Definition: schema_interface.h:54
ProtoBufPrinter
Definition: protobuf_plugin.h:134
ProtoBufMethod::output_type_name
std::string output_type_name() const
Definition: protobuf_plugin.h:47
ProtoBufMethod::ClientStreaming
bool ClientStreaming() const
Definition: protobuf_plugin.h:81
grpc::protobuf::io::StringOutputStream
GRPC_CUSTOM_STRINGOUTPUTSTREAM StringOutputStream
Definition: src/compiler/config.h:56
ProtoBufService::name
std::string name() const
Definition: protobuf_plugin.h:110
ProtoBufFile::GetTrailingComments
std::string GetTrailingComments(const std::string prefix) const
Definition: protobuf_plugin.h:185
ProtoBufFile::service_count
int service_count() const
Definition: protobuf_plugin.h:170
ProtoBufMethod::get_module_and_message_path_input
bool get_module_and_message_path_input(std::string *str, std::string generator_file_name, bool generate_in_pb2_grpc, std::string import_prefix, const std::vector< std::string > &prefixes_to_filter) const final
Definition: protobuf_plugin.h:59
ProtoBufMethod::ServerStreaming
bool ServerStreaming() const
Definition: protobuf_plugin.h:83
python_private_generator.h
ProtoBufFile::GetAllComments
vector< std::string > GetAllComments() const
Definition: protobuf_plugin.h:189
grpc::protobuf::MethodDescriptor
GRPC_CUSTOM_METHODDESCRIPTOR MethodDescriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:87
ProtoBufService::ProtoBufService
ProtoBufService(const grpc::protobuf::ServiceDescriptor *service)
Definition: protobuf_plugin.h:107
ProtoBufPrinter::Print
void Print(const std::map< std::string, std::string > &vars, const char *string_template)
Definition: protobuf_plugin.h:139
ProtoBufMethod::GetLeadingComments
std::string GetLeadingComments(const std::string prefix) const
Definition: protobuf_plugin.h:89
ProtoBufService
Definition: protobuf_plugin.h:105
ProtoBufService::method
std::unique_ptr< const grpc_generator::Method > method(int i) const
Definition: protobuf_plugin.h:113
ProtoBufMethod::method_
const grpc::protobuf::MethodDescriptor * method_
Definition: protobuf_plugin.h:102
ProtoBufFile::package
std::string package() const
Definition: protobuf_plugin.h:163
ProtoBufFile::CreatePrinter
std::unique_ptr< grpc_generator::Printer > CreatePrinter(std::string *str) const
Definition: protobuf_plugin.h:176
ProtoBufFile::additional_headers
std::string additional_headers() const
Definition: protobuf_plugin.h:168
generate_in_pb2_grpc
bool generate_in_pb2_grpc
Definition: python_private_generator.h:40
ProtoBufFile::GetLeadingComments
std::string GetLeadingComments(const std::string prefix) const
Definition: protobuf_plugin.h:181
grpc_generator::StripProto
std::string StripProto(std::string filename)
Definition: generator_helpers.h:54
grpc_generator::Printer
Definition: schema_interface.h:89
ProtoBufFile::GetImportNames
vector< std::string > GetImportNames() const
Definition: protobuf_plugin.h:193
prefix
static const char prefix[]
Definition: head_of_line_blocking.cc:28
grpc_cpp_generator::ClassName
std::string ClassName(const grpc::protobuf::Descriptor *descriptor, bool qualified)
Definition: cpp_generator_helpers.h:37
ProtoBufMethod::get_output_type_name
std::string get_output_type_name() const
Definition: protobuf_plugin.h:54
desc
#define desc
Definition: bloaty/third_party/protobuf/src/google/protobuf/extension_set.h:338
ProtoBufFile::ProtoBufFile
ProtoBufFile(const grpc::protobuf::FileDescriptor *file)
Definition: protobuf_plugin.h:156
ProtoBufMethod::GetTrailingComments
std::string GetTrailingComments(const std::string prefix) const
Definition: protobuf_plugin.h:93
ProtoBufPrinter::output_stream_
grpc::protobuf::io::StringOutputStream output_stream_
Definition: protobuf_plugin.h:150
ProtoBufMethod::get_input_type_name
std::string get_input_type_name() const
Definition: protobuf_plugin.h:51
schema_interface.h
ProtoBufService::GetAllComments
vector< std::string > GetAllComments() const
Definition: protobuf_plugin.h:126
ProtoBufMethod::name
std::string name() const
Definition: protobuf_plugin.h:42
cpp_generator_helpers.h
ProtoBufMethod::get_module_and_message_path_output
bool get_module_and_message_path_output(std::string *str, std::string generator_file_name, bool generate_in_pb2_grpc, std::string import_prefix, const std::vector< std::string > &prefixes_to_filter) const final
Definition: protobuf_plugin.h:68
ProtoBufService::GetLeadingComments
std::string GetLeadingComments(const std::string prefix) const
Definition: protobuf_plugin.h:118
ProtoBufPrinter::Indent
void Indent()
Definition: protobuf_plugin.h:146
service
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
Definition: ProtoMethod.h:25
ProtoBufMethod::GetAllComments
vector< std::string > GetAllComments() const
Definition: protobuf_plugin.h:97
method
NSString * method
Definition: ProtoMethod.h:28
grpc::protobuf::ServiceDescriptor
GRPC_CUSTOM_SERVICEDESCRIPTOR ServiceDescriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:88
ProtoBufMethod::ProtoBufMethod
ProtoBufMethod(const grpc::protobuf::MethodDescriptor *method)
Definition: protobuf_plugin.h:39
fix_build_deps.dep
string dep
Definition: fix_build_deps.py:439
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:57