Google Cloud Pub/Sub C++ Client  2.4.0
A C++ Client Library for Google Cloud Pub/Sub
topic_builder.h
Go to the documentation of this file.
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 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_TOPIC_BUILDER_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_TOPIC_BUILDER_H
17 
18 #include "google/cloud/pubsub/schema.h"
19 #include "google/cloud/pubsub/topic.h"
20 #include "google/cloud/pubsub/version.h"
21 #include "google/cloud/internal/time_utils.h"
22 #include <google/pubsub/v1/pubsub.pb.h>
23 #include <set>
24 #include <string>
25 
26 namespace google {
27 namespace cloud {
28 namespace pubsub {
30 
31 /**
32  * Builds requests to create or update a Cloud Pub/Sub topic.
33  *
34  * Makes it easier to create the protobuf messages consumed by
35  * `TopicAdminClient`. The main advantages are:
36  *
37  * - Use a fluent API to set multiple values when constructing complex objects.
38  * - Automatically compute the set of paths for update requests.
39  */
40 class TopicBuilder {
41  public:
42  explicit TopicBuilder(Topic const& topic) {
43  proto_.set_name(topic.FullName());
44  }
45 
46  /// Build a protocol buffer message to create a new topic.
47  google::pubsub::v1::Topic BuildCreateRequest() &&;
48 
49  /// Build a protocol buffer message to update an existing topic.
50  google::pubsub::v1::UpdateTopicRequest BuildUpdateRequest() &&;
51 
52  /// @name Setters for each protocol buffer field.
53  ///@{
54  TopicBuilder& add_label(std::string const& key, std::string const& value) & {
55  using value_type =
56  google::protobuf::Map<std::string, std::string>::value_type;
57  proto_.mutable_labels()->insert(value_type(key, value));
58  paths_.insert("labels");
59  return *this;
60  }
61  TopicBuilder&& add_label(std::string const& key,
62  std::string const& value) && {
63  return std::move(add_label(key, value));
64  }
65 
67  proto_.clear_labels();
68  paths_.insert("labels");
69  return *this;
70  }
71  TopicBuilder&& clear_labels() && { return std::move(clear_labels()); }
72 
74  proto_.mutable_message_storage_policy()->add_allowed_persistence_regions(
75  std::move(region));
76  paths_.insert("message_storage_policy");
77  return *this;
78  }
79  TopicBuilder&& add_allowed_persistence_region(std::string region) && {
80  return std::move(add_allowed_persistence_region(std::move(region)));
81  }
82 
84  proto_.mutable_message_storage_policy()
85  ->clear_allowed_persistence_regions();
86  paths_.insert("message_storage_policy");
87  return *this;
88  }
90  return std::move(clear_allowed_persistence_regions());
91  }
92 
93  TopicBuilder& set_kms_key_name(std::string key_name) & {
94  proto_.set_kms_key_name(std::move(key_name));
95  paths_.insert("kms_key_name");
96  return *this;
97  }
98  TopicBuilder&& set_kms_key_name(std::string key_name) && {
99  return std::move(set_kms_key_name(std::move(key_name)));
100  }
101 
102  TopicBuilder& set_schema(pubsub::Schema const& schema) & {
103  proto_.mutable_schema_settings()->set_schema(schema.FullName());
104  paths_.insert("schema_settings.schema");
105  return *this;
106  }
107  TopicBuilder&& set_schema(pubsub::Schema const& schema) && {
108  return std::move(set_schema(schema));
109  }
110  TopicBuilder& set_encoding(google::pubsub::v1::Encoding encoding) & {
111  proto_.mutable_schema_settings()->set_encoding(encoding);
112  paths_.insert("schema_settings.encoding");
113  return *this;
114  }
115  TopicBuilder&& set_encoding(google::pubsub::v1::Encoding encoding) && {
116  return std::move(set_encoding(encoding));
117  }
118 
119  template <typename Rep, typename Period>
121  std::chrono::duration<Rep, Period> d) & {
122  *proto_.mutable_message_retention_duration() =
123  google::cloud::internal::ToDurationProto(d);
124  paths_.insert("message_retention_duration");
125  return *this;
126  }
127  template <typename Rep, typename Period>
129  std::chrono::duration<Rep, Period> d) && {
130  return std::move(set_message_retention_duration(d));
131  }
133  google::protobuf::Duration const& d) & {
134  *proto_.mutable_message_retention_duration() = d;
135  paths_.insert("message_retention_duration");
136  return *this;
137  }
139  google::protobuf::Duration const& d) && {
140  return std::move(set_message_retention_duration(d));
141  }
142  ///@}
143 
144  private:
145  google::pubsub::v1::Topic proto_;
146  std::set<std::string> paths_;
147 };
148 
150 } // namespace pubsub
151 } // namespace cloud
152 } // namespace google
153 
154 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_TOPIC_BUILDER_H