Google Cloud Storage C++ Client  1.32.1
A C++ Client Library for Google Cloud Storage
notification_metadata.cc
Go to the documentation of this file.
1 // Copyright 2018 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 "google/cloud/storage/notification_metadata.h"
16 #include "google/cloud/storage/internal/notification_requests.h"
17 #include "google/cloud/internal/absl_str_join_quiet.h"
18 #include <nlohmann/json.hpp>
19 
20 namespace google {
21 namespace cloud {
22 namespace storage {
23 inline namespace STORAGE_CLIENT_NS {
25  // Required fields, always include them, even if empty.
26  nlohmann::json json{
27  {"topic", topic()},
28  {"payload_format", payload_format()},
29  };
30 
31  // custom_attributes()
32  if (!custom_attributes().empty()) {
33  nlohmann::json ca;
34  for (auto const& kv : custom_attributes()) {
35  ca[kv.first] = kv.second;
36  }
37  json["custom_attributes"] = ca;
38  }
39 
40  // event_types()
41  if (!event_types().empty()) {
42  nlohmann::json events;
43  for (auto const& v : event_types()) {
44  events.push_back(v);
45  }
46  json["event_types"] = events;
47  }
48 
49  if (!object_name_prefix().empty()) {
50  json["object_name_prefix"] = object_name_prefix();
51  }
52 
53  return json.dump();
54 }
55 
56 std::ostream& operator<<(std::ostream& os, NotificationMetadata const& rhs) {
57  os << "NotificationMetadata={id=" << rhs.id();
58 
59  // custom_attributes()
60  if (!rhs.custom_attributes().empty()) {
61  os << "custom_attributes."
62  << absl::StrJoin(rhs.custom_attributes(), ", custom_attributes.",
63  absl::PairFormatter("="));
64  }
65 
66  os << ", etag=" << rhs.etag();
67 
68  // event_types()
69  os << ", event_types=[";
70  os << absl::StrJoin(rhs.event_types(), ", ");
71  os << "]";
72 
73  return os << ", kind=" << rhs.kind()
74  << ", object_name_prefix=" << rhs.object_name_prefix()
75  << ", payload_format=" << rhs.payload_format()
76  << ", self_link=" << rhs.self_link() << ", topic=" << rhs.topic()
77  << "}";
78 }
79 
80 } // namespace STORAGE_CLIENT_NS
81 } // namespace storage
82 } // namespace cloud
83 } // namespace google