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 #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 & parameter,grpc::protobuf::compiler::GeneratorContext * context,std::string * error) 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::vector<std::pair<std::string, std::string> > options;
54 grpc::protobuf::compiler::ParseGeneratorParameter(parameter, &options);
55
56 bool generate_client = true;
57 bool generate_server = true;
58 bool internal_access = false;
59 std::string base_namespace = "";
60 bool base_namespace_present = false;
61
62 // the suffix that will get appended to the name generated from the name
63 // of the original .proto file
64 std::string file_suffix = "Grpc.cs";
65 for (size_t i = 0; i < options.size(); i++) {
66 if (options[i].first == "no_client") {
67 generate_client = false;
68 } else if (options[i].first == "no_server") {
69 generate_server = false;
70 } else if (options[i].first == "internal_access") {
71 internal_access = true;
72 } else if (options[i].first == "file_suffix") {
73 file_suffix = options[i].second;
74 } else if (options[i].first == "base_namespace") {
75 // Support for base_namespace option in this plugin is experimental.
76 // The option may be removed or file names generated may change
77 // in the future.
78 base_namespace = options[i].second;
79 base_namespace_present = true;
80 } else {
81 *error = "Unknown generator option: " + options[i].first;
82 return false;
83 }
84 }
85
86 std::string code = grpc_csharp_generator::GetServices(
87 file, generate_client, generate_server, internal_access);
88 if (code.size() == 0) {
89 return true; // don't generate a file if there are no services
90 }
91
92 // Get output file name.
93 std::string file_name;
94 if (!grpc_csharp_generator::ServicesFilename(
95 file, file_suffix, base_namespace_present, base_namespace,
96 file_name, error)) {
97 return false;
98 }
99 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
100 context->Open(file_name));
101 grpc::protobuf::io::CodedOutputStream coded_out(output.get());
102 coded_out.WriteRaw(code.data(), code.size());
103 return true;
104 }
105 };
106
main(int argc,char * argv[])107 int main(int argc, char* argv[]) {
108 CSharpGrpcGenerator generator;
109 return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
110 }
111