Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 16 | * |
| 17 | */ |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 18 | |
| 19 | #ifndef GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H |
| 20 | #define GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H |
| 21 | |
| 22 | #include "src/compiler/config.h" |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 23 | #include "src/compiler/cpp_generator_helpers.h" |
Harsh Vardhan | 93077ae | 2016-12-26 18:23:45 +0530 | [diff] [blame] | 24 | #include "src/compiler/python_generator_helpers.h" |
Vijay Pai | 4fdb08a | 2017-11-14 16:48:45 -0800 | [diff] [blame] | 25 | #include "src/compiler/python_private_generator.h" |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 26 | #include "src/compiler/schema_interface.h" |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 27 | |
| 28 | #include <vector> |
| 29 | |
| 30 | // Get leading or trailing comments in a string. |
| 31 | template <typename DescriptorType> |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 32 | inline grpc::string GetCommentsHelper(const DescriptorType* desc, bool leading, |
| 33 | const grpc::string& prefix) { |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 34 | return grpc_generator::GetPrefixedComments(desc, leading, prefix); |
| 35 | } |
| 36 | |
| 37 | class ProtoBufMethod : public grpc_generator::Method { |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 38 | public: |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 39 | ProtoBufMethod(const grpc::protobuf::MethodDescriptor* method) |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 40 | : method_(method) {} |
| 41 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 42 | grpc::string name() const { return method_->name(); } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 43 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 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 | } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 50 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 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 | } |
Harsh Vardhan | 6a9950d | 2017-02-07 12:01:44 +0530 | [diff] [blame] | 57 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 58 | bool get_module_and_message_path_input(grpc::string* str, |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 59 | grpc::string generator_file_name, |
Harsh Vardhan | abf9ce2 | 2017-03-14 22:56:31 +0530 | [diff] [blame] | 60 | bool generate_in_pb2_grpc, |
Harsh Vardhan | d48ee25 | 2017-03-14 23:10:03 +0530 | [diff] [blame] | 61 | grpc::string import_prefix) const { |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 62 | return grpc_python_generator::GetModuleAndMessagePath( |
Harsh Vardhan | abf9ce2 | 2017-03-14 22:56:31 +0530 | [diff] [blame] | 63 | method_->input_type(), str, generator_file_name, generate_in_pb2_grpc, |
| 64 | import_prefix); |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 65 | } |
Harsh Vardhan | 93077ae | 2016-12-26 18:23:45 +0530 | [diff] [blame] | 66 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 67 | bool get_module_and_message_path_output(grpc::string* str, |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 68 | grpc::string generator_file_name, |
Harsh Vardhan | abf9ce2 | 2017-03-14 22:56:31 +0530 | [diff] [blame] | 69 | bool generate_in_pb2_grpc, |
Harsh Vardhan | d48ee25 | 2017-03-14 23:10:03 +0530 | [diff] [blame] | 70 | grpc::string import_prefix) const { |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 71 | return grpc_python_generator::GetModuleAndMessagePath( |
Harsh Vardhan | abf9ce2 | 2017-03-14 22:56:31 +0530 | [diff] [blame] | 72 | method_->output_type(), str, generator_file_name, generate_in_pb2_grpc, |
| 73 | import_prefix); |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 74 | } |
Harsh Vardhan | 93077ae | 2016-12-26 18:23:45 +0530 | [diff] [blame] | 75 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 76 | bool NoStreaming() const { |
| 77 | return !method_->client_streaming() && !method_->server_streaming(); |
| 78 | } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 79 | |
Harsh Vardhan | 974f3d7 | 2017-03-03 14:19:32 +0530 | [diff] [blame] | 80 | bool ClientStreaming() const { return method_->client_streaming(); } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 81 | |
Harsh Vardhan | 974f3d7 | 2017-03-03 14:19:32 +0530 | [diff] [blame] | 82 | bool ServerStreaming() const { return method_->server_streaming(); } |
Harsh Vardhan | cacd5e8 | 2017-02-26 21:18:36 +0530 | [diff] [blame] | 83 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 84 | bool BidiStreaming() const { |
| 85 | return method_->client_streaming() && method_->server_streaming(); |
| 86 | } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 87 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 88 | grpc::string GetLeadingComments(const grpc::string prefix) const { |
| 89 | return GetCommentsHelper(method_, true, prefix); |
| 90 | } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 91 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 92 | grpc::string GetTrailingComments(const grpc::string prefix) const { |
| 93 | return GetCommentsHelper(method_, false, prefix); |
| 94 | } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 95 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 96 | vector<grpc::string> GetAllComments() const { |
| 97 | return grpc_python_generator::get_all_comments(method_); |
| 98 | } |
Harsh Vardhan | 93077ae | 2016-12-26 18:23:45 +0530 | [diff] [blame] | 99 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 100 | private: |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 101 | const grpc::protobuf::MethodDescriptor* method_; |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 102 | }; |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 103 | |
| 104 | class ProtoBufService : public grpc_generator::Service { |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 105 | public: |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 106 | ProtoBufService(const grpc::protobuf::ServiceDescriptor* service) |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 107 | : service_(service) {} |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 108 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 109 | grpc::string name() const { return service_->name(); } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 110 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 111 | int method_count() const { return service_->method_count(); }; |
| 112 | std::unique_ptr<const grpc_generator::Method> method(int i) const { |
| 113 | return std::unique_ptr<const grpc_generator::Method>( |
| 114 | new ProtoBufMethod(service_->method(i))); |
| 115 | }; |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 116 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 117 | grpc::string GetLeadingComments(const grpc::string prefix) const { |
| 118 | return GetCommentsHelper(service_, true, prefix); |
| 119 | } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 120 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 121 | grpc::string GetTrailingComments(const grpc::string prefix) const { |
| 122 | return GetCommentsHelper(service_, false, prefix); |
| 123 | } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 124 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 125 | vector<grpc::string> GetAllComments() const { |
| 126 | return grpc_python_generator::get_all_comments(service_); |
| 127 | } |
Harsh Vardhan | 93077ae | 2016-12-26 18:23:45 +0530 | [diff] [blame] | 128 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 129 | private: |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 130 | const grpc::protobuf::ServiceDescriptor* service_; |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | class ProtoBufPrinter : public grpc_generator::Printer { |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 134 | public: |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 135 | ProtoBufPrinter(grpc::string* str) |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 136 | : output_stream_(str), printer_(&output_stream_, '$') {} |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 137 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 138 | void Print(const std::map<grpc::string, grpc::string>& vars, |
| 139 | const char* string_template) { |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 140 | printer_.Print(vars, string_template); |
| 141 | } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 142 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 143 | void Print(const char* string) { printer_.Print(string); } |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 144 | void Indent() { printer_.Indent(); } |
| 145 | void Outdent() { printer_.Outdent(); } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 146 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 147 | private: |
| 148 | grpc::protobuf::io::StringOutputStream output_stream_; |
| 149 | grpc::protobuf::io::Printer printer_; |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 150 | }; |
| 151 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 152 | class ProtoBufFile : public grpc_generator::File { |
| 153 | public: |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 154 | ProtoBufFile(const grpc::protobuf::FileDescriptor* file) : file_(file) {} |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 155 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 156 | grpc::string filename() const { return file_->name(); } |
| 157 | grpc::string filename_without_ext() const { |
| 158 | return grpc_generator::StripProto(filename()); |
| 159 | } |
| 160 | |
| 161 | grpc::string package() const { return file_->package(); } |
| 162 | std::vector<grpc::string> package_parts() const { |
| 163 | return grpc_generator::tokenize(package(), "."); |
| 164 | } |
| 165 | |
| 166 | grpc::string additional_headers() const { return ""; } |
| 167 | |
| 168 | int service_count() const { return file_->service_count(); }; |
| 169 | std::unique_ptr<const grpc_generator::Service> service(int i) const { |
| 170 | return std::unique_ptr<const grpc_generator::Service>( |
| 171 | new ProtoBufService(file_->service(i))); |
| 172 | } |
| 173 | |
| 174 | std::unique_ptr<grpc_generator::Printer> CreatePrinter( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 175 | grpc::string* str) const { |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 176 | return std::unique_ptr<grpc_generator::Printer>(new ProtoBufPrinter(str)); |
| 177 | } |
| 178 | |
| 179 | grpc::string GetLeadingComments(const grpc::string prefix) const { |
| 180 | return GetCommentsHelper(file_, true, prefix); |
| 181 | } |
| 182 | |
| 183 | grpc::string GetTrailingComments(const grpc::string prefix) const { |
| 184 | return GetCommentsHelper(file_, false, prefix); |
| 185 | } |
| 186 | |
| 187 | vector<grpc::string> GetAllComments() const { |
| 188 | return grpc_python_generator::get_all_comments(file_); |
| 189 | } |
| 190 | |
| 191 | private: |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 192 | const grpc::protobuf::FileDescriptor* file_; |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 193 | }; |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 194 | |
Harsh Vardhan | cacd5e8 | 2017-02-26 21:18:36 +0530 | [diff] [blame] | 195 | #endif // GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H |