Google Cloud Pub/Sub C++ Client 2.13.0
A C++ Client Library for Google Cloud Pub/Sub
Loading...
Searching...
No Matches
topic_builder.h
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
26namespace google {
27namespace cloud {
28namespace pubsub {
29GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
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 */
40class 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
73 TopicBuilder& add_allowed_persistence_region(std::string region) & {
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 }
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 TopicBuilder& set_first_revision_id(std::string const& revision_id) & {
119 proto_.mutable_schema_settings()->set_first_revision_id(revision_id);
120 paths_.insert("schema_settings.first_revision_id");
121 return *this;
122 }
123 TopicBuilder&& set_first_revision_id(std::string const& revision_id) && {
124 return std::move(set_first_revision_id(revision_id));
125 }
126 TopicBuilder& set_last_revision_id(std::string const& revision_id) & {
127 proto_.mutable_schema_settings()->set_last_revision_id(revision_id);
128 paths_.insert("schema_settings.last_revision_id");
129 return *this;
130 }
131 TopicBuilder&& set_last_revision_id(std::string const& revision_id) && {
132 return std::move(set_last_revision_id(revision_id));
133 }
134
135 template <typename Rep, typename Period>
137 std::chrono::duration<Rep, Period> d) & {
138 *proto_.mutable_message_retention_duration() =
139 google::cloud::internal::ToDurationProto(d);
140 paths_.insert("message_retention_duration");
141 return *this;
142 }
143 template <typename Rep, typename Period>
145 std::chrono::duration<Rep, Period> d) && {
146 return std::move(set_message_retention_duration(d));
147 }
149 google::protobuf::Duration const& d) & {
150 *proto_.mutable_message_retention_duration() = d;
151 paths_.insert("message_retention_duration");
152 return *this;
153 }
155 google::protobuf::Duration const& d) && {
156 return std::move(set_message_retention_duration(d));
157 }
158 ///@}
159
160 private:
161 google::pubsub::v1::Topic proto_;
162 std::set<std::string> paths_;
163};
164
165GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
166} // namespace pubsub
167} // namespace cloud
168} // namespace google
169
170#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_TOPIC_BUILDER_H
Objects of this class identify a Cloud Pub/Sub schema.
Definition: schema.h:38
std::string FullName() const
Returns the fully qualified schema name as a string of the form: "projects/<project-id>/schemas/<sche...
Builds requests to create or update a Cloud Pub/Sub topic.
Definition: topic_builder.h:40
TopicBuilder && add_label(std::string const &key, std::string const &value) &&
Definition: topic_builder.h:61
TopicBuilder && clear_labels() &&
Definition: topic_builder.h:71
TopicBuilder & set_last_revision_id(std::string const &revision_id) &
Definition: topic_builder.h:126
TopicBuilder && add_allowed_persistence_region(std::string region) &&
Definition: topic_builder.h:79
TopicBuilder & set_schema(pubsub::Schema const &schema) &
Definition: topic_builder.h:102
TopicBuilder & add_allowed_persistence_region(std::string region) &
Definition: topic_builder.h:73
TopicBuilder(Topic const &topic)
Definition: topic_builder.h:42
TopicBuilder & set_message_retention_duration(google::protobuf::Duration const &d) &
Definition: topic_builder.h:148
TopicBuilder && set_encoding(google::pubsub::v1::Encoding encoding) &&
Definition: topic_builder.h:115
TopicBuilder & clear_allowed_persistence_regions() &
Definition: topic_builder.h:83
TopicBuilder && set_schema(pubsub::Schema const &schema) &&
Definition: topic_builder.h:107
TopicBuilder && set_message_retention_duration(std::chrono::duration< Rep, Period > d) &&
Definition: topic_builder.h:144
TopicBuilder & set_kms_key_name(std::string key_name) &
Definition: topic_builder.h:93
TopicBuilder & add_label(std::string const &key, std::string const &value) &
Definition: topic_builder.h:54
TopicBuilder & set_encoding(google::pubsub::v1::Encoding encoding) &
Definition: topic_builder.h:110
TopicBuilder & clear_labels() &
Definition: topic_builder.h:66
TopicBuilder && set_first_revision_id(std::string const &revision_id) &&
Definition: topic_builder.h:123
TopicBuilder && set_kms_key_name(std::string key_name) &&
Definition: topic_builder.h:98
TopicBuilder & set_message_retention_duration(std::chrono::duration< Rep, Period > d) &
Definition: topic_builder.h:136
TopicBuilder & set_first_revision_id(std::string const &revision_id) &
Definition: topic_builder.h:118
TopicBuilder && clear_allowed_persistence_regions() &&
Definition: topic_builder.h:89
TopicBuilder && set_message_retention_duration(google::protobuf::Duration const &d) &&
Definition: topic_builder.h:154
TopicBuilder && set_last_revision_id(std::string const &revision_id) &&
Definition: topic_builder.h:131
google::pubsub::v1::UpdateTopicRequest BuildUpdateRequest() &&
Build a protocol buffer message to update an existing topic.
google::pubsub::v1::Topic BuildCreateRequest() &&
Build a protocol buffer message to create a new topic.
Objects of this class identify a Cloud Pub/Sub topic.
Definition: topic.h:37
std::string FullName() const
Returns the fully qualified topic name as a string of the form: "projects/<project-id>/topics/<topic-...
Contains all the Cloud Pub/Sub C++ client types and functions.
Definition: ack_handler.h:25
The namespace Google Cloud Platform C++ client libraries.