Skip to content

Commit fe94e38

Browse files
authored
feat(GCS+gRPC): implement parser for BucketMetadata components (#7766)
1 parent 62c0682 commit fe94e38

4 files changed

Lines changed: 516 additions & 3 deletions

File tree

google/cloud/storage/internal/grpc_bucket_metadata_parser.cc

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,30 @@
1414

1515
#include "google/cloud/storage/internal/grpc_bucket_metadata_parser.h"
1616
#include "google/cloud/storage/version.h"
17+
#include "google/cloud/internal/time_utils.h"
18+
#include "absl/algorithm/container.h"
19+
#include "absl/time/civil_time.h"
1720

1821
namespace google {
1922
namespace cloud {
2023
namespace storage {
2124
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
2225
namespace internal {
26+
namespace {
27+
28+
absl::CivilDay ToCivilDay(google::type::Date const& date) {
29+
return absl::CivilDay(date.year(), date.month(), date.day());
30+
}
31+
32+
google::type::Date ToProtoDate(absl::CivilDay d) {
33+
google::type::Date date;
34+
date.set_year(static_cast<std::int32_t>(d.year()));
35+
date.set_month(d.month());
36+
date.set_day(d.day());
37+
return date;
38+
}
39+
40+
} // namespace
2341

2442
google::storage::v2::Bucket::Billing GrpcBucketMetadataParser::ToProto(
2543
BucketBilling const& rhs) {
@@ -35,6 +53,243 @@ BucketBilling GrpcBucketMetadataParser::FromProto(
3553
return result;
3654
}
3755

56+
google::storage::v2::Bucket::Cors GrpcBucketMetadataParser::ToProto(
57+
CorsEntry const& rhs) {
58+
google::storage::v2::Bucket::Cors result;
59+
for (auto const& v : rhs.origin) {
60+
result.add_origin(v);
61+
}
62+
for (auto const& v : rhs.method) {
63+
result.add_method(v);
64+
}
65+
for (auto const& v : rhs.response_header) {
66+
result.add_response_header(v);
67+
}
68+
if (rhs.max_age_seconds.has_value()) {
69+
result.set_max_age_seconds(static_cast<std::int32_t>(*rhs.max_age_seconds));
70+
}
71+
return result;
72+
}
73+
74+
CorsEntry GrpcBucketMetadataParser::FromProto(
75+
google::storage::v2::Bucket::Cors const& rhs) {
76+
CorsEntry result;
77+
absl::c_copy(rhs.origin(), std::back_inserter(result.origin));
78+
absl::c_copy(rhs.method(), std::back_inserter(result.method));
79+
absl::c_copy(rhs.response_header(),
80+
std::back_inserter(result.response_header));
81+
result.max_age_seconds = rhs.max_age_seconds();
82+
return result;
83+
}
84+
85+
google::storage::v2::Bucket::Encryption GrpcBucketMetadataParser::ToProto(
86+
BucketEncryption const& rhs) {
87+
google::storage::v2::Bucket::Encryption result;
88+
result.set_default_kms_key(rhs.default_kms_key_name);
89+
return result;
90+
}
91+
92+
BucketEncryption GrpcBucketMetadataParser::FromProto(
93+
google::storage::v2::Bucket::Encryption const& rhs) {
94+
BucketEncryption result;
95+
result.default_kms_key_name = rhs.default_kms_key();
96+
return result;
97+
}
98+
99+
google::storage::v2::Bucket::IamConfig GrpcBucketMetadataParser::ToProto(
100+
BucketIamConfiguration const& rhs) {
101+
google::storage::v2::Bucket::IamConfig result;
102+
if (rhs.uniform_bucket_level_access.has_value()) {
103+
auto& ubla = *result.mutable_uniform_bucket_level_access();
104+
*ubla.mutable_lock_time() = google::cloud::internal::ToProtoTimestamp(
105+
rhs.uniform_bucket_level_access->locked_time);
106+
ubla.set_enabled(rhs.uniform_bucket_level_access->enabled);
107+
}
108+
return result;
109+
}
110+
111+
BucketIamConfiguration GrpcBucketMetadataParser::FromProto(
112+
google::storage::v2::Bucket::IamConfig const& rhs) {
113+
BucketIamConfiguration result;
114+
if (rhs.has_uniform_bucket_level_access()) {
115+
UniformBucketLevelAccess ubla;
116+
ubla.enabled = rhs.uniform_bucket_level_access().enabled();
117+
ubla.locked_time = google::cloud::internal::ToChronoTimePoint(
118+
rhs.uniform_bucket_level_access().lock_time());
119+
result.uniform_bucket_level_access = std::move(ubla);
120+
}
121+
return result;
122+
}
123+
124+
google::storage::v2::Bucket::Lifecycle::Rule::Action
125+
GrpcBucketMetadataParser::ToProto(LifecycleRuleAction rhs) {
126+
google::storage::v2::Bucket::Lifecycle::Rule::Action result;
127+
result.set_type(std::move(rhs.type));
128+
result.set_storage_class(std::move(rhs.storage_class));
129+
return result;
130+
}
131+
132+
LifecycleRuleAction GrpcBucketMetadataParser::FromProto(
133+
google::storage::v2::Bucket::Lifecycle::Rule::Action rhs) {
134+
LifecycleRuleAction result;
135+
result.type = std::move(*rhs.mutable_type());
136+
result.storage_class = std::move(*rhs.mutable_storage_class());
137+
return result;
138+
}
139+
140+
google::storage::v2::Bucket::Lifecycle::Rule::Condition
141+
GrpcBucketMetadataParser::ToProto(LifecycleRuleCondition rhs) {
142+
google::storage::v2::Bucket::Lifecycle::Rule::Condition result;
143+
if (rhs.age.has_value()) {
144+
result.set_age_days(*rhs.age);
145+
}
146+
if (rhs.created_before.has_value()) {
147+
*result.mutable_created_before() = ToProtoDate(*rhs.created_before);
148+
}
149+
if (rhs.is_live.has_value()) {
150+
result.set_is_live(*rhs.is_live);
151+
}
152+
if (rhs.num_newer_versions.has_value()) {
153+
result.set_num_newer_versions(*rhs.num_newer_versions);
154+
}
155+
if (rhs.matches_storage_class.has_value()) {
156+
for (auto& v : *rhs.matches_storage_class) {
157+
*result.add_matches_storage_class() = std::move(v);
158+
}
159+
}
160+
return result;
161+
}
162+
163+
LifecycleRuleCondition GrpcBucketMetadataParser::FromProto(
164+
google::storage::v2::Bucket::Lifecycle::Rule::Condition rhs) {
165+
LifecycleRuleCondition result;
166+
if (rhs.age_days() != 0) {
167+
result.age = rhs.age_days();
168+
}
169+
if (rhs.has_created_before()) {
170+
result.created_before = ToCivilDay(rhs.created_before());
171+
}
172+
if (rhs.has_is_live()) {
173+
result.is_live = rhs.is_live();
174+
}
175+
if (rhs.num_newer_versions() != 0) {
176+
result.num_newer_versions = rhs.num_newer_versions();
177+
}
178+
if (rhs.matches_storage_class_size() != 0) {
179+
std::vector<std::string> tmp;
180+
for (auto& v : *rhs.mutable_matches_storage_class()) {
181+
tmp.push_back(std::move(v));
182+
}
183+
result.matches_storage_class = std::move(tmp);
184+
}
185+
return result;
186+
}
187+
188+
google::storage::v2::Bucket::Lifecycle::Rule GrpcBucketMetadataParser::ToProto(
189+
LifecycleRule rhs) {
190+
google::storage::v2::Bucket::Lifecycle::Rule result;
191+
*result.mutable_action() = ToProto(std::move(rhs.action_));
192+
*result.mutable_condition() = ToProto(std::move(rhs.condition_));
193+
return result;
194+
}
195+
196+
LifecycleRule GrpcBucketMetadataParser::FromProto(
197+
google::storage::v2::Bucket::Lifecycle::Rule rhs) {
198+
LifecycleRuleAction action;
199+
LifecycleRuleCondition condition;
200+
if (rhs.has_action()) {
201+
action = FromProto(std::move(*rhs.mutable_action()));
202+
}
203+
if (rhs.has_condition()) {
204+
condition = FromProto(std::move(*rhs.mutable_condition()));
205+
}
206+
return LifecycleRule(std::move(condition), std::move(action));
207+
}
208+
209+
google::storage::v2::Bucket::Lifecycle GrpcBucketMetadataParser::ToProto(
210+
BucketLifecycle rhs) {
211+
google::storage::v2::Bucket::Lifecycle result;
212+
for (auto& v : rhs.rule) {
213+
*result.add_rule() = ToProto(std::move(v));
214+
}
215+
return result;
216+
}
217+
218+
BucketLifecycle GrpcBucketMetadataParser::FromProto(
219+
google::storage::v2::Bucket::Lifecycle rhs) {
220+
BucketLifecycle result;
221+
for (auto& v : *rhs.mutable_rule()) {
222+
result.rule.push_back(FromProto(std::move(v)));
223+
}
224+
return result;
225+
}
226+
227+
google::storage::v2::Bucket::Logging GrpcBucketMetadataParser::ToProto(
228+
BucketLogging const& rhs) {
229+
google::storage::v2::Bucket::Logging result;
230+
result.set_log_bucket(rhs.log_bucket);
231+
result.set_log_object_prefix(rhs.log_object_prefix);
232+
return result;
233+
}
234+
235+
BucketLogging GrpcBucketMetadataParser::FromProto(
236+
google::storage::v2::Bucket::Logging const& rhs) {
237+
BucketLogging result;
238+
result.log_bucket = rhs.log_bucket();
239+
result.log_object_prefix = rhs.log_object_prefix();
240+
return result;
241+
}
242+
243+
google::storage::v2::Bucket::RetentionPolicy GrpcBucketMetadataParser::ToProto(
244+
BucketRetentionPolicy const& rhs) {
245+
google::storage::v2::Bucket::RetentionPolicy result;
246+
*result.mutable_effective_time() =
247+
google::cloud::internal::ToProtoTimestamp(rhs.effective_time);
248+
result.set_is_locked(rhs.is_locked);
249+
result.set_retention_period(rhs.retention_period.count());
250+
return result;
251+
}
252+
253+
BucketRetentionPolicy GrpcBucketMetadataParser::FromProto(
254+
google::storage::v2::Bucket::RetentionPolicy const& rhs) {
255+
BucketRetentionPolicy result;
256+
result.effective_time =
257+
google::cloud::internal::ToChronoTimePoint(rhs.effective_time());
258+
result.is_locked = rhs.is_locked();
259+
result.retention_period = std::chrono::seconds(rhs.retention_period());
260+
return result;
261+
}
262+
263+
google::storage::v2::Bucket::Versioning GrpcBucketMetadataParser::ToProto(
264+
BucketVersioning const& rhs) {
265+
google::storage::v2::Bucket::Versioning result;
266+
result.set_enabled(rhs.enabled);
267+
return result;
268+
}
269+
270+
BucketVersioning GrpcBucketMetadataParser::FromProto(
271+
google::storage::v2::Bucket::Versioning const& rhs) {
272+
BucketVersioning result;
273+
result.enabled = rhs.enabled();
274+
return result;
275+
}
276+
277+
google::storage::v2::Bucket::Website GrpcBucketMetadataParser::ToProto(
278+
BucketWebsite rhs) {
279+
google::storage::v2::Bucket::Website result;
280+
result.set_main_page_suffix(std::move(rhs.main_page_suffix));
281+
result.set_not_found_page(std::move(rhs.not_found_page));
282+
return result;
283+
}
284+
285+
BucketWebsite GrpcBucketMetadataParser::FromProto(
286+
google::storage::v2::Bucket::Website rhs) {
287+
BucketWebsite result;
288+
result.main_page_suffix = std::move(*rhs.mutable_main_page_suffix());
289+
result.not_found_page = std::move(*rhs.mutable_not_found_page());
290+
return result;
291+
}
292+
38293
} // namespace internal
39294
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
40295
} // namespace storage

google/cloud/storage/internal/grpc_bucket_metadata_parser.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,54 @@ struct GrpcBucketMetadataParser {
2929
static google::storage::v2::Bucket::Billing ToProto(BucketBilling const& rhs);
3030
static BucketBilling FromProto(
3131
google::storage::v2::Bucket::Billing const& rhs);
32+
33+
static google::storage::v2::Bucket::Cors ToProto(CorsEntry const& rhs);
34+
static CorsEntry FromProto(google::storage::v2::Bucket::Cors const& rhs);
35+
36+
static google::storage::v2::Bucket::Encryption ToProto(
37+
BucketEncryption const& rhs);
38+
static BucketEncryption FromProto(
39+
google::storage::v2::Bucket::Encryption const& rhs);
40+
41+
static google::storage::v2::Bucket::IamConfig ToProto(
42+
BucketIamConfiguration const& rhs);
43+
static BucketIamConfiguration FromProto(
44+
google::storage::v2::Bucket::IamConfig const& rhs);
45+
46+
static google::storage::v2::Bucket::Lifecycle::Rule::Action ToProto(
47+
LifecycleRuleAction rhs);
48+
static LifecycleRuleAction FromProto(
49+
google::storage::v2::Bucket::Lifecycle::Rule::Action rhs);
50+
51+
static google::storage::v2::Bucket::Lifecycle::Rule::Condition ToProto(
52+
LifecycleRuleCondition rhs);
53+
static LifecycleRuleCondition FromProto(
54+
google::storage::v2::Bucket::Lifecycle::Rule::Condition rhs);
55+
56+
static google::storage::v2::Bucket::Lifecycle::Rule ToProto(
57+
LifecycleRule rhs);
58+
static LifecycleRule FromProto(
59+
google::storage::v2::Bucket::Lifecycle::Rule rhs);
60+
61+
static google::storage::v2::Bucket::Lifecycle ToProto(BucketLifecycle rhs);
62+
static BucketLifecycle FromProto(google::storage::v2::Bucket::Lifecycle rhs);
63+
64+
static google::storage::v2::Bucket::Logging ToProto(BucketLogging const& rhs);
65+
static BucketLogging FromProto(
66+
google::storage::v2::Bucket::Logging const& rhs);
67+
68+
static google::storage::v2::Bucket::RetentionPolicy ToProto(
69+
BucketRetentionPolicy const& rhs);
70+
static BucketRetentionPolicy FromProto(
71+
google::storage::v2::Bucket::RetentionPolicy const& rhs);
72+
73+
static google::storage::v2::Bucket::Versioning ToProto(
74+
BucketVersioning const& rhs);
75+
static BucketVersioning FromProto(
76+
google::storage::v2::Bucket::Versioning const& rhs);
77+
78+
static google::storage::v2::Bucket::Website ToProto(BucketWebsite rhs);
79+
static BucketWebsite FromProto(google::storage::v2::Bucket::Website rhs);
3280
};
3381

3482
} // namespace internal

0 commit comments

Comments
 (0)