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 C# gRPC service interface out of Protobuf IDL.
20 
21 #include <memory>
22 
23 #include "src/compiler/config.h"
24 #include "src/compiler/csharp_generator.h"
25 #include "src/compiler/csharp_generator_helpers.h"
26 
27 class CSharpGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
28  public:
CSharpGrpcGenerator()29   CSharpGrpcGenerator() {}
~CSharpGrpcGenerator()30   ~CSharpGrpcGenerator() {}
31 
GetSupportedFeatures() const32   uint64_t GetSupportedFeatures() const override {
33     return FEATURE_PROTO3_OPTIONAL;
34   }
35 
Generate(const grpc::protobuf::FileDescriptor * file,const std::string & parameter,grpc::protobuf::compiler::GeneratorContext * context,std::string * error) const36   bool Generate(const grpc::protobuf::FileDescriptor* file,
37                 const std::string& parameter,
38                 grpc::protobuf::compiler::GeneratorContext* context,
39                 std::string* error) const override {
40     std::vector<std::pair<std::string, std::string> > options;
41     grpc::protobuf::compiler::ParseGeneratorParameter(parameter, &options);
42 
43     bool generate_client = true;
44     bool generate_server = true;
45     bool internal_access = false;
46     for (size_t i = 0; i < options.size(); i++) {
47       if (options[i].first == "no_client") {
48         generate_client = false;
49       } else if (options[i].first == "no_server") {
50         generate_server = false;
51       } else if (options[i].first == "internal_access") {
52         internal_access = true;
53       } else {
54         *error = "Unknown generator option: " + options[i].first;
55         return false;
56       }
57     }
58 
59     std::string code = grpc_csharp_generator::GetServices(
60         file, generate_client, generate_server, internal_access);
61     if (code.size() == 0) {
62       return true;  // don't generate a file if there are no services
63     }
64 
65     // Get output file name.
66     std::string file_name;
67     if (!grpc_csharp_generator::ServicesFilename(file, &file_name)) {
68       return false;
69     }
70     std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
71         context->Open(file_name));
72     grpc::protobuf::io::CodedOutputStream coded_out(output.get());
73     coded_out.WriteRaw(code.data(), code.size());
74     return true;
75   }
76 };
77 
main(int argc,char * argv[])78 int main(int argc, char* argv[]) {
79   CSharpGrpcGenerator generator;
80   return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
81 }
82