forked from googleapis/google-cloud-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_code_generator.cc
More file actions
521 lines (456 loc) · 18.3 KB
/
service_code_generator.cc
File metadata and controls
521 lines (456 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "generator/internal/service_code_generator.h"
#include "generator/internal/codegen_utils.h"
#include "generator/internal/longrunning.h"
#include "generator/internal/pagination.h"
#include "generator/internal/printer.h"
#include "generator/internal/request_id.h"
#include "google/cloud/internal/algorithm.h"
#include "google/cloud/internal/make_status.h"
#include "google/cloud/log.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_replace.h"
#include "absl/strings/str_split.h"
#include "absl/strings/strip.h"
#include "google/api/client.pb.h"
#include "google/api/routing.pb.h"
#include <google/protobuf/descriptor.h>
#include <algorithm>
#include <unordered_map>
namespace google {
namespace cloud {
namespace generator_internal {
namespace {
absl::optional<std::string> IncludePathForWellKnownProtobufType(
google::protobuf::FieldDescriptor const& parameter) {
// This hash is not intended to be comprehensive. Problematic types and their
// includes should be added as needed.
static auto const* const kTypeIncludeMap =
new std::unordered_map<std::string, std::string>(
{{"google.protobuf.Duration", "google/protobuf/duration.pb.h"}});
if (parameter.type() == google::protobuf::FieldDescriptor::TYPE_MESSAGE) {
auto iter = kTypeIncludeMap->find(
std::string{parameter.message_type()->full_name()});
if (iter != kTypeIncludeMap->end()) {
return iter->second;
}
}
return {};
}
} // namespace
ServiceCodeGenerator::ServiceCodeGenerator(
std::string const& header_path_key, std::string const& cc_path_key,
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods)
: service_descriptor_(service_descriptor),
service_vars_(std::move(service_vars)),
service_method_vars_(std::move(service_method_vars)),
header_(context, service_vars_[header_path_key]),
cc_(context, service_vars_[cc_path_key]),
mixin_methods_(mixin_methods) {
assert(service_descriptor != nullptr);
assert(context != nullptr);
SetVars(service_vars_[header_path_key]);
SetMethods();
}
ServiceCodeGenerator::ServiceCodeGenerator(
std::string const& header_path_key,
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods)
: service_descriptor_(service_descriptor),
service_vars_(std::move(service_vars)),
service_method_vars_(std::move(service_method_vars)),
header_(context, service_vars_[header_path_key]),
mixin_methods_(mixin_methods) {
assert(service_descriptor != nullptr);
assert(context != nullptr);
SetVars(service_vars_[header_path_key]);
SetMethods();
}
ServiceCodeGenerator::ServiceConfiguration::EndpointLocationStyle
ServiceCodeGenerator::EndpointLocationStyle() const {
auto endpoint_location_style = ServiceConfiguration::LOCATION_INDEPENDENT;
ServiceConfiguration::EndpointLocationStyle_Parse(
vars("endpoint_location_style"), &endpoint_location_style);
return endpoint_location_style;
}
bool ServiceCodeGenerator::IsExperimental() const {
auto iter = vars().find("experimental");
return iter != vars().end() && iter->second == "true";
}
bool ServiceCodeGenerator::HasLongrunningMethod() const {
return std::any_of(methods_.begin(), methods_.end(),
[](google::protobuf::MethodDescriptor const& m) {
return IsLongrunningOperation(m);
});
}
bool ServiceCodeGenerator::HasGRPCLongrunningOperation() const {
return std::any_of(methods_.begin(), methods_.end(),
[](google::protobuf::MethodDescriptor const& m) {
return IsGRPCLongrunningOperation(m);
});
}
bool ServiceCodeGenerator::HasAsyncMethod() const {
return !async_methods_.empty() || HasLongrunningMethod();
}
bool ServiceCodeGenerator::HasPaginatedMethod() const {
return std::any_of(methods_.begin(), methods_.end(),
[](google::protobuf::MethodDescriptor const& m) {
return IsPaginated(m);
});
}
bool ServiceCodeGenerator::HasMessageWithMapField() const {
for (auto method : methods_) {
auto const* const request = method.get().input_type();
auto const* const response = method.get().output_type();
for (int j = 0; j < request->field_count(); ++j) {
if (request->field(j)->is_map()) {
return true;
}
}
for (int k = 0; k < response->field_count(); ++k) {
if (response->field(k)->is_map()) {
return true;
}
}
}
return false;
}
bool ServiceCodeGenerator::HasStreamingReadMethod() const {
return std::any_of(methods_.begin(), methods_.end(),
[](google::protobuf::MethodDescriptor const& m) {
return IsStreamingRead(m);
});
}
bool ServiceCodeGenerator::HasAsynchronousStreamingReadMethod() const {
return std::any_of(async_methods_.begin(), async_methods_.end(),
[](google::protobuf::MethodDescriptor const& m) {
return IsStreamingRead(m);
});
}
bool ServiceCodeGenerator::HasAsynchronousStreamingWriteMethod() const {
return std::any_of(async_methods_.begin(), async_methods_.end(),
[](google::protobuf::MethodDescriptor const& m) {
return IsStreamingWrite(m);
});
}
bool ServiceCodeGenerator::HasStreamingWriteMethod() const {
return std::any_of(methods_.begin(), methods_.end(),
[](google::protobuf::MethodDescriptor const& m) {
return IsStreamingWrite(m);
});
}
bool ServiceCodeGenerator::HasBidirStreamingMethod() const {
return std::any_of(methods_.begin(), methods_.end(),
[](google::protobuf::MethodDescriptor const& m) {
return IsBidirStreaming(m);
});
}
bool ServiceCodeGenerator::HasExplicitRoutingMethod() const {
return std::any_of(methods_.begin(), methods_.end(),
[](google::protobuf::MethodDescriptor const& m) {
return m.options().HasExtension(google::api::routing);
});
}
bool ServiceCodeGenerator::HasGenerateRestTransport() const {
auto const generate_rest_transport =
service_vars_.find("generate_rest_transport");
return generate_rest_transport != service_vars_.end() &&
generate_rest_transport->second == "true";
}
bool ServiceCodeGenerator::HasGenerateGrpcTransport() const {
auto const generate_grpc_transport =
service_vars_.find("generate_grpc_transport");
return generate_grpc_transport != service_vars_.end() &&
generate_grpc_transport->second == "true";
}
bool ServiceCodeGenerator::HasRequestId() const {
return std::any_of(service_method_vars_.begin(), service_method_vars_.end(),
[](auto const& kv) {
return kv.second.find("request_id_field_name") !=
kv.second.end();
});
}
bool ServiceCodeGenerator::HasRequestId(
google::protobuf::MethodDescriptor const& method) const {
auto mv = service_method_vars_.find(std::string{method.full_name()});
if (mv == service_method_vars_.end()) return false;
auto const& method_vars = mv->second;
return method_vars.find("request_id_field_name") != method_vars.end();
}
bool ServiceCodeGenerator::HasApiVersion() const {
auto iter = service_vars_.find("api_version");
return iter != service_vars_.end() && !iter->second.empty();
}
std::vector<std::string>
ServiceCodeGenerator::MethodSignatureWellKnownProtobufTypeIncludes() const {
std::vector<std::string> include_paths;
for (auto method : methods_) {
auto method_signature_extension =
method.get().options().GetRepeatedExtension(
google::api::method_signature);
google::protobuf::Descriptor const* input_type = method.get().input_type();
for (auto const& extension : method_signature_extension) {
std::vector<std::string> parameters =
absl::StrSplit(extension, ',', absl::SkipEmpty());
for (auto& parameter : parameters) {
absl::StripAsciiWhitespace(¶meter);
auto path = IncludePathForWellKnownProtobufType(
*input_type->FindFieldByName(parameter));
if (path) include_paths.push_back(*path);
}
}
}
return include_paths;
}
bool ServiceCodeGenerator::MethodSignatureUsesDeprecatedField() const {
return std::any_of(
service_method_vars_.begin(), service_method_vars_.end(),
[](auto const& method_vars) {
return method_vars.second.find("uses_deprecated_field") !=
method_vars.second.end();
});
}
bool ServiceCodeGenerator::OmitMethodSignature(
google::protobuf::MethodDescriptor const& method,
int method_signature_number) const {
auto method_vars = service_method_vars_.find(std::string{method.full_name()});
if (method_vars == service_method_vars_.end()) {
GCP_LOG(FATAL) << method.full_name()
<< " not found in service_method_vars_\n";
}
return method_vars->second.find("method_signature" +
std::to_string(method_signature_number)) ==
method_vars->second.end();
}
VarsDictionary const& ServiceCodeGenerator::vars() const {
return service_vars_;
}
std::string ServiceCodeGenerator::vars(std::string const& key) const {
auto iter = service_vars_.find(key);
if (iter == service_vars_.end()) {
GCP_LOG(FATAL) << key << " not found in service_vars_\n";
}
return iter->second;
}
VarsDictionary ServiceCodeGenerator::MergeServiceAndMethodVars(
google::protobuf::MethodDescriptor const& method) const {
auto vars = service_vars_;
// NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
std::string const method_full_name{method.full_name()};
vars.insert(service_method_vars_.at(method_full_name).begin(),
service_method_vars_.at(method_full_name).end());
return vars;
}
void ServiceCodeGenerator::HeaderLocalIncludes(
std::vector<std::string> const& local_includes) {
GenerateLocalIncludes(header_, local_includes, FileType::kHeaderFile);
}
void ServiceCodeGenerator::CcLocalIncludes(
std::vector<std::string> const& local_includes) {
GenerateLocalIncludes(cc_, local_includes, FileType::kCcFile);
}
void ServiceCodeGenerator::HeaderSystemIncludes(
std::vector<std::string> const& system_includes) {
GenerateSystemIncludes(header_, system_includes);
}
void ServiceCodeGenerator::CcSystemIncludes(
std::vector<std::string> const& system_includes) {
GenerateSystemIncludes(cc_, system_includes);
}
Status ServiceCodeGenerator::HeaderOpenNamespaces(NamespaceType ns_type) {
return OpenNamespaces(header_, ns_type, "product_path");
}
void ServiceCodeGenerator::HeaderCloseNamespaces() { CloseNamespaces(header_); }
Status ServiceCodeGenerator::CcOpenNamespaces(NamespaceType ns_type) {
return OpenNamespaces(cc_, ns_type, "product_path");
}
void ServiceCodeGenerator::CcCloseNamespaces() { CloseNamespaces(cc_); }
void ServiceCodeGenerator::HeaderPrint(std::string const& text) {
header_.Print(service_vars_, text);
}
void ServiceCodeGenerator::HeaderPrint(
std::vector<PredicatedFragment<void>> const& text) {
for (auto const& fragment : text) {
header_.Print(service_vars_, fragment());
}
}
Status ServiceCodeGenerator::HeaderPrintMethod(
google::protobuf::MethodDescriptor const& method,
std::vector<MethodPattern> const& patterns, char const* file, int line) {
return PrintMethod(method, header_, MergeServiceAndMethodVars(method),
patterns, file, line);
}
void ServiceCodeGenerator::HeaderPrintMethod(
google::protobuf::MethodDescriptor const& method, char const* file,
int line, std::string const& text) {
header_.Print(line, file, MergeServiceAndMethodVars(method), text);
}
void ServiceCodeGenerator::CcPrint(std::string const& text) {
cc_.Print(service_vars_, text);
}
void ServiceCodeGenerator::CcPrint(
std::vector<PredicatedFragment<void>> const& text) {
for (auto const& fragment : text) {
cc_.Print(service_vars_, fragment());
}
}
Status ServiceCodeGenerator::CcPrintMethod(
google::protobuf::MethodDescriptor const& method,
std::vector<MethodPattern> const& patterns, char const* file, int line) {
return PrintMethod(method, cc_, MergeServiceAndMethodVars(method), patterns,
file, line);
}
void ServiceCodeGenerator::CcPrintMethod(
google::protobuf::MethodDescriptor const& method, char const* file,
int line, std::string const& text) {
cc_.Print(line, file, MergeServiceAndMethodVars(method), text);
}
void ServiceCodeGenerator::GenerateLocalIncludes(
Printer& p, std::vector<std::string> local_includes, FileType file_type) {
if (file_type == FileType::kCcFile) {
std::sort(local_includes.begin() + 1, local_includes.end());
} else {
std::sort(local_includes.begin(), local_includes.end());
}
for (auto const& include : local_includes) {
p.Print(LocalInclude(include));
}
}
void ServiceCodeGenerator::GenerateSystemIncludes(
Printer& p, std::vector<std::string> system_includes) {
std::sort(system_includes.begin(), system_includes.end());
for (auto const& include : system_includes) {
p.Print(SystemInclude(include));
}
}
void ServiceCodeGenerator::HeaderProtobufGenCodeIncludes(
std::vector<std::string> const& pb_h_includes) {
if (pb_h_system_includes_) {
HeaderSystemIncludes(pb_h_includes);
} else {
HeaderLocalIncludes(pb_h_includes);
}
}
void ServiceCodeGenerator::CcProtobufGenCodeIncludes(
std::vector<std::string> const& pb_h_includes) {
if (pb_h_system_includes_) {
CcSystemIncludes(pb_h_includes);
} else {
CcLocalIncludes(pb_h_includes);
}
}
Status ServiceCodeGenerator::OpenNamespaces(
Printer& p, NamespaceType ns_type, std::string const& product_path_var,
std::string const& ns_documentation) {
auto result = service_vars_.find(product_path_var);
if (result == service_vars_.end()) {
return internal::InternalError(product_path_var + " not found in vars",
GCP_ERROR_INFO());
}
namespace_ = Namespace(service_vars_[product_path_var], ns_type);
p.Print(R"""(
namespace google {
namespace cloud {)""");
p.Print(service_vars_, ns_documentation);
p.Print(R"""(
namespace $namespace$ {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
)""",
"namespace", namespace_);
return {};
}
void ServiceCodeGenerator::CloseNamespaces(Printer& p) {
p.Print(R"""(
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END)""");
p.Print(R"""(
} // namespace $namespace$
} // namespace cloud
} // namespace google
)""",
"namespace", namespace_);
}
Status ServiceCodeGenerator::Generate() {
auto result = GenerateHeader();
if (!result.ok()) return result;
return GenerateCc();
}
void ServiceCodeGenerator::SetVars(absl::string_view header_path) {
service_vars_["header_include_guard"] = FormatHeaderIncludeGuard(header_path);
}
void ServiceCodeGenerator::SetMethods() {
auto split_arg = [this](std::string const& arg) -> std::set<std::string> {
auto l = service_vars_.find(arg);
if (l == service_vars_.end()) return {};
std::vector<std::string> s = absl::StrSplit(l->second, ',');
for (auto& a : s) a = SafeReplaceAll(a, "@", ",");
return {s.begin(), s.end()};
};
auto const omitted_rpcs = split_arg("omitted_rpcs");
auto const gen_async_rpcs = split_arg("gen_async_rpcs");
auto service_name = service_descriptor_->name();
for (int i = 0; i < service_descriptor_->method_count(); ++i) {
auto const* method = service_descriptor_->method(i);
auto method_name = method->name();
auto qualified_method_name = absl::StrCat(service_name, ".", method_name);
auto any_match = [&](std::string const& v) {
return v == method_name || v == qualified_method_name;
};
bool omit_rpc = internal::ContainsIf(omitted_rpcs, any_match);
if (!omit_rpc) methods_.emplace_back(*method);
if (internal::ContainsIf(gen_async_rpcs, any_match)) {
// We still generate the async API for omitted (and possibly
// deprecated) RPCs when they appear in gen_async_rpcs.
async_methods_.emplace_back(*method);
}
}
for (auto const& mixin_method : mixin_methods_) {
methods_.emplace_back(mixin_method.method.get());
}
}
std::string ServiceCodeGenerator::GetPbIncludeByTransport() const {
if (HasGenerateGrpcTransport()) return vars("proto_grpc_header_path");
return vars("proto_header_path");
}
std::vector<std::string> ServiceCodeGenerator::GetMixinPbIncludeByTransport()
const {
std::string const& mixin_pb_header_paths =
HasGenerateGrpcTransport() ? vars("mixin_proto_grpc_header_paths")
: vars("mixin_proto_header_paths");
return absl::StrSplit(mixin_pb_header_paths, ',');
}
bool ServiceCodeGenerator::IsDiscoveryDocumentProto() const {
auto iter = service_vars_.find("proto_file_source");
return (iter != service_vars_.end() && iter->second == "discovery_document");
}
std::vector<MixinMethod> const& ServiceCodeGenerator::MixinMethods() const {
return mixin_methods_;
}
bool ServiceCodeGenerator::IsDeprecated() const {
return service_descriptor_->options().deprecated();
}
bool ServiceCodeGenerator::HasEmitCompletionQueueAccessor() const {
return vars().find("emit_completion_queue_accessor") != vars().end() &&
vars().at("emit_completion_queue_accessor") == "true";
}
} // namespace generator_internal
} // namespace cloud
} // namespace google