Skip to content

Commit 77ebc1e

Browse files
feat(bigtable): add minimal Value class (#15462)
* feat(bigtable): add minimal `Value` class * fix: remove warnings * fix: remove unused param warning * chore: fix todos * fix: remove unused struct.pb.h * chore: remove deprecated PrintTo * fix: use bitables own value class
1 parent 7ec4475 commit 77ebc1e

File tree

6 files changed

+535
-0
lines changed

6 files changed

+535
-0
lines changed

google/cloud/bigtable/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ add_library(
251251
table_config.h
252252
table_resource.cc
253253
table_resource.h
254+
value.cc
255+
value.h
254256
version.cc
255257
version.h
256258
version_info.h
@@ -488,6 +490,7 @@ if (BUILD_TESTING)
488490
table_test.cc
489491
testing/cleanup_stale_resources_test.cc
490492
testing/random_names_test.cc
493+
value_test.cc
491494
wait_for_consistency_test.cc)
492495

493496
# Export the list of unit tests so the Bazel BUILD file can pick it up.

google/cloud/bigtable/bigtable_client_unit_tests.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,6 @@ bigtable_client_unit_tests = [
9393
"table_test.cc",
9494
"testing/cleanup_stale_resources_test.cc",
9595
"testing/random_names_test.cc",
96+
"value_test.cc",
9697
"wait_for_consistency_test.cc",
9798
]

google/cloud/bigtable/google_cloud_cpp_bigtable.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ google_cloud_cpp_bigtable_hdrs = [
130130
"table_admin.h",
131131
"table_config.h",
132132
"table_resource.h",
133+
"value.h",
133134
"version.h",
134135
"version_info.h",
135136
"wait_for_consistency.h",
@@ -222,6 +223,7 @@ google_cloud_cpp_bigtable_srcs = [
222223
"table_admin.cc",
223224
"table_config.cc",
224225
"table_resource.cc",
226+
"value.cc",
225227
"version.cc",
226228
"wait_for_consistency.cc",
227229
]

google/cloud/bigtable/value.cc

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright 2025 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+
#include "google/cloud/bigtable/value.h"
16+
#include <google/bigtable/v2/types.pb.h>
17+
#include <google/protobuf/descriptor.h>
18+
#include <google/protobuf/message.h>
19+
20+
namespace google {
21+
namespace cloud {
22+
namespace bigtable {
23+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
24+
namespace {
25+
// Compares two sets of Type and Value protos for equality. This method calls
26+
// itself recursively to compare subtypes and subvalues.
27+
bool Equal(google::bigtable::v2::Type const& pt1, // NOLINT(misc-no-recursion)
28+
google::bigtable::v2::Value const& pv1,
29+
google::bigtable::v2::Type const& pt2,
30+
google::bigtable::v2::Value const& pv2) {
31+
if (pt1.kind_case() != pt2.kind_case()) return false;
32+
if (pv1.kind_case() != pv2.kind_case()) return false;
33+
if (pt1.has_bool_type()) {
34+
return pv1.bool_value() == pv2.bool_value();
35+
}
36+
return false;
37+
}
38+
39+
// From the proto description, `NULL` values are represented by having a kind
40+
// equal to KIND_NOT_SET
41+
bool IsNullValue(google::bigtable::v2::Value const& value) {
42+
return value.kind_case() == google::bigtable::v2::Value::KIND_NOT_SET;
43+
}
44+
45+
// An enum to tell StreamHelper() whether a value is being printed as a scalar
46+
// or as part of an aggregate type (i.e., a vector or tuple). Some types may
47+
// format themselves differently in each case.
48+
enum class StreamMode { kScalar, kAggregate };
49+
50+
std::ostream& StreamHelper(std::ostream& os, // NOLINT(misc-no-recursion)
51+
google::bigtable::v2::Value const& v,
52+
google::bigtable::v2::Type const&, StreamMode) {
53+
if (IsNullValue(v)) {
54+
return os << "NULL";
55+
}
56+
57+
if (v.kind_case() == google::bigtable::v2::Value::kBoolValue) {
58+
return os << v.bool_value();
59+
}
60+
// this should include type name
61+
return os << "Error: unknown value type code ";
62+
}
63+
} // namespace
64+
65+
bool operator==(Value const& a, Value const& b) {
66+
return Equal(a.type_, a.value_, b.type_, b.value_);
67+
}
68+
69+
std::ostream& operator<<(std::ostream& os, Value const& v) {
70+
return StreamHelper(os, v.value_, v.type_, StreamMode::kScalar);
71+
}
72+
73+
//
74+
// Value::TypeProtoIs
75+
//
76+
77+
bool Value::TypeProtoIs(bool, google::bigtable::v2::Type const& type) {
78+
return type.has_bool_type();
79+
}
80+
81+
//
82+
// Value::MakeTypeProto
83+
//
84+
85+
google::bigtable::v2::Type Value::MakeTypeProto(bool) {
86+
google::bigtable::v2::Type t;
87+
t.set_allocated_bool_type(std::move(new google::bigtable::v2::Type_Bool()));
88+
return t;
89+
}
90+
91+
//
92+
// Value::MakeValueProto
93+
//
94+
95+
google::bigtable::v2::Value Value::MakeValueProto(bool b) {
96+
google::bigtable::v2::Value v;
97+
v.set_bool_value(b);
98+
return v;
99+
}
100+
101+
//
102+
// Value::GetValue
103+
//
104+
105+
StatusOr<bool> Value::GetValue(bool, google::bigtable::v2::Value const& pv,
106+
google::bigtable::v2::Type const&) {
107+
if (pv.kind_case() != google::bigtable::v2::Value::kBoolValue) {
108+
return internal::UnknownError("missing BOOL", GCP_ERROR_INFO());
109+
}
110+
return pv.bool_value();
111+
}
112+
113+
bool Value::is_null() const { return IsNullValue(value_); }
114+
115+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
116+
} // namespace bigtable
117+
} // namespace cloud
118+
} // namespace google

0 commit comments

Comments
 (0)