Skip to content

Commit ba899ad

Browse files
authored
impl(v3): ensure generator has mixin proto descriptors available (#15797)
1 parent 913da4d commit ba899ad

File tree

5 files changed

+64
-25
lines changed

5 files changed

+64
-25
lines changed

generator/generator.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ bool Generator::Generate(google::protobuf::FileDescriptor const* file,
7272
services.reserve(file->service_count());
7373
for (int i = 0; i < file->service_count(); ++i) {
7474
auto const* service = file->service(i);
75-
if (!internal::Contains(omitted_services, service->name())) {
75+
if (!internal::Contains(omitted_services, service->name()) &&
76+
!internal::Contains(omitted_services, service->full_name())) {
7677
services.push_back(generator_internal::MakeGenerators(
7778
service, context, service_config, *command_line_args));
7879
}

generator/internal/mixin_utils.cc

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,28 @@ std::unordered_set<std::string> GetMethodNames(
140140

141141
} // namespace
142142

143-
std::vector<std::string> GetMixinProtoPaths(YAML::Node const& service_config) {
144-
std::vector<std::string> proto_paths;
145-
if (service_config.Type() != YAML::NodeType::Map) return proto_paths;
143+
std::vector<MixinService> GetMixinServiceProto(
144+
YAML::Node const& service_config) {
145+
std::vector<MixinService> mixin_services;
146+
if (service_config.Type() != YAML::NodeType::Map) return mixin_services;
146147
auto const& apis = service_config["apis"];
147-
if (apis.Type() != YAML::NodeType::Sequence) return proto_paths;
148+
if (apis.Type() != YAML::NodeType::Sequence) return mixin_services;
148149
for (auto const& api : apis) {
149150
if (api.Type() != YAML::NodeType::Map) continue;
150151
auto const& name = api["name"];
151152
if (name.Type() != YAML::NodeType::Scalar) continue;
152-
auto const package_path = name.as<std::string>();
153+
auto const service = name.as<std::string>();
153154
auto const& mixin_proto_path_map = GetMixinProtoPathMap();
154-
auto const it = mixin_proto_path_map.find(package_path);
155+
auto const it = mixin_proto_path_map.find(service);
155156
if (it == mixin_proto_path_map.end()) continue;
156-
proto_paths.push_back(it->second);
157+
mixin_services.push_back({it->first, it->second});
157158
}
158-
return proto_paths;
159+
return mixin_services;
159160
}
160161

161-
std::vector<std::string> GetMixinProtoPaths(
162+
std::vector<MixinService> GetMixinServiceProto(
162163
std::string const& service_yaml_path) {
163-
return GetMixinProtoPaths(YAML::LoadFile(service_yaml_path));
164+
return GetMixinServiceProto(YAML::LoadFile(service_yaml_path));
164165
}
165166

166167
std::vector<MixinMethod> GetMixinMethods(YAML::Node const& service_config,
@@ -173,15 +174,16 @@ std::vector<MixinMethod> GetMixinMethods(YAML::Node const& service_config,
173174
<< service.full_name();
174175
}
175176
std::unordered_set<std::string> const method_names = GetMethodNames(service);
176-
auto const mixin_proto_paths = GetMixinProtoPaths(service_config);
177+
auto const mixin_proto_paths = GetMixinServiceProto(service_config);
177178
auto const mixin_http_overrides = GetMixinHttpOverrides(service_config);
178179

179180
for (auto const& mixin_proto_path : mixin_proto_paths) {
180-
FileDescriptor const* mixin_file = pool->FindFileByName(mixin_proto_path);
181+
FileDescriptor const* mixin_file =
182+
pool->FindFileByName(mixin_proto_path.proto_file_path);
181183
if (mixin_file == nullptr) {
182184
GCP_LOG(FATAL) << __FILE__ << ":" << __LINE__
183185
<< " Mixin FileDescriptor is not found for path: "
184-
<< mixin_proto_path
186+
<< mixin_proto_path.proto_file_path
185187
<< " in service: " << service.full_name();
186188
}
187189
for (int i = 0; i < mixin_file->service_count(); ++i) {

generator/internal/mixin_utils.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,30 @@ struct MixinMethod {
3838
google::api::HttpRule http_override;
3939
};
4040

41+
struct MixinService {
42+
std::string service_full_name;
43+
std::string proto_file_path;
44+
};
45+
46+
inline bool operator==(MixinService const& lhs, MixinService const& rhs) {
47+
return lhs.service_full_name == rhs.service_full_name &&
48+
lhs.proto_file_path == rhs.proto_file_path;
49+
}
50+
inline bool operator!=(MixinService const& lhs, MixinService const& rhs) {
51+
return !(lhs == rhs);
52+
}
53+
4154
/**
4255
* Extract Mixin proto file paths from the YAML Node.
4356
*/
44-
std::vector<std::string> GetMixinProtoPaths(YAML::Node const& service_config);
57+
std::vector<MixinService> GetMixinServiceProto(
58+
YAML::Node const& service_config);
4559

4660
/**
4761
* Extract Mixin proto file paths from the YAML Node loaded from a YAML file
4862
* path.
4963
*/
50-
std::vector<std::string> GetMixinProtoPaths(
64+
std::vector<MixinService> GetMixinServiceProto(
5165
std::string const& service_yaml_path);
5266

5367
/**

generator/internal/mixin_utils_test.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,16 @@ TEST_F(MixinUtilsTest, FilesParseSuccessfully) {
207207
}
208208

209209
TEST_F(MixinUtilsTest, ExtractMixinProtoPathsFromYaml) {
210-
auto const mixin_proto_paths = GetMixinProtoPaths(service_config_);
211-
EXPECT_THAT(mixin_proto_paths,
212-
AllOf(Contains("google/cloud/location/locations.proto"),
213-
Contains("google/iam/v1/iam_policy.proto"),
214-
Contains("google/longrunning/operations.proto")));
210+
std::vector<MixinService> const mixin_proto_paths =
211+
GetMixinServiceProto(service_config_);
212+
EXPECT_THAT(
213+
mixin_proto_paths,
214+
AllOf(Contains(MixinService{"google.cloud.location.Locations",
215+
"google/cloud/location/locations.proto"}),
216+
Contains(MixinService{"google.iam.v1.IAMPolicy",
217+
"google/iam/v1/iam_policy.proto"}),
218+
Contains(MixinService{"google.longrunning.Operations",
219+
"google/longrunning/operations.proto"})));
215220
}
216221

217222
TEST_F(MixinUtilsTest, GetMixinMethods) {

generator/standalone_main.cc

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "generator/internal/descriptor_utils.h"
1919
#include "generator/internal/discovery_to_proto.h"
2020
#include "generator/internal/format_method_comments.h"
21+
#include "generator/internal/mixin_utils.h"
2122
#include "generator/internal/scaffold_generator.h"
2223
#include "google/cloud/internal/absl_str_cat_quiet.h"
2324
#include "google/cloud/internal/absl_str_join_quiet.h"
@@ -329,10 +330,6 @@ std::vector<std::future<google::cloud::Status>> GenerateCodeFromProtos(
329330
if (service.generate_rest_transport()) {
330331
args.emplace_back("--cpp_codegen_opt=generate_rest_transport=true");
331332
}
332-
args.emplace_back(service.service_proto_path());
333-
for (auto const& additional_proto_file : service.additional_proto_files()) {
334-
args.emplace_back(additional_proto_file);
335-
}
336333
if (service.experimental()) {
337334
args.emplace_back("--cpp_codegen_opt=experimental=true");
338335
}
@@ -388,10 +385,30 @@ std::vector<std::future<google::cloud::Status>> GenerateCodeFromProtos(
388385
"=", kv.second));
389386
}
390387

388+
std::vector<std::string> mixin_files_to_append;
391389
auto path = ServiceConfigYamlPath(yaml_root, scaffold_vars);
392390
if (!path.empty()) {
391+
auto mixins =
392+
google::cloud::generator_internal::GetMixinServiceProto(path);
393393
args.emplace_back(absl::StrCat("--cpp_codegen_opt=service_config_yaml=",
394394
std::move(path)));
395+
for (auto& mixin_service : mixins) {
396+
if (mixin_service.proto_file_path != service.service_proto_path()) {
397+
args.emplace_back("--cpp_codegen_opt=omit_service=" +
398+
std::move(mixin_service.service_full_name));
399+
mixin_files_to_append.push_back(
400+
std::move(mixin_service.proto_file_path));
401+
}
402+
}
403+
}
404+
405+
args.push_back(service.service_proto_path());
406+
for (auto const& additional_proto_file : service.additional_proto_files()) {
407+
args.push_back(additional_proto_file);
408+
}
409+
410+
for (auto& mixin_path : mixin_files_to_append) {
411+
args.push_back(std::move(mixin_path));
395412
}
396413

397414
GCP_LOG(INFO) << "Generating service code using: "

0 commit comments

Comments
 (0)