1313// limitations under the License.
1414
1515#include " google/cloud/bigtable/value.h"
16+ #include " google/cloud/internal/throw_delegate.h"
1617#include < google/bigtable/v2/types.pb.h>
1718#include < google/protobuf/descriptor.h>
1819#include < google/protobuf/message.h>
@@ -33,6 +34,15 @@ bool Equal(google::bigtable::v2::Type const& pt1, // NOLINT(misc-no-recursion)
3334 if (pt1.has_bool_type ()) {
3435 return pv1.bool_value () == pv2.bool_value ();
3536 }
37+ if (pt1.has_int64_type ()) {
38+ return pv1.int_value () == pv2.int_value ();
39+ }
40+ if (pt1.has_float32_type () || pt1.has_float64_type ()) {
41+ return pv1.float_value () == pv2.float_value ();
42+ }
43+ if (pt1.has_string_type ()) {
44+ return pv1.string_value () == pv2.string_value ();
45+ }
3646 return false ;
3747}
3848
@@ -57,6 +67,15 @@ std::ostream& StreamHelper(std::ostream& os, // NOLINT(misc-no-recursion)
5767 if (v.kind_case () == google::bigtable::v2::Value::kBoolValue ) {
5868 return os << v.bool_value ();
5969 }
70+ if (v.kind_case () == google::bigtable::v2::Value::kIntValue ) {
71+ return os << v.int_value ();
72+ }
73+ if (v.kind_case () == google::bigtable::v2::Value::kFloatValue ) {
74+ return os << v.float_value ();
75+ }
76+ if (v.kind_case () == google::bigtable::v2::Value::kStringValue ) {
77+ return os << v.string_value ();
78+ }
6079 // this should include type name
6180 return os << " Error: unknown value type code " ;
6281}
@@ -77,6 +96,19 @@ std::ostream& operator<<(std::ostream& os, Value const& v) {
7796bool Value::TypeProtoIs (bool , google::bigtable::v2::Type const & type) {
7897 return type.has_bool_type ();
7998}
99+ bool Value::TypeProtoIs (std::int64_t , google::bigtable::v2::Type const & type) {
100+ return type.has_int64_type ();
101+ }
102+ bool Value::TypeProtoIs (float , google::bigtable::v2::Type const & type) {
103+ return type.has_float32_type ();
104+ }
105+ bool Value::TypeProtoIs (double , google::bigtable::v2::Type const & type) {
106+ return type.has_float64_type ();
107+ }
108+ bool Value::TypeProtoIs (std::string const &,
109+ google::bigtable::v2::Type const & type) {
110+ return type.has_string_type ();
111+ }
80112
81113//
82114// Value::MakeTypeProto
@@ -87,6 +119,35 @@ google::bigtable::v2::Type Value::MakeTypeProto(bool) {
87119 t.set_allocated_bool_type (std::move (new google::bigtable::v2::Type_Bool ()));
88120 return t;
89121}
122+ google::bigtable::v2::Type Value::MakeTypeProto (int const i) {
123+ return Value::MakeTypeProto (static_cast <std::int64_t >(i));
124+ }
125+ google::bigtable::v2::Type Value::MakeTypeProto (std::int64_t ) {
126+ google::bigtable::v2::Type t;
127+ t.set_allocated_int64_type (std::move (new google::bigtable::v2::Type_Int64 ()));
128+ return t;
129+ }
130+ google::bigtable::v2::Type Value::MakeTypeProto (float ) {
131+ google::bigtable::v2::Type t;
132+ t.set_allocated_float32_type (
133+ std::move (new google::bigtable::v2::Type_Float32 ()));
134+ return t;
135+ }
136+ google::bigtable::v2::Type Value::MakeTypeProto (double ) {
137+ google::bigtable::v2::Type t;
138+ t.set_allocated_float64_type (
139+ std::move (new google::bigtable::v2::Type_Float64 ()));
140+ return t;
141+ }
142+ google::bigtable::v2::Type Value::MakeTypeProto (std::string const &) {
143+ google::bigtable::v2::Type t;
144+ t.set_allocated_string_type (
145+ std::move (new google::bigtable::v2::Type_String ()));
146+ return t;
147+ }
148+ google::bigtable::v2::Type Value::MakeTypeProto (char const * s) {
149+ return Value::MakeTypeProto (std::string (std::move (s)));
150+ }
90151
91152//
92153// Value::MakeValueProto
@@ -97,6 +158,44 @@ google::bigtable::v2::Value Value::MakeValueProto(bool b) {
97158 v.set_bool_value (b);
98159 return v;
99160}
161+ google::bigtable::v2::Value Value::MakeValueProto (std::int64_t i) {
162+ google::bigtable::v2::Value v;
163+ v.set_int_value (i);
164+ return v;
165+ }
166+ google::bigtable::v2::Value Value::MakeValueProto (int i) {
167+ return Value::MakeValueProto (static_cast <std::int64_t >(i));
168+ }
169+ google::bigtable::v2::Value Value::MakeValueProto (float f) {
170+ // NaN and Infinity are not supported. See
171+ // https://github.com/googleapis/googleapis/blob/5caeec4d72173ea3f2772b1b67a5c3f9192a6d06/google/bigtable/v2/data.proto#L140-L142
172+ if (std::isnan (f) || std::isinf (f)) {
173+ internal::ThrowInvalidArgument (
174+ bigtable_internal::kInvalidFloatValueMessage );
175+ }
176+ google::bigtable::v2::Value v;
177+ v.set_float_value (f);
178+ return v;
179+ }
180+ google::bigtable::v2::Value Value::MakeValueProto (double d) {
181+ // NaN and Infinity are not supported. See
182+ // https://github.com/googleapis/googleapis/blob/5caeec4d72173ea3f2772b1b67a5c3f9192a6d06/google/bigtable/v2/data.proto#L140-L142
183+ if (std::isnan (d) || std::isinf (d)) {
184+ internal::ThrowInvalidArgument (
185+ bigtable_internal::kInvalidFloatValueMessage );
186+ }
187+ google::bigtable::v2::Value v;
188+ v.set_float_value (d);
189+ return v;
190+ }
191+ google::bigtable::v2::Value Value::MakeValueProto (std::string s) {
192+ google::bigtable::v2::Value v;
193+ v.set_string_value (std::move (s));
194+ return v;
195+ }
196+ google::bigtable::v2::Value Value::MakeValueProto (char const * s) {
197+ return Value::MakeValueProto (std::string (s));
198+ }
100199
101200//
102201// Value::GetValue
@@ -109,6 +208,52 @@ StatusOr<bool> Value::GetValue(bool, google::bigtable::v2::Value const& pv,
109208 }
110209 return pv.bool_value ();
111210}
211+ StatusOr<std::int64_t > Value::GetValue (std::int64_t ,
212+ google::bigtable::v2::Value const & pv,
213+ google::bigtable::v2::Type const &) {
214+ if (pv.kind_case () != google::bigtable::v2::Value::kIntValue ) {
215+ return internal::UnknownError (" missing INT64" , GCP_ERROR_INFO ());
216+ }
217+ return pv.int_value ();
218+ }
219+ StatusOr<float > Value::GetValue (float , google::bigtable::v2::Value const & pv,
220+ google::bigtable::v2::Type const &) {
221+ if (pv.kind_case () != google::bigtable::v2::Value::kFloatValue ) {
222+ return internal::UnknownError (" missing FLOAT32" , GCP_ERROR_INFO ());
223+ }
224+ if (std::isnan (pv.float_value ()) || std::isinf (pv.float_value ())) {
225+ return internal::UnimplementedError (
226+ bigtable_internal::kInvalidFloatValueMessage );
227+ }
228+ return static_cast <float >(pv.float_value ());
229+ }
230+ StatusOr<double > Value::GetValue (double , google::bigtable::v2::Value const & pv,
231+ google::bigtable::v2::Type const &) {
232+ if (pv.kind_case () != google::bigtable::v2::Value::kFloatValue ) {
233+ return internal::UnknownError (" missing FLOAT64" , GCP_ERROR_INFO ());
234+ }
235+ if (std::isnan (pv.float_value ()) || std::isinf (pv.float_value ())) {
236+ return internal::UnimplementedError (
237+ bigtable_internal::kInvalidFloatValueMessage );
238+ }
239+ return pv.float_value ();
240+ }
241+ StatusOr<std::string> Value::GetValue (std::string const &,
242+ google::bigtable::v2::Value const & pv,
243+ google::bigtable::v2::Type const &) {
244+ if (pv.kind_case () != google::bigtable::v2::Value::kStringValue ) {
245+ return internal::UnknownError (" missing STRING" , GCP_ERROR_INFO ());
246+ }
247+ return pv.string_value ();
248+ }
249+ StatusOr<std::string> Value::GetValue (std::string const &,
250+ google::bigtable::v2::Value&& pv,
251+ google::bigtable::v2::Type const &) {
252+ if (pv.kind_case () != google::bigtable::v2::Value::kStringValue ) {
253+ return internal::UnknownError (" missing STRING" , GCP_ERROR_INFO ());
254+ }
255+ return std::move (*pv.mutable_string_value ());
256+ }
112257
113258bool Value::is_null () const { return IsNullValue (value_); }
114259
0 commit comments