Skip to content

Commit f553c49

Browse files
authored
Fix optional fields in ObjectMetadata. (#951)
This is part of the fixes for #934. It fixes CommonMetadata and ObjectMetadata.
1 parent cfa1084 commit f553c49

File tree

4 files changed

+107
-29
lines changed

4 files changed

+107
-29
lines changed

google/cloud/storage/bucket_metadata.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,14 @@ std::ostream& operator<<(std::ostream& os, BucketMetadata const& rhs) {
193193
}
194194

195195
os << ", location=" << rhs.location() << ", logging=" << rhs.logging()
196-
<< ", metageneration=" << rhs.metageneration() << ", name=" << rhs.name()
197-
<< ", owner.entity=" << rhs.owner().entity
198-
<< ", owner.entity_id=" << rhs.owner().entity_id
199-
<< ", self_link=" << rhs.self_link()
196+
<< ", metageneration=" << rhs.metageneration() << ", name=" << rhs.name();
197+
198+
if (rhs.has_owner()) {
199+
os << ", owner.entity=" << rhs.owner().entity
200+
<< ", owner.entity_id=" << rhs.owner().entity_id;
201+
}
202+
203+
os << ", self_link=" << rhs.self_link()
200204
<< ", storage_class=" << rhs.storage_class()
201205
<< ", time_created=" << rhs.time_created().time_since_epoch().count()
202206
<< ", updated=" << rhs.updated().time_since_epoch().count();

google/cloud/storage/internal/common_metadata.h

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_COMMON_METADATA_H_
1616
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_COMMON_METADATA_H_
1717

18+
#include "google/cloud/internal/optional.h"
1819
#include "google/cloud/storage/internal/metadata_parser.h"
1920
#include "google/cloud/storage/internal/nljson.h"
2021
#include <chrono>
@@ -31,6 +32,32 @@ struct Owner {
3132
std::string entity_id;
3233
};
3334

35+
inline bool operator==(Owner const& lhs, Owner const& rhs) {
36+
return std::tie(lhs.entity, lhs.entity_id) ==
37+
std::tie(rhs.entity, rhs.entity_id);
38+
}
39+
40+
inline bool operator<(Owner const& lhs, Owner const& rhs) {
41+
return std::tie(lhs.entity, lhs.entity_id) <
42+
std::tie(rhs.entity, rhs.entity_id);
43+
}
44+
45+
inline bool operator!=(Owner const& lhs, Owner const& rhs) {
46+
return std::rel_ops::operator!=(lhs, rhs);
47+
}
48+
49+
inline bool operator>(Owner const& lhs, Owner const& rhs) {
50+
return std::rel_ops::operator>(lhs, rhs);
51+
}
52+
53+
inline bool operator<=(Owner const& lhs, Owner const& rhs) {
54+
return std::rel_ops::operator<=(lhs, rhs);
55+
}
56+
57+
inline bool operator>=(Owner const& lhs, Owner const& rhs) {
58+
return std::rel_ops::operator>=(lhs, rhs);
59+
}
60+
3461
namespace internal {
3562
/**
3663
* Refactor common functionality in BucketMetadata and ObjectMetadata.
@@ -43,7 +70,7 @@ namespace internal {
4370
template <typename Derived>
4471
class CommonMetadata {
4572
public:
46-
CommonMetadata() : metageneration_(0) {}
73+
CommonMetadata() : metageneration_(0), owner_() {}
4774

4875
static CommonMetadata<Derived> ParseFromJson(internal::nl::json const& json) {
4976
CommonMetadata<Derived> result{};
@@ -53,8 +80,10 @@ class CommonMetadata {
5380
result.metageneration_ = ParseLongField(json, "metageneration");
5481
result.name_ = json.value("name", "");
5582
if (json.count("owner") != 0) {
56-
result.owner_.entity = json["owner"].value("entity", "");
57-
result.owner_.entity_id = json["owner"].value("entityId", "");
83+
Owner o;
84+
o.entity = json["owner"].value("entity", "");
85+
o.entity_id = json["owner"].value("entityId", "");
86+
result.owner_ = std::move(o);
5887
}
5988
result.self_link_ = json.value("selfLink", "");
6089
result.storage_class_ = json.value("storageClass", "");
@@ -78,7 +107,9 @@ class CommonMetadata {
78107
return *static_cast<Derived*>(this);
79108
}
80109

81-
Owner const& owner() const { return owner_; }
110+
bool has_owner() const { return owner_.has_value(); }
111+
Owner const& owner() const { return owner_.value(); }
112+
82113
std::string const& self_link() const { return self_link_; }
83114

84115
std::string const& storage_class() const { return storage_class_; }
@@ -112,7 +143,7 @@ class CommonMetadata {
112143
std::string kind_;
113144
std::int64_t metageneration_;
114145
std::string name_;
115-
Owner owner_;
146+
google::cloud::internal::optional<Owner> owner_;
116147
std::string self_link_;
117148
std::string storage_class_;
118149
std::chrono::system_clock::time_point time_created_;

google/cloud/storage/object_metadata.cc

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ ObjectMetadata ObjectMetadata::ParseFromJson(internal::nl::json const& json) {
4141
result.crc32c_ = json.value("crc32c", "");
4242
if (json.count("customerEncryption") != 0) {
4343
auto field = json["customerEncryption"];
44-
result.customer_encryption_.encryption_algorithm =
45-
field.value("encryptionAlgorithm", "");
46-
result.customer_encryption_.key_sha256 = field.value("keySha256", "");
44+
CustomerEncryption e;
45+
e.encryption_algorithm = field.value("encryptionAlgorithm", "");
46+
e.key_sha256 = field.value("keySha256", "");
47+
result.customer_encryption_ = std::move(e);
4748
}
4849
result.generation_ = internal::ParseLongField(json, "generation");
4950
result.kms_key_name_ = json.value("kmsKeyName", "");
@@ -76,10 +77,7 @@ bool ObjectMetadata::operator==(ObjectMetadata const& rhs) const {
7677
content_encoding_ == rhs.content_encoding_ and
7778
content_language_ == rhs.content_language_ and
7879
content_type_ == rhs.content_type_ and crc32c_ == rhs.crc32c_ and
79-
customer_encryption_.encryption_algorithm ==
80-
rhs.customer_encryption_.encryption_algorithm and
81-
customer_encryption_.key_sha256 ==
82-
rhs.customer_encryption_.key_sha256 and
80+
customer_encryption_ == customer_encryption_ and
8381
generation_ == rhs.generation_ and
8482
kms_key_name_ == rhs.kms_key_name_ and md5_hash_ == rhs.md5_hash_ and
8583
media_link_ == rhs.media_link_ and metadata_ == rhs.metadata_ and
@@ -102,23 +100,32 @@ std::ostream& operator<<(std::ostream& os, ObjectMetadata const& rhs) {
102100
<< ", content_disposition=" << rhs.content_disposition()
103101
<< ", content_encoding=" << rhs.content_encoding()
104102
<< ", content_language=" << rhs.content_language()
105-
<< ", content_type=" << rhs.content_type() << ", crc32c=" << rhs.crc32c()
106-
<< ", customer_encryption.encryption_algorithm="
107-
<< rhs.customer_encryption().encryption_algorithm
108-
<< ", customer_encryption.key_sha256="
109-
<< rhs.customer_encryption().key_sha256 << ", etag=" << rhs.etag()
110-
<< ", generation=" << rhs.generation() << ", id=" << rhs.id()
111-
<< ", kind=" << rhs.kind() << ", kms_key_name=" << rhs.kms_key_name()
103+
<< ", content_type=" << rhs.content_type() << ", crc32c=" << rhs.crc32c();
104+
105+
if (rhs.has_customer_encryption()) {
106+
os << ", customer_encryption.encryption_algorithm="
107+
<< rhs.customer_encryption().encryption_algorithm
108+
<< ", customer_encryption.key_sha256="
109+
<< rhs.customer_encryption().key_sha256;
110+
}
111+
112+
os << ", etag=" << rhs.etag() << ", generation=" << rhs.generation()
113+
<< ", id=" << rhs.id() << ", kind=" << rhs.kind()
114+
<< ", kms_key_name=" << rhs.kms_key_name()
112115
<< ", md5_hash=" << rhs.md5_hash() << ", media_link=" << rhs.media_link();
113116
sep = "metadata.";
114117
for (auto const& kv : rhs.metadata_) {
115118
os << sep << kv.first << "=" << kv.second;
116119
sep = ", metadata.";
117120
}
118-
os << ", metageneration=" << rhs.metageneration() << ", name=" << rhs.name()
119-
<< ", owner.entity=" << rhs.owner().entity
120-
<< ", owner.entity_id=" << rhs.owner().entity_id
121-
<< ", self_link=" << rhs.self_link() << ", size=" << rhs.size()
121+
os << ", metageneration=" << rhs.metageneration() << ", name=" << rhs.name();
122+
123+
if (rhs.has_owner()) {
124+
os << ", owner.entity=" << rhs.owner().entity
125+
<< ", owner.entity_id=" << rhs.owner().entity_id;
126+
}
127+
128+
os << ", self_link=" << rhs.self_link() << ", size=" << rhs.size()
122129
<< ", storage_class=" << rhs.storage_class()
123130
<< ", time_created=" << rhs.time_created().time_since_epoch().count()
124131
<< ", time_deleted=" << rhs.time_deleted().time_since_epoch().count()

google/cloud/storage/object_metadata.h

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,38 @@ struct CustomerEncryption {
3131
std::string key_sha256;
3232
};
3333

34+
inline bool operator==(CustomerEncryption const& lhs,
35+
CustomerEncryption const& rhs) {
36+
return std::tie(lhs.encryption_algorithm, lhs.key_sha256) ==
37+
std::tie(rhs.encryption_algorithm, lhs.key_sha256);
38+
}
39+
40+
inline bool operator<(CustomerEncryption const& lhs,
41+
CustomerEncryption const& rhs) {
42+
return std::tie(lhs.encryption_algorithm, lhs.key_sha256) <
43+
std::tie(rhs.encryption_algorithm, lhs.key_sha256);
44+
}
45+
46+
inline bool operator!=(CustomerEncryption const& lhs,
47+
CustomerEncryption const& rhs) {
48+
return std::rel_ops::operator!=(lhs, rhs);
49+
}
50+
51+
inline bool operator>(CustomerEncryption const& lhs,
52+
CustomerEncryption const& rhs) {
53+
return std::rel_ops::operator>(lhs, rhs);
54+
}
55+
56+
inline bool operator<=(CustomerEncryption const& lhs,
57+
CustomerEncryption const& rhs) {
58+
return std::rel_ops::operator<=(lhs, rhs);
59+
}
60+
61+
inline bool operator>=(CustomerEncryption const& lhs,
62+
CustomerEncryption const& rhs) {
63+
return std::rel_ops::operator>=(lhs, rhs);
64+
}
65+
3466
/**
3567
* Represents the metadata for a Google Cloud Storage Object.
3668
*/
@@ -85,8 +117,12 @@ class ObjectMetadata : private internal::CommonMetadata<ObjectMetadata> {
85117
}
86118

87119
std::string const& crc32c() const { return crc32c_; }
120+
121+
bool has_customer_encryption() const {
122+
return customer_encryption_.has_value();
123+
}
88124
CustomerEncryption const& customer_encryption() const {
89-
return customer_encryption_;
125+
return customer_encryption_.value();
90126
}
91127

92128
using CommonMetadata::etag;
@@ -167,7 +203,7 @@ class ObjectMetadata : private internal::CommonMetadata<ObjectMetadata> {
167203
std::string content_language_;
168204
std::string content_type_;
169205
std::string crc32c_;
170-
CustomerEncryption customer_encryption_;
206+
google::cloud::internal::optional<CustomerEncryption> customer_encryption_;
171207
std::int64_t generation_;
172208
std::string kms_key_name_;
173209
std::string md5_hash_;

0 commit comments

Comments
 (0)