Skip to content

Commit

Permalink
Refactor value serialization to be handle agnostic
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 589898508
  • Loading branch information
jcking authored and copybara-github committed Dec 11, 2023
1 parent 197a710 commit 002fef4
Show file tree
Hide file tree
Showing 18 changed files with 380 additions and 161 deletions.
1 change: 1 addition & 0 deletions base/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ cc_library(
"//internal:number",
"//internal:overloaded",
"//internal:proto_wire",
"//internal:serialize",
"//internal:status_macros",
"//internal:strings",
"//internal:time",
Expand Down
15 changes: 3 additions & 12 deletions base/values/bool_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@
#include "base/value_factory.h"
#include "common/any.h"
#include "common/json.h"
#include "internal/proto_wire.h"
#include "internal/serialize.h"
#include "internal/status_macros.h"

namespace cel {

namespace {

using internal::ProtoWireEncoder;
using internal::ProtoWireTag;
using internal::ProtoWireType;
using internal::SerializeBoolValue;

} // namespace

Expand All @@ -52,15 +50,8 @@ std::string BoolValue::DebugString() const {

absl::StatusOr<Any> BoolValue::ConvertToAny(ValueFactory&) const {
static constexpr absl::string_view kTypeName = "google.protobuf.BoolValue";
const auto value = this->NativeValue();
absl::Cord data;
if (value) {
ProtoWireEncoder encoder(kTypeName, data);
CEL_RETURN_IF_ERROR(
encoder.WriteTag(ProtoWireTag(1, ProtoWireType::kVarint)));
CEL_RETURN_IF_ERROR(encoder.WriteVarint(value));
encoder.EnsureFullyEncoded();
}
CEL_RETURN_IF_ERROR(SerializeBoolValue(this->NativeValue(), data));
return MakeAny(MakeTypeUrl(kTypeName), std::move(data));
}

Expand Down
15 changes: 3 additions & 12 deletions base/values/bytes_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include "common/any.h"
#include "common/json.h"
#include "internal/overloaded.h"
#include "internal/proto_wire.h"
#include "internal/serialize.h"
#include "internal/status_macros.h"
#include "internal/strings.h"
#include "internal/utf8.h"
Expand All @@ -50,9 +50,7 @@ CEL_INTERNAL_VALUE_IMPL(BytesValue);

namespace {

using internal::ProtoWireEncoder;
using internal::ProtoWireTag;
using internal::ProtoWireType;
using internal::SerializeBytesValue;

struct BytesValueDebugStringVisitor final {
std::string operator()(absl::string_view value) const {
Expand Down Expand Up @@ -296,15 +294,8 @@ std::string BytesValue::DebugString() const {

absl::StatusOr<Any> BytesValue::ConvertToAny(ValueFactory&) const {
static constexpr absl::string_view kTypeName = "google.protobuf.BytesValue";
const auto value = ToCord();
absl::Cord data;
if (!value.empty()) {
ProtoWireEncoder encoder(kTypeName, data);
CEL_RETURN_IF_ERROR(
encoder.WriteTag(ProtoWireTag(1, ProtoWireType::kLengthDelimited)));
CEL_RETURN_IF_ERROR(encoder.WriteLengthDelimited(value));
encoder.EnsureFullyEncoded();
}
CEL_RETURN_IF_ERROR(SerializeBytesValue(ToCord(), data));
return MakeAny(MakeTypeUrl(kTypeName), std::move(data));
}

Expand Down
17 changes: 3 additions & 14 deletions base/values/double_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
#include "base/values/double_value.h"

#include <cmath>
#include <cstdint>
#include <string>
#include <utility>

#include "absl/base/casts.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/cord.h"
Expand All @@ -36,7 +34,7 @@
#include "common/any.h"
#include "common/json.h"
#include "internal/number.h"
#include "internal/proto_wire.h"
#include "internal/serialize.h"
#include "internal/status_macros.h"

namespace cel {
Expand All @@ -45,9 +43,7 @@ CEL_INTERNAL_VALUE_IMPL(DoubleValue);

namespace {

using internal::ProtoWireEncoder;
using internal::ProtoWireTag;
using internal::ProtoWireType;
using internal::SerializeDoubleValue;

std::string DoubleToString(double value) {
if (std::isfinite(value)) {
Expand Down Expand Up @@ -88,15 +84,8 @@ std::string DoubleValue::DebugString() const {

absl::StatusOr<Any> DoubleValue::ConvertToAny(ValueFactory&) const {
static constexpr absl::string_view kTypeName = "google.protobuf.DoubleValue";
const auto value = this->NativeValue();
absl::Cord data;
if (absl::bit_cast<uint64_t>(value) != 0) {
ProtoWireEncoder encoder(kTypeName, data);
CEL_RETURN_IF_ERROR(
encoder.WriteTag(ProtoWireTag(1, ProtoWireType::kFixed64)));
CEL_RETURN_IF_ERROR(encoder.WriteFixed64(value));
encoder.EnsureFullyEncoded();
}
CEL_RETURN_IF_ERROR(SerializeDoubleValue(this->NativeValue(), data));
return MakeAny(MakeTypeUrl(kTypeName), std::move(data));
}

Expand Down
21 changes: 3 additions & 18 deletions base/values/duration_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#include "base/values/duration_value.h"

#include <cstdint>
#include <string>
#include <utility>

Expand All @@ -31,7 +30,7 @@
#include "base/value_factory.h"
#include "common/any.h"
#include "common/json.h"
#include "internal/proto_wire.h"
#include "internal/serialize.h"
#include "internal/status_macros.h"
#include "internal/time.h"

Expand All @@ -41,9 +40,7 @@ CEL_INTERNAL_VALUE_IMPL(DurationValue);

namespace {

using internal::ProtoWireEncoder;
using internal::ProtoWireTag;
using internal::ProtoWireType;
using internal::SerializeDuration;

} // namespace

Expand All @@ -64,19 +61,7 @@ absl::StatusOr<Any> DurationValue::ConvertToAny(ValueFactory&) const {
"infinite duration values cannot be converted to google.protobuf.Any");
}
absl::Cord data;
if (value != absl::ZeroDuration()) {
auto seconds = absl::IDivDuration(value, absl::Seconds(1), &value);
auto nanos = static_cast<int32_t>(
absl::IDivDuration(value, absl::Nanoseconds(1), &value));
ProtoWireEncoder encoder(kTypeName, data);
CEL_RETURN_IF_ERROR(
encoder.WriteTag(ProtoWireTag(1, ProtoWireType::kVarint)));
CEL_RETURN_IF_ERROR(encoder.WriteVarint(seconds));
CEL_RETURN_IF_ERROR(
encoder.WriteTag(ProtoWireTag(2, ProtoWireType::kVarint)));
CEL_RETURN_IF_ERROR(encoder.WriteVarint(nanos));
encoder.EnsureFullyEncoded();
}
CEL_RETURN_IF_ERROR(SerializeDuration(value, data));
return MakeAny(MakeTypeUrl(kTypeName), std::move(data));
}

Expand Down
15 changes: 3 additions & 12 deletions base/values/int_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@
#include "common/any.h"
#include "common/json.h"
#include "internal/number.h"
#include "internal/proto_wire.h"
#include "internal/serialize.h"
#include "internal/status_macros.h"

namespace cel {

namespace {

using internal::ProtoWireEncoder;
using internal::ProtoWireTag;
using internal::ProtoWireType;
using internal::SerializeInt64Value;

} // namespace

Expand All @@ -55,15 +53,8 @@ std::string IntValue::DebugString() const { return DebugString(NativeValue()); }

absl::StatusOr<Any> IntValue::ConvertToAny(ValueFactory&) const {
static constexpr absl::string_view kTypeName = "google.protobuf.Int64Value";
const auto value = this->NativeValue();
absl::Cord data;
if (value) {
ProtoWireEncoder encoder(kTypeName, data);
CEL_RETURN_IF_ERROR(
encoder.WriteTag(ProtoWireTag(1, ProtoWireType::kVarint)));
CEL_RETURN_IF_ERROR(encoder.WriteVarint(value));
encoder.EnsureFullyEncoded();
}
CEL_RETURN_IF_ERROR(SerializeInt64Value(this->NativeValue(), data));
return MakeAny(MakeTypeUrl(kTypeName), std::move(data));
}

Expand Down
14 changes: 12 additions & 2 deletions base/values/null_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "base/values/null_value.h"

#include <string>
#include <utility>

#include "absl/status/statusor.h"
#include "absl/strings/cord.h"
Expand All @@ -26,16 +27,25 @@
#include "base/value_factory.h"
#include "common/any.h"
#include "common/json.h"
#include "internal/serialize.h"
#include "internal/status_macros.h"

namespace cel {

namespace {

using internal::SerializeValue;

}

CEL_INTERNAL_VALUE_IMPL(NullValue);

std::string NullValue::DebugString() { return "null"; }

absl::StatusOr<Any> NullValue::ConvertToAny(ValueFactory&) const {
return MakeAny(MakeTypeUrl("google.protobuf.Value"),
absl::Cord(absl::string_view("\x08\x00", 2)));
absl::Cord data;
CEL_RETURN_IF_ERROR(SerializeValue(kJsonNull, data));
return MakeAny(MakeTypeUrl("google.protobuf.Value"), std::move(data));
}

absl::StatusOr<Json> NullValue::ConvertToJson(ValueFactory&) const {
Expand Down
15 changes: 3 additions & 12 deletions base/values/string_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include "base/values/bytes_value.h"
#include "common/any.h"
#include "common/json.h"
#include "internal/proto_wire.h"
#include "internal/serialize.h"
#include "internal/status_macros.h"
#include "internal/strings.h"
#include "internal/time.h"
Expand All @@ -52,9 +52,7 @@ CEL_INTERNAL_VALUE_IMPL(StringValue);

namespace {

using internal::ProtoWireEncoder;
using internal::ProtoWireTag;
using internal::ProtoWireType;
using internal::SerializeStringValue;

struct StringValueDebugStringVisitor final {
std::string operator()(absl::string_view value) const {
Expand Down Expand Up @@ -322,15 +320,8 @@ std::string StringValue::DebugString() const {

absl::StatusOr<Any> StringValue::ConvertToAny(ValueFactory&) const {
static constexpr absl::string_view kTypeName = "google.protobuf.StringValue";
const auto value = ToCord();
absl::Cord data;
if (!value.empty()) {
ProtoWireEncoder encoder(kTypeName, data);
CEL_RETURN_IF_ERROR(
encoder.WriteTag(ProtoWireTag(1, ProtoWireType::kLengthDelimited)));
CEL_RETURN_IF_ERROR(encoder.WriteLengthDelimited(value));
encoder.EnsureFullyEncoded();
}
CEL_RETURN_IF_ERROR(SerializeStringValue(ToCord(), data));
return MakeAny(MakeTypeUrl(kTypeName), std::move(data));
}

Expand Down
27 changes: 6 additions & 21 deletions base/values/timestamp_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#include "base/values/timestamp_value.h"

#include <cstdint>
#include <string>
#include <utility>

Expand All @@ -31,17 +30,15 @@
#include "base/value_factory.h"
#include "common/any.h"
#include "common/json.h"
#include "internal/proto_wire.h"
#include "internal/serialize.h"
#include "internal/status_macros.h"
#include "internal/time.h"

namespace cel {

namespace {

using internal::ProtoWireEncoder;
using internal::ProtoWireTag;
using internal::ProtoWireType;
using internal::SerializeTimestamp;

} // namespace

Expand All @@ -57,26 +54,14 @@ std::string TimestampValue::DebugString() const {

absl::StatusOr<Any> TimestampValue::ConvertToAny(ValueFactory&) const {
static constexpr absl::string_view kTypeName = "google.protobuf.Timestamp";
auto value = this->NativeValue() - absl::UnixEpoch();
if (ABSL_PREDICT_FALSE(value == absl::InfiniteDuration() ||
value == -absl::InfiniteDuration())) {
auto value = this->NativeValue();
if (ABSL_PREDICT_FALSE(value == absl::InfiniteFuture() ||
value == absl::InfinitePast())) {
return absl::FailedPreconditionError(
"infinite timestamp values cannot be converted to google.protobuf.Any");
}
absl::Cord data;
if (value != absl::ZeroDuration()) {
auto seconds = absl::IDivDuration(value, absl::Seconds(1), &value);
auto nanos = static_cast<int32_t>(
absl::IDivDuration(value, absl::Nanoseconds(1), &value));
ProtoWireEncoder encoder(kTypeName, data);
CEL_RETURN_IF_ERROR(
encoder.WriteTag(ProtoWireTag(1, ProtoWireType::kVarint)));
CEL_RETURN_IF_ERROR(encoder.WriteVarint(seconds));
CEL_RETURN_IF_ERROR(
encoder.WriteTag(ProtoWireTag(2, ProtoWireType::kVarint)));
CEL_RETURN_IF_ERROR(encoder.WriteVarint(nanos));
encoder.EnsureFullyEncoded();
}
CEL_RETURN_IF_ERROR(SerializeTimestamp(value, data));
return MakeAny(MakeTypeUrl(kTypeName), std::move(data));
}

Expand Down
15 changes: 3 additions & 12 deletions base/values/uint_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,14 @@
#include "common/any.h"
#include "common/json.h"
#include "internal/number.h"
#include "internal/proto_wire.h"
#include "internal/serialize.h"
#include "internal/status_macros.h"

namespace cel {

namespace {

using internal::ProtoWireEncoder;
using internal::ProtoWireTag;
using internal::ProtoWireType;
using internal::SerializeUInt64Value;

} // namespace

Expand All @@ -58,15 +56,8 @@ std::string UintValue::DebugString() const {

absl::StatusOr<Any> UintValue::ConvertToAny(ValueFactory&) const {
static constexpr absl::string_view kTypeName = "google.protobuf.UInt64Value";
const auto value = this->NativeValue();
absl::Cord data;
if (value) {
ProtoWireEncoder encoder(kTypeName, data);
CEL_RETURN_IF_ERROR(
encoder.WriteTag(ProtoWireTag(1, ProtoWireType::kVarint)));
CEL_RETURN_IF_ERROR(encoder.WriteVarint(value));
encoder.EnsureFullyEncoded();
}
CEL_RETURN_IF_ERROR(SerializeUInt64Value(this->NativeValue(), data));
return MakeAny(MakeTypeUrl(kTypeName), std::move(data));
}

Expand Down
1 change: 1 addition & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ cc_library(
"//internal:status_macros",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:cord",
Expand Down
Loading

0 comments on commit 002fef4

Please sign in to comment.