Couchbase C++ SDK 1.0.1 (rev. 58d46d7)
Loading...
Searching...
No Matches
lookup_in_result.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright 2020-Present Couchbase, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#pragma once
19
21#include <couchbase/result.hxx>
23
24#include <optional>
25
26namespace couchbase
27{
28
36{
37public:
42 struct entry {
43 std::string path;
45 std::size_t original_index;
46 bool exists;
47 std::error_code ec;
48 };
49
54 lookup_in_result() = default;
55
67 : result{ cas }
68 , entries_{ std::move(entries) }
69 , is_deleted_{ is_deleted }
70 {
71 }
72
83 template<typename Document>
84 [[nodiscard]] auto content_as(std::size_t index) const -> Document
85 {
86 for (const entry& e : entries_) {
87 if (e.original_index == index) {
88 if (e.ec) {
89 throw std::system_error(e.ec,
90 "error getting result for spec at index " +
91 std::to_string(index) + ", path \"" + e.path + "\"");
92 }
93
94 return codec::tao_json_serializer::deserialize<Document>(e.value);
95 }
96 }
97 throw std::system_error(errc::key_value::path_invalid,
98 "invalid index for lookup_in result: {}" + std::to_string(index));
99 }
100
111 template<typename Document>
112 [[nodiscard]] auto content_as(const std::string& path) const -> Document
113 {
114 for (const entry& e : entries_) {
115 if (e.path == path) {
116 if (e.ec) {
117 throw std::system_error(e.ec, "error getting result for path \"" + e.path + "\"");
118 }
119
120 return codec::tao_json_serializer::deserialize<Document>(e.value);
121 }
122 }
123 throw std::system_error(errc::key_value::path_invalid,
124 "invalid path for lookup_in result: " + path);
125 }
126
137 template<typename Document>
139 {
140 const auto& macro_string = subdoc::to_string(macro);
141 for (const entry& e : entries_) {
142 if (e.path == macro_string) {
143 if (e.ec) {
144 throw std::system_error(e.ec, "error getting result for macro \"" + macro_string + "\"");
145 }
146
147 return codec::tao_json_serializer::deserialize<Document>(e.value);
148 }
149 }
150 throw std::system_error(errc::key_value::path_invalid,
151 "invalid path for lookup_in result: macro#" +
152 std::to_string(static_cast<std::uint32_t>(macro)));
153 }
154
164 [[nodiscard]] auto exists(std::size_t index) const -> bool
165 {
166 for (const entry& e : entries_) {
167 if (e.original_index == index) {
169 throw std::system_error(e.ec, "error getting result for path \"" + e.path + "\"");
170 }
171
172 return e.exists;
173 }
174 }
175 return false;
176 }
177
188 {
189 const auto& macro_string = subdoc::to_string(macro);
190 for (const entry& e : entries_) {
191 if (e.path == macro_string) {
193 throw std::system_error(e.ec, "error getting result for macro \"" + macro_string + "\"");
194 }
195
196 return e.exists;
197 }
198 }
199 return false;
200 }
201
211 [[nodiscard]] auto exists(const std::string& path) const -> bool
212 {
213 for (const entry& e : entries_) {
214 if (e.path == path) {
216 throw std::system_error(e.ec, "error getting result for path \"" + e.path + "\"");
217 }
218
219 return e.exists;
220 }
221 }
222 return false;
223 }
224
237 [[nodiscard]] auto is_deleted() const -> bool
238 {
239 return is_deleted_;
240 }
241
251 [[nodiscard]] auto has_value(std::size_t index) const -> bool
252 {
253 for (const entry& e : entries_) {
254 if (e.original_index == index) {
255 return !e.value.empty();
256 }
257 }
258 throw std::system_error(errc::key_value::path_invalid,
259 "invalid index for mutate_in result: " + std::to_string(index));
260 }
261
271 [[nodiscard]] auto has_value(const std::string& path) const -> bool
272 {
273 for (const entry& e : entries_) {
274 if (e.path == path) {
275 return !e.value.empty();
276 }
277 }
278 throw std::system_error(errc::key_value::path_invalid,
279 "invalid path for mutate_in result: " + path);
280 }
281
282private:
283 std::vector<entry> entries_{};
284 bool is_deleted_{ false };
285};
286
287} // namespace couchbase
CAS is a special type that represented in protocol using unsigned 64-bit integer, but only equality c...
Definition cas.hxx:34
Represents result of lookup_in operations.
Definition lookup_in_result.hxx:36
auto exists(subdoc::lookup_in_macro macro) const -> bool
Allows to check if a value at the given index exists.
Definition lookup_in_result.hxx:187
lookup_in_result(couchbase::cas cas, std::vector< entry > entries, bool is_deleted)
Constructs result for lookup_in_result operation.
Definition lookup_in_result.hxx:66
auto exists(std::size_t index) const -> bool
Allows to check if a value at the given index exists.
Definition lookup_in_result.hxx:164
auto has_value(const std::string &path) const -> bool
Returns whether the field has value.
Definition lookup_in_result.hxx:271
auto content_as(subdoc::lookup_in_macro macro) const -> Document
Decodes field of the document into type.
Definition lookup_in_result.hxx:138
auto content_as(const std::string &path) const -> Document
Decodes field of the document into type.
Definition lookup_in_result.hxx:112
auto has_value(std::size_t index) const -> bool
Returns whether the field has value.
Definition lookup_in_result.hxx:251
auto content_as(std::size_t index) const -> Document
Decodes field of the document into type.
Definition lookup_in_result.hxx:84
auto is_deleted() const -> bool
Returns whether this document was deleted (a tombstone).
Definition lookup_in_result.hxx:237
auto exists(const std::string &path) const -> bool
Allows to check if a value at the given index exists.
Definition lookup_in_result.hxx:211
Base class for operations of data service.
Definition result.hxx:32
std::vector< std::byte > binary
Definition encoded_value.hxx:25
@ path_not_found
The path provided for a sub-document operation was not found.
@ path_invalid
The path provided for a sub-document operation was not syntactically correct.
lookup_in_macro
Definition lookup_in_macro.hxx:29
auto to_string(lookup_in_macro value) -> std::string
Converts macro into binary array suitable for sending to the server.
Represents a single item from the result of collection::scan()
Definition allow_querying_search_index_options.hxx:28
Definition lookup_in_result.hxx:42
std::error_code ec
Definition lookup_in_result.hxx:47
std::string path
Definition lookup_in_result.hxx:43
codec::binary value
Definition lookup_in_result.hxx:44
std::size_t original_index
Definition lookup_in_result.hxx:45
bool exists
Definition lookup_in_result.hxx:46