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 #include "google/cloud/pubsub/snapshot_mutation_builder.h"
16 #include <google/protobuf/util/field_mask_util.h>
17 
18 namespace google {
19 namespace cloud {
20 namespace pubsub {
21 inline namespace GOOGLE_CLOUD_CPP_PUBSUB_NS {
22 
23 google::pubsub::v1::CreateSnapshotRequest
BuildCreateMutation(Subscription const & subscription)24 SnapshotMutationBuilder::BuildCreateMutation(
25     Subscription const& subscription) && {
26   google::pubsub::v1::CreateSnapshotRequest request;
27   request.set_subscription(subscription.FullName());
28   request.mutable_labels()->swap(*proto_.mutable_labels());
29   return request;
30 }
31 
32 google::pubsub::v1::CreateSnapshotRequest
BuildCreateMutation(Subscription const & subscription,Snapshot const & snapshot)33 SnapshotMutationBuilder::BuildCreateMutation(Subscription const& subscription,
34                                              Snapshot const& snapshot) && {
35   google::pubsub::v1::CreateSnapshotRequest request;
36   request.set_subscription(subscription.FullName());
37   request.set_name(snapshot.FullName());
38   request.mutable_labels()->swap(*proto_.mutable_labels());
39   return request;
40 }
41 
42 google::pubsub::v1::UpdateSnapshotRequest
BuildUpdateMutation(Snapshot const & snapshot)43 SnapshotMutationBuilder::BuildUpdateMutation(Snapshot const& snapshot) && {
44   google::pubsub::v1::UpdateSnapshotRequest request;
45   for (auto const& p : paths_) {
46     google::protobuf::util::FieldMaskUtil::AddPathToFieldMask<
47         google::pubsub::v1::Snapshot>(p, request.mutable_update_mask());
48   }
49   proto_.set_name(snapshot.FullName());
50   *request.mutable_snapshot() = std::move(proto_);
51   return request;
52 }
53 
54 }  // namespace GOOGLE_CLOUD_CPP_PUBSUB_NS
55 }  // namespace pubsub
56 }  // namespace cloud
57 }  // namespace google
58