Skip to content

Commit 9d3f524

Browse files
committed
stub stubfactory http mixin
1 parent 5ddde99 commit 9d3f524

File tree

11 files changed

+195
-104
lines changed

11 files changed

+195
-104
lines changed

generator/internal/descriptor_utils.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -889,14 +889,13 @@ std::map<std::string, VarsDictionary> CreateMethodVars(
889889
SetLongrunningOperationMethodVars(method, method_vars);
890890
AssignPaginationMethodVars(method, method_vars);
891891
SetMethodSignatureMethodVars(service, method, omitted_rpcs, method_vars);
892-
auto parsed_http_info = ParseHttpExtension(method);
892+
auto parsed_http_info =
893+
ParseHttpExtension(method, mixin_method.method_override);
893894
method_vars["request_resource"] =
894895
FormatRequestResource(*method.input_type(), parsed_http_info);
895896
SetHttpDerivedMethodVars(parsed_http_info, method, method_vars);
896897
SetHttpQueryParameters(parsed_http_info, method, method_vars);
897898
service_methods_vars[method.full_name()] = method_vars;
898-
std::cout << "*******************************" << method.full_name()
899-
<< std::endl;
900899
}
901900
return service_methods_vars;
902901
}

generator/internal/http_option_utils.cc

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -263,38 +263,44 @@ void SetHttpQueryParameters(HttpExtensionInfo const& info,
263263
}
264264

265265
HttpExtensionInfo ParseHttpExtension(
266-
google::protobuf::MethodDescriptor const& method) {
266+
google::protobuf::MethodDescriptor const& method,
267+
absl::optional<MixinMethodOverride> method_override) {
267268
if (!method.options().HasExtension(google::api::http)) return {};
268269

269270
HttpExtensionInfo info;
270271
google::api::HttpRule http_rule =
271272
method.options().GetExtension(google::api::http);
272273

273274
std::string url_pattern;
274-
switch (http_rule.pattern_case()) {
275-
case google::api::HttpRule::kGet:
276-
info.http_verb = "Get";
277-
url_pattern = http_rule.get();
278-
break;
279-
case google::api::HttpRule::kPut:
280-
info.http_verb = "Put";
281-
url_pattern = http_rule.put();
282-
break;
283-
case google::api::HttpRule::kPost:
284-
info.http_verb = "Post";
285-
url_pattern = http_rule.post();
286-
break;
287-
case google::api::HttpRule::kDelete:
288-
info.http_verb = "Delete";
289-
url_pattern = http_rule.delete_();
290-
break;
291-
case google::api::HttpRule::kPatch:
292-
info.http_verb = "Patch";
293-
url_pattern = http_rule.patch();
294-
break;
295-
default:
296-
GCP_LOG(FATAL) << __FILE__ << ":" << __LINE__
297-
<< ": google::api::HttpRule not handled";
275+
if (method_override) {
276+
url_pattern = method_override->http_path;
277+
info.http_verb = method_override->http_verb;
278+
} else {
279+
switch (http_rule.pattern_case()) {
280+
case google::api::HttpRule::kGet:
281+
info.http_verb = "Get";
282+
url_pattern = http_rule.get();
283+
break;
284+
case google::api::HttpRule::kPut:
285+
info.http_verb = "Put";
286+
url_pattern = http_rule.put();
287+
break;
288+
case google::api::HttpRule::kPost:
289+
info.http_verb = "Post";
290+
url_pattern = http_rule.post();
291+
break;
292+
case google::api::HttpRule::kDelete:
293+
info.http_verb = "Delete";
294+
url_pattern = http_rule.delete_();
295+
break;
296+
case google::api::HttpRule::kPatch:
297+
info.http_verb = "Patch";
298+
url_pattern = http_rule.patch();
299+
break;
300+
default:
301+
GCP_LOG(FATAL) << __FILE__ << ":" << __LINE__
302+
<< ": google::api::HttpRule not handled";
303+
}
298304
}
299305

300306
auto parsed_http_rule = ParsePathTemplate(url_pattern);

generator/internal/http_option_utils.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
#define GOOGLE_CLOUD_CPP_GENERATOR_INTERNAL_HTTP_OPTION_UTILS_H
1717

1818
#include "generator/internal/http_annotation_parser.h"
19+
#include "generator/internal/mixin_utils.h"
1920
#include "generator/internal/printer.h"
21+
#include "absl/types/optional.h"
2022
#include <google/protobuf/descriptor.h>
2123
#include <string>
2224

@@ -41,7 +43,8 @@ struct HttpExtensionInfo {
4143
* transcoding and REST transport.
4244
*/
4345
HttpExtensionInfo ParseHttpExtension(
44-
google::protobuf::MethodDescriptor const& method);
46+
google::protobuf::MethodDescriptor const& method,
47+
absl::optional<MixinMethodOverride> method_override = absl::nullopt);
4548

4649
/**
4750
* Sets the following method_vars based on the provided parsed_http_info:

generator/internal/mixin_utils.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ std::unordered_map<std::string, std::string> mixin_proto_path_map = {
2727
// {"google.longrunning.Operations", "google/longrunning/operations.proto"},
2828
};
2929

30-
std::unordered_set<std::string> http_verbs = {"get", "post", "put", "patch",
31-
"delete"};
30+
std::unordered_map<std::string, std::string> http_verbs = {
31+
{"get", "Get"}, {"post", "Post"}, {"put", "Put"},
32+
{"patch", "Patch"}, {"delete", "Delete"},
33+
};
3234

3335
std::unordered_map<std::string, MixinMethodOverride> GetMixinMethodOverrides(
3436
YAML::Node const& service_config) {
@@ -51,8 +53,10 @@ std::unordered_map<std::string, MixinMethodOverride> GetMixinMethodOverrides(
5153
kv.second.Type() != YAML::NodeType::Scalar)
5254
continue;
5355

54-
std::string http_verb = kv.first.as<std::string>();
56+
std::string http_verb = absl::AsciiStrToLower(kv.first.as<std::string>());
5557
if (http_verbs.find(http_verb) == http_verbs.end()) continue;
58+
http_verb = http_verbs[http_verb];
59+
5660
std::string http_path = kv.second.as<std::string>();
5761

5862
absl::optional<std::string> http_body;

generator/internal/service_code_generator.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,6 @@ void ServiceCodeGenerator::SetMethods() {
481481

482482
for (auto const& mixin_method : mixin_methods_) {
483483
methods_.emplace_back(mixin_method.method.get());
484-
std::cout << "-----------------------"
485-
<< mixin_method.method.get().full_name() << std::endl;
486484
}
487485
}
488486

generator/internal/stub_factory_generator.cc

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ StubFactoryGenerator::StubFactoryGenerator(
3131
: ServiceCodeGenerator("stub_factory_header_path", "stub_factory_cc_path",
3232
service_descriptor, std::move(service_vars),
3333
std::move(service_method_vars), context,
34-
mixin_methods) {}
34+
mixin_methods),
35+
mixin_methods_(mixin_methods) {}
3536

3637
Status StubFactoryGenerator::GenerateHeader() {
3738
HeaderPrint(CopyrightLicenseFileHeader());
@@ -96,6 +97,21 @@ Status StubFactoryGenerator::GenerateCc() {
9697
auto result = CcOpenNamespaces(NamespaceType::kInternal);
9798
if (!result.ok()) return result;
9899

100+
std::unordered_map<std::string, std::string> mixin_grpc_stubs;
101+
for (auto const& mixin_method : mixin_methods_) {
102+
mixin_grpc_stubs[mixin_method.grpc_stub_name] = mixin_method.grpc_stub_fqn;
103+
}
104+
std::string mixin_stub_inits = "";
105+
std::string mixin_stub_moves = "";
106+
for (auto const& mixin_grpc_stub : mixin_grpc_stubs) {
107+
mixin_stub_inits += absl::StrFormat(
108+
R"""(
109+
auto service_%s = %s::NewStub(channel);)""",
110+
mixin_grpc_stub.first, mixin_grpc_stub.second);
111+
mixin_stub_moves +=
112+
absl::StrFormat(", std::move(service_%s)", mixin_grpc_stub.first);
113+
}
114+
99115
// factory function implementation
100116
CcPrint(R"""(
101117
std::shared_ptr<$stub_class_name$>
@@ -106,18 +122,22 @@ CreateDefault$stub_class_name$(
106122
options.get<EndpointOption>(), internal::MakeChannelArguments(options));
107123
auto service_grpc_stub = $grpc_stub_fqn$::NewStub(channel);)""");
108124

125+
CcPrint(mixin_stub_inits);
126+
109127
if (!HasLongrunningMethod()) {
110-
CcPrint(R"""(
128+
CcPrint(absl::StrFormat(R"""(
111129
std::shared_ptr<$stub_class_name$> stub =
112-
std::make_shared<Default$stub_class_name$>(std::move(service_grpc_stub));
113-
)""");
130+
std::make_shared<Default$stub_class_name$>(std::move(service_grpc_stub)%s);
131+
)""",
132+
mixin_stub_moves));
114133
} else {
115-
CcPrint(R"""(
134+
CcPrint(absl::StrFormat(R"""(
116135
std::shared_ptr<$stub_class_name$> stub =
117136
std::make_shared<Default$stub_class_name$>(
118-
std::move(service_grpc_stub),
137+
std::move(service_grpc_stub)%s,
119138
google::longrunning::Operations::NewStub(channel));
120-
)""");
139+
)""",
140+
mixin_stub_moves));
121141
}
122142
CcPrint(R"""(
123143
if (auth->RequiresConfigureContext()) {

generator/internal/stub_factory_generator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class StubFactoryGenerator : public ServiceCodeGenerator {
5050
private:
5151
Status GenerateHeader() override;
5252
Status GenerateCc() override;
53+
std::vector<MixinMethod> mixin_methods_;
5354
};
5455

5556
} // namespace generator_internal

0 commit comments

Comments
 (0)