Skip to content

Commit

Permalink
UnknownValue and UnknownValueView
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 585794277
  • Loading branch information
jcking authored and copybara-github committed Nov 28, 2023
1 parent 23c9805 commit d65ec54
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 4 deletions.
1 change: 1 addition & 0 deletions common/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "common/values/timestamp_value.h" // IWYU pragma: export
#include "common/values/type_value.h" // IWYU pragma: export
#include "common/values/uint_value.h" // IWYU pragma: export
#include "common/values/unknown_value.h" // IWYU pragma: export
#include "common/values/values.h"

namespace cel {
Expand Down
110 changes: 110 additions & 0 deletions common/values/unknown_value.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// IWYU pragma: private, include "common/value.h"
// IWYU pragma: friend "common/value.h"

// TODO(uncreated-issue/61): finish implementing this is just a placeholder for now

#ifndef THIRD_PARTY_CEL_CPP_COMMON_VALUES_UNKNOWN_VALUE_H_
#define THIRD_PARTY_CEL_CPP_COMMON_VALUES_UNKNOWN_VALUE_H_

#include <ostream>
#include <string>

#include "absl/base/attributes.h"
#include "common/type.h"
#include "common/value_kind.h"

namespace cel {

class UnknownValue;
class UnknownValueView;

// `UnknownValue` represents values of the primitive `duration` type.
class UnknownValue final {
public:
using view_alternative_type = UnknownValueView;

static constexpr ValueKind kKind = ValueKind::kUnknown;

explicit UnknownValue(UnknownValueView) noexcept;

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

constexpr ValueKind kind() const { return kKind; }

UnknownTypeView type() const { return UnknownTypeView(); }

std::string DebugString() const { return ""; }

void swap(UnknownValue& other) noexcept {}

private:
friend class UnknownValueView;
};

inline void swap(UnknownValue& lhs, UnknownValue& rhs) noexcept {
lhs.swap(rhs);
}

inline std::ostream& operator<<(std::ostream& out, const UnknownValue& value) {
return out << value.DebugString();
}

class UnknownValueView final {
public:
using alternative_type = UnknownValue;

static constexpr ValueKind kKind = UnknownValue::kKind;

// NOLINTNEXTLINE(google-explicit-constructor)
UnknownValueView(const UnknownValue& value ABSL_ATTRIBUTE_LIFETIME_BOUND
ABSL_ATTRIBUTE_UNUSED) noexcept {}

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

constexpr ValueKind kind() const { return kKind; }

UnknownTypeView type() const { return UnknownTypeView(); }

std::string DebugString() const { return ""; }

void swap(UnknownValueView& other) noexcept {}

private:
friend class UnknownValue;
};

inline void swap(UnknownValueView& lhs, UnknownValueView& rhs) noexcept {
lhs.swap(rhs);
}

inline std::ostream& operator<<(std::ostream& out, UnknownValueView value) {
return out << value.DebugString();
}

inline UnknownValue::UnknownValue(UnknownValueView) noexcept {}

} // namespace cel

#endif // THIRD_PARTY_CEL_CPP_COMMON_VALUES_UNKNOWN_VALUE_H_
124 changes: 124 additions & 0 deletions common/values/unknown_value_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <sstream>

#include "absl/types/optional.h"
#include "common/casting.h"
#include "common/native_type.h"
#include "common/type.h"
#include "common/value.h"
#include "internal/testing.h"

namespace cel {
namespace {

using testing::An;
using testing::Ne;

TEST(UnknownValue, Kind) {
EXPECT_EQ(UnknownValue().kind(), UnknownValue::kKind);
EXPECT_EQ(Value(UnknownValue()).kind(), UnknownValue::kKind);
}

TEST(UnknownValue, Type) {
EXPECT_EQ(UnknownValue().type(), UnknownType());
EXPECT_EQ(Value(UnknownValue()).type(), UnknownType());
}

TEST(UnknownValue, DebugString) {
{
std::ostringstream out;
out << UnknownValue();
EXPECT_EQ(out.str(), "");
}
{
std::ostringstream out;
out << Value(UnknownValue());
EXPECT_EQ(out.str(), "");
}
}

TEST(UnknownValue, NativeTypeId) {
EXPECT_EQ(NativeTypeId::Of(UnknownValue()),
NativeTypeId::For<UnknownValue>());
EXPECT_EQ(NativeTypeId::Of(Value(UnknownValue())),
NativeTypeId::For<UnknownValue>());
}

TEST(UnknownValue, InstanceOf) {
EXPECT_TRUE(InstanceOf<UnknownValue>(UnknownValue()));
EXPECT_TRUE(InstanceOf<UnknownValue>(Value(UnknownValue())));
}

TEST(UnknownValue, Cast) {
EXPECT_THAT(Cast<UnknownValue>(UnknownValue()), An<UnknownValue>());
EXPECT_THAT(Cast<UnknownValue>(Value(UnknownValue())), An<UnknownValue>());
}

TEST(UnknownValue, As) {
EXPECT_THAT(As<UnknownValue>(UnknownValue()), Ne(absl::nullopt));
EXPECT_THAT(As<UnknownValue>(Value(UnknownValue())), Ne(absl::nullopt));
}

TEST(UnknownValueView, Kind) {
EXPECT_EQ(UnknownValueView().kind(), UnknownValueView::kKind);
EXPECT_EQ(ValueView(UnknownValueView()).kind(), UnknownValueView::kKind);
}

TEST(UnknownValueView, Type) {
EXPECT_EQ(UnknownValueView().type(), UnknownType());
EXPECT_EQ(ValueView(UnknownValueView()).type(), UnknownType());
}

TEST(UnknownValueView, DebugString) {
{
std::ostringstream out;
out << UnknownValueView();
EXPECT_EQ(out.str(), "");
}
{
std::ostringstream out;
out << ValueView(UnknownValueView());
EXPECT_EQ(out.str(), "");
}
}

TEST(UnknownValueView, NativeTypeId) {
EXPECT_EQ(NativeTypeId::Of(UnknownValueView()),
NativeTypeId::For<UnknownValueView>());
EXPECT_EQ(NativeTypeId::Of(ValueView(UnknownValueView())),
NativeTypeId::For<UnknownValueView>());
}

TEST(UnknownValueView, InstanceOf) {
EXPECT_TRUE(InstanceOf<UnknownValueView>(UnknownValueView()));
EXPECT_TRUE(InstanceOf<UnknownValueView>(ValueView(UnknownValueView())));
}

TEST(UnknownValueView, Cast) {
EXPECT_THAT(Cast<UnknownValueView>(UnknownValueView()),
An<UnknownValueView>());
EXPECT_THAT(Cast<UnknownValueView>(ValueView(UnknownValueView())),
An<UnknownValueView>());
}

TEST(UnknownValueView, As) {
EXPECT_THAT(As<UnknownValueView>(UnknownValueView()), Ne(absl::nullopt));
EXPECT_THAT(As<UnknownValueView>(ValueView(UnknownValueView())),
Ne(absl::nullopt));
}

} // namespace
} // namespace cel
13 changes: 9 additions & 4 deletions common/values/values.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class StringValue;
class TimestampValue;
class TypeValue;
class UintValue;
class UnknownValue;

class ValueView;
class BoolValueView;
Expand All @@ -52,6 +53,7 @@ class StringValueView;
class TimestampValueView;
class TypeValueView;
class UintValueView;
class UnknownValueView;

namespace common_internal {

Expand All @@ -72,7 +74,8 @@ struct IsValueAlternative
std::is_same<EnumValue, T>, std::is_same<ErrorValue, T>,
std::is_same<IntValue, T>, std::is_same<NullValue, T>,
std::is_same<StringValue, T>, std::is_same<TimestampValue, T>,
std::is_same<TypeValue, T>, std::is_same<UintValue, T>>> {};
std::is_same<TypeValue, T>, std::is_same<UintValue, T>,
std::is_same<UnknownValue, T>>> {};

template <typename T>
inline constexpr bool IsValueAlternativeV = IsValueAlternative<T>::value;
Expand All @@ -85,7 +88,8 @@ using ValueVariant = absl::variant<
absl::monostate,
#endif
BoolValue, BytesValue, DoubleValue, DurationValue, EnumValue, ErrorValue,
IntValue, NullValue, StringValue, TimestampValue, TypeValue, UintValue>;
IntValue, NullValue, StringValue, TimestampValue, TypeValue, UintValue,
UnknownValue>;

template <typename T>
struct IsValueViewAlternative
Expand All @@ -95,7 +99,8 @@ struct IsValueViewAlternative
std::is_same<EnumValueView, T>, std::is_same<ErrorValueView, T>,
std::is_same<IntValueView, T>, std::is_same<NullValueView, T>,
std::is_same<StringValueView, T>, std::is_same<TimestampValueView, T>,
std::is_same<TypeValueView, T>, std::is_same<UintValueView, T>>> {};
std::is_same<TypeValueView, T>, std::is_same<UintValueView, T>,
std::is_same<UnknownValueView, T>>> {};

template <typename T>
inline constexpr bool IsValueViewAlternativeV =
Expand All @@ -110,7 +115,7 @@ using ValueViewVariant = absl::variant<
#endif
BoolValueView, BytesValueView, DoubleValueView, DurationValueView,
EnumValueView, ErrorValueView, IntValueView, NullValueView, StringValueView,
TimestampValueView, TypeValueView, UintValueView>;
TimestampValueView, TypeValueView, UintValueView, UnknownValueView>;

// Get the base type alternative for the given alternative or interface. The
// base type alternative is the type stored in the `ValueVariant`.
Expand Down

0 comments on commit d65ec54

Please # to comment.