|
| 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