Skip to content

Commit

Permalink
impl(mixin): add mixin support in code generators (#14738)
Browse files Browse the repository at this point in the history
* code changes and generate code for datamigration

* remove code generated for datamigration

* recover quickstart

* recover quickstart

* recover quickstart

* fix

* fix

* fix following comments
  • Loading branch information
cuiy0006 authored Oct 1, 2024
1 parent b0bac64 commit 0ec814f
Show file tree
Hide file tree
Showing 66 changed files with 469 additions and 215 deletions.
5 changes: 3 additions & 2 deletions generator/internal/auth_decorator_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ AuthDecoratorGenerator::AuthDecoratorGenerator(
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context)
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods)
: StubGeneratorBase("auth_header_path", "auth_cc_path", service_descriptor,
std::move(service_vars), std::move(service_method_vars),
context) {}
context, mixin_methods) {}

Status AuthDecoratorGenerator::GenerateHeader() {
HeaderPrint(CopyrightLicenseFileHeader());
Expand Down
3 changes: 2 additions & 1 deletion generator/internal/auth_decorator_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class AuthDecoratorGenerator : public StubGeneratorBase {
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context);
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods);

~AuthDecoratorGenerator() override = default;

Expand Down
6 changes: 4 additions & 2 deletions generator/internal/client_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ ClientGenerator::ClientGenerator(
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context)
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods)
: ServiceCodeGenerator("client_header_path", "client_cc_path",
service_descriptor, std::move(service_vars),
std::move(service_method_vars), context) {
std::move(service_method_vars), context,
mixin_methods) {
// Remember if there are methods from google.iam.v1.GetIamPolicyRequest and
// google.iam.v1.SetIamPolicyRequest to google.iam.v1.Policy with signature
// extensions. If so, we'll generate a "set" wrapper method to help prevent
Expand Down
3 changes: 2 additions & 1 deletion generator/internal/client_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class ClientGenerator : public ServiceCodeGenerator {
ClientGenerator(google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context);
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods);

~ClientGenerator() override = default;

Expand Down
20 changes: 13 additions & 7 deletions generator/internal/connection_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "generator/internal/pagination.h"
#include "generator/internal/predicate_utils.h"
#include "generator/internal/printer.h"
#include "absl/strings/str_split.h"
#include <google/protobuf/descriptor.h>

namespace google {
Expand All @@ -29,10 +30,12 @@ ConnectionGenerator::ConnectionGenerator(
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context)
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods)
: ServiceCodeGenerator("connection_header_path", "connection_cc_path",
service_descriptor, std::move(service_vars),
std::move(service_method_vars), context) {}
std::move(service_method_vars), context,
mixin_methods) {}

Status ConnectionGenerator::GenerateHeader() {
HeaderPrint(CopyrightLicenseFileHeader());
Expand Down Expand Up @@ -68,11 +71,14 @@ Status ConnectionGenerator::GenerateHeader() {
: "",
IsExperimental() ? "google/cloud/experimental_tag.h" : "",
"google/cloud/version.h"});
HeaderSystemIncludes(
{vars("proto_header_path"), vars("additional_pb_header_paths"),
HasGRPCLongrunningOperation() ? "google/longrunning/operations.grpc.pb.h"
: "",
"memory"});
std::vector<std::string> const additional_pb_header_paths =
absl::StrSplit(vars("additional_pb_header_paths"), absl::ByChar(','));
HeaderSystemIncludes(additional_pb_header_paths);
HeaderSystemIncludes({vars("proto_header_path"),
HasGRPCLongrunningOperation()
? "google/longrunning/operations.grpc.pb.h"
: "",
"memory"});
switch (endpoint_location_style) {
case ServiceConfiguration::LOCATION_DEPENDENT:
case ServiceConfiguration::LOCATION_DEPENDENT_COMPAT:
Expand Down
3 changes: 2 additions & 1 deletion generator/internal/connection_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class ConnectionGenerator : public ServiceCodeGenerator {
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context);
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods);

~ConnectionGenerator() override = default;

Expand Down
11 changes: 6 additions & 5 deletions generator/internal/connection_impl_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ ConnectionImplGenerator::ConnectionImplGenerator(
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context)
: ServiceCodeGenerator("connection_impl_header_path",
"connection_impl_cc_path", service_descriptor,
std::move(service_vars),
std::move(service_method_vars), context) {}
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods)
: ServiceCodeGenerator(
"connection_impl_header_path", "connection_impl_cc_path",
service_descriptor, std::move(service_vars),
std::move(service_method_vars), context, mixin_methods) {}

Status ConnectionImplGenerator::GenerateHeader() {
HeaderPrint(CopyrightLicenseFileHeader());
Expand Down
3 changes: 2 additions & 1 deletion generator/internal/connection_impl_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class ConnectionImplGenerator : public ServiceCodeGenerator {
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context);
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods);

~ConnectionImplGenerator() override = default;

