Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

DurationValue serialization #492

Merged
1 commit merged into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions common/values/duration_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <cstddef>
#include <string>
#include <utility>

#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "common/any.h"
#include "common/value.h"
#include "internal/serialize.h"
#include "internal/status_macros.h"
#include "internal/time.h"

namespace cel {
Expand All @@ -32,8 +41,60 @@ std::string DurationValue::DebugString() const {
return DurationDebugString(NativeValue());
}

absl::StatusOr<size_t> DurationValue::GetSerializedSize() const {
return internal::SerializedDurationSize(NativeValue());
}

absl::Status DurationValue::SerializeTo(absl::Cord& value) const {
return internal::SerializeDuration(NativeValue(), value);
}

absl::StatusOr<absl::Cord> DurationValue::Serialize() const {
absl::Cord value;
CEL_RETURN_IF_ERROR(SerializeTo(value));
return value;
}

absl::StatusOr<std::string> DurationValue::GetTypeUrl(
absl::string_view prefix) const {
return MakeTypeUrlWithPrefix(prefix, "google.protobuf.Duration");
}

absl::StatusOr<Any> DurationValue::ConvertToAny(
absl::string_view prefix) const {
CEL_ASSIGN_OR_RETURN(auto value, Serialize());
CEL_ASSIGN_OR_RETURN(auto type_url, GetTypeUrl(prefix));
return MakeAny(std::move(type_url), std::move(value));
}

std::string DurationValueView::DebugString() const {
return DurationDebugString(NativeValue());
}

absl::StatusOr<size_t> DurationValueView::GetSerializedSize() const {
return internal::SerializedDurationSize(NativeValue());
}

absl::Status DurationValueView::SerializeTo(absl::Cord& value) const {
return internal::SerializeDuration(NativeValue(), value);
}

absl::StatusOr<absl::Cord> DurationValueView::Serialize() const {
absl::Cord value;
CEL_RETURN_IF_ERROR(SerializeTo(value));
return value;
}

absl::StatusOr<std::string> DurationValueView::GetTypeUrl(
absl::string_view prefix) const {
return MakeTypeUrlWithPrefix(prefix, "google.protobuf.Duration");
}

absl::StatusOr<Any> DurationValueView::ConvertToAny(
absl::string_view prefix) const {
CEL_ASSIGN_OR_RETURN(auto value, Serialize());
CEL_ASSIGN_OR_RETURN(auto type_url, GetTypeUrl(prefix));
return MakeAny(std::move(type_url), std::move(value));
}

} // namespace cel
30 changes: 30 additions & 0 deletions common/values/duration_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@
#ifndef THIRD_PARTY_CEL_CPP_COMMON_VALUES_DURATION_VALUE_H_
#define THIRD_PARTY_CEL_CPP_COMMON_VALUES_DURATION_VALUE_H_

#include <cstddef>
#include <ostream>
#include <string>

#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "common/any.h"
#include "common/type.h"
#include "common/value_kind.h"

Expand Down Expand Up @@ -54,6 +60,18 @@ class DurationValue final {

std::string DebugString() const;

absl::StatusOr<size_t> GetSerializedSize() const;

absl::Status SerializeTo(absl::Cord& value) const;

absl::StatusOr<absl::Cord> Serialize() const;

absl::StatusOr<std::string> GetTypeUrl(
absl::string_view prefix = kTypeGoogleApisComPrefix) const;

absl::StatusOr<Any> ConvertToAny(
absl::string_view prefix = kTypeGoogleApisComPrefix) const;

constexpr absl::Duration NativeValue() const { return value_; }

void swap(DurationValue& other) noexcept {
Expand Down Expand Up @@ -124,6 +142,18 @@ class DurationValueView final {

std::string DebugString() const;

absl::StatusOr<size_t> GetSerializedSize() const;

absl::Status SerializeTo(absl::Cord& value) const;

absl::StatusOr<absl::Cord> Serialize() const;

absl::StatusOr<std::string> GetTypeUrl(
absl::string_view prefix = kTypeGoogleApisComPrefix) const;

absl::StatusOr<Any> ConvertToAny(
absl::string_view prefix = kTypeGoogleApisComPrefix) const;

constexpr absl::Duration NativeValue() const { return value_; }

void swap(DurationValueView& other) noexcept {
Expand Down
23 changes: 23 additions & 0 deletions common/values/duration_value_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

#include <sstream>

#include "absl/strings/cord.h"
#include "absl/time/time.h"
#include "absl/types/optional.h"
#include "common/any.h"
#include "common/casting.h"
#include "common/native_type.h"
#include "common/type.h"
Expand All @@ -27,6 +29,7 @@ namespace {

using testing::An;
using testing::Ne;
using cel::internal::IsOkAndHolds;

TEST(DurationValue, Kind) {
EXPECT_EQ(DurationValue().kind(), DurationValue::kKind);
Expand All @@ -52,6 +55,16 @@ TEST(DurationValue, DebugString) {
}
}

TEST(DurationValue, GetSerializedSize) {
EXPECT_THAT(DurationValue().GetSerializedSize(), IsOkAndHolds(0));
}

TEST(DurationValue, ConvertToAny) {
EXPECT_THAT(DurationValue().ConvertToAny(),
IsOkAndHolds(MakeAny(MakeTypeUrl("google.protobuf.Duration"),
absl::Cord())));
}

TEST(DurationValue, NativeTypeId) {
EXPECT_EQ(NativeTypeId::Of(DurationValue(absl::Seconds(1))),
NativeTypeId::For<DurationValue>());
Expand Down Expand Up @@ -112,6 +125,16 @@ TEST(DurationValueView, DebugString) {
}
}

TEST(DurationValueView, GetSerializedSize) {
EXPECT_THAT(DurationValueView().GetSerializedSize(), IsOkAndHolds(0));
}

TEST(DurationValueView, ConvertToAny) {
EXPECT_THAT(DurationValueView().ConvertToAny(),
IsOkAndHolds(MakeAny(MakeTypeUrl("google.protobuf.Duration"),
absl::Cord())));
}

TEST(DurationValueView, NativeTypeId) {
EXPECT_EQ(NativeTypeId::Of(DurationValueView(absl::Seconds(1))),
NativeTypeId::For<DurationValueView>());
Expand Down