The Value class represents a type-safe, nullable Bigtable value.
For now, this is a minimal class to unblock further development of the Bigtable GoogleSQL feature. It is conceptually similar to a std::any
except the only allowed types will be those supported by Bigtable, and a "null" value (similar to a std::any
without a value) still has an associated type. The supported types are shown in the following table along with how they map to the Bigtable types (https://cloud.google.com/bigtable/docs/data-types):
Bigtable Type | C++ Type T |
---|---|
BOOL | bool |
INT64 | std::int64_t |
FLOAT32 | float |
FLOAT64 | double |
STRING | std::string |
BYTES | google::cloud::bigtable::Bytes |
TIMESTAMP | google::cloud::bigtable::Timestamp |
DATE | absl::CivilDay |
ARRAY | std::vector<T> // [1] |
STRUCT | std::tuple<Ts...> |
[1] The type T
may be any of the other supported types, except for ARRAY/std::vector
.
Callers may create instances by passing any of the supported values (shown in the table above) to the constructor. "Null" values are created using the MakeNullValue<T>()
factory function or by passing an empty absl::optional<T>
to the Value constructor.
Bigtable Arrays
Bigtable arrays are represented in C++ as a std::vector<T>
, where the type T
may be any of the other allowed Bigtable types, such as bool
, std::int64_t
, etc. The only exception is that arrays may not directly contain another array; to achieve a similar result you could create an array of a 1-element struct holding an array. The following examples show usage of arrays.
std::vector<std::int64_t> vec = {1, 2, 3, 4, 5};
bigtable::Value v(vec);
auto copy = *v.get<std::vector<std::int64_t>>();
assert(vec == copy);
Bigtable Structs
Bigtable structs are represented in C++ as instances of std::tuple
holding zero or more of the allowed Bigtable types, such as bool
, std::int64_t
, std::vector
, and even other std::tuple
objects. Each tuple element corresponds to a single field in a Bigtable STRUCT.
Bigtable STRUCT fields may optionally contain a string indicating the field's name. Fields names may be empty, unique, or repeated. A named field may be specified as a tuple element of type std::pair<std::string, T>
, where the pair's .first
member indicates the field's name, and the .second
member is any valid Bigtable type T
.
using Struct = std::tuple<bool, std::pair<std::string, std::int64_t>>;
Struct s = {true, {"Foo", 42}};
bigtable::Value v(s);
assert(s == *v.get<Struct>());
Constructors
Value()
Constructs a Value
that holds nothing.
Value(bool)
Constructs an instance with the specified type and value.
Parameter | |
---|---|
Name | Description |
v |
bool
|
Value(std::int64_t)
Constructs an instance with the specified type and value.
Parameter | |
---|---|
Name | Description |
v |
std::int64_t
|
Value(float)
Constructs an instance with the specified type and value.
Parameter | |
---|---|
Name | Description |
v |
float
|
Value(double)
Constructs an instance with the specified type and value.
Parameter | |
---|---|
Name | Description |
v |
double
|
Value(std::string)
Constructs an instance with the specified type and value.
Parameter | |
---|---|
Name | Description |
v |
std::string
|
Value(Bytes const &)
Constructs an instance with the specified type and value.
Parameter | |
---|---|
Name | Description |
v |
Bytes const &
|
Value(Timestamp const &)
Constructs an instance with the specified type and value.
Parameter | |
---|---|
Name | Description |
v |
Timestamp const &
|
Value(absl::CivilDay const &)
Constructs an instance with the specified type and value.
Parameter | |
---|---|
Name | Description |
v |
absl::CivilDay const &
|
Value(int)
Constructs an instance from common C++ literal types that closely, though not exactly, match supported Bigtable types.
An integer literal in C++ is of type int
, which is not exactly an allowed Bigtable type. This will be allowed but it will be implicitly up converted to a std::int64_t
. Similarly, a C++ string literal will be implicitly converted to a std::string
. For example:
bigtable::Value v1(42);
assert(42 == *v1.get<std::int64_t>());
bigtable::Value v2("hello");
assert("hello" == *v2.get<std::string>());
Parameter | |
---|---|
Name | Description |
v |
int
|
Value(char const *)
Constructs an instance from common C++ literal types that closely, though not exactly, match supported Bigtable types.
An integer literal in C++ is of type int
, which is not exactly an allowed Bigtable type. This will be allowed but it will be implicitly up converted to a std::int64_t
. Similarly, a C++ string literal will be implicitly converted to a std::string
. For example:
bigtable::Value v1(42);
assert(42 == *v1.get<std::int64_t>());
bigtable::Value v2("hello");
assert("hello" == *v2.get<std::string>());
Parameter | |
---|---|
Name | Description |
v |
char const *
|
Value(absl::optional< T >)
Constructs a non-null instance if opt
has a value, otherwise constructs a null instance with the specified type T
.
Parameters | |
---|---|
Name | Description |
opt |
absl::optional< T >
|
typename T |
|
Value(std::vector< T >)
Constructs an instance from a Bigtable ARRAY of the specified type and values.
The type T
may be any valid type shown above, except vectors of vectors are not allowed.
Parameters | |
---|---|
Name | Description |
v |
std::vector< T >
|
typename T |
|
Value(std::tuple< Ts... >)
Constructs an instance from a Bigtable STRUCT with a type and values matching the given std::tuple
.
Any STRUCT field may optionally have a name, which is specified as std::pair<std::string, T>
.
Parameters | |
---|---|
Name | Description |
tup |
std::tuple< Ts... >
|
typename... |
|
Value(Value const &)
Parameter | |
---|---|
Name | Description |
|
Value const &
|
Value(Value &&)
Parameter | |
---|---|
Name | Description |
|
Value &&
|
Operators
operator=(Value const &)
Parameter | |
---|---|
Name | Description |
|
Value const &
|
Returns | |
---|---|
Type | Description |
Value & |
operator=(Value &&)
Parameter | |
---|---|
Name | Description |
|
Value &&
|
Returns | |
---|---|
Type | Description |
Value & |
Functions
get() const &
Parameter | |
---|---|
Name | Description |
typename T |
|
Returns | |
---|---|
Type | Description |
StatusOr< T > |
get() &&
Parameter | |
---|---|
Name | Description |
typename T |
|
Returns | |
---|---|
Type | Description |
StatusOr< T > |
is_null() const
Returns | |
---|---|
Type | Description |
bool |