Skip to content
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
29 changes: 14 additions & 15 deletions common/values/timestamp_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "internal/status_macros.h"
#include "internal/time.h"

namespace cel::common_internal {
namespace cel {

namespace {

Expand All @@ -39,48 +39,47 @@ std::string TimestampDebugString(absl::Time value) {

} // namespace

std::string TimestampValueBase::DebugString() const {
std::string TimestampValue::DebugString() const {
return TimestampDebugString(NativeValue());
}

absl::StatusOr<size_t> TimestampValueBase::GetSerializedSize(
absl::StatusOr<size_t> TimestampValue::GetSerializedSize(
AnyToJsonConverter&) const {
return internal::SerializedTimestampSize(NativeValue());
}

absl::Status TimestampValueBase::SerializeTo(AnyToJsonConverter&,
absl::Cord& value) const {
absl::Status TimestampValue::SerializeTo(AnyToJsonConverter&,
absl::Cord& value) const {
return internal::SerializeTimestamp(NativeValue(), value);
}

absl::StatusOr<absl::Cord> TimestampValueBase::Serialize(
absl::StatusOr<absl::Cord> TimestampValue::Serialize(
AnyToJsonConverter& value_manager) const {
absl::Cord value;
CEL_RETURN_IF_ERROR(SerializeTo(value_manager, value));
return value;
}

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

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

absl::StatusOr<Json> TimestampValueBase::ConvertToJson(
AnyToJsonConverter&) const {
absl::StatusOr<Json> TimestampValue::ConvertToJson(AnyToJsonConverter&) const {
CEL_ASSIGN_OR_RETURN(auto json,
internal::EncodeTimestampToJson(NativeValue()));
return JsonString(std::move(json));
}

absl::Status TimestampValueBase::Equal(ValueManager&, const Value& other,
Value& result) const {
absl::Status TimestampValue::Equal(ValueManager&, const Value& other,
Value& result) const {
if (auto other_value = As<TimestampValue>(other); other_value.has_value()) {
result = BoolValue{NativeValue() == other_value->NativeValue()};
return absl::OkStatus();
Expand All @@ -89,11 +88,11 @@ absl::Status TimestampValueBase::Equal(ValueManager&, const Value& other,
return absl::OkStatus();
}

absl::StatusOr<Value> TimestampValueBase::Equal(ValueManager& value_manager,
const Value& other) const {
absl::StatusOr<Value> TimestampValue::Equal(ValueManager& value_manager,
const Value& other) const {
Value result;
CEL_RETURN_IF_ERROR(Equal(value_manager, other, result));
return result;
}

} // namespace cel::common_internal
} // namespace cel
88 changes: 23 additions & 65 deletions common/values/timestamp_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,25 @@ class ValueManager;
class TimestampValue;
class TypeManager;

namespace common_internal {

struct TimestampValueBase {
// `TimestampValue` represents values of the primitive `timestamp` type.
class TimestampValue final {
public:
static constexpr ValueKind kKind = ValueKind::kTimestamp;

constexpr explicit TimestampValueBase(absl::Time value) noexcept
: value(value) {}
explicit TimestampValue(absl::Time value) noexcept : value_(value) {}

TimestampValueBase() = default;
TimestampValueBase(const TimestampValueBase&) = default;
TimestampValueBase(TimestampValueBase&&) = default;
TimestampValueBase& operator=(const TimestampValueBase&) = default;
TimestampValueBase& operator=(TimestampValueBase&&) = default;
TimestampValue& operator=(absl::Time value) noexcept {
value_ = value;
return *this;
}

constexpr ValueKind kind() const { return kKind; }
TimestampValue() = default;
TimestampValue(const TimestampValue&) = default;
TimestampValue(TimestampValue&&) = default;
TimestampValue& operator=(const TimestampValue&) = default;
TimestampValue& operator=(TimestampValue&&) = default;

ValueKind kind() const { return kKind; }

TimestampType GetType(TypeManager&) const { return TimestampType(); }

Expand Down Expand Up @@ -83,71 +87,25 @@ struct TimestampValueBase {

bool IsZeroValue() const { return NativeValue() == absl::UnixEpoch(); }

constexpr absl::Time NativeValue() const { return value; }
absl::Time NativeValue() const { return static_cast<absl::Time>(*this); }

// NOLINTNEXTLINE(google-explicit-constructor)
constexpr operator absl::Time() const noexcept { return value; }

absl::Time value = absl::UnixEpoch();
};

} // namespace common_internal

// `TimestampValue` represents values of the primitive `timestamp` type.
class TimestampValue final : private common_internal::TimestampValueBase {
private:
using Base = TimestampValueBase;

public:
using Base::kKind;

TimestampValue() = default;
TimestampValue(const TimestampValue&) = default;
TimestampValue(TimestampValue&&) = default;
TimestampValue& operator=(const TimestampValue&) = default;
TimestampValue& operator=(TimestampValue&&) = default;

constexpr explicit TimestampValue(absl::Time value) noexcept : Base(value) {}

using Base::kind;

using Base::GetType;

using Base::GetTypeName;

using Base::DebugString;

using Base::GetSerializedSize;

using Base::SerializeTo;

using Base::Serialize;

using Base::GetTypeUrl;

using Base::ConvertToAny;

using Base::ConvertToJson;

using Base::Equal;

using Base::IsZeroValue;

using Base::NativeValue;

using Base::operator absl::Time;
operator absl::Time() const noexcept { return value_; }

friend void swap(TimestampValue& lhs, TimestampValue& rhs) noexcept {
using std::swap;
swap(lhs.value, rhs.value);
swap(lhs.value_, rhs.value_);
}

private:
absl::Time value_ = absl::UnixEpoch();
};

constexpr bool operator==(TimestampValue lhs, TimestampValue rhs) {
inline bool operator==(TimestampValue lhs, TimestampValue rhs) {
return lhs.NativeValue() == rhs.NativeValue();
}

constexpr bool operator!=(TimestampValue lhs, TimestampValue rhs) {
inline bool operator!=(TimestampValue lhs, TimestampValue rhs) {
return !operator==(lhs, rhs);
}

Expand Down