Expand Down
11 changes: 6 additions & 5 deletions generator/internal/connection_impl_rest_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ ConnectionImplRestGenerator::ConnectionImplRestGenerator(
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context)
: ServiceCodeGenerator("connection_impl_rest_header_path",
"connection_impl_rest_cc_path", service_descriptor,
std::move(service_vars),
std::move(service_method_vars), context) {}
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods)
: ServiceCodeGenerator(
"connection_impl_rest_header_path", "connection_impl_rest_cc_path",
service_descriptor, std::move(service_vars),
std::move(service_method_vars), context, mixin_methods) {}

Status ConnectionImplRestGenerator::GenerateHeader() {
HeaderPrint(CopyrightLicenseFileHeader());
Expand Down
3 changes: 2 additions & 1 deletion generator/internal/connection_impl_rest_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class ConnectionImplRestGenerator : public ServiceCodeGenerator {
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context);
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods);

~ConnectionImplRestGenerator() override = default;

Expand Down
11 changes: 6 additions & 5 deletions generator/internal/connection_rest_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ ConnectionRestGenerator::ConnectionRestGenerator(
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context)
: ServiceCodeGenerator("connection_rest_header_path",
"connection_rest_cc_path", service_descriptor,
std::move(service_vars),
std::move(service_method_vars), context) {}
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods)
: ServiceCodeGenerator(
"connection_rest_header_path", "connection_rest_cc_path",
service_descriptor, std::move(service_vars),
std::move(service_method_vars), context, mixin_methods) {}

Status ConnectionRestGenerator::GenerateHeader() {
HeaderPrint(CopyrightLicenseFileHeader());
Expand Down
3 changes: 2 additions & 1 deletion generator/internal/connection_rest_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class ConnectionRestGenerator : public ServiceCodeGenerator {
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context);
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods);

~ConnectionRestGenerator() override = default;

Expand Down
6 changes: 4 additions & 2 deletions generator/internal/descriptor_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ VarsDictionary GetMethodVars(
YAML::Node const& service_config,
google::protobuf::MethodDescriptor const& method,
absl::optional<google::api::HttpRule> const& http_rule,
std::string const& grpc_stub_name,
VarsDictionary const& idempotency_overrides,
std::set<std::string> const& omitted_rpcs) {
VarsDictionary method_vars;
Expand All @@ -524,6 +525,7 @@ VarsDictionary GetMethodVars(
method_vars["idempotency"] = iter->second;
}
}
method_vars["grpc_stub"] = grpc_stub_name + "_";
method_vars["method_name"] = method.name();
method_vars["method_name_snake"] = CamelCaseToSnakeCase(method.name());
method_vars["request_type"] =
Expand Down Expand Up @@ -888,14 +890,14 @@ std::map<std::string, VarsDictionary> CreateMethodVars(
http_rule = method.options().GetExtension(google::api::http);
}
service_methods_vars[method.full_name()] =
GetMethodVars(service, service_config, method, http_rule,
GetMethodVars(service, service_config, method, http_rule, "grpc_stub",
idempotency_overrides, omitted_rpcs);
}
for (auto const& mixin_method : mixin_methods) {
auto const& method = mixin_method.method.get();
service_methods_vars[method.full_name()] = GetMethodVars(
service, service_config, method, mixin_method.http_override,
idempotency_overrides, omitted_rpcs);
mixin_method.grpc_stub_name, idempotency_overrides, omitted_rpcs);
}
return service_methods_vars;
}
Expand Down
8 changes: 6 additions & 2 deletions generator/internal/descriptor_utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1318,8 +1318,8 @@ TEST_F(CreateMethodVarsTest, CreateMixinMethodsVars) {
kListLocationsHttpRule, &http_rule1));

std::vector<MixinMethod> mixin_methods{
{"", "", *mixin_file->service(0)->method(0), http_rule0},
{"", "", *mixin_file->service(0)->method(1), http_rule1}};
{"stub1", "", *mixin_file->service(0)->method(0), http_rule0},
{"stub2", "", *mixin_file->service(0)->method(1), http_rule1}};

