15#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_VALUE_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_VALUE_H
18#include "google/cloud/spanner/bytes.h"
19#include "google/cloud/spanner/date.h"
20#include "google/cloud/spanner/internal/tuple_utils.h"
21#include "google/cloud/spanner/json.h"
22#include "google/cloud/spanner/numeric.h"
23#include "google/cloud/spanner/timestamp.h"
24#include "google/cloud/spanner/version.h"
25#include "google/cloud/internal/throw_delegate.h"
26#include "google/cloud/optional.h"
27#include "google/cloud/status_or.h"
28#include "absl/time/civil_time.h"
29#include "absl/types/optional.h"
30#include <google/protobuf/struct.pb.h>
31#include <google/protobuf/util/message_differencer.h>
32#include <google/spanner/v1/type.pb.h>
42namespace spanner_internal {
43GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
45GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
49GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
173
174
175
176
186 explicit Value(
bool v) :
Value(PrivateConstructor{}, v) {}
188 explicit Value(std::int64_t v) :
Value(PrivateConstructor{}, v) {}
190 explicit Value(
double v) :
Value(PrivateConstructor{}, v) {}
192 explicit Value(std::string v) :
Value(PrivateConstructor{}, std::move(v)) {}
196 explicit Value(
Json v) :
Value(PrivateConstructor{}, std::move(v)) {}
200 explicit Value(Numeric v) :
Value(PrivateConstructor{}, std::move(v)) {}
202 explicit Value(PgNumeric v) :
Value(PrivateConstructor{}, std::move(v)) {}
207 :
Value(PrivateConstructor{}, std::move(v)) {}
209 explicit Value(absl::CivilDay v)
210 :
Value(PrivateConstructor{}, std::move(v)) {}
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229 explicit Value(
int v) :
Value(PrivateConstructor{}, v) {}
231 explicit Value(
char const* v) :
Value(PrivateConstructor{}, v) {}
234
235
236
237 template <
typename T>
238 explicit Value(absl::optional<T> opt)
239 :
Value(PrivateConstructor{}, std::move(opt)) {}
242
243
244
245
246
247
248
249
250
251
252
253 template <
typename T>
254 explicit Value(std::vector<T> v) :
Value(PrivateConstructor{}, std::move(v)) {
255 static_assert(!IsVector<
typename std::decay<T>::type>::value,
256 "vector of vector not allowed. See value.h documentation.");
260
261
262
263
264
265
266 template <
typename... Ts>
267 explicit Value(std::tuple<Ts...> tup)
268 :
Value(PrivateConstructor{}, std::move(tup)) {}
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302 template <
typename T>
303 StatusOr<T>
get()
const& {
304 if (!TypeProtoIs(T{}, type_))
306 if (value_.kind_case() ==
google::protobuf::Value::kNullValue) {
307 if (IsOptional<T>::value)
return T{};
310 return GetValue(T{}, value_, type_);
314 template <
typename T>
315 StatusOr<T>
get() && {
316 if (!TypeProtoIs(T{}, type_))
318 if (value_.kind_case() ==
google::protobuf::Value::kNullValue) {
319 if (IsOptional<T>::value)
return T{};
323 return GetValue(std::move(tag), std::move(value_), type_);
327
328
329
330
331
332
333
334
335
336
337
338 friend std::ostream& operator<<(std::ostream& os,
Value const& v);
341
342
343
344
345
346 friend void PrintTo(
Value const& v, std::ostream* os) { *os << v; }
350 template <
typename T>
351 struct IsOptional : std::false_type {};
352 template <
typename T>
353 struct IsOptional<absl::optional<T>> : std::true_type {};
356 template <
typename T>
357 struct IsVector : std::false_type {};
358 template <
typename... Ts>
359 struct IsVector<std::vector<Ts...>> : std::true_type {};
363 static bool TypeProtoIs(
bool,
google::spanner::v1::Type
const&);
364 static bool TypeProtoIs(std::int64_t,
google::spanner::v1::Type
const&);
365 static bool TypeProtoIs(
double,
google::spanner::v1::Type
const&);
368 static bool TypeProtoIs(absl::CivilDay,
google::spanner::v1::Type
const&);
369 static bool TypeProtoIs(std::string
const&,
google::spanner::v1::Type
const&);
370 static bool TypeProtoIs(
Bytes const&,
google::spanner::v1::Type
const&);
371 static bool TypeProtoIs(
Json const&,
google::spanner::v1::Type
const&);
372 static bool TypeProtoIs(
JsonB const&,
google::spanner::v1::Type
const&);
373 static bool TypeProtoIs(Numeric
const&,
google::spanner::v1::Type
const&);
374 static bool TypeProtoIs(PgNumeric
const&,
google::spanner::v1::Type
const&);
375 template <
typename T>
376 static bool TypeProtoIs(absl::optional<T>,
377 google::spanner::v1::Type
const& type) {
378 return TypeProtoIs(T{}, type);
380 template <
typename T>
381 static bool TypeProtoIs(std::vector<T>
const&,
382 google::spanner::v1::Type
const& type) {
383 return type.code() ==
google::spanner::v1::TypeCode::ARRAY &&
384 TypeProtoIs(T{}, type.array_element_type());
386 template <
typename... Ts>
387 static bool TypeProtoIs(std::tuple<Ts...>
const& tup,
388 google::spanner::v1::Type
const& type) {
389 bool ok = type.code() ==
google::spanner::v1::TypeCode::STRUCT;
390 ok = ok && type.struct_type().fields().size() ==
sizeof...(Ts);
391 spanner_internal::ForEach(tup, IsStructTypeProto{ok, 0},
398 struct IsStructTypeProto {
401 template <
typename T>
402 void operator()(T
const&,
google::spanner::v1::StructType
const& type) {
403 ok = ok && TypeProtoIs(T{}, type.fields(field).type());
406 template <
typename T>
407 void operator()(std::pair<std::string, T>
const&,
408 google::spanner::v1::StructType
const& type) {
409 operator()(T{}, type);
415 static google::spanner::v1::Type MakeTypeProto(
bool);
416 static google::spanner::v1::Type MakeTypeProto(std::int64_t);
417 static google::spanner::v1::Type MakeTypeProto(
double);
418 static google::spanner::v1::Type MakeTypeProto(std::string
const&);
419 static google::spanner::v1::Type MakeTypeProto(
Bytes const&);
420 static google::spanner::v1::Type MakeTypeProto(
Json const&);
421 static google::spanner::v1::Type MakeTypeProto(
JsonB const&);
422 static google::spanner::v1::Type MakeTypeProto(Numeric
const&);
423 static google::spanner::v1::Type MakeTypeProto(PgNumeric
const&);
426 static google::spanner::v1::Type MakeTypeProto(absl::CivilDay);
427 static google::spanner::v1::Type MakeTypeProto(
int);
428 static google::spanner::v1::Type MakeTypeProto(
char const*);
429 template <
typename T>
430 static google::spanner::v1::Type MakeTypeProto(absl::optional<T>
const&) {
431 return MakeTypeProto(T{});
433 template <
typename T>
434 static google::spanner::v1::Type MakeTypeProto(std::vector<T>
const& v) {
435 google::spanner::v1::Type t;
436 t.set_code(
google::spanner::v1::TypeCode::ARRAY);
437 *t.mutable_array_element_type() = MakeTypeProto(v.empty() ? T{} : v[0]);
441 if (!
google::protobuf::util::MessageDifferencer::Equals(
442 MakeTypeProto(e), t.array_element_type())) {
443 google::
cloud::internal::ThrowInvalidArgument(
"Mismatched types");
448 template <
typename... Ts>
449 static google::spanner::v1::Type MakeTypeProto(std::tuple<Ts...>
const& tup) {
450 google::spanner::v1::Type t;
451 t.set_code(
google::spanner::v1::TypeCode::STRUCT);
452 spanner_internal::ForEach(tup, AddStructTypes{}, *t.mutable_struct_type());
458 struct AddStructTypes {
459 template <
typename T>
460 void operator()(T
const& t,
461 google::spanner::v1::StructType& struct_type)
const {
462 auto* field = struct_type.add_fields();
463 *field->mutable_type() = MakeTypeProto(t);
465 template <
typename S,
typename T,
466 typename std::enable_if<
467 std::is_convertible<S, std::string>::value,
int>::type = 0>
468 void operator()(std::pair<S, T>
const& p,
469 google::spanner::v1::StructType& struct_type)
const {
470 auto* field = struct_type.add_fields();
471 field->set_name(p.first);
472 *field->mutable_type() = MakeTypeProto(p.second);
478 static google::protobuf::Value MakeValueProto(
bool b);
479 static google::protobuf::Value MakeValueProto(std::int64_t i);
480 static google::protobuf::Value MakeValueProto(
double d);
481 static google::protobuf::Value MakeValueProto(std::string s);
482 static google::protobuf::Value MakeValueProto(
Bytes b);
483 static google::protobuf::Value MakeValueProto(
Json j);
484 static google::protobuf::Value MakeValueProto(
JsonB j);
485 static google::protobuf::Value MakeValueProto(Numeric n);
486 static google::protobuf::Value MakeValueProto(PgNumeric n);
489 static google::protobuf::Value MakeValueProto(absl::CivilDay d);
490 static google::protobuf::Value MakeValueProto(
int i);
491 static google::protobuf::Value MakeValueProto(
char const* s);
492 template <
typename T>
493 static google::protobuf::Value MakeValueProto(absl::optional<T> opt) {
494 if (opt.has_value())
return MakeValueProto(*std::move(opt));
495 google::protobuf::Value v;
496 v.set_null_value(
google::protobuf::NullValue::NULL_VALUE);
499 template <
typename T>
500 static google::protobuf::Value MakeValueProto(std::vector<T> vec) {
501 google::protobuf::Value v;
502 auto& list = *v.mutable_list_value();
503 for (
auto&& e : vec) {
504 *list.add_values() = MakeValueProto(std::move(e));
508 template <
typename... Ts>
509 static google::protobuf::Value MakeValueProto(std::tuple<Ts...> tup) {
510 google::protobuf::Value v;
511 spanner_internal::ForEach(tup, AddStructValues{}, *v.mutable_list_value());
517 struct AddStructValues {
518 template <
typename T>
519 void operator()(T& t,
google::protobuf::ListValue& list_value)
const {
520 *list_value.add_values() = MakeValueProto(std::move(t));
522 template <
typename S,
typename T,
523 typename std::enable_if<
524 std::is_convertible<S, std::string>::value,
int>::type = 0>
525 void operator()(std::pair<S, T> p,
526 google::protobuf::ListValue& list_value)
const {
527 *list_value.add_values() = MakeValueProto(std::move(p.second));
533 static StatusOr<
bool> GetValue(
bool,
google::protobuf::Value
const&,
534 google::spanner::v1::Type
const&);
535 static StatusOr<std::int64_t> GetValue(std::int64_t,
536 google::protobuf::Value
const&,
537 google::spanner::v1::Type
const&);
538 static StatusOr<
double> GetValue(
double,
google::protobuf::Value
const&,
539 google::spanner::v1::Type
const&);
540 static StatusOr<std::string> GetValue(std::string
const&,
541 google::protobuf::Value
const&,
542 google::spanner::v1::Type
const&);
543 static StatusOr<std::string> GetValue(std::string
const&,
544 google::protobuf::Value&&,
545 google::spanner::v1::Type
const&);
546 static StatusOr<
Bytes> GetValue(
Bytes const&,
google::protobuf::Value
const&,
547 google::spanner::v1::Type
const&);
548 static StatusOr<
Json> GetValue(
Json const&,
google::protobuf::Value
const&,
549 google::spanner::v1::Type
const&);
550 static StatusOr<
JsonB> GetValue(
JsonB const&,
google::protobuf::Value
const&,
551 google::spanner::v1::Type
const&);
552 static StatusOr<Numeric> GetValue(Numeric
const&,
553 google::protobuf::Value
const&,
554 google::spanner::v1::Type
const&);
555 static StatusOr<PgNumeric> GetValue(PgNumeric
const&,
556 google::protobuf::Value
const&,
557 google::spanner::v1::Type
const&);
559 google::spanner::v1::Type
const&);
561 google::protobuf::Value
const&,
562 google::spanner::v1::Type
const&);
563 static StatusOr<absl::CivilDay> GetValue(absl::CivilDay,
564 google::protobuf::Value
const&,
565 google::spanner::v1::Type
const&);
566 template <
typename T,
typename V>
567 static StatusOr<absl::optional<T>> GetValue(
568 absl::optional<T>
const&, V&& pv,
google::spanner::v1::Type
const& pt) {
569 if (pv.kind_case() ==
google::protobuf::Value::kNullValue) {
570 return absl::optional<T>{};
572 auto value = GetValue(T{}, std::forward<V>(pv), pt);
573 if (!value)
return std::move(value).status();
574 return absl::optional<T>{*std::move(value)};
576 template <
typename T,
typename V>
577 static StatusOr<std::vector<T>> GetValue(
578 std::vector<T>
const&, V&& pv,
google::spanner::v1::Type
const& pt) {
579 if (pv.kind_case() !=
google::protobuf::Value::kListValue) {
583 for (
int i = 0; i < pv.list_value().values().size(); ++i) {
584 auto&& e = GetProtoListValueElement(std::forward<V>(pv), i);
585 using ET =
decltype(e);
586 auto value = GetValue(T{}, std::forward<ET>(e), pt.array_element_type());
587 if (!value)
return std::move(value).status();
588 v.push_back(*std::move(value));
592 template <
typename V,
typename... Ts>
593 static StatusOr<std::tuple<Ts...>> GetValue(
594 std::tuple<Ts...>
const&, V&& pv,
google::spanner::v1::Type
const& pt) {
595 if (pv.kind_case() !=
google::protobuf::Value::kListValue) {
598 std::tuple<Ts...> tup;
600 ExtractTupleValues<V> f{status, 0, std::forward<V>(pv), pt};
601 spanner_internal::ForEach(tup, f);
602 if (!status
.ok())
return status;
608 template <
typename V>
609 struct ExtractTupleValues {
613 google::spanner::v1::Type
const& type;
614 template <
typename T>
615 void operator()(T& t) {
616 auto&& e = GetProtoListValueElement(std::forward<V>(pv), i);
617 using ET =
decltype(e);
618 auto value = GetValue(T{}, std::forward<ET>(e), type);
621 status = std::move(value).status();
623 t = *std::move(value);
626 template <
typename T>
627 void operator()(std::pair<std::string, T>& p) {
628 p.first = type.struct_type().fields(i).name();
629 auto&& e = GetProtoListValueElement(std::forward<V>(pv), i);
630 using ET =
decltype(e);
631 auto value = GetValue(T{}, std::forward<ET>(e), type);
634 status = std::move(value).status();
636 p.second = *std::move(value);
645 static google::protobuf::Value
const& GetProtoListValueElement(
646 google::protobuf::Value
const& pv,
int pos) {
647 return pv.list_value().values(pos);
649 static google::protobuf::Value&& GetProtoListValueElement(
650 google::protobuf::Value&& pv,
int pos) {
651 return std::move(*pv.mutable_list_value()->mutable_values(pos));
660 struct PrivateConstructor {};
661 template <
typename T>
662 Value(PrivateConstructor, T&& t)
663 : type_(MakeTypeProto(t)), value_(MakeValueProto(std::forward<T>(t))) {}
665 Value(
google::spanner::v1::Type t,
google::protobuf::Value v)
666 : type_(std::move(t)), value_(std::move(v)) {}
668 friend struct spanner_internal::ValueInternals;
670 google::spanner::v1::Type type_;
671 google::protobuf::Value value_;
675
676
677
678
679
680
683 return Value(absl::optional<T>{});
686GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
689namespace spanner_internal {
690GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
692struct ValueInternals {
694 google::protobuf::Value v) {
698 static std::pair<
google::spanner::v1::Type,
google::protobuf::Value> ToProto(
700 return std::make_pair(std::move(v.type_), std::move(v.value_));
705 google::protobuf::Value v) {
706 return ValueInternals::FromProto(std::move(t), std::move(v));
709inline std::pair<
google::spanner::v1::Type,
google::protobuf::Value> ToProto(
711 return ValueInternals::ToProto(std::move(v));
714GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
Status(StatusCode code, std::string message, ErrorInfo info={})
A representation of the Spanner BYTES type: variable-length binary data.
Definition: bytes.h:43
JsonB is a variant of Json (see above).
Definition: json.h:101
A simple representation for the Spanner JSON type: a lightweight, text-based, language-independent da...
Definition: json.h:39
A representation of the Spanner TIMESTAMP type: An instant in time.
Definition: timestamp.h:54
The Value class represents a type-safe, nullable Spanner value.
Definition: value.h:170
friend void PrintTo(Value const &v, std::ostream *os)
Prints the same output as operator<<.
Definition: value.h:346
Value & operator=(Value const &)=default
Value(Numeric v)
Constructs an instance with the specified type and value.
Definition: value.h:200
Value(Value const &)=default
Value(absl::CivilDay v)
Constructs an instance with the specified type and value.
Definition: value.h:209
Value(CommitTimestamp v)
Constructs an instance with the specified type and value.
Definition: value.h:206
Value(bool v)
Constructs an instance with the specified type and value.
Definition: value.h:186
Value & operator=(Value &&)=default
Value(PgNumeric v)
Constructs an instance with the specified type and value.
Definition: value.h:202
Value(JsonB v)
Constructs an instance with the specified type and value.
Definition: value.h:198
friend bool operator!=(Value const &a, Value const &b)
Definition: value.h:271
Value(std::vector< T > v)
Constructs an instance from a Spanner ARRAY of the specified type and values.
Definition: value.h:254
Value(Timestamp v)
Constructs an instance with the specified type and value.
Definition: value.h:204
Value(Bytes v)
Constructs an instance with the specified type and value.
Definition: value.h:194
Value(double v)
Constructs an instance with the specified type and value.
Definition: value.h:190
StatusOr< T > get() const &
Returns the contained value wrapped in a google::cloud::StatusOr<T>.
Definition: value.h:303
Value(char const *v)
Constructs an instance from common C++ literal types that closely, though not exactly,...
Definition: value.h:231
Value(std::tuple< Ts... > tup)
Constructs an instance from a Spanner STRUCT with a type and values matching the given std::tuple.
Definition: value.h:267
Value(std::string v)
Constructs an instance with the specified type and value.
Definition: value.h:192
StatusOr< T > get() &&
Returns the contained value wrapped in a google::cloud::StatusOr<T>.
Definition: value.h:315
Value(int v)
Constructs an instance from common C++ literal types that closely, though not exactly,...
Definition: value.h:229
Value(std::int64_t v)
Constructs an instance with the specified type and value.
Definition: value.h:188
friend bool operator==(Value const &a, Value const &b)
Value(Json v)
Constructs an instance with the specified type and value.
Definition: value.h:196
Value(absl::optional< T > opt)
Constructs a non-null instance if opt has a value, otherwise constructs a null instance with the spec...
Definition: value.h:238
Value()=default
Constructs a Value that holds nothing.
Contains all the Cloud Spanner C++ client types and functions.
Definition: backoff_policy.h:23
Value MakeNullValue()
Factory to construct a "null" Value of the specified type T.
Definition: value.h:682
A sentinel type used to update a commit timestamp column.
Definition: timestamp.h:195