xref: /aosp_15_r20/external/grpc-grpc/src/compiler/ruby_plugin.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 // Generates Ruby gRPC service interface out of Protobuf IDL.
20 
21 #include <memory>
22 
23 #include "src/compiler/config.h"
24 #include "src/compiler/ruby_generator.h"
25 #include "src/compiler/ruby_generator_helpers-inl.h"
26 
27 class RubyGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
28  public:
RubyGrpcGenerator()29   RubyGrpcGenerator() {}
~RubyGrpcGenerator()30   ~RubyGrpcGenerator() {}
31 
GetSupportedFeatures() const32   uint64_t GetSupportedFeatures() const override {
33     return FEATURE_PROTO3_OPTIONAL
34 #ifdef GRPC_PROTOBUF_EDITION_SUPPORT
35            | FEATURE_SUPPORTS_EDITIONS
36 #endif
37         ;
38   }
39 
40 #ifdef GRPC_PROTOBUF_EDITION_SUPPORT
GetMinimumEdition() const41   grpc::protobuf::Edition GetMinimumEdition() const override {
42     return grpc::protobuf::Edition::EDITION_PROTO2;
43   }
GetMaximumEdition() const44   grpc::protobuf::Edition GetMaximumEdition() const override {
45     return grpc::protobuf::Edition::EDITION_2023;
46   }
47 #endif
48 
Generate(const grpc::protobuf::FileDescriptor * file,const std::string &,grpc::protobuf::compiler::GeneratorContext * context,std::string *) const49   bool Generate(const grpc::protobuf::FileDescriptor* file,
50                 const std::string& /*parameter*/,
51                 grpc::protobuf::compiler::GeneratorContext* context,
52                 std::string* /*error*/) const override {
53     std::string code = grpc_ruby_generator::GetServices(file);
54     if (code.size() == 0) {
55       return true;  // don't generate a file if there are no services
56     }
57 
58     // Get output file name.
59     std::string file_name;
60     if (!grpc_ruby_generator::ServicesFilename(file, &file_name)) {
61       return false;
62     }
63     std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
64         context->Open(file_name));
65     grpc::protobuf::io::CodedOutputStream coded_out(output.get());
66     coded_out.WriteRaw(code.data(), code.size());
67     return true;
68   }
69 };
70 
main(int argc,char * argv[])71 int main(int argc, char* argv[]) {
72   RubyGrpcGenerator generator;
73   return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
74 }
75