Home | History | Annotate | Download | only in compiler
      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 GetCommentsHelper(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   bool get_module_and_message_path_input(grpc::string* str,
     59                                          grpc::string generator_file_name,
     60                                          bool generate_in_pb2_grpc,
     61                                          grpc::string import_prefix) const {
     62     return grpc_python_generator::GetModuleAndMessagePath(
     63         method_->input_type(), str, generator_file_name, generate_in_pb2_grpc,
     64         import_prefix);
     65   }
     66 
     67   bool get_module_and_message_path_output(grpc::string* str,
     68                                           grpc::string generator_file_name,
     69                                           bool generate_in_pb2_grpc,
     70                                           grpc::string import_prefix) const {
     71     return grpc_python_generator::GetModuleAndMessagePath(
     72         method_->output_type(), str, generator_file_name, generate_in_pb2_grpc,
     73         import_prefix);
     74   }
     75 
     76   bool NoStreaming() const {
     77     return !method_->client_streaming() && !method_->server_streaming();
     78   }
     79 
     80   bool ClientStreaming() const { return method_->client_streaming(); }
     81 
     82   bool ServerStreaming() const { return method_->server_streaming(); }
     83 
     84   bool BidiStreaming() const {
     85     return method_->client_streaming() && method_->server_streaming();
     86   }
     87 
     88   grpc::string GetLeadingComments(const grpc::string prefix) const {
     89     return GetCommentsHelper(method_, true, prefix);
     90   }
     91 
     92   grpc::string GetTrailingComments(const grpc::string prefix) const {
     93     return GetCommentsHelper(method_, false, prefix);
     94   }
     95 
     96   vector<grpc::string> GetAllComments() const {
     97     return grpc_python_generator::get_all_comments(method_);
     98   }
     99 
    100  private:
    101   const grpc::protobuf::MethodDescriptor* method_;
    102 };
    103 
    104 class ProtoBufService : public grpc_generator::Service {
    105  public:
    106   ProtoBufService(const grpc::protobuf::ServiceDescriptor* service)
    107       : service_(service) {}
    108 
    109   grpc::string name() const { return service_->name(); }
    110 
    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   };
    116 
    117   grpc::string GetLeadingComments(const grpc::string prefix) const {
    118     return GetCommentsHelper(service_, true, prefix);
    119   }
    120 
    121   grpc::string GetTrailingComments(const grpc::string prefix) const {
    122     return GetCommentsHelper(service_, false, prefix);
    123   }
    124 
    125   vector<grpc::string> GetAllComments() const {
    126     return grpc_python_generator::get_all_comments(service_);
    127   }
    128 
    129  private:
    130   const grpc::protobuf::ServiceDescriptor* service_;
    131 };
    132 
    133 class ProtoBufPrinter : public grpc_generator::Printer {
    134  public:
    135   ProtoBufPrinter(grpc::string* str)
    136       : output_stream_(str), printer_(&output_stream_, '$') {}
    137 
    138   void Print(const std::map<grpc::string, grpc::string>& vars,
    139              const char* string_template) {
    140     printer_.Print(vars, string_template);
    141   }
    142 
    143   void Print(const char* string) { printer_.Print(string); }
    144   void PrintRaw(const char* string) { printer_.PrintRaw(string); }
    145   void Indent() { printer_.Indent(); }
    146   void Outdent() { printer_.Outdent(); }
    147 
    148  private:
    149   grpc::protobuf::io::StringOutputStream output_stream_;
    150   grpc::protobuf::io::Printer printer_;
    151 };
    152 
    153 class ProtoBufFile : public grpc_generator::File {
    154  public:
    155   ProtoBufFile(const grpc::protobuf::FileDescriptor* file) : file_(file) {}
    156 
    157   grpc::string filename() const { return file_->name(); }
    158   grpc::string filename_without_ext() const {
    159     return grpc_generator::StripProto(filename());
    160   }
    161 
    162   grpc::string package() const { return file_->package(); }
    163   std::vector<grpc::string> package_parts() const {
    164     return grpc_generator::tokenize(package(), ".");
    165   }
    166 
    167   grpc::string additional_headers() const { return ""; }
    168 
    169   int service_count() const { return file_->service_count(); };
    170   std::unique_ptr<const grpc_generator::Service> service(int i) const {
    171     return std::unique_ptr<const grpc_generator::Service>(
    172         new ProtoBufService(file_->service(i)));
    173   }
    174 
    175   std::unique_ptr<grpc_generator::Printer> CreatePrinter(
    176       grpc::string* str) const {
    177     return std::unique_ptr<grpc_generator::Printer>(new ProtoBufPrinter(str));
    178   }
    179 
    180   grpc::string GetLeadingComments(const grpc::string prefix) const {
    181     return GetCommentsHelper(file_, true, prefix);
    182   }
    183 
    184   grpc::string GetTrailingComments(const grpc::string prefix) const {
    185     return GetCommentsHelper(file_, false, prefix);
    186   }
    187 
    188   vector<grpc::string> GetAllComments() const {
    189     return grpc_python_generator::get_all_comments(file_);
    190   }
    191 
    192  private:
    193   const grpc::protobuf::FileDescriptor* file_;
    194 };
    195 
    196 #endif  // GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H
    197