Skip to content

Commit 7ed741b

Browse files
authored
cleanup(generator): parse api version from url pattern (#14595)
* cleanup(generator): parse api version from url pattern * format * add tests * format * make regex string static * format
1 parent 98f5040 commit 7ed741b

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

generator/internal/http_option_utils.cc

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ void SetHttpQueryParameters(HttpExtensionInfo const& info,
264264
HttpExtensionInfo ParseHttpExtension(
265265
google::protobuf::MethodDescriptor const& method) {
266266
if (!method.options().HasExtension(google::api::http)) return {};
267-
auto api_version = FormatApiVersionFromPackageName(method);
268267

269268
HttpExtensionInfo info;
270269
google::api::HttpRule http_rule =
@@ -322,6 +321,9 @@ HttpExtensionInfo ParseHttpExtension(
322321
out->append(absl::visit(SegmentAsStringVisitor{}, s->value));
323322
};
324323

324+
auto api_version =
325+
FormatApiVersionFromUrlPattern(url_pattern, method.file()->name());
326+
325327
auto rest_path_visitor = RestPathVisitor(api_version, info.rest_path);
326328
for (auto const& s : parsed_http_rule->segments) {
327329
if (absl::holds_alternative<PathTemplate::Variable>(s->value)) {
@@ -392,6 +394,22 @@ std::string FormatApiVersionFromPackageName(
392394
return {}; // Suppress clang-tidy warnings
393395
}
394396

397+
// Generate api version by extracting the version from the url pattern.
398+
// In some cases(i.e. location), there is no version in the package name.
399+
std::string FormatApiVersionFromUrlPattern(std::string const& url_pattern,
400+
std::string const& file_name) {
401+
std::vector<std::string> parts = absl::StrSplit(url_pattern, '/');
402+
static std::regex const kRe{R"(v\d+)"};
403+
for (auto const& part : parts) {
404+
if (std::regex_match(part, kRe)) {
405+
return part;
406+
}
407+
}
408+
GCP_LOG(FATAL) << "Unrecognized API version in file: " << file_name
409+
<< ", url pattern: " << url_pattern;
410+
return {}; // Suppress clang-tidy warnings
411+
}
412+
395413
} // namespace generator_internal
396414
} // namespace cloud
397415
} // namespace google

generator/internal/http_option_utils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ std::string FormatRequestResource(google::protobuf::Descriptor const& request,
112112
std::string FormatApiVersionFromPackageName(
113113
google::protobuf::MethodDescriptor const& method);
114114

115+
/**
116+
* Parses the url pattern of the method and returns its API version.
117+
*/
118+
std::string FormatApiVersionFromUrlPattern(std::string const& url_pattern,
119+
std::string const& file_name);
120+
115121
} // namespace generator_internal
116122
} // namespace cloud
117123
} // namespace google

generator/internal/http_option_utils_test.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,25 @@ TEST_F(HttpOptionUtilsTest, FormatApiVersionFromPackageNameError) {
772772
"google/foo/v1/service_without_version.proto, package: my.service");
773773
}
774774

775+
TEST_F(HttpOptionUtilsTest, FormatApiVersionFromUrlPattern) {
776+
std::string file_name = "google/foo/v1/service.proto";
777+
std::string url_pattern_v1 = "/v1/foo/bar";
778+
EXPECT_THAT(FormatApiVersionFromUrlPattern(url_pattern_v1, file_name),
779+
Eq("v1"));
780+
std::string url_pattern_v2 = "/foo/v2/bar";
781+
EXPECT_THAT(FormatApiVersionFromUrlPattern(url_pattern_v2, file_name),
782+
Eq("v2"));
783+
}
784+
785+
TEST_F(HttpOptionUtilsTest, FormatApiVersionFromUrlPatternError) {
786+
std::string file_name = "google/foo/v1/service.proto";
787+
std::string url_pattern = "/foo/bar";
788+
EXPECT_DEATH_IF_SUPPORTED(
789+
FormatApiVersionFromUrlPattern(url_pattern, file_name),
790+
"Unrecognized API version in file: "
791+
"google/foo/v1/service.proto, url pattern: /foo/bar");
792+
}
793+
775794
} // namespace
776795
} // namespace generator_internal
777796
} // namespace cloud

0 commit comments

Comments
 (0)