|
| 1 | +// Copyright 2019 Google Inc. All rights reserved |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <string> |
| 16 | +#include <map> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +#include "absl/strings/str_cat.h" |
| 20 | +#include "absl/strings/string_view.h" |
| 21 | +#include "absl/strings/str_replace.h" |
| 22 | +#include "generator/gapic_generator.h" |
| 23 | +#include "internal/gapic_utils.h" |
| 24 | + |
| 25 | +#include "google/api/client.pb.h" |
| 26 | + |
| 27 | +namespace pb = google::protobuf; |
| 28 | + |
| 29 | +namespace google { |
| 30 | +namespace api { |
| 31 | +namespace codegen { |
| 32 | + |
| 33 | +using Vars = std::map<std::string, std::string>; |
| 34 | + |
| 35 | +void SetServiceVars( |
| 36 | + pb::ServiceDescriptor const* service, |
| 37 | + std::map<std::string, std::string>& vars) { |
| 38 | + vars["class_name"] = service->name(); |
| 39 | + vars["proto_file_name"] = service->file()->name(); |
| 40 | + vars["header_include_guard_const"] = absl::StrCat(service->name(), "_H_"); |
| 41 | + vars["class_comment_block"] = "// TODO: pull in comments"; |
| 42 | + vars["grpc_stub_fqn"] = internal::ProtoNameToCppName(service->full_name()); |
| 43 | + vars["service_endpoint"] = service->options().GetExtension(google::api::default_host); |
| 44 | +} |
| 45 | + |
| 46 | +void SetMethodVars( |
| 47 | + pb::MethodDescriptor const* method, |
| 48 | + std::map<std::string, std::string>& vars) { |
| 49 | + vars["method_name"] = method->name(); |
| 50 | + vars["request_object"] = internal::ProtoNameToCppName(method->input_type()->full_name()); |
| 51 | + vars["response_object"] = internal::ProtoNameToCppName(method->output_type()->full_name()); |
| 52 | +} |
| 53 | + |
| 54 | +std::vector<std::string> BuildHeaderIncludes( |
| 55 | + pb::ServiceDescriptor const* /* service */) { |
| 56 | + return std::vector<std::string>(); |
| 57 | +} |
| 58 | + |
| 59 | +std::vector<std::string> BuildCCIncludes( |
| 60 | + pb::ServiceDescriptor const* /* service */) { |
| 61 | + return std::vector<std::string>(); |
| 62 | +} |
| 63 | + |
| 64 | +std::vector<std::string> BuildNamespaces( |
| 65 | + pb::ServiceDescriptor const* /* service */) { |
| 66 | + return std::vector<std::string>(); |
| 67 | +} |
| 68 | + |
| 69 | +void PrintMethods( |
| 70 | + pb::ServiceDescriptor const* service, |
| 71 | + std::map<std::string, std::string> vars, |
| 72 | + pb::io::Printer* p, |
| 73 | + char const* tmplt) { |
| 74 | + for (int i = 0; i < service->method_count(); i++) { |
| 75 | + const pb::MethodDescriptor* method = service->method(i); |
| 76 | + SetMethodVars(method, vars); |
| 77 | + p->Print(vars, tmplt); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +bool GenerateClientHeaderFile( |
| 82 | + pb::ServiceDescriptor const* service, |
| 83 | + std::map<std::string, std::string> const& vars, |
| 84 | + pb::io::Printer* p, |
| 85 | + std::string * /* error */) { |
| 86 | + auto includes = BuildHeaderIncludes(service); |
| 87 | + auto namespaces = BuildNamespaces(service); |
| 88 | + |
| 89 | + p->Print(vars, |
| 90 | + "// Generated by the GAPIC C++ plugin.\n" |
| 91 | + "// If you make any local changes, they will be lost.\n" |
| 92 | + "// source: $proto_file_name$\n" |
| 93 | + "#ifndef $header_include_guard_const$\n" |
| 94 | + "#define $header_include_guard_const$"); |
| 95 | + |
| 96 | + for (auto include : includes) { |
| 97 | + p->Print("\n#include $include$", "include", include); |
| 98 | + } |
| 99 | + for (auto nspace : namespaces) { |
| 100 | + p->Print("\nnamespace $namespace$ {", "namespace", nspace); |
| 101 | + } |
| 102 | + |
| 103 | + p->Print(vars, "\n" |
| 104 | + "$class_comment_block$\n" |
| 105 | + "class $class_name$ {"); |
| 106 | + |
| 107 | + PrintMethods(service, vars, p, ""); |
| 108 | + |
| 109 | + p->Print(vars, "\n}; // $class_name$"); |
| 110 | + |
| 111 | + for (auto nspace : namespaces) { |
| 112 | + p->Print("\n} // namespace $namespace$", "namespace", nspace); |
| 113 | + } |
| 114 | + |
| 115 | + p->Print(vars, "\n" |
| 116 | + "#endif // $header_include_guard_const$\n"); |
| 117 | + |
| 118 | + return true; |
| 119 | +} |
| 120 | + |
| 121 | +bool GenerateClientCCFile( |
| 122 | + pb::ServiceDescriptor const* service, |
| 123 | + std::map<std::string, std::string> const& vars, |
| 124 | + pb::io::Printer* p, |
| 125 | + std::string * /* error */) { |
| 126 | + auto includes = BuildCCIncludes(service); |
| 127 | + auto namespaces = BuildNamespaces(service); |
| 128 | + |
| 129 | + p->Print(vars, |
| 130 | + "// Generated by the GAPIC C++ plugin.\n" |
| 131 | + "// If you make any local changes, they will be lost.\n" |
| 132 | + "// source: $proto_file_name$"); |
| 133 | + |
| 134 | + for (auto include : includes) { |
| 135 | + p->Print("\n#include $include$", "include", include); |
| 136 | + } |
| 137 | + for (auto nspace : namespaces) { |
| 138 | + p->Print("\nnamespace $namespace$ {", "namespace", nspace); |
| 139 | + } |
| 140 | + |
| 141 | + p->Print(vars, "\n" |
| 142 | + "// TODO: add content"); |
| 143 | + |
| 144 | + for (auto nspace : namespaces) { |
| 145 | + p->Print("\n} // namespace $namespace$", "namespace", nspace); |
| 146 | + } |
| 147 | + |
| 148 | + p->Print("\n"); |
| 149 | + |
| 150 | + return true; |
| 151 | +} |
| 152 | + |
| 153 | +bool GapicGenerator::Generate(pb::FileDescriptor const* file, |
| 154 | + std::string const& /* parameter */, |
| 155 | + pb::compiler::GeneratorContext* generator_context, |
| 156 | + std::string* error) const { |
| 157 | + if (file->options().cc_generic_services()) { |
| 158 | + *error = |
| 159 | + "cpp gapic proto compiler plugin does not work with generic " |
| 160 | + "services. To generate cpp grpc APIs, please set \"" |
| 161 | + "cc_generic_service = false\"."; |
| 162 | + return false; |
| 163 | + } |
| 164 | + |
| 165 | + for (int i = 0; i < file->service_count(); i++) { |
| 166 | + pb::ServiceDescriptor const* service = file->service(i); |
| 167 | + |
| 168 | + // TODO(michaelbausor): initialize Vars with cross-file-descriptor |
| 169 | + // configuration, e.g. metadata annotation. |
| 170 | + std::map<std::string, std::string> vars; |
| 171 | + SetServiceVars(service, vars); |
| 172 | + |
| 173 | + std::string service_file_path = internal::ServiceNameToFilePath( |
| 174 | + service->full_name()); |
| 175 | + |
| 176 | + std::string header_file_path = absl::StrCat(service_file_path, ".gapic.h"); |
| 177 | + std::unique_ptr<pb::io::ZeroCopyOutputStream> header_output( |
| 178 | + generator_context->Open(header_file_path)); |
| 179 | + pb::io::Printer header_printer(header_output.get(), '$', NULL); |
| 180 | + if (!GenerateClientHeaderFile(service, vars, &header_printer, error)) { |
| 181 | + return false; |
| 182 | + } |
| 183 | + |
| 184 | + std::string cc_file_path = absl::StrCat(service_file_path, ".gapic.cc"); |
| 185 | + std::unique_ptr<pb::io::ZeroCopyOutputStream> cc_output( |
| 186 | + generator_context->Open(cc_file_path)); |
| 187 | + pb::io::Printer cc_printer(cc_output.get(), '$', NULL); |
| 188 | + if (!GenerateClientCCFile(service, vars, &cc_printer, error)) { |
| 189 | + return false; |
| 190 | + } |
| 191 | + } |
| 192 | + return true; |
| 193 | +} |
| 194 | + |
| 195 | +} // namespace codegen |
| 196 | +} // namespace api |
| 197 | +} // namespace google |
0 commit comments