service_vars_ = CreateServiceVars(
*service_file_descriptor->service(0),
Expand All @@ -1339,6 +1339,7 @@ TEST_F(CreateMethodVarsTest, CreateMixinMethodsVars) {
AllOf(Contains(Pair("idempotency", "kIdempotent")),
Contains(Pair("method_http_query_parameters", "")),
Contains(Pair("method_http_verb", "Get")),
Contains(Pair("grpc_stub", "stub1_")),
Contains(Pair("method_name", "GetLocation")),
Contains(Pair("method_name_snake", "get_location")),
Contains(Pair("method_request_body", "")),
Expand Down Expand Up @@ -1368,6 +1369,7 @@ TEST_F(CreateMethodVarsTest, CreateMixinMethodsVars) {
AllOf(Contains(Pair("idempotency", "kNonIdempotent")),
Contains(Pair("method_http_query_parameters", "")),
Contains(Pair("method_http_verb", "Post")),
Contains(Pair("grpc_stub", "stub2_")),
Contains(Pair("method_name", "ListLocations")),
Contains(Pair("method_name_snake", "list_locations")),
Contains(Pair("method_request_body", "*")),
Expand Down Expand Up @@ -1415,6 +1417,8 @@ INSTANTIATE_TEST_SUITE_P(
"Method0"),
MethodVarsTestValues("my.service.v1.Service.Method0",
"method_name_snake", "method0"),
MethodVarsTestValues("my.service.v1.Service.Method0", "grpc_stub",
"grpc_stub_"),
MethodVarsTestValues("my.service.v1.Service.Method0", "request_type",
"my::service::v1::Bar"),
MethodVarsTestValues("my.service.v1.Service.Method0",
Expand Down
6 changes: 4 additions & 2 deletions generator/internal/forwarding_client_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ ForwardingClientGenerator::ForwardingClientGenerator(
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context)
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods)
: ServiceCodeGenerator("forwarding_client_header_path", service_descriptor,
std::move(service_vars),
std::move(service_method_vars), context) {}
std::move(service_method_vars), context,
mixin_methods) {}

Status ForwardingClientGenerator::GenerateHeader() {
HeaderPrint(CopyrightLicenseFileHeader());
Expand Down
3 changes: 2 additions & 1 deletion generator/internal/forwarding_client_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class ForwardingClientGenerator : public ServiceCodeGenerator {
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context);
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods);

~ForwardingClientGenerator() override = default;

Expand Down
6 changes: 4 additions & 2 deletions generator/internal/forwarding_connection_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ ForwardingConnectionGenerator::ForwardingConnectionGenerator(
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context)
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods)
: ServiceCodeGenerator("forwarding_connection_header_path",
service_descriptor, std::move(service_vars),
std::move(service_method_vars), context) {}
std::move(service_method_vars), context,
mixin_methods) {}

Status ForwardingConnectionGenerator::GenerateHeader() {
HeaderPrint(CopyrightLicenseFileHeader());
Expand Down
3 changes: 2 additions & 1 deletion generator/internal/forwarding_connection_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class ForwardingConnectionGenerator : public ServiceCodeGenerator {
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context);
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods);

~ForwardingConnectionGenerator() override = default;

Expand Down
6 changes: 4 additions & 2 deletions generator/internal/forwarding_idempotency_policy_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ ForwardingIdempotencyPolicyGenerator::ForwardingIdempotencyPolicyGenerator(
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context)
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods)
: ServiceCodeGenerator("forwarding_idempotency_policy_header_path",
service_descriptor, std::move(service_vars),
std::move(service_method_vars), context) {}
std::move(service_method_vars), context,
mixin_methods) {}

Status ForwardingIdempotencyPolicyGenerator::GenerateHeader() {
HeaderPrint(CopyrightLicenseFileHeader());
Expand Down
3 changes: 2 additions & 1 deletion generator/internal/forwarding_idempotency_policy_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class ForwardingIdempotencyPolicyGenerator : public ServiceCodeGenerator {
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context);
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods);

~ForwardingIdempotencyPolicyGenerator() override = default;

Expand Down
6 changes: 4 additions & 2 deletions generator/internal/forwarding_mock_connection_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ ForwardingMockConnectionGenerator::ForwardingMockConnectionGenerator(
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context)
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods)
: ServiceCodeGenerator("forwarding_mock_connection_header_path",
service_descriptor, std::move(service_vars),
std::move(service_method_vars), context) {}
std::move(service_method_vars), context,
mixin_methods) {}

Status ForwardingMockConnectionGenerator::GenerateHeader() {
HeaderPrint(CopyrightLicenseFileHeader());
Expand Down
3 changes: 2 additions & 1 deletion generator/internal/forwarding_mock_connection_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class ForwardingMockConnectionGenerator : public ServiceCodeGenerator {
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context);
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods);

~ForwardingMockConnectionGenerator() override = default;

Expand Down
6 changes: 4 additions & 2 deletions generator/internal/forwarding_options_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ ForwardingOptionsGenerator::ForwardingOptionsGenerator(
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context)
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods)
: ServiceCodeGenerator("forwarding_options_header_path", service_descriptor,
std::move(service_vars),
std::move(service_method_vars), context) {}
std::move(service_method_vars), context,
mixin_methods) {}

Status ForwardingOptionsGenerator::GenerateHeader() {
HeaderPrint(CopyrightLicenseFileHeader());
Expand Down
3 changes: 2 additions & 1 deletion generator/internal/forwarding_options_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class ForwardingOptionsGenerator : public ServiceCodeGenerator {
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context);
google::protobuf::compiler::GeneratorContext* context,
std::vector<MixinMethod> const& mixin_methods);

~ForwardingOptionsGenerator() override = default;

Expand Down
Loading

0 comments on commit 0ec814f

Please sign in to comment.