Google Cloud Pub/Sub C++ Client  1.32.1
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 // 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 #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 {
29 inline namespace GOOGLE_CLOUD_CPP_PUBSUB_NS {
30 
31 /**
32  * Builds requests to create or update a Cloud Pub/Sub topic.
33  */
34 class TopicBuilder {
35  public:
36  explicit TopicBuilder(Topic const& topic) {
37  proto_.set_name(topic.FullName());
38  }
39 
40  google::pubsub::v1::Topic BuildCreateRequest() &&;
41 
42  google::pubsub::v1::UpdateTopicRequest BuildUpdateRequest() &&;
43 
44  TopicBuilder& add_label(std::string const& key, std::string const& value) & {
45  using value_type = protobuf::Map<std::string, std::string>::value_type;
46  proto_.mutable_labels()->insert(value_type(key, value));
47  paths_.insert("labels");
48  return *this;
49  }
50  TopicBuilder&& add_label(std::string const& key,
51  std::string const& value) && {
52  return std::move(add_label(key, value));
53  }
54 
56  proto_.clear_labels();
57  paths_.insert("labels");
58  return *this;
59  }
60  TopicBuilder&& clear_labels() && { return std::move(clear_labels()); }
61 
63  proto_.mutable_message_storage_policy()->add_allowed_persistence_regions(
64  std::move(region));
65  paths_.insert("message_storage_policy");
66  return *this;
67  }
68  TopicBuilder&& add_allowed_persistence_region(std::string region) && {
69  return std::move(add_allowed_persistence_region(std::move(region)));
70  }
71 
73  proto_.mutable_message_storage_policy()
74  ->clear_allowed_persistence_regions();
75  paths_.insert("message_storage_policy");
76  return *this;
77  }
79  return std::move(clear_allowed_persistence_regions());
80  }
81 
82  TopicBuilder& set_kms_key_name(std::string key_name) & {
83  proto_.set_kms_key_name(std::move(key_name));
84  paths_.insert("kms_key_name");
85  return *this;
86  }
87  TopicBuilder&& set_kms_key_name(std::string key_name) && {
88  return std::move(set_kms_key_name(std::move(key_name)));
89  }
90 
91  TopicBuilder& set_schema(pubsub::Schema const& schema) & {
92  proto_.mutable_schema_settings()->set_schema(schema.FullName());
93  paths_.insert("schema_settings.schema");
94  return *this;
95  }
96  TopicBuilder&& set_schema(pubsub::Schema const& schema) && {
97  return std::move(set_schema(schema));
98  }
99  TopicBuilder& set_encoding(google::pubsub::v1::Encoding encoding) & {
100  proto_.mutable_schema_settings()->set_encoding(encoding);
101  paths_.insert("schema_settings.encoding");
102  return *this;
103  }
104  TopicBuilder&& set_encoding(google::pubsub::v1::Encoding encoding) && {
105  return std::move(set_encoding(encoding));
106  }
107 
108  template <typename Rep, typename Period>
110  std::chrono::duration<Rep, Period> d) & {
111  *proto_.mutable_message_retention_duration() =
112  google::cloud::internal::ToDurationProto(d);
113  paths_.insert("message_retention_duration");
114  return *this;
115  }
116  template <typename Rep, typename Period>
118  std::chrono::duration<Rep, Period> d) && {
119  return std::move(set_message_retention_duration(d));
120  }
122  google::protobuf::Duration const& d) & {
123  *proto_.mutable_message_retention_duration() = d;
124  paths_.insert("message_retention_duration");
125  return *this;
126  }
128  google::protobuf::Duration const& d) && {
129  return std::move(set_message_retention_duration(d));
130  }
131 
132  private:
133  google::pubsub::v1::Topic proto_;
134  std::set<std::string> paths_;
135 };
136 
137 } // namespace GOOGLE_CLOUD_CPP_PUBSUB_NS
138 } // namespace pubsub
139 } // namespace cloud
140 } // namespace google
141 
142 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_TOPIC_BUILDER_H