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 "src/compiler/config.h" |
23 | #include "src/compiler/cpp_generator_helpers.h" |
24 | #include "src/compiler/python_generator_helpers.h" |
25 | #include "src/compiler/python_private_generator.h" |
26 | #include "src/compiler/schema_interface.h" |
27 | |
28 | #include <vector> |
29 | |
30 | // Get leading or trailing comments in a string. |
31 | template <typename DescriptorType> |
32 | inline grpc::string (const DescriptorType* desc, bool leading, |
33 | const grpc::string& prefix) { |
34 | return grpc_generator::GetPrefixedComments(desc, leading, prefix); |
35 | } |
36 | |
37 | class ProtoBufMethod : public grpc_generator::Method { |
38 | public: |
39 | ProtoBufMethod(const grpc::protobuf::MethodDescriptor* method) |
40 | : method_(method) {} |
41 | |
42 | grpc::string name() const { return method_->name(); } |
43 | |
44 | grpc::string input_type_name() const { |
45 | return grpc_cpp_generator::ClassName(method_->input_type(), true); |
46 | } |
47 | grpc::string output_type_name() const { |
48 | return grpc_cpp_generator::ClassName(method_->output_type(), true); |
49 | } |
50 | |
51 | grpc::string get_input_type_name() const { |
52 | return method_->input_type()->file()->name(); |
53 | } |
54 | grpc::string get_output_type_name() const { |
55 | return method_->output_type()->file()->name(); |
56 | } |
57 | |
58 | // TODO(https://github.com/grpc/grpc/issues/18800): Clean this up. |
59 | bool get_module_and_message_path_input( |
60 | grpc::string* str, grpc::string generator_file_name, |
61 | bool generate_in_pb2_grpc, grpc::string import_prefix, |
62 | const std::vector<grpc::string>& prefixes_to_filter) const final { |
63 | return grpc_python_generator::GetModuleAndMessagePath( |
64 | method_->input_type(), str, generator_file_name, generate_in_pb2_grpc, |
65 | import_prefix, prefixes_to_filter); |
66 | } |
67 | |
68 | bool get_module_and_message_path_output( |
69 | grpc::string* str, grpc::string generator_file_name, |
70 | bool generate_in_pb2_grpc, grpc::string import_prefix, |
71 | const std::vector<grpc::string>& prefixes_to_filter) const final { |
72 | return grpc_python_generator::GetModuleAndMessagePath( |
73 | method_->output_type(), str, generator_file_name, generate_in_pb2_grpc, |
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 | |
89 | grpc::string (const grpc::string prefix) const { |
90 | return GetCommentsHelper(method_, true, prefix); |
91 | } |
92 | |
93 | grpc::string (const grpc::string prefix) const { |
94 | return GetCommentsHelper(method_, false, prefix); |
95 | } |
96 | |
97 | vector<grpc::string> () const { |
98 | return grpc_python_generator::get_all_comments(method_); |
99 | } |
100 | |
101 | private: |
102 | const grpc::protobuf::MethodDescriptor* method_; |
103 | }; |
104 | |
105 | class ProtoBufService : public grpc_generator::Service { |
106 | public: |
107 | ProtoBufService(const grpc::protobuf::ServiceDescriptor* service) |
108 | : service_(service) {} |
109 | |
110 | grpc::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 | |
118 | grpc::string (const grpc::string prefix) const { |
119 | return GetCommentsHelper(service_, true, prefix); |
120 | } |
121 | |
122 | grpc::string (const grpc::string prefix) const { |
123 | return GetCommentsHelper(service_, false, prefix); |
124 | } |
125 | |
126 | vector<grpc::string> () const { |
127 | return grpc_python_generator::get_all_comments(service_); |
128 | } |
129 | |
130 | private: |
131 | const grpc::protobuf::ServiceDescriptor* service_; |
132 | }; |
133 | |
134 | class ProtoBufPrinter : public grpc_generator::Printer { |
135 | public: |
136 | ProtoBufPrinter(grpc::string* str) |
137 | : output_stream_(str), printer_(&output_stream_, '$') {} |
138 | |
139 | void Print(const std::map<grpc::string, grpc::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: |
150 | grpc::protobuf::io::StringOutputStream output_stream_; |
151 | grpc::protobuf::io::Printer printer_; |
152 | }; |
153 | |
154 | class ProtoBufFile : public grpc_generator::File { |
155 | public: |
156 | ProtoBufFile(const grpc::protobuf::FileDescriptor* file) : file_(file) {} |
157 | |
158 | grpc::string filename() const { return file_->name(); } |
159 | grpc::string filename_without_ext() const { |
160 | return grpc_generator::StripProto(filename()); |
161 | } |
162 | |
163 | grpc::string package() const { return file_->package(); } |
164 | std::vector<grpc::string> package_parts() const { |
165 | return grpc_generator::tokenize(package(), "." ); |
166 | } |
167 | |
168 | grpc::string () 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 | grpc::string* str) const { |
178 | return std::unique_ptr<grpc_generator::Printer>(new ProtoBufPrinter(str)); |
179 | } |
180 | |
181 | grpc::string (const grpc::string prefix) const { |
182 | return GetCommentsHelper(file_, true, prefix); |
183 | } |
184 | |
185 | grpc::string (const grpc::string prefix) const { |
186 | return GetCommentsHelper(file_, false, prefix); |
187 | } |
188 | |
189 | vector<grpc::string> () const { |
190 | return grpc_python_generator::get_all_comments(file_); |
191 | } |
192 | |
193 | vector<grpc::string> GetImportNames() const { |
194 | vector<grpc::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: |
203 | const grpc::protobuf::FileDescriptor* file_; |
204 | }; |
205 | |
206 | #endif // GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H |
207 | |