Skip to content

Commit 0aa8ce5

Browse files
authored
feature(generator): connection options generator (#5171)
* feature(generator): connection options generator
1 parent 6d4ec5b commit 0aa8ce5

22 files changed

+367
-33
lines changed

generator/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ add_library(
2929
generator.h
3030
internal/codegen_utils.cc
3131
internal/codegen_utils.h
32+
internal/connection_options_generator.cc
33+
internal/connection_options_generator.h
3234
internal/descriptor_utils.cc
3335
internal/descriptor_utils.h
3436
internal/generator_interface.h

generator/generator_test.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ TEST_F(GeneratorTest, BadCommandLineArgs) {
7676
}
7777

7878
TEST_F(GeneratorTest, GenerateServicesSuccess) {
79+
auto connection_options_header_output =
80+
absl::make_unique<generator_testing::MockZeroCopyOutputStream>();
81+
auto connection_options_cc_output =
82+
absl::make_unique<generator_testing::MockZeroCopyOutputStream>();
7983
auto stub_header_output =
8084
absl::make_unique<generator_testing::MockZeroCopyOutputStream>();
8185
auto stub_cc_output =
@@ -100,6 +104,10 @@ TEST_F(GeneratorTest, GenerateServicesSuccess) {
100104
service_file.mutable_options()->set_cc_generic_services(false);
101105
const FileDescriptor* service_file_descriptor = pool.BuildFile(service_file);
102106

107+
EXPECT_CALL(*connection_options_header_output, Next(_, _))
108+
.WillRepeatedly(Return(false));
109+
EXPECT_CALL(*connection_options_cc_output, Next(_, _))
110+
.WillRepeatedly(Return(false));
103111
EXPECT_CALL(*stub_header_output, Next(_, _)).WillRepeatedly(Return(false));
104112
EXPECT_CALL(*stub_cc_output, Next(_, _)).WillRepeatedly(Return(false));
105113
EXPECT_CALL(*logging_header_output, Next(_, _)).WillRepeatedly(Return(false));
@@ -113,6 +121,8 @@ TEST_F(GeneratorTest, GenerateServicesSuccess) {
113121
.WillRepeatedly(Return(false));
114122

115123
EXPECT_CALL(*context_, Open(_))
124+
.WillOnce(Return(connection_options_header_output.release()))
125+
.WillOnce(Return(connection_options_cc_output.release()))
116126
.WillOnce(Return(stub_header_output.release()))
117127
.WillOnce(Return(stub_cc_output.release()))
118128
.WillOnce(Return(logging_header_output.release()))

generator/google_cloud_cpp_generator.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
google_cloud_cpp_generator_hdrs = [
2020
"generator.h",
2121
"internal/codegen_utils.h",
22+
"internal/connection_options_generator.h",
2223
"internal/descriptor_utils.h",
2324
"internal/generator_interface.h",
2425
"internal/logging_decorator_generator.h",
@@ -33,6 +34,7 @@ google_cloud_cpp_generator_hdrs = [
3334
google_cloud_cpp_generator_srcs = [
3435
"generator.cc",
3536
"internal/codegen_utils.cc",
37+
"internal/connection_options_generator.cc",
3638
"internal/descriptor_utils.cc",
3739
"internal/logging_decorator_generator.cc",
3840
"internal/metadata_decorator_generator.cc",

generator/integration_tests/generator_integration_test.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ TEST_P(GeneratorIntegrationTest, CompareGeneratedToGolden) {
136136

137137
INSTANTIATE_TEST_SUITE_P(
138138
Generator, GeneratorIntegrationTest,
139-
testing::Values("internal/database_admin_logging_decorator.gcpcxx.pb.h",
139+
testing::Values("connection_options.gcpcxx.pb.h",
140+
"connection_options.gcpcxx.pb.cc",
141+
"internal/database_admin_logging_decorator.gcpcxx.pb.h",
140142
"internal/database_admin_logging_decorator.gcpcxx.pb.cc",
141143
"internal/database_admin_metadata_decorator.gcpcxx.pb.h",
142144
"internal/database_admin_metadata_decorator.gcpcxx.pb.cc",

generator/integration_tests/golden/BUILD

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ load(":google_cloud_cpp_generator_golden_files.bzl", "google_cloud_cpp_generator
2121
filegroup(
2222
name = "golden_filegroup",
2323
srcs = [
24-
"connection_options.cc",
25-
"connection_options.h",
24+
"connection_options.gcpcxx.pb.cc",
25+
"connection_options.gcpcxx.pb.h",
2626
"version.cc",
2727
"version.h",
2828
"version_info.h",

generator/integration_tests/golden/CMakeLists.txt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
set(google_cloud_cpp_generator_golden_files
1818
# cmake-format: sort
19-
connection_options.cc
20-
connection_options.h
19+
connection_options.gcpcxx.pb.cc
20+
connection_options.gcpcxx.pb.h
2121
internal/database_admin_logging_decorator.gcpcxx.pb.cc
2222
internal/database_admin_logging_decorator.gcpcxx.pb.h
2323
internal/database_admin_metadata_decorator.gcpcxx.pb.cc
@@ -38,8 +38,8 @@ export_list_to_bazel("google_cloud_cpp_generator_golden_files.bzl"
3838
# Create a library from the where the golden code was copied to.
3939
add_library(
4040
google_cloud_cpp_generator_golden_lib # cmake-format: sort
41-
connection_options.cc
42-
connection_options.h
41+
connection_options.gcpcxx.pb.cc
42+
connection_options.gcpcxx.pb.h
4343
internal/database_admin_logging_decorator.gcpcxx.pb.cc
4444
internal/database_admin_logging_decorator.gcpcxx.pb.h
4545
internal/database_admin_metadata_decorator.gcpcxx.pb.cc
@@ -78,8 +78,11 @@ function (google_cloud_cpp_generator_define_golden_tests)
7878

7979
set(google_cloud_cpp_generator_golden_tests
8080
# cmake-format: sort
81-
golden_logging_decorator_test.cc golden_metadata_decorator_test.cc
82-
golden_stub_factory_test.cc golden_stub_test.cc)
81+
golden_connection_options_test.cc
82+
golden_logging_decorator_test.cc
83+
golden_metadata_decorator_test.cc
84+
golden_stub_factory_test.cc
85+
golden_stub_test.cc)
8386

8487
# Export the list of unit tests to a .bzl file so we do not need to maintain
8588
# the list in two places.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Generated by the Codegen C++ plugin.
16+
// If you make any local changes, they will be lost.
17+
// source: generator/integration_tests/test.proto
18+
19+
#include "generator/integration_tests/golden/connection_options.gcpcxx.pb.h"
20+
#include "absl/strings/str_cat.h"
21+
#include "google/cloud/internal/compiler_info.h"
22+
#include <string>
23+
24+
namespace google {
25+
namespace cloud {
26+
namespace golden {
27+
inline namespace GOLDEN_CLIENT_NS {
28+
29+
std::string ConnectionOptionsTraits::default_endpoint() {
30+
return "test.googleapis.com";
31+
}
32+
33+
std::string ConnectionOptionsTraits::user_agent_prefix() {
34+
return absl::StrCat("gcloud-cpp/",
35+
VersionString(), " (",
36+
google::cloud::internal::CompilerId(), "-",
37+
google::cloud::internal::CompilerVersion(), "; ",
38+
google::cloud::internal::CompilerFeatures(), ")");
39+
}
40+
41+
int ConnectionOptionsTraits::default_num_channels() { return 4; }
42+
43+
} // namespace GOLDEN_CLIENT_NS
44+
} // namespace golden
45+
} // namespace cloud
46+
} // namespace google
47+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Generated by the Codegen C++ plugin.
16+
// If you make any local changes, they will be lost.
17+
// source: generator/integration_tests/test.proto
18+
#ifndef GOOGLE_CLOUD_CPP_GENERATOR_INTEGRATION_TESTS_GOLDEN_CONNECTION_OPTIONS_GCPCXX_PB_H
19+
#define GOOGLE_CLOUD_CPP_GENERATOR_INTEGRATION_TESTS_GOLDEN_CONNECTION_OPTIONS_GCPCXX_PB_H
20+
21+
#include "generator/integration_tests/golden/version.h"
22+
#include "google/cloud/connection_options.h"
23+
#include <string>
24+
25+
namespace google {
26+
namespace cloud {
27+
namespace golden {
28+
inline namespace GOLDEN_CLIENT_NS {
29+
30+
struct ConnectionOptionsTraits {
31+
static std::string default_endpoint();
32+
static std::string user_agent_prefix();
33+
static int default_num_channels();
34+
};
35+
36+
using ConnectionOptions =
37+
google::cloud::ConnectionOptions<ConnectionOptionsTraits>;
38+
39+
} // namespace GOLDEN_CLIENT_NS
40+
} // namespace golden
41+
} // namespace cloud
42+
} // namespace google
43+
44+
#endif // GOOGLE_CLOUD_CPP_GENERATOR_INTEGRATION_TESTS_GOLDEN_CONNECTION_OPTIONS_GCPCXX_PB_H
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "generator/integration_tests/golden/connection_options.gcpcxx.pb.h"
16+
#include <gmock/gmock.h>
17+
#include <memory>
18+
19+
namespace google {
20+
namespace cloud {
21+
namespace golden {
22+
inline namespace GOLDEN_CLIENT_NS {
23+
namespace {
24+
25+
using ::testing::HasSubstr;
26+
27+
TEST(GoldenConnectionOptionsTest, DefaultEndpoint) {
28+
ConnectionOptions options;
29+
EXPECT_EQ("test.googleapis.com", options.endpoint());
30+
}
31+
32+
TEST(GoldenConnectionOptionsTest, UserAgentPrefix) {
33+
ConnectionOptions options;
34+
EXPECT_THAT(options.user_agent_prefix(), HasSubstr("gcloud-cpp/v1.19.0"));
35+
}
36+
37+
TEST(GoldenConnectionOptionsTest, DefaultNumChannels) {
38+
ConnectionOptions options;
39+
EXPECT_EQ(4, options.num_channels());
40+
}
41+
42+
} // namespace
43+
} // namespace GOLDEN_CLIENT_NS
44+
} // namespace golden
45+
} // namespace cloud
46+
} // namespace google

generator/integration_tests/golden/golden_stub_factory_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ TEST_F(GoldenStubFactoryTest, DefaultStubWithoutLogging) {
5656
}
5757

5858
TEST_F(GoldenStubFactoryTest, DefaultStubWithLogging) {
59-
ConnectionOptions options;
59+
golden::ConnectionOptions options;
6060
options.enable_tracing("rpc");
6161
auto default_stub = CreateDefaultDatabaseAdminStub(options);
6262
auto const log_lines = ClearLogLines();

0 commit comments

Comments
 (0)