From 56b53afecf70c0fef0b244f634d813c394cd43e6 Mon Sep 17 00:00:00 2001 From: Justin King Date: Tue, 6 Oct 2026 17:40:22 -0700 Subject: [PATCH] Remove deprecated CEL casting utilities PiperOrigin-RevId: 994736001 --- common/BUILD | 12 - common/casting.h | 75 ------ common/internal/BUILD | 11 - common/internal/casting.h | 237 ------------------ common/legacy_value.cc | 33 ++- common/type_reflector_test.cc | 49 ++-- common/values/custom_list_value.cc | 5 +- common/values/list_value_test.cc | 27 +- common/values/map_value_test.cc | 51 ++-- common/values/null_value_test.cc | 18 -- eval/compiler/BUILD | 3 - .../regex_precompilation_optimization.cc | 12 +- eval/eval/BUILD | 20 -- eval/eval/attribute_utility.cc | 4 +- eval/eval/comprehension_step.cc | 3 - eval/eval/container_access_step.cc | 15 +- eval/eval/create_list_step.cc | 3 - eval/eval/create_list_step_test.cc | 36 ++- eval/eval/create_map_step.cc | 3 - eval/eval/create_struct_step.cc | 3 - eval/eval/function_step.cc | 3 +- eval/eval/ident_step_test.cc | 19 +- eval/eval/logic_step.cc | 13 +- eval/eval/logic_step_test.cc | 3 - eval/eval/optional_or_step.cc | 27 +- eval/eval/optional_or_step_test.cc | 3 - eval/eval/select_step_test.cc | 76 +++--- eval/eval/ternary_step_test.cc | 29 +-- eval/tests/BUILD | 2 - eval/tests/modern_benchmark_test.cc | 92 ++++--- extensions/BUILD | 2 - extensions/math_ext.cc | 7 +- extensions/protobuf/BUILD | 4 - .../protobuf/bind_proto_to_activation_test.cc | 6 +- extensions/protobuf/value_test.cc | 83 +++--- extensions/select_optimization.cc | 8 +- runtime/BUILD | 3 - runtime/bind_proto_to_activation_test.cc | 8 +- runtime/internal/BUILD | 1 - runtime/internal/function_adapter.h | 11 +- runtime/optional_types.cc | 11 +- .../container_membership_functions.cc | 13 +- 42 files changed, 289 insertions(+), 755 deletions(-) delete mode 100644 common/casting.h delete mode 100644 common/internal/casting.h diff --git a/common/BUILD b/common/BUILD index 14e3b6c0d..27ae6ded9 100644 --- a/common/BUILD +++ b/common/BUILD @@ -143,7 +143,6 @@ cc_test( "//internal:testing_descriptor_pool", "@com_google_absl//absl/status:status_matchers", "@com_google_absl//absl/status:statusor", - "@com_google_absl//absl/types:optional", ], ) @@ -463,15 +462,6 @@ cc_test( ], ) -cc_library( - name = "casting", - hdrs = ["casting.h"], - deps = [ - "//common/internal:casting", - "@com_google_absl//absl/base:core_headers", - ], -) - cc_library( name = "json", hdrs = ["json.h"], @@ -757,7 +747,6 @@ cc_library( ":allocator", ":any", ":arena", - ":casting", ":kind", ":memory", ":native_type", @@ -833,7 +822,6 @@ cc_test( "value_test.cc", ], deps = [ - ":casting", ":memory", ":native_type", ":type", diff --git a/common/casting.h b/common/casting.h deleted file mode 100644 index 69074d4d9..000000000 --- a/common/casting.h +++ /dev/null @@ -1,75 +0,0 @@ -// 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. - -#ifndef THIRD_PARTY_CEL_CPP_COMMON_CASTING_H_ -#define THIRD_PARTY_CEL_CPP_COMMON_CASTING_H_ - -#include "absl/base/attributes.h" -#include "common/internal/casting.h" - -namespace cel { - -// `InstanceOf(const From&)` determines whether `From` holds or is `To`. -// -// `To` must be a plain non-union class type that is not qualified. -// -// We expose `InstanceOf` this way to avoid ADL. -// -// Example: -// -// if (InstanceOf(superclass)) { -// Cast(superclass).SomeMethod(); -// } -template -ABSL_DEPRECATED("Use Is member functions instead.") -inline constexpr common_internal::InstanceOfImpl InstanceOf{}; - -// `Cast(From)` is a "checked cast". In debug builds an assertion is emitted -// which verifies `From` is an instance-of `To`. In non-debug builds, invalid -// casts are undefined behavior. -// -// We expose `Cast` this way to avoid ADL. -// -// Example: -// -// if (InstanceOf(superclass)) { -// Cast(superclass).SomeMethod(); -// } -template -ABSL_DEPRECATED( - "Use explicit conversion functions instead through static_cast.") -inline constexpr common_internal::CastImpl Cast{}; - -// `As(From)` is a "checking cast". The result is explicitly convertible to -// `bool`, such that it can be used with `if` statements. The result can be -// accessed with `operator*` or `operator->`. The return type should be treated -// as an implementation detail, with no assumptions on the concrete type. You -// should use `auto`. -// -// `As` is analogous to the paradigm `if (InstanceOf(a)) Cast(a)`. -// -// We expose `As` this way to avoid ADL. -// -// Example: -// -// if (auto subclass = As(superclass); subclass) { -// subclass->SomeMethod(); -// } -template -ABSL_DEPRECATED("Use As member functions instead.") -inline constexpr common_internal::AsImpl As{}; - -} // namespace cel - -#endif // THIRD_PARTY_CEL_CPP_COMMON_INSTANCE_OF_H_ diff --git a/common/internal/BUILD b/common/internal/BUILD index 0b6f6238c..571f1ef14 100644 --- a/common/internal/BUILD +++ b/common/internal/BUILD @@ -17,17 +17,6 @@ load("@rules_cc//cc:cc_test.bzl", "cc_test") package(default_visibility = ["//visibility:public"]) -cc_library( - name = "casting", - hdrs = ["casting.h"], - deps = [ - "//internal:casts", - "@com_google_absl//absl/base:core_headers", - "@com_google_absl//absl/meta:type_traits", - "@com_google_absl//absl/types:optional", - ], -) - cc_library( name = "byte_string", srcs = ["byte_string.cc"], diff --git a/common/internal/casting.h b/common/internal/casting.h deleted file mode 100644 index fe7d03279..000000000 --- a/common/internal/casting.h +++ /dev/null @@ -1,237 +0,0 @@ -// 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/casting.h" - -#ifndef THIRD_PARTY_CEL_CPP_COMMON_INTERNAL_CASTING_H_ -#define THIRD_PARTY_CEL_CPP_COMMON_INTERNAL_CASTING_H_ - -#include -#include -#include - -#include "absl/base/attributes.h" -#include "absl/meta/type_traits.h" -#include "absl/types/optional.h" -#include "internal/casts.h" - -namespace cel { - -namespace common_internal { - -template -using propagate_const_t = - std::conditional_t>, - std::add_const_t, To>; - -template -using propagate_volatile_t = - std::conditional_t>, - std::add_volatile_t, To>; - -template -using propagate_reference_t = - std::conditional_t, - std::add_lvalue_reference_t, - std::conditional_t, - std::add_rvalue_reference_t, To>>; - -template -using propagate_cvref_t = propagate_reference_t< - propagate_volatile_t, From>, From>; - -} // namespace common_internal - -namespace common_internal { - -// Implementation of `cel::InstanceOf`. -template -struct ABSL_DEPRECATED("Use Is member functions instead.") - InstanceOfImpl final { - static_assert(!std::is_pointer_v, "To must not be a pointer"); - static_assert(!std::is_array_v, "To must not be an array"); - static_assert(!std::is_lvalue_reference_v, - "To must not be a lvalue reference"); - static_assert(!std::is_rvalue_reference_v, - "To must not be a lvalue reference"); - static_assert(!std::is_const_v, "To must not be const qualified"); - static_assert(!std::is_volatile_v, "To must not be volatile qualified"); - static_assert(std::is_class_v, "To must be a non-union class"); - - explicit InstanceOfImpl() = default; - - template - ABSL_DEPRECATED("Use Is member functions instead.") - ABSL_MUST_USE_RESULT bool operator()(const From& from) const { - static_assert(!std::is_volatile_v, - "From must not be volatile qualified"); - static_assert(std::is_class_v, "From must be a non-union class"); - if constexpr (std::is_same_v, To>) { - // Same type. Separate from the next `else if` to work on in-complete - // types. - return true; - } else if constexpr (std::is_polymorphic_v && - std::is_polymorphic_v> && - std::is_base_of_v>) { - // Polymorphic upcast. - return true; - } else if constexpr (!std::is_polymorphic_v && - !std::is_polymorphic_v> && - (std::is_convertible_v || - std::is_convertible_v || - std::is_convertible_v || - std::is_convertible_v)) { - // Implicitly convertible. - return true; - } else { - // Something else. - return from.template Is(); - } - } - - template - ABSL_DEPRECATED("Use Is member functions instead.") - ABSL_MUST_USE_RESULT bool operator()(const From* from) const { - static_assert(!std::is_volatile_v, - "From must not be volatile qualified"); - static_assert(std::is_class_v, "From must be a non-union class"); - return from != nullptr && (*this)(*from); - } -}; - -// Implementation of `cel::Cast`. -template -struct ABSL_DEPRECATED( - "Use explicit conversion functions instead through static_cast.") - CastImpl final { - static_assert(!std::is_pointer_v, "To must not be a pointer"); - static_assert(!std::is_array_v, "To must not be an array"); - static_assert(!std::is_lvalue_reference_v, - "To must not be a lvalue reference"); - static_assert(!std::is_rvalue_reference_v, - "To must not be a lvalue reference"); - static_assert(!std::is_const_v, "To must not be const qualified"); - static_assert(!std::is_volatile_v, "To must not be volatile qualified"); - static_assert(std::is_class_v, "To must be a non-union class"); - - explicit CastImpl() = default; - - template - ABSL_DEPRECATED( - "Use explicit conversion functions instead through static_cast.") - ABSL_MUST_USE_RESULT decltype(auto) - operator()(From&& from) const { - static_assert(!std::is_volatile_v, - "From must not be volatile qualified"); - static_assert(std::is_class_v>, - "From must be a non-union class"); - if constexpr (std::is_polymorphic_v) { - static_assert(std::is_lvalue_reference_v, - "polymorphic casts are only possible on lvalue references"); - } - if constexpr (std::is_same_v, To>) { - // Same type. Separate from the next `else if` to work on in-complete - // types. - return static_cast>(from); - } else if constexpr (std::is_polymorphic_v && - std::is_polymorphic_v> && - std::is_base_of_v>) { - // Polymorphic upcast. - return static_cast>(from); - } else if constexpr (std::is_polymorphic_v && - std::is_polymorphic_v> && - std::is_base_of_v, To>) { - // Polymorphic downcast. - return cel::internal::down_cast>( - std::forward(from)); - } else if constexpr (std::is_convertible_v && - !std::is_polymorphic_v && - !std::is_polymorphic_v>) { - return static_cast(std::forward(from)); - } else { - // Something else. - return std::forward(from).template Get(); - } - } - - template - ABSL_DEPRECATED( - "Use explicit conversion functions instead through static_cast.") - ABSL_MUST_USE_RESULT decltype(auto) - operator()(From* from) const { - static_assert(!std::is_volatile_v, - "From must not be volatile qualified"); - static_assert(std::is_class_v, "From must be a non-union class"); - using R = decltype((*this)(*from)); - static_assert(std::is_lvalue_reference_v); - if (from == nullptr) { - return static_cast>>( - nullptr); - } - return static_cast>>( - std::addressof((*this)(*from))); - } -}; - -// Implementation of `cel::As`. -template -struct ABSL_DEPRECATED("Use As member functions instead.") AsImpl final { - static_assert(!std::is_pointer_v, "To must not be a pointer"); - static_assert(!std::is_array_v, "To must not be an array"); - static_assert(!std::is_lvalue_reference_v, - "To must not be a lvalue reference"); - static_assert(!std::is_rvalue_reference_v, - "To must not be a lvalue reference"); - static_assert(!std::is_const_v, "To must not be const qualified"); - static_assert(!std::is_volatile_v, "To must not be volatile qualified"); - static_assert(std::is_class_v, "To must be a non-union class"); - - explicit AsImpl() = default; - - template - ABSL_DEPRECATED("Use As member functions instead.") - ABSL_MUST_USE_RESULT decltype(auto) operator()(From&& from) const { - // Returns either `absl::optional` or `cel::optional_ref` - // depending on the return type of `CastTraits::Convert`. The use of these - // two types is an implementation detail. - static_assert(!std::is_volatile_v, - "From must not be volatile qualified"); - static_assert(std::is_class_v>, - "From must be a non-union class"); - return std::forward(from).template As(); - } - - // Returns a pointer. - template - ABSL_DEPRECATED("Use As member functions instead.") - ABSL_MUST_USE_RESULT decltype(auto) operator()(From* from) const { - // Returns either `absl::optional` or `To*` depending on the return type of - // `CastTraits::Convert`. The use of these two types is an implementation - // detail. - static_assert(!std::is_volatile_v, - "From must not be volatile qualified"); - static_assert(std::is_class_v, "From must be a non-union class"); - using R = decltype(from->template As()); - if (from == nullptr) { - return R{absl::nullopt}; - } - return from->template As(); - } -}; - -} // namespace common_internal - -} // namespace cel - -#endif // THIRD_PARTY_CEL_CPP_COMMON_INTERNAL_CASTING_H_ diff --git a/common/legacy_value.cc b/common/legacy_value.cc index d107c6f8a..505f2fbf5 100644 --- a/common/legacy_value.cc +++ b/common/legacy_value.cc @@ -36,7 +36,6 @@ #include "absl/types/span.h" #include "absl/types/variant.h" #include "base/attribute.h" -#include "common/casting.h" #include "common/kind.h" #include "common/memory.h" #include "common/type.h" @@ -1103,15 +1102,15 @@ absl::StatusOr LegacyValue( case ValueKind::kNull: return CelValue::CreateNull(); case ValueKind::kBool: - return CelValue::CreateBool(Cast(modern_value).NativeValue()); + return CelValue::CreateBool(modern_value.Get().NativeValue()); case ValueKind::kInt: - return CelValue::CreateInt64(Cast(modern_value).NativeValue()); + return CelValue::CreateInt64(modern_value.Get().NativeValue()); case ValueKind::kUint: return CelValue::CreateUint64( - Cast(modern_value).NativeValue()); + modern_value.Get().NativeValue()); case ValueKind::kDouble: return CelValue::CreateDouble( - Cast(modern_value).NativeValue()); + modern_value.Get().NativeValue()); case ValueKind::kString: return CelValue::CreateStringView(common_internal::LegacyStringValue( modern_value.GetString(), /*stable=*/false, arena)); @@ -1133,14 +1132,14 @@ absl::StatusOr LegacyValue( case ValueKind::kUnknown: return CelValue::CreateUnknownSet(google::protobuf::Arena::Create( arena, - common_internal::GetUnknown(Cast(modern_value)))); + common_internal::GetUnknown(modern_value.Get()))); case ValueKind::kType: return CelValue::CreateCelType( CelValue::CelTypeHolder(google::protobuf::Arena::Create( - arena, Cast(modern_value).NativeValue().name()))); + arena, modern_value.Get().NativeValue().name()))); case ValueKind::kError: return CelValue::CreateError(google::protobuf::Arena::Create( - arena, Cast(modern_value).NativeValue())); + arena, modern_value.Get().NativeValue())); default: return absl::InvalidArgumentError( absl::StrCat("google::api::expr::runtime::CelValue does not support ", @@ -1208,13 +1207,13 @@ absl::StatusOr ToLegacyValue( case ValueKind::kNull: return CelValue::CreateNull(); case ValueKind::kBool: - return CelValue::CreateBool(Cast(value).NativeValue()); + return CelValue::CreateBool(value.Get().NativeValue()); case ValueKind::kInt: - return CelValue::CreateInt64(Cast(value).NativeValue()); + return CelValue::CreateInt64(value.Get().NativeValue()); case ValueKind::kUint: - return CelValue::CreateUint64(Cast(value).NativeValue()); + return CelValue::CreateUint64(value.Get().NativeValue()); case ValueKind::kDouble: - return CelValue::CreateDouble(Cast(value).NativeValue()); + return CelValue::CreateDouble(value.Get().NativeValue()); case ValueKind::kString: return CelValue::CreateStringView(common_internal::LegacyStringValue( value.GetString(), /*stable=*/false, arena)); @@ -1225,24 +1224,24 @@ absl::StatusOr ToLegacyValue( return common_internal::LegacyTrivialStructValue(arena, value); case ValueKind::kDuration: return CelValue::CreateUncheckedDuration( - Cast(value).NativeValue()); + value.Get().NativeValue()); case ValueKind::kTimestamp: return CelValue::CreateTimestamp( - Cast(value).NativeValue()); + value.Get().NativeValue()); case ValueKind::kList: return common_internal::LegacyTrivialListValue(arena, value); case ValueKind::kMap: return common_internal::LegacyTrivialMapValue(arena, value); case ValueKind::kUnknown: return CelValue::CreateUnknownSet(google::protobuf::Arena::Create( - arena, common_internal::GetUnknown(Cast(value)))); + arena, common_internal::GetUnknown(value.Get()))); case ValueKind::kType: return CelValue::CreateCelType( CelValue::CelTypeHolder(google::protobuf::Arena::Create( - arena, Cast(value).NativeValue().name()))); + arena, value.Get().NativeValue().name()))); case ValueKind::kError: return CelValue::CreateError(google::protobuf::Arena::Create( - arena, Cast(value).NativeValue())); + arena, value.Get().NativeValue())); default: return absl::InvalidArgumentError( absl::StrCat("google::api::expr::runtime::CelValue does not support ", diff --git a/common/type_reflector_test.cc b/common/type_reflector_test.cc index 961de0d8e..52cd176ab 100644 --- a/common/type_reflector_test.cc +++ b/common/type_reflector_test.cc @@ -20,7 +20,6 @@ #include "absl/status/status_matchers.h" #include "absl/time/time.h" #include "absl/types/optional.h" -#include "common/casting.h" #include "common/value.h" #include "common/value_testing.h" #include "common/values/list_value.h" @@ -227,8 +226,8 @@ TEST_F(TypeReflectorTest, NewValueBuilder_BoolValue) { IsOkAndHolds(Optional( ErrorValueIs(StatusIs(absl::StatusCode::kInvalidArgument))))); ASSERT_OK_AND_ASSIGN(auto value, std::move(*builder).Build()); - EXPECT_TRUE(InstanceOf(value)); - EXPECT_EQ(Cast(value).NativeValue(), true); + EXPECT_TRUE(value.Is()); + EXPECT_EQ(value.Get().NativeValue(), true); } TEST_F(TypeReflectorTest, NewValueBuilder_Int32Value) { @@ -261,8 +260,8 @@ TEST_F(TypeReflectorTest, NewValueBuilder_Int32Value) { IsOkAndHolds(Optional( ErrorValueIs(StatusIs(absl::StatusCode::kOutOfRange))))); ASSERT_OK_AND_ASSIGN(auto value, std::move(*builder).Build()); - EXPECT_TRUE(InstanceOf(value)); - EXPECT_EQ(Cast(value).NativeValue(), 1); + EXPECT_TRUE(value.Is()); + EXPECT_EQ(value.Get().NativeValue(), 1); } TEST_F(TypeReflectorTest, NewValueBuilder_Int64Value) { @@ -287,8 +286,8 @@ TEST_F(TypeReflectorTest, NewValueBuilder_Int64Value) { IsOkAndHolds(Optional( ErrorValueIs(StatusIs(absl::StatusCode::kInvalidArgument))))); ASSERT_OK_AND_ASSIGN(auto value, std::move(*builder).Build()); - EXPECT_TRUE(InstanceOf(value)); - EXPECT_EQ(Cast(value).NativeValue(), 1); + EXPECT_TRUE(value.Is()); + EXPECT_EQ(value.Get().NativeValue(), 1); } TEST_F(TypeReflectorTest, NewValueBuilder_UInt32Value) { @@ -321,8 +320,8 @@ TEST_F(TypeReflectorTest, NewValueBuilder_UInt32Value) { IsOkAndHolds(Optional( ErrorValueIs(StatusIs(absl::StatusCode::kOutOfRange))))); ASSERT_OK_AND_ASSIGN(auto value, std::move(*builder).Build()); - EXPECT_TRUE(InstanceOf(value)); - EXPECT_EQ(Cast(value).NativeValue(), 1); + EXPECT_TRUE(value.Is()); + EXPECT_EQ(value.Get().NativeValue(), 1); } TEST_F(TypeReflectorTest, NewValueBuilder_UInt64Value) { @@ -347,8 +346,8 @@ TEST_F(TypeReflectorTest, NewValueBuilder_UInt64Value) { IsOkAndHolds(Optional( ErrorValueIs(StatusIs(absl::StatusCode::kInvalidArgument))))); ASSERT_OK_AND_ASSIGN(auto value, std::move(*builder).Build()); - EXPECT_TRUE(InstanceOf(value)); - EXPECT_EQ(Cast(value).NativeValue(), 1); + EXPECT_TRUE(value.Is()); + EXPECT_EQ(value.Get().NativeValue(), 1); } TEST_F(TypeReflectorTest, NewValueBuilder_FloatValue) { @@ -373,8 +372,8 @@ TEST_F(TypeReflectorTest, NewValueBuilder_FloatValue) { IsOkAndHolds(Optional( ErrorValueIs(StatusIs(absl::StatusCode::kInvalidArgument))))); ASSERT_OK_AND_ASSIGN(auto value, std::move(*builder).Build()); - EXPECT_TRUE(InstanceOf(value)); - EXPECT_EQ(Cast(value).NativeValue(), 1); + EXPECT_TRUE(value.Is()); + EXPECT_EQ(value.Get().NativeValue(), 1); } TEST_F(TypeReflectorTest, NewValueBuilder_DoubleValue) { @@ -399,8 +398,8 @@ TEST_F(TypeReflectorTest, NewValueBuilder_DoubleValue) { IsOkAndHolds(Optional( ErrorValueIs(StatusIs(absl::StatusCode::kInvalidArgument))))); ASSERT_OK_AND_ASSIGN(auto value, std::move(*builder).Build()); - EXPECT_TRUE(InstanceOf(value)); - EXPECT_EQ(Cast(value).NativeValue(), 1); + EXPECT_TRUE(value.Is()); + EXPECT_EQ(value.Get().NativeValue(), 1); } TEST_F(TypeReflectorTest, NewValueBuilder_StringValue) { @@ -426,8 +425,8 @@ TEST_F(TypeReflectorTest, NewValueBuilder_StringValue) { IsOkAndHolds(Optional( ErrorValueIs(StatusIs(absl::StatusCode::kInvalidArgument))))); ASSERT_OK_AND_ASSIGN(auto value, std::move(*builder).Build()); - EXPECT_TRUE(InstanceOf(value)); - EXPECT_EQ(Cast(value).NativeString(), "foo"); + EXPECT_TRUE(value.Is()); + EXPECT_EQ(value.Get().NativeString(), "foo"); } TEST_F(TypeReflectorTest, NewValueBuilder_BytesValue) { @@ -453,8 +452,8 @@ TEST_F(TypeReflectorTest, NewValueBuilder_BytesValue) { IsOkAndHolds(Optional( ErrorValueIs(StatusIs(absl::StatusCode::kInvalidArgument))))); ASSERT_OK_AND_ASSIGN(auto value, std::move(*builder).Build()); - EXPECT_TRUE(InstanceOf(value)); - EXPECT_EQ(Cast(value).NativeString(), "foo"); + EXPECT_TRUE(value.Is()); + EXPECT_EQ(value.Get().NativeString(), "foo"); } TEST_F(TypeReflectorTest, NewValueBuilder_Duration) { @@ -497,8 +496,8 @@ TEST_F(TypeReflectorTest, NewValueBuilder_Duration) { IsOkAndHolds(Optional( ErrorValueIs(StatusIs(absl::StatusCode::kInvalidArgument))))); ASSERT_OK_AND_ASSIGN(auto value, std::move(*builder).Build()); - EXPECT_TRUE(InstanceOf(value)); - EXPECT_EQ(Cast(value).NativeValue(), + EXPECT_TRUE(value.Is()); + EXPECT_EQ(value.Get().NativeValue(), absl::Seconds(1) + absl::Nanoseconds(1)); } @@ -542,8 +541,8 @@ TEST_F(TypeReflectorTest, NewValueBuilder_Timestamp) { IsOkAndHolds(Optional( ErrorValueIs(StatusIs(absl::StatusCode::kInvalidArgument))))); ASSERT_OK_AND_ASSIGN(auto value, std::move(*builder).Build()); - EXPECT_TRUE(InstanceOf(value)); - EXPECT_EQ(Cast(value).NativeValue(), + EXPECT_TRUE(value.Is()); + EXPECT_EQ(value.Get().NativeValue(), absl::UnixEpoch() + absl::Seconds(1) + absl::Nanoseconds(1)); } @@ -584,8 +583,8 @@ TEST_F(TypeReflectorTest, NewValueBuilder_Any) { IsOkAndHolds(Optional( ErrorValueIs(StatusIs(absl::StatusCode::kInvalidArgument))))); ASSERT_OK_AND_ASSIGN(auto value, std::move(*builder).Build()); - EXPECT_TRUE(InstanceOf(value)); - EXPECT_EQ(Cast(value).NativeValue(), false); + EXPECT_TRUE(value.Is()); + EXPECT_EQ(value.Get().NativeValue(), false); } } // namespace diff --git a/common/values/custom_list_value.cc b/common/values/custom_list_value.cc index 303f49080..0e55115b0 100644 --- a/common/values/custom_list_value.cc +++ b/common/values/custom_list_value.cc @@ -24,7 +24,6 @@ #include "absl/status/statusor.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" -#include "common/casting.h" #include "common/native_type.h" #include "common/value.h" #include "common/values/list_value_builder.h" @@ -334,7 +333,7 @@ absl::Status CustomListValueInterface::Contains( [&](size_t index, const Value& element) -> absl::StatusOr { CEL_RETURN_IF_ERROR(element.Equal(other, descriptor_pool, message_factory, arena, &equal)); - if (auto bool_result = As(equal); + if (auto bool_result = equal.AsBool(); bool_result.has_value() && bool_result->NativeValue()) { outcome = BoolValue(true); return false; @@ -596,7 +595,7 @@ absl::Status CustomListValue::Contains( [&](size_t index, const Value& element) -> absl::StatusOr { CEL_RETURN_IF_ERROR(element.Equal(other, descriptor_pool, message_factory, arena, &equal)); - if (auto bool_result = As(equal); + if (auto bool_result = equal.As(); bool_result.has_value() && bool_result->NativeValue()) { outcome = BoolValue(true); return false; diff --git a/common/values/list_value_test.cc b/common/values/list_value_test.cc index 321c05249..11a2b865d 100644 --- a/common/values/list_value_test.cc +++ b/common/values/list_value_test.cc @@ -21,7 +21,6 @@ #include "absl/status/status.h" #include "absl/status/status_matchers.h" #include "absl/status/statusor.h" -#include "common/casting.h" #include "common/value.h" #include "common/value_testing.h" #include "internal/testing.h" @@ -91,16 +90,16 @@ TEST_F(ListValueTest, Get) { NewIntListValue(IntValue(0), IntValue(1), IntValue(2))); ASSERT_OK_AND_ASSIGN(auto element, value.Get(0, descriptor_pool(), message_factory(), arena())); - ASSERT_TRUE(InstanceOf(element)); - ASSERT_EQ(Cast(element).NativeValue(), 0); + ASSERT_TRUE(element.Is()); + ASSERT_EQ(element.Get().NativeValue(), 0); ASSERT_OK_AND_ASSIGN( element, value.Get(1, descriptor_pool(), message_factory(), arena())); - ASSERT_TRUE(InstanceOf(element)); - ASSERT_EQ(Cast(element).NativeValue(), 1); + ASSERT_TRUE(element.Is()); + ASSERT_EQ(element.Get().NativeValue(), 1); ASSERT_OK_AND_ASSIGN( element, value.Get(2, descriptor_pool(), message_factory(), arena())); - ASSERT_TRUE(InstanceOf(element)); - ASSERT_EQ(Cast(element).NativeValue(), 2); + ASSERT_TRUE(element.Is()); + ASSERT_EQ(element.Get().NativeValue(), 2); EXPECT_THAT( value.Get(3, descriptor_pool(), message_factory(), arena()), IsOkAndHolds(ErrorValueIs(StatusIs(absl::StatusCode::kInvalidArgument)))); @@ -112,7 +111,7 @@ TEST_F(ListValueTest, ForEach) { std::vector elements; EXPECT_THAT(value.ForEach( [&elements](const Value& element) { - elements.push_back(Cast(element).NativeValue()); + elements.push_back(element.Get().NativeValue()); return true; }, descriptor_pool(), message_factory(), arena()), @@ -126,12 +125,12 @@ TEST_F(ListValueTest, Contains) { ASSERT_OK_AND_ASSIGN(auto contained, value.Contains(IntValue(2), descriptor_pool(), message_factory(), arena())); - ASSERT_TRUE(InstanceOf(contained)); - EXPECT_TRUE(Cast(contained).NativeValue()); + ASSERT_TRUE(contained.Is()); + EXPECT_TRUE(contained.Get().NativeValue()); ASSERT_OK_AND_ASSIGN(contained, value.Contains(IntValue(3), descriptor_pool(), message_factory(), arena())); - ASSERT_TRUE(InstanceOf(contained)); - EXPECT_FALSE(Cast(contained).NativeValue()); + ASSERT_TRUE(contained.Is()); + EXPECT_FALSE(contained.Get().NativeValue()); } TEST_F(ListValueTest, NewIterator) { @@ -143,8 +142,8 @@ TEST_F(ListValueTest, NewIterator) { ASSERT_OK_AND_ASSIGN( auto element, iterator->Next(descriptor_pool(), message_factory(), arena())); - ASSERT_TRUE(InstanceOf(element)); - elements.push_back(Cast(element).NativeValue()); + ASSERT_TRUE(element.Is()); + elements.push_back(element.Get().NativeValue()); } EXPECT_EQ(iterator->HasNext(), false); EXPECT_THAT(iterator->Next(descriptor_pool(), message_factory(), arena()), diff --git a/common/values/map_value_test.cc b/common/values/map_value_test.cc index a1a82fd03..5b3deca27 100644 --- a/common/values/map_value_test.cc +++ b/common/values/map_value_test.cc @@ -22,7 +22,6 @@ #include "absl/status/status_matchers.h" #include "absl/status/statusor.h" #include "absl/types/optional.h" -#include "common/casting.h" #include "common/value.h" #include "common/value_testing.h" #include "internal/testing.h" @@ -139,16 +138,16 @@ TEST_F(MapValueTest, Get) { std::pair{IntValue(2), DoubleValue(5.0)})); ASSERT_OK_AND_ASSIGN(auto value, map_value.Get(IntValue(0), descriptor_pool(), message_factory(), arena())); - ASSERT_TRUE(InstanceOf(value)); - ASSERT_EQ(Cast(value).NativeValue(), 3.0); + ASSERT_TRUE(value.Is()); + ASSERT_EQ(value.Get().NativeValue(), 3.0); ASSERT_OK_AND_ASSIGN(value, map_value.Get(IntValue(1), descriptor_pool(), message_factory(), arena())); - ASSERT_TRUE(InstanceOf(value)); - ASSERT_EQ(Cast(value).NativeValue(), 4.0); + ASSERT_TRUE(value.Is()); + ASSERT_EQ(value.Get().NativeValue(), 4.0); ASSERT_OK_AND_ASSIGN(value, map_value.Get(IntValue(2), descriptor_pool(), message_factory(), arena())); - ASSERT_TRUE(InstanceOf(value)); - ASSERT_EQ(Cast(value).NativeValue(), 5.0); + ASSERT_TRUE(value.Is()); + ASSERT_EQ(value.Get().NativeValue(), 5.0); EXPECT_THAT( map_value.Get(IntValue(3), descriptor_pool(), message_factory(), arena()), IsOkAndHolds(ErrorValueIs(StatusIs(absl::StatusCode::kNotFound)))); @@ -164,18 +163,18 @@ TEST_F(MapValueTest, Find) { ASSERT_OK_AND_ASSIGN(entry, map_value.Find(IntValue(0), descriptor_pool(), message_factory(), arena())); ASSERT_TRUE(entry); - ASSERT_TRUE(InstanceOf(*entry)); - ASSERT_EQ(Cast(*entry).NativeValue(), 3.0); + ASSERT_TRUE(entry->Is()); + ASSERT_EQ(entry->Get().NativeValue(), 3.0); ASSERT_OK_AND_ASSIGN(entry, map_value.Find(IntValue(1), descriptor_pool(), message_factory(), arena())); ASSERT_TRUE(entry); - ASSERT_TRUE(InstanceOf(*entry)); - ASSERT_EQ(Cast(*entry).NativeValue(), 4.0); + ASSERT_TRUE(entry->Is()); + ASSERT_EQ(entry->Get().NativeValue(), 4.0); ASSERT_OK_AND_ASSIGN(entry, map_value.Find(IntValue(2), descriptor_pool(), message_factory(), arena())); ASSERT_TRUE(entry); - ASSERT_TRUE(InstanceOf(*entry)); - ASSERT_EQ(Cast(*entry).NativeValue(), 5.0); + ASSERT_TRUE(entry->Is()); + ASSERT_EQ(entry->Get().NativeValue(), 5.0); ASSERT_OK_AND_ASSIGN(entry, map_value.Find(IntValue(3), descriptor_pool(), message_factory(), arena())); ASSERT_FALSE(entry); @@ -189,20 +188,20 @@ TEST_F(MapValueTest, Has) { std::pair{IntValue(2), DoubleValue(5.0)})); ASSERT_OK_AND_ASSIGN(auto value, map_value.Has(IntValue(0), descriptor_pool(), message_factory(), arena())); - ASSERT_TRUE(InstanceOf(value)); - ASSERT_TRUE(Cast(value).NativeValue()); + ASSERT_TRUE(value.Is()); + ASSERT_TRUE(value.Get().NativeValue()); ASSERT_OK_AND_ASSIGN(value, map_value.Has(IntValue(1), descriptor_pool(), message_factory(), arena())); - ASSERT_TRUE(InstanceOf(value)); - ASSERT_TRUE(Cast(value).NativeValue()); + ASSERT_TRUE(value.Is()); + ASSERT_TRUE(value.Get().NativeValue()); ASSERT_OK_AND_ASSIGN(value, map_value.Has(IntValue(2), descriptor_pool(), message_factory(), arena())); - ASSERT_TRUE(InstanceOf(value)); - ASSERT_TRUE(Cast(value).NativeValue()); + ASSERT_TRUE(value.Is()); + ASSERT_TRUE(value.Get().NativeValue()); ASSERT_OK_AND_ASSIGN(value, map_value.Has(IntValue(3), descriptor_pool(), message_factory(), arena())); - ASSERT_TRUE(InstanceOf(value)); - ASSERT_FALSE(Cast(value).NativeValue()); + ASSERT_TRUE(value.Is()); + ASSERT_FALSE(value.Get().NativeValue()); } TEST_F(MapValueTest, ListKeys) { @@ -217,7 +216,7 @@ TEST_F(MapValueTest, ListKeys) { std::vector keys; ASSERT_THAT(list_keys.ForEach( [&keys](const Value& element) -> bool { - keys.push_back(Cast(element).NativeValue()); + keys.push_back(element.Get().NativeValue()); return true; }, descriptor_pool(), message_factory(), arena()), @@ -235,8 +234,8 @@ TEST_F(MapValueTest, ForEach) { EXPECT_THAT(value.ForEach( [&entries](const Value& key, const Value& value) { entries.push_back( - std::pair{Cast(key).NativeValue(), - Cast(value).NativeValue()}); + std::pair{key.Get().NativeValue(), + value.Get().NativeValue()}); return true; }, descriptor_pool(), message_factory(), arena()), @@ -258,8 +257,8 @@ TEST_F(MapValueTest, NewIterator) { ASSERT_OK_AND_ASSIGN( auto element, iterator->Next(descriptor_pool(), message_factory(), arena())); - ASSERT_TRUE(InstanceOf(element)); - keys.push_back(Cast(element).NativeValue()); + ASSERT_TRUE(element.Is()); + keys.push_back(element.Get().NativeValue()); } EXPECT_EQ(iterator->HasNext(), false); EXPECT_THAT(iterator->Next(descriptor_pool(), message_factory(), arena()), diff --git a/common/values/null_value_test.cc b/common/values/null_value_test.cc index f217ddac4..ac40fef20 100644 --- a/common/values/null_value_test.cc +++ b/common/values/null_value_test.cc @@ -16,8 +16,6 @@ #include "absl/status/status_matchers.h" #include "absl/strings/string_view.h" -#include "absl/types/optional.h" -#include "common/casting.h" #include "common/native_type.h" #include "common/value.h" #include "common/value_testing.h" @@ -27,8 +25,6 @@ namespace cel { namespace { using ::absl_testing::IsOk; -using ::testing::An; -using ::testing::Ne; using NullValueTest = common_internal::ValueTest<>; @@ -64,19 +60,5 @@ TEST_F(NullValueTest, NativeTypeId) { NativeTypeId::For()); } -TEST_F(NullValueTest, InstanceOf) { - EXPECT_TRUE(InstanceOf(NullValue())); - EXPECT_TRUE(InstanceOf(Value(NullValue()))); -} - -TEST_F(NullValueTest, Cast) { - EXPECT_THAT(Cast(NullValue()), An()); - EXPECT_THAT(Cast(Value(NullValue())), An()); -} - -TEST_F(NullValueTest, As) { - EXPECT_THAT(As(Value(NullValue())), Ne(std::nullopt)); -} - } // namespace } // namespace cel diff --git a/eval/compiler/BUILD b/eval/compiler/BUILD index 8df9912bf..e71c1cdf2 100644 --- a/eval/compiler/BUILD +++ b/eval/compiler/BUILD @@ -543,7 +543,6 @@ cc_library( ":flat_expr_builder_extensions", "//base:builtins", "//common:ast", - "//common:casting", "//common:expr", "//common:native_type", "//common:value", @@ -551,7 +550,6 @@ cc_library( "//eval/eval:direct_expression_step", "//eval/eval:evaluator_core", "//eval/eval:regex_match_step", - "//internal:casts", "//internal:re2_options", "//internal:status_macros", "@com_google_absl//absl/base:nullability", @@ -559,7 +557,6 @@ cc_library( "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", - "@com_google_absl//absl/types:optional", "@com_googlesource_code_re2//:re2", ], ) diff --git a/eval/compiler/regex_precompilation_optimization.cc b/eval/compiler/regex_precompilation_optimization.cc index fa0d289f2..bd3a841ed 100644 --- a/eval/compiler/regex_precompilation_optimization.cc +++ b/eval/compiler/regex_precompilation_optimization.cc @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -26,10 +27,8 @@ #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/strings/string_view.h" -#include "absl/types/optional.h" #include "base/builtins.h" #include "common/ast.h" -#include "common/casting.h" #include "common/expr.h" #include "common/native_type.h" #include "common/value.h" @@ -38,7 +37,6 @@ #include "eval/eval/direct_expression_step.h" #include "eval/eval/evaluator_core.h" #include "eval/eval/regex_match_step.h" -#include "internal/casts.h" #include "internal/re2_options.h" #include "internal/status_macros.h" #include "re2/re2.h" @@ -48,14 +46,10 @@ namespace { using ::cel::Ast; using ::cel::CallExpr; -using ::cel::Cast; using ::cel::Expr; -using ::cel::InstanceOf; using ::cel::NativeTypeId; using ::cel::Reference; -using ::cel::StringValue; using ::cel::Value; -using ::cel::internal::down_cast; using ReferenceMap = absl::flat_hash_map; @@ -202,8 +196,8 @@ class RegexPrecompilationOptimization : public ProgramOptimizer { } } - if (constant.has_value() && InstanceOf(*constant)) { - return Cast(*constant).ToString(); + if (constant.has_value() && constant->IsString()) { + return constant->GetString().ToString(); } return std::nullopt; diff --git a/eval/eval/BUILD b/eval/eval/BUILD index 66d5092ab..6338b9461 100644 --- a/eval/eval/BUILD +++ b/eval/eval/BUILD @@ -63,7 +63,6 @@ cc_library( "//base:attributes", "//base:builtins", "//base:data", - "//common:casting", "//common:expr", "//common:function_descriptor", "//common:kind", @@ -252,7 +251,6 @@ cc_library( ":evaluator_core", ":expression_step_base", "//base:attributes", - "//common:casting", "//common:expr", "//common:kind", "//common:value", @@ -349,7 +347,6 @@ cc_library( ":direct_expression_step", ":evaluator_core", ":expression_step_base", - "//common:casting", "//common:expr", "//common:value", "//internal:status_macros", @@ -373,7 +370,6 @@ cc_library( ":direct_expression_step", ":evaluator_core", ":expression_step_base", - "//common:casting", "//common:value", "//internal:status_macros", "@com_google_absl//absl/container:flat_hash_set", @@ -397,7 +393,6 @@ cc_library( ":direct_expression_step", ":evaluator_core", ":expression_step_base", - "//common:casting", "//common:value", "//internal:status_macros", "@com_google_absl//absl/container:flat_hash_set", @@ -584,9 +579,7 @@ cc_test( ":attribute_trail", ":cel_expression_flat_impl", ":evaluator_core", - ":ident_step", "//base:data", - "//common:casting", "//common:memory", "//common:value", "//eval/public:activation", @@ -662,10 +655,8 @@ cc_test( ":const_value_step", ":direct_expression_step", ":evaluator_core", - ":ident_step", "//base:attributes", "//base:data", - "//common:casting", "//common:expr", "//common:unknown", "//common:value", @@ -703,11 +694,9 @@ cc_test( ":cel_expression_flat_impl", ":const_value_step", ":evaluator_core", - ":ident_step", ":select_step", "//base:attributes", "//base:data", - "//common:casting", "//common:expr", "//common:legacy_value", "//common:type", @@ -761,10 +750,8 @@ cc_test( ":create_list_step", ":direct_expression_step", ":evaluator_core", - ":ident_step", "//base:attributes", "//base:data", - "//common:casting", "//common:expr", "//common:value", "//common:value_testing", @@ -900,7 +887,6 @@ cc_library( "//base:function_result", "//base:function_result_set", "//base/internal:unknown_set", - "//common:casting", "//common:function_descriptor", "//common:unknown", "//common:value", @@ -972,18 +958,15 @@ cc_test( ":const_value_step", ":direct_expression_step", ":evaluator_core", - ":ident_step", ":ternary_step", "//base:attributes", "//base:data", - "//common:casting", "//common:expr", "//common:value", "//eval/public:activation", "//eval/public:cel_value", "//eval/public:unknown_attribute_set", "//eval/public:unknown_set", - "//internal:status_macros", "//internal:testing", "//internal:testing_descriptor_pool", "//internal:testing_message_factory", @@ -1130,8 +1113,6 @@ cc_library( ":evaluator_core", ":expression_step_base", ":expression_step_logic", - "//common:casting", - "//common:optional_ref", "//common:value", "//internal:status_macros", "//runtime/internal:errors", @@ -1151,7 +1132,6 @@ cc_test( ":direct_expression_step", ":evaluator_core", ":optional_or_step", - "//common:casting", "//common:value", "//common:value_kind", "//common:value_testing", diff --git a/eval/eval/attribute_utility.cc b/eval/eval/attribute_utility.cc index af63e9f91..a0e57161a 100644 --- a/eval/eval/attribute_utility.cc +++ b/eval/eval/attribute_utility.cc @@ -1,6 +1,7 @@ #include "eval/eval/attribute_utility.h" #include +#include #include #include @@ -12,7 +13,6 @@ #include "base/function_result.h" #include "base/function_result_set.h" #include "base/internal/unknown_set.h" -#include "common/casting.h" #include "common/function_descriptor.h" #include "common/unknown.h" #include "common/value.h" @@ -27,11 +27,9 @@ namespace google::api::expr::runtime { using ::cel::Attribute; using ::cel::AttributePattern; using ::cel::AttributeSet; -using ::cel::Cast; using ::cel::ErrorValue; using ::cel::FunctionResult; using ::cel::FunctionResultSet; -using ::cel::InstanceOf; using ::cel::UnknownValue; using ::cel::Value; using ::cel::base_internal::UnknownSet; diff --git a/eval/eval/comprehension_step.cc b/eval/eval/comprehension_step.cc index 6293f97f4..661a8f2e4 100644 --- a/eval/eval/comprehension_step.cc +++ b/eval/eval/comprehension_step.cc @@ -13,7 +13,6 @@ #include "absl/status/status.h" #include "absl/status/statusor.h" #include "base/attribute.h" -#include "common/casting.h" #include "common/value.h" #include "common/value_kind.h" #include "eval/eval/attribute_trail.h" @@ -33,8 +32,6 @@ enum class IterableKind { }; using ::cel::AttributeQualifier; -using ::cel::Cast; -using ::cel::InstanceOf; using ::cel::UnknownValue; using ::cel::Value; using ::cel::ValueIterator; diff --git a/eval/eval/container_access_step.cc b/eval/eval/container_access_step.cc index f4a33b217..dc1efbd12 100644 --- a/eval/eval/container_access_step.cc +++ b/eval/eval/container_access_step.cc @@ -11,7 +11,6 @@ #include "absl/types/optional.h" #include "absl/types/span.h" #include "base/attribute.h" -#include "common/casting.h" #include "common/expr.h" #include "common/kind.h" #include "common/value.h" @@ -31,9 +30,7 @@ namespace google::api::expr::runtime { namespace { using ::cel::AttributeQualifier; -using ::cel::Cast; using ::cel::ErrorValue; -using ::cel::InstanceOf; using ::cel::IntValue; using ::cel::ListValue; using ::cel::MapValue; @@ -170,7 +167,7 @@ void LookupInList(const ListValue& cel_list, const Value& key, if (number.has_value() && number->LosslessConvertibleToInt()) { maybe_idx = number->AsInt(); } - } else if (InstanceOf(key)) { + } else if (key.IsInt()) { maybe_idx = key.GetInt().NativeValue(); } @@ -212,11 +209,11 @@ void LookupInContainer(const Value& container, const Value& key, // Select steps can be applied to either maps or messages switch (container.kind()) { case ValueKind::kMap: { - LookupInMap(Cast(container), key, frame, result); + LookupInMap(container.GetMap(), key, frame, result); return; } case ValueKind::kList: { - LookupInList(Cast(container), key, frame, result); + LookupInList(container.GetList(), key, frame, result); return; } default: @@ -252,11 +249,11 @@ void PerformLookup(ExecutionFrameBase& frame, const Value& container, } } - if (InstanceOf(container)) { + if (container.IsError()) { result = container; return; } - if (InstanceOf(key)) { + if (key.IsError()) { result = key; return; } @@ -270,7 +267,7 @@ void PerformLookup(ExecutionFrameBase& frame, const Value& container, Value value; optional_value.Value(&value); LookupInContainer(value, key, frame, result); - if (auto error_value = cel::As(result); + if (auto error_value = result.AsError(); error_value && cel::IsNoSuchKey(*error_value)) { result = cel::OptionalValue::None(); return; diff --git a/eval/eval/create_list_step.cc b/eval/eval/create_list_step.cc index 9e8344fcf..8a7470fa7 100644 --- a/eval/eval/create_list_step.cc +++ b/eval/eval/create_list_step.cc @@ -10,7 +10,6 @@ #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/types/optional.h" -#include "common/casting.h" #include "common/expr.h" #include "common/value.h" #include "common/values/list_value_builder.h" @@ -25,9 +24,7 @@ namespace google::api::expr::runtime { namespace { -using ::cel::Cast; using ::cel::ErrorValue; -using ::cel::InstanceOf; using ::cel::ListValueBuilderPtr; using ::cel::UnknownValue; using ::cel::Value; diff --git a/eval/eval/create_list_step_test.cc b/eval/eval/create_list_step_test.cc index 4475c1fb2..119504a14 100644 --- a/eval/eval/create_list_step_test.cc +++ b/eval/eval/create_list_step_test.cc @@ -14,7 +14,6 @@ #include "base/attribute.h" #include "base/attribute_set.h" #include "base/type_provider.h" -#include "common/casting.h" #include "common/expr.h" #include "common/value.h" #include "common/value_testing.h" @@ -51,12 +50,9 @@ using ::absl_testing::StatusIs; using ::cel::Attribute; using ::cel::AttributeQualifier; using ::cel::AttributeSet; -using ::cel::Cast; using ::cel::ErrorValue; using ::cel::Expr; -using ::cel::InstanceOf; using ::cel::IntValue; -using ::cel::ListValue; using ::cel::TypeProvider; using ::cel::UnknownValue; using ::cel::Value; @@ -289,8 +285,8 @@ TEST(CreateDirectListStep, Basic) { ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_THAT(Cast(result).Size(), IsOkAndHolds(2)); + ASSERT_TRUE(result.IsList()); + EXPECT_THAT(result.GetList().Size(), IsOkAndHolds(2)); } TEST(CreateDirectListStep, ForwardFirstError) { @@ -317,8 +313,8 @@ TEST(CreateDirectListStep, ForwardFirstError) { ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_THAT(Cast(result).NativeValue(), + ASSERT_TRUE(result.IsError()); + EXPECT_THAT(result.GetError().NativeValue(), StatusIs(absl::StatusCode::kInternal, "test1")); } @@ -366,8 +362,8 @@ TEST(CreateDirectListStep, MergeUnknowns) { ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_THAT(UnknownAttrNames(Cast(result)), + ASSERT_TRUE(result.IsUnknown()); + EXPECT_THAT(UnknownAttrNames(result.GetUnknown()), UnorderedElementsAre("var1", "var2")); } @@ -397,8 +393,8 @@ TEST(CreateDirectListStep, ErrorBeforeUnknown) { ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_THAT(Cast(result).NativeValue(), + ASSERT_TRUE(result.IsError()); + EXPECT_THAT(result.GetError().NativeValue(), StatusIs(absl::StatusCode::kInternal, "test1")); } @@ -445,9 +441,9 @@ TEST(CreateDirectListStep, MissingAttribute) { ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); + ASSERT_TRUE(result.IsError()); EXPECT_THAT( - Cast(result).NativeValue(), + result.GetError().NativeValue(), StatusIs(absl::StatusCode::kInvalidArgument, HasSubstr("var1.field1"))); } @@ -474,8 +470,8 @@ TEST(CreateDirectListStep, OptionalPresentSet) { ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - auto list = Cast(result); + ASSERT_TRUE(result.IsList()); + auto list = result.GetList(); EXPECT_THAT(list.Size(), IsOkAndHolds(2)); EXPECT_THAT(list.Get(0, cel::internal::GetTestingDescriptorPool(), cel::internal::GetTestingMessageFactory(), &arena), @@ -507,8 +503,8 @@ TEST(CreateDirectListStep, OptionalAbsentNotSet) { ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - auto list = Cast(result); + ASSERT_TRUE(result.IsList()); + auto list = result.GetList(); EXPECT_THAT(list.Size(), IsOkAndHolds(1)); EXPECT_THAT(list.Get(0, cel::internal::GetTestingDescriptorPool(), cel::internal::GetTestingMessageFactory(), &arena), @@ -540,8 +536,8 @@ TEST(CreateDirectListStep, PartialUnknown) { ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_THAT(UnknownAttrNames(Cast(result)), + ASSERT_TRUE(result.IsUnknown()); + EXPECT_THAT(UnknownAttrNames(result.GetUnknown()), UnorderedElementsAre("var1")); } diff --git a/eval/eval/create_map_step.cc b/eval/eval/create_map_step.cc index f8f099047..15e696782 100644 --- a/eval/eval/create_map_step.cc +++ b/eval/eval/create_map_step.cc @@ -25,7 +25,6 @@ #include "absl/status/statusor.h" #include "absl/strings/string_view.h" #include "absl/types/optional.h" -#include "common/casting.h" #include "common/value.h" #include "common/values/map_value_builder.h" #include "eval/eval/attribute_trail.h" @@ -38,11 +37,9 @@ namespace google::api::expr::runtime { namespace { -using ::cel::Cast; using ::cel::ErrorValue; using ::cel::ErrorValueAssign; using ::cel::ErrorValueReturn; -using ::cel::InstanceOf; using ::cel::MapValueBuilderPtr; using ::cel::UnknownValue; using ::cel::Value; diff --git a/eval/eval/create_struct_step.cc b/eval/eval/create_struct_step.cc index 3ac6caabe..8fcdd3c5a 100644 --- a/eval/eval/create_struct_step.cc +++ b/eval/eval/create_struct_step.cc @@ -26,7 +26,6 @@ #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" #include "absl/types/optional.h" -#include "common/casting.h" #include "common/value.h" #include "eval/eval/attribute_trail.h" #include "eval/eval/direct_expression_step.h" @@ -38,9 +37,7 @@ namespace google::api::expr::runtime { namespace { -using ::cel::Cast; using ::cel::ErrorValue; -using ::cel::InstanceOf; using ::cel::StructValueBuilderInterface; using ::cel::UnknownValue; using ::cel::Value; diff --git a/eval/eval/function_step.cc b/eval/eval/function_step.cc index 91a9fa87f..f3d3fb93a 100644 --- a/eval/eval/function_step.cc +++ b/eval/eval/function_step.cc @@ -16,7 +16,6 @@ #include "absl/strings/string_view.h" #include "absl/types/optional.h" #include "absl/types/span.h" -#include "common/casting.h" #include "common/expr.h" #include "common/function_descriptor.h" #include "common/kind.h" @@ -179,7 +178,7 @@ Value NoOverloadResult(absl::string_view name, // should be propagated along execution path. for (size_t i = 0; i < args.size(); i++) { const auto& arg = args[i]; - if (cel::InstanceOf(arg)) { + if (arg.IsError()) { return arg; } } diff --git a/eval/eval/ident_step_test.cc b/eval/eval/ident_step_test.cc index 91b73a0fd..f27eaf477 100644 --- a/eval/eval/ident_step_test.cc +++ b/eval/eval/ident_step_test.cc @@ -7,7 +7,6 @@ #include "absl/status/status.h" #include "base/type_provider.h" -#include "common/casting.h" #include "common/memory.h" #include "common/value.h" #include "eval/eval/attribute_trail.h" @@ -31,9 +30,7 @@ namespace { using ::absl_testing::IsOk; using ::absl_testing::StatusIs; -using ::cel::Cast; using ::cel::ErrorValue; -using ::cel::InstanceOf; using ::cel::IntValue; using ::cel::MemoryManagerRef; using ::cel::RuntimeOptions; @@ -224,8 +221,8 @@ TEST(DirectIdentStepTest, Basic) { ASSERT_THAT(step->Evaluate(frame, result, trail), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_THAT(Cast(result).NativeValue(), Eq(42)); + ASSERT_TRUE(result.IsInt()); + EXPECT_THAT(result.GetInt().NativeValue(), Eq(42)); } TEST(DirectIdentStepTest, UnknownAttribute) { @@ -249,8 +246,8 @@ TEST(DirectIdentStepTest, UnknownAttribute) { ASSERT_THAT(step->Evaluate(frame, result, trail), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_THAT(Cast(result).ToAttributeSet(), SizeIs(1)); + ASSERT_TRUE(result.IsUnknown()); + EXPECT_THAT(result.GetUnknown().ToAttributeSet(), SizeIs(1)); } TEST(DirectIdentStepTest, MissingAttribute) { @@ -274,8 +271,8 @@ TEST(DirectIdentStepTest, MissingAttribute) { ASSERT_THAT(step->Evaluate(frame, result, trail), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_THAT(Cast(result).NativeValue(), + ASSERT_TRUE(result.IsError()); + EXPECT_THAT(result.GetError().NativeValue(), StatusIs(absl::StatusCode::kInvalidArgument, HasSubstr("var1"))); } @@ -296,8 +293,8 @@ TEST(DirectIdentStepTest, NotFound) { ASSERT_THAT(step->Evaluate(frame, result, trail), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_THAT(Cast(result).NativeValue(), + ASSERT_TRUE(result.IsError()); + EXPECT_THAT(result.GetError().NativeValue(), StatusIs(absl::StatusCode::kUnknown, HasSubstr("\"var1\" found in Activation"))); } diff --git a/eval/eval/logic_step.cc b/eval/eval/logic_step.cc index bd6803059..d7bab5f8c 100644 --- a/eval/eval/logic_step.cc +++ b/eval/eval/logic_step.cc @@ -10,7 +10,6 @@ #include "absl/strings/string_view.h" #include "absl/types/span.h" #include "base/builtins.h" -#include "common/casting.h" #include "common/value.h" #include "common/value_kind.h" #include "eval/eval/attribute_trail.h" @@ -25,9 +24,7 @@ namespace google::api::expr::runtime { namespace { using ::cel::BoolValue; -using ::cel::Cast; using ::cel::ErrorValue; -using ::cel::InstanceOf; using ::cel::UnknownValue; using ::cel::Value; using ::cel::ValueKind; @@ -45,7 +42,7 @@ absl::Status ReturnLogicResult(ExecutionFrameBase& frame, BoolLogicKind op_type, if (frame.unknown_processing_enabled()) { if (lhs_kind == ValueKind::kUnknown && rhs_kind == ValueKind::kUnknown) { lhs_result = frame.attribute_utility().MergeUnknownValues( - Cast(lhs_result), Cast(rhs_result)); + lhs_result.GetUnknown(), rhs_result.GetUnknown()); // Clear attribute trail so this doesn't get re-identified as a new // unknown and reset the accumulated attributes. attribute_trail = AttributeTrail(); @@ -112,7 +109,7 @@ absl::Status ExhaustiveDirectLogicStep::Evaluate( ValueKind rhs_kind = rhs_result.kind(); if (lhs_kind == ValueKind::kBool) { - bool lhs_bool = Cast(result).NativeValue(); + bool lhs_bool = result.GetBool().NativeValue(); if ((op_type_ == BoolLogicKind::kOr && lhs_bool) || (op_type_ == BoolLogicKind::kAnd && !lhs_bool)) { return absl::OkStatus(); @@ -120,7 +117,7 @@ absl::Status ExhaustiveDirectLogicStep::Evaluate( } if (rhs_kind == ValueKind::kBool) { - bool rhs_bool = Cast(rhs_result).NativeValue(); + bool rhs_bool = rhs_result.GetBool().NativeValue(); if ((op_type_ == BoolLogicKind::kOr && rhs_bool) || (op_type_ == BoolLogicKind::kAnd && !rhs_bool)) { result = std::move(rhs_result); @@ -157,7 +154,7 @@ absl::Status DirectLogicStep::Evaluate(ExecutionFrameBase& frame, Value& result, CEL_RETURN_IF_ERROR(lhs_->Evaluate(frame, result, attribute_trail)); ValueKind lhs_kind = result.kind(); if (lhs_kind == ValueKind::kBool) { - bool lhs_bool = Cast(result).NativeValue(); + bool lhs_bool = result.GetBool().NativeValue(); if ((op_type_ == BoolLogicKind::kOr && lhs_bool) || (op_type_ == BoolLogicKind::kAnd && !lhs_bool)) { return absl::OkStatus(); @@ -172,7 +169,7 @@ absl::Status DirectLogicStep::Evaluate(ExecutionFrameBase& frame, Value& result, ValueKind rhs_kind = rhs_result.kind(); if (rhs_kind == ValueKind::kBool) { - bool rhs_bool = Cast(rhs_result).NativeValue(); + bool rhs_bool = rhs_result.GetBool().NativeValue(); if ((op_type_ == BoolLogicKind::kOr && rhs_bool) || (op_type_ == BoolLogicKind::kAnd && !rhs_bool)) { result = std::move(rhs_result); diff --git a/eval/eval/logic_step_test.cc b/eval/eval/logic_step_test.cc index b289e0e0a..04b9fd127 100644 --- a/eval/eval/logic_step_test.cc +++ b/eval/eval/logic_step_test.cc @@ -14,7 +14,6 @@ #include "base/attribute.h" #include "base/attribute_set.h" #include "base/type_provider.h" -#include "common/casting.h" #include "common/expr.h" #include "common/unknown.h" #include "common/value.h" @@ -48,9 +47,7 @@ using ::absl_testing::IsOk; using ::cel::Attribute; using ::cel::AttributeSet; using ::cel::BoolValue; -using ::cel::Cast; using ::cel::Expr; -using ::cel::InstanceOf; using ::cel::IntValue; using ::cel::TypeProvider; using ::cel::UnknownValue; diff --git a/eval/eval/optional_or_step.cc b/eval/eval/optional_or_step.cc index 5ac1e8400..983f229b1 100644 --- a/eval/eval/optional_or_step.cc +++ b/eval/eval/optional_or_step.cc @@ -22,8 +22,6 @@ #include "absl/base/optimization.h" #include "absl/status/status.h" #include "absl/types/span.h" -#include "common/casting.h" -#include "common/optional_ref.h" #include "common/value.h" #include "eval/eval/attribute_trail.h" #include "eval/eval/direct_expression_step.h" @@ -38,11 +36,7 @@ namespace google::api::expr::runtime { namespace { -using ::cel::As; using ::cel::ErrorValue; -using ::cel::InstanceOf; -using ::cel::OptionalValue; -using ::cel::UnknownValue; using ::cel::Value; using ::cel::runtime_internal::CreateNoMatchingOverloadError; @@ -78,13 +72,13 @@ absl::Status EvalOptionalOr(OptionalOrKind kind, const Value& lhs, const Value& rhs, const AttributeTrail& lhs_attr, const AttributeTrail& rhs_attr, Value& result, AttributeTrail& result_attr, google::protobuf::Arena* arena) { - if (InstanceOf(lhs) || InstanceOf(lhs)) { + if (lhs.IsError() || lhs.IsUnknown()) { result = lhs; result_attr = lhs_attr; return absl::OkStatus(); } - auto lhs_optional_value = As(lhs); + auto lhs_optional_value = lhs.AsOptional(); if (!lhs_optional_value.has_value()) { result = MakeNoOverloadError(kind, arena); result_attr = AttributeTrail(); @@ -101,8 +95,8 @@ absl::Status EvalOptionalOr(OptionalOrKind kind, const Value& lhs, return absl::OkStatus(); } - if (kind == OptionalOrKind::kOrOptional && !InstanceOf(rhs) && - !InstanceOf(rhs) && !InstanceOf(rhs)) { + if (kind == OptionalOrKind::kOrOptional && !rhs.IsError() && + !rhs.IsUnknown() && !rhs.IsOptional()) { result = MakeNoOverloadError(kind, arena); result_attr = AttributeTrail(); return absl::OkStatus(); @@ -193,13 +187,13 @@ absl::Status DirectOptionalOrStep::Evaluate(ExecutionFrameBase& frame, AttributeTrail& attribute) const { CEL_RETURN_IF_ERROR(optional_->Evaluate(frame, result, attribute)); - if (InstanceOf(result) || InstanceOf(result)) { + if (result.IsUnknown() || result.IsError()) { // Forward the lhs error instead of attempting to evaluate the alternative // (unlike CEL's commutative logic operators). return absl::OkStatus(); } - auto optional_value = As(static_cast(result)); + auto optional_value = result.AsOptional(); if (!optional_value.has_value()) { result = MakeNoOverloadError(kind_, frame.arena()); return absl::OkStatus(); @@ -218,8 +212,7 @@ absl::Status DirectOptionalOrStep::Evaluate(ExecutionFrameBase& frame, // // Otherwise, we don't know what type to expect so can't check anything. if (kind_ == OptionalOrKind::kOrOptional) { - if (!InstanceOf(result) && !InstanceOf(result) && - !InstanceOf(result)) { + if (!result.IsOptional() && !result.IsError() && !result.IsUnknown()) { result = MakeNoOverloadError(kind_, frame.arena()); } } @@ -235,15 +228,13 @@ void OptionalHasValueJumpStep::Evaluate(ExecutionFrame* frame) const { return; } const Value& value = frame->value_stack().Peek(); - cel::optional_ref optional_value = - As(value); + auto optional_value = value.AsOptional(); // We jump if the receiver is `optional_type` which has a value or the // receiver is an error/unknown. Unlike `_||_` we are not commutative. If // we run into an error/unknown, we skip the `else` branch. const bool should_jump = (optional_value.has_value() && optional_value->HasValue()) || - (!optional_value.has_value() && (cel::InstanceOf(value) || - cel::InstanceOf(value))); + (!optional_value.has_value() && (value.IsError() || value.IsUnknown())); if (should_jump) { if (is_or_value_ && optional_value.has_value()) { frame->value_stack().PopAndPush(optional_value->Value()); diff --git a/eval/eval/optional_or_step_test.cc b/eval/eval/optional_or_step_test.cc index 2962ee38c..f0641ca6c 100644 --- a/eval/eval/optional_or_step_test.cc +++ b/eval/eval/optional_or_step_test.cc @@ -18,7 +18,6 @@ #include "absl/memory/memory.h" #include "absl/status/status.h" -#include "common/casting.h" #include "common/value.h" #include "common/value_kind.h" #include "common/value_testing.h" @@ -40,9 +39,7 @@ namespace { using ::absl_testing::StatusIs; using ::cel::Activation; -using ::cel::As; using ::cel::ErrorValue; -using ::cel::InstanceOf; using ::cel::IntValue; using ::cel::OptionalValue; using ::cel::RuntimeOptions; diff --git a/eval/eval/select_step_test.cc b/eval/eval/select_step_test.cc index 3ab35fd63..a1e5b4a09 100644 --- a/eval/eval/select_step_test.cc +++ b/eval/eval/select_step_test.cc @@ -17,7 +17,6 @@ #include "base/attribute.h" #include "base/attribute_set.h" #include "base/type_provider.h" -#include "common/casting.h" #include "common/expr.h" #include "common/legacy_value.h" #include "common/type.h" @@ -62,10 +61,8 @@ using ::cel::Attribute; using ::cel::AttributeQualifier; using ::cel::AttributeSet; using ::cel::BoolValue; -using ::cel::Cast; using ::cel::ErrorValue; using ::cel::Expr; -using ::cel::InstanceOf; using ::cel::IntValue; using ::cel::OptionalValue; using ::cel::RuntimeOptions; @@ -1140,9 +1137,9 @@ TEST_F(DirectSelectStepTest, SelectFromMap) { AttributeTrail attr; ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); + ASSERT_TRUE(result.IsInt()); - EXPECT_EQ(Cast(result).NativeValue(), 1); + EXPECT_EQ(result.GetInt().NativeValue(), 1); } TEST_F(DirectSelectStepTest, HasMap) { @@ -1171,9 +1168,9 @@ TEST_F(DirectSelectStepTest, HasMap) { AttributeTrail attr; ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); + ASSERT_TRUE(result.GetBool()); - EXPECT_TRUE(Cast(result).NativeValue()); + EXPECT_TRUE(result.GetBool().NativeValue()); } TEST_F(DirectSelectStepTest, SelectFromOptionalMap) { @@ -1204,9 +1201,8 @@ TEST_F(DirectSelectStepTest, SelectFromOptionalMap) { AttributeTrail attr; ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_THAT(Cast(static_cast(result)).Value(), - IntValueIs(1)); + ASSERT_TRUE(result.IsOptional()); + EXPECT_THAT(result.GetOptional().Value(), IntValueIs(1)); } TEST_F(DirectSelectStepTest, SelectFromOptionalMapAbsent) { @@ -1237,9 +1233,8 @@ TEST_F(DirectSelectStepTest, SelectFromOptionalMapAbsent) { AttributeTrail attr; ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_FALSE( - Cast(static_cast(result)).HasValue()); + ASSERT_TRUE(result.IsOptional()); + EXPECT_FALSE(result.GetOptional().HasValue()); } TEST_F(DirectSelectStepTest, SelectFromOptionalStruct) { @@ -1272,9 +1267,8 @@ TEST_F(DirectSelectStepTest, SelectFromOptionalStruct) { AttributeTrail attr; ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_THAT(Cast(static_cast(result)).Value(), - IntValueIs(1)); + ASSERT_TRUE(result.IsOptional()); + EXPECT_THAT(result.GetOptional().Value(), IntValueIs(1)); } TEST_F(DirectSelectStepTest, SelectFromOptionalStructFieldNotSet) { @@ -1307,9 +1301,8 @@ TEST_F(DirectSelectStepTest, SelectFromOptionalStructFieldNotSet) { AttributeTrail attr; ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_FALSE( - Cast(static_cast(result)).HasValue()); + ASSERT_TRUE(result.IsOptional()); + EXPECT_FALSE(result.GetOptional().HasValue()); } TEST_F(DirectSelectStepTest, SelectFromEmptyOptional) { @@ -1332,9 +1325,8 @@ TEST_F(DirectSelectStepTest, SelectFromEmptyOptional) { AttributeTrail attr; ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_FALSE( - cel::Cast(static_cast(result)).HasValue()); + ASSERT_TRUE(result.IsOptional()); + EXPECT_FALSE(result.GetOptional().HasValue()); } TEST_F(DirectSelectStepTest, HasOptional) { @@ -1365,9 +1357,9 @@ TEST_F(DirectSelectStepTest, HasOptional) { AttributeTrail attr; ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); + ASSERT_TRUE(result.IsBool()); - EXPECT_TRUE(Cast(result).NativeValue()); + EXPECT_TRUE(result.GetBool().NativeValue()); } TEST_F(DirectSelectStepTest, HasEmptyOptional) { @@ -1390,9 +1382,9 @@ TEST_F(DirectSelectStepTest, HasEmptyOptional) { AttributeTrail attr; ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); + ASSERT_TRUE(result.IsBool()); - EXPECT_FALSE(Cast(result).NativeValue()); + EXPECT_FALSE(result.GetBool().NativeValue()); } TEST_F(DirectSelectStepTest, SelectFromStruct) { @@ -1416,9 +1408,9 @@ TEST_F(DirectSelectStepTest, SelectFromStruct) { AttributeTrail attr; ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); + ASSERT_TRUE(result.IsInt()); - EXPECT_EQ(Cast(result).NativeValue(), 1); + EXPECT_EQ(result.GetInt().NativeValue(), 1); } TEST_F(DirectSelectStepTest, HasStruct) { @@ -1444,8 +1436,8 @@ TEST_F(DirectSelectStepTest, HasStruct) { // has(test_all_types.single_string) ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_FALSE(Cast(result).NativeValue()); + ASSERT_TRUE(result.IsBool()); + EXPECT_FALSE(result.GetBool().NativeValue()); } TEST_F(DirectSelectStepTest, SelectFromUnsupportedType) { @@ -1467,9 +1459,9 @@ TEST_F(DirectSelectStepTest, SelectFromUnsupportedType) { AttributeTrail attr; ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); + ASSERT_TRUE(result.IsError()); - EXPECT_THAT(Cast(result).NativeValue(), + EXPECT_THAT(result.GetError().NativeValue(), StatusIs(absl::StatusCode::kInvalidArgument, HasSubstr("Applying SELECT to non-message type"))); } @@ -1496,8 +1488,8 @@ TEST_F(DirectSelectStepTest, AttributeUpdatedIfRequested) { AttributeTrail attr; ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_EQ(Cast(result).NativeValue(), 1); + ASSERT_TRUE(result.IsInt()); + EXPECT_EQ(result.GetInt().NativeValue(), 1); ASSERT_OK_AND_ASSIGN(std::string attr_str, attr.attribute().AsString()); EXPECT_EQ(attr_str, "test_all_types.single_int64"); @@ -1528,8 +1520,8 @@ TEST_F(DirectSelectStepTest, MissingAttributesToErrors) { AttributeTrail attr; ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_THAT(Cast(result).NativeValue(), + ASSERT_TRUE(result.IsError()); + EXPECT_THAT(result.GetError().NativeValue(), StatusIs(absl::StatusCode::kInvalidArgument, HasSubstr("test_all_types.single_int64"))); } @@ -1559,9 +1551,9 @@ TEST_F(DirectSelectStepTest, IdentifiesUnknowns) { AttributeTrail attr; ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); + ASSERT_TRUE(result.IsUnknown()); - EXPECT_THAT(AttributeStrings(Cast(result)), + EXPECT_THAT(AttributeStrings(result.GetUnknown()), UnorderedElementsAre("test_all_types.single_int64")); } @@ -1585,8 +1577,8 @@ TEST_F(DirectSelectStepTest, ForwardErrorValue) { AttributeTrail attr; ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_THAT(Cast(result).NativeValue(), + ASSERT_TRUE(result.IsError()); + EXPECT_THAT(result.GetError().NativeValue(), StatusIs(absl::StatusCode::kInternal, HasSubstr("test1"))); } @@ -1616,8 +1608,8 @@ TEST_F(DirectSelectStepTest, ForwardUnknownOperand) { AttributeTrail attr; ASSERT_THAT(step->Evaluate(frame, result, attr), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_THAT(AttributeStrings(Cast(result)), + ASSERT_TRUE(result.IsUnknown()); + EXPECT_THAT(AttributeStrings(result.GetUnknown()), UnorderedElementsAre("attr[0]")); } diff --git a/eval/eval/ternary_step_test.cc b/eval/eval/ternary_step_test.cc index cedab0ca2..2d49700cc 100644 --- a/eval/eval/ternary_step_test.cc +++ b/eval/eval/ternary_step_test.cc @@ -10,7 +10,6 @@ #include "base/attribute.h" #include "base/attribute_set.h" #include "base/type_provider.h" -#include "common/casting.h" #include "common/expr.h" #include "common/value.h" #include "eval/eval/attribute_trail.h" @@ -18,12 +17,10 @@ #include "eval/eval/const_value_step.h" #include "eval/eval/direct_expression_step.h" #include "eval/eval/evaluator_core.h" -#include "eval/eval/ident_step.h" #include "eval/public/activation.h" #include "eval/public/cel_value.h" #include "eval/public/unknown_attribute_set.h" #include "eval/public/unknown_set.h" -#include "internal/status_macros.h" #include "internal/testing.h" #include "internal/testing_descriptor_pool.h" #include "internal/testing_message_factory.h" @@ -41,10 +38,8 @@ namespace { using ::absl_testing::IsOk; using ::absl_testing::StatusIs; using ::cel::BoolValue; -using ::cel::Cast; using ::cel::ErrorValue; using ::cel::Expr; -using ::cel::InstanceOf; using ::cel::IntValue; using ::cel::RuntimeOptions; using ::cel::TypeProvider; @@ -227,8 +222,8 @@ TEST_P(TernaryStepDirectTest, ReturnLhs) { ASSERT_THAT(step->Evaluate(frame, result, attr_unused), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_EQ(Cast(result).NativeValue(), 1); + ASSERT_TRUE(result.IsInt()); + EXPECT_EQ(result.GetInt().NativeValue(), 1); } TEST_P(TernaryStepDirectTest, ReturnRhs) { @@ -248,8 +243,8 @@ TEST_P(TernaryStepDirectTest, ReturnRhs) { ASSERT_THAT(step->Evaluate(frame, result, attr_unused), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_EQ(Cast(result).NativeValue(), 2); + ASSERT_TRUE(result.IsInt()); + EXPECT_EQ(result.GetInt().NativeValue(), 2); } TEST_P(TernaryStepDirectTest, ForwardError) { @@ -272,8 +267,8 @@ TEST_P(TernaryStepDirectTest, ForwardError) { ASSERT_THAT(step->Evaluate(frame, result, attr_unused), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_THAT(Cast(result).NativeValue(), + ASSERT_TRUE(result.IsError()); + EXPECT_THAT(result.GetError().NativeValue(), StatusIs(absl::StatusCode::kInternal, "test error")); } @@ -299,8 +294,8 @@ TEST_P(TernaryStepDirectTest, ForwardUnknown) { AttributeTrail attr_unused; ASSERT_THAT(step->Evaluate(frame, result, attr_unused), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_THAT(Cast(result).ToAttributeSet(), + ASSERT_TRUE(result.IsUnknown()); + EXPECT_THAT(result.GetUnknown().ToAttributeSet(), ElementsAre(Truly([](const cel::Attribute& attr) { return attr.variable_name() == "var"; }))); @@ -323,8 +318,8 @@ TEST_P(TernaryStepDirectTest, UnexpectedCondtionKind) { ASSERT_THAT(step->Evaluate(frame, result, attr_unused), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_THAT(Cast(result).NativeValue(), + ASSERT_TRUE(result.IsError()); + EXPECT_THAT(result.GetError().NativeValue(), StatusIs(absl::StatusCode::kUnknown, HasSubstr("No matching overloads found"))); } @@ -364,8 +359,8 @@ TEST_P(TernaryStepDirectTest, Shortcircuiting) { ASSERT_THAT(step->Evaluate(frame, result, attr_unused), IsOk()); - ASSERT_TRUE(InstanceOf(result)); - EXPECT_THAT(Cast(result).NativeValue(), Eq(1)); + ASSERT_TRUE(result.IsInt()); + EXPECT_THAT(result.GetInt().NativeValue(), Eq(1)); bool expect_eager_eval = !Shortcircuiting(); EXPECT_EQ(lhs_was_called, expect_eager_eval); EXPECT_TRUE(rhs_was_called); diff --git a/eval/tests/BUILD b/eval/tests/BUILD index e4dacea3f..b706101df 100644 --- a/eval/tests/BUILD +++ b/eval/tests/BUILD @@ -69,8 +69,6 @@ cc_test( deps = [ ":request_context_cc_proto", "//checker:validation_result", - "//common:allocator", - "//common:casting", "//common:decl", "//common:legacy_value", "//common:native_type", diff --git a/eval/tests/modern_benchmark_test.cc b/eval/tests/modern_benchmark_test.cc index 1d7731e6a..de984617e 100644 --- a/eval/tests/modern_benchmark_test.cc +++ b/eval/tests/modern_benchmark_test.cc @@ -37,8 +37,6 @@ #include "absl/strings/match.h" #include "absl/strings/string_view.h" #include "checker/validation_result.h" -#include "common/allocator.h" -#include "common/casting.h" #include "common/decl.h" #include "common/native_type.h" #include "common/type.h" @@ -164,8 +162,8 @@ static void BM_Eval(benchmark::State& state) { Activation activation; ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Evaluate(&arena, activation)); - ASSERT_TRUE(InstanceOf(result)); - ASSERT_TRUE(Cast(result) == len + 1); + ASSERT_TRUE(result.Is()); + ASSERT_TRUE(result.Get() == len + 1); } } @@ -210,8 +208,8 @@ static void BM_Eval_Trace(benchmark::State& state) { Activation activation; ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Trace(&arena, activation, EmptyCallback)); - ASSERT_TRUE(InstanceOf(result)); - ASSERT_TRUE(Cast(result) == len + 1); + ASSERT_TRUE(result.Is()); + ASSERT_TRUE(result.Get() == len + 1); } } @@ -250,8 +248,8 @@ static void BM_EvalString(benchmark::State& state) { Activation activation; ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Evaluate(&arena, activation)); - ASSERT_TRUE(InstanceOf(result)); - ASSERT_TRUE(Cast(result).Size() == len + 1); + ASSERT_TRUE(result.Is()); + ASSERT_TRUE(result.Get().Size() == len + 1); } } @@ -291,8 +289,8 @@ static void BM_EvalString_Trace(benchmark::State& state) { Activation activation; ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Trace(&arena, activation, EmptyCallback)); - ASSERT_TRUE(InstanceOf(result)); - ASSERT_TRUE(Cast(result).Size() == len + 1); + ASSERT_TRUE(result.Is()); + ASSERT_TRUE(result.Get().Size() == len + 1); } } @@ -390,7 +388,7 @@ void BM_PolicySymbolic(benchmark::State& state) { for (auto _ : state) { ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Evaluate(&arena, activation)); - auto result_bool = As(result); + auto result_bool = result.As(); ASSERT_TRUE(result_bool && result_bool->NativeValue()); } } @@ -434,7 +432,7 @@ class RequestMapImpl : public CustomMapValueInterface { google::protobuf::MessageFactory* absl_nonnull message_factory, google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const override { - auto string_value = As(key); + auto string_value = key.As(); if (!string_value) { return false; } @@ -503,8 +501,8 @@ void BM_PolicySymbolicMap(benchmark::State& state) { for (auto _ : state) { ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Evaluate(&arena, activation)); - ASSERT_TRUE(InstanceOf(result) && - Cast(result).NativeValue()); + ASSERT_TRUE(result.Is() && + result.Get().NativeValue()); } } @@ -630,8 +628,8 @@ void BM_Comprehension(benchmark::State& state) { for (auto _ : state) { ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Evaluate(&arena, activation)); - ASSERT_TRUE(InstanceOf(result)); - ASSERT_EQ(Cast(result), len); + ASSERT_TRUE(result.Is()); + ASSERT_EQ(result.Get(), len); } } @@ -663,8 +661,8 @@ void BM_Comprehension_Trace(benchmark::State& state) { for (auto _ : state) { ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Trace(&arena, activation, EmptyCallback)); - ASSERT_TRUE(InstanceOf(result)); - ASSERT_EQ(Cast(result), len); + ASSERT_TRUE(result.Is()); + ASSERT_EQ(result.Get(), len); } } @@ -693,8 +691,8 @@ void BM_HasMap(benchmark::State& state) { for (auto _ : state) { ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Evaluate(&arena, activation)); - ASSERT_TRUE(InstanceOf(result) && - Cast(result).NativeValue()); + ASSERT_TRUE(result.Is() && + result.Get().NativeValue()); } } @@ -733,8 +731,8 @@ void BM_HasProto(benchmark::State& state) { for (auto _ : state) { ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Evaluate(&arena, activation)); - ASSERT_TRUE(InstanceOf(result) && - Cast(result).NativeValue()); + ASSERT_TRUE(result.Is() && + result.Get().NativeValue()); } } @@ -772,8 +770,8 @@ void BM_HasProtoMap(benchmark::State& state) { for (auto _ : state) { ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Evaluate(&arena, activation)); - ASSERT_TRUE(InstanceOf(result) && - Cast(result).NativeValue()); + ASSERT_TRUE(result.Is() && + result.Get().NativeValue()); } } @@ -812,8 +810,8 @@ void BM_ReadProtoMap(benchmark::State& state) { for (auto _ : state) { ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Evaluate(&arena, activation)); - ASSERT_TRUE(InstanceOf(result) && - Cast(result).NativeValue()); + ASSERT_TRUE(result.Is() && + result.Get().NativeValue()); } } @@ -852,8 +850,8 @@ void BM_NestedProtoFieldRead(benchmark::State& state) { for (auto _ : state) { ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Evaluate(&arena, activation)); - ASSERT_TRUE(InstanceOf(result) && - Cast(result).NativeValue()); + ASSERT_TRUE(result.Is() && + result.Get().NativeValue()); } } @@ -891,8 +889,8 @@ void BM_NestedProtoFieldReadDefaults(benchmark::State& state) { for (auto _ : state) { ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Evaluate(&arena, activation)); - ASSERT_TRUE(InstanceOf(result) && - Cast(result).NativeValue()); + ASSERT_TRUE(result.Is() && + result.Get().NativeValue()); } } @@ -934,8 +932,8 @@ void BM_ProtoStructAccess(benchmark::State& state) { for (auto _ : state) { ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Evaluate(&arena, activation)); - ASSERT_TRUE(InstanceOf(result) && - Cast(result).NativeValue()); + ASSERT_TRUE(result.Is() && + result.Get().NativeValue()); } } @@ -980,8 +978,8 @@ void BM_ProtoListAccess(benchmark::State& state) { for (auto _ : state) { ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Evaluate(&arena, activation)); - ASSERT_TRUE(InstanceOf(result) && - Cast(result).NativeValue()); + ASSERT_TRUE(result.Is() && + result.Get().NativeValue()); } } @@ -1108,8 +1106,8 @@ void BM_NestedComprehension(benchmark::State& state) { for (auto _ : state) { ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Evaluate(&arena, activation)); - ASSERT_TRUE(InstanceOf(result)); - ASSERT_EQ(Cast(result), len * len); + ASSERT_TRUE(result.Is()); + ASSERT_EQ(result.Get(), len * len); } } @@ -1145,8 +1143,8 @@ void BM_NestedComprehension_Trace(benchmark::State& state) { for (auto _ : state) { ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Trace(&arena, activation, &EmptyCallback)); - ASSERT_TRUE(InstanceOf(result)); - ASSERT_EQ(Cast(result), len * len); + ASSERT_TRUE(result.Is()); + ASSERT_EQ(result.Get(), len * len); } } @@ -1179,8 +1177,8 @@ void BM_ListComprehension(benchmark::State& state) { for (auto _ : state) { ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Evaluate(&arena, activation)); - ASSERT_TRUE(InstanceOf(result)); - ASSERT_THAT(Cast(result).Size(), IsOkAndHolds(len)); + ASSERT_TRUE(result.Is()); + ASSERT_THAT(result.Get().Size(), IsOkAndHolds(len)); } } @@ -1214,8 +1212,8 @@ void BM_ListComprehension_Trace(benchmark::State& state) { for (auto _ : state) { ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Trace(&arena, activation, EmptyCallback)); - ASSERT_TRUE(InstanceOf(result)); - ASSERT_THAT(Cast(result).Size(), IsOkAndHolds(len)); + ASSERT_TRUE(result.Is()); + ASSERT_THAT(result.Get().Size(), IsOkAndHolds(len)); } } @@ -1378,8 +1376,8 @@ void BM_ListComprehension_Opt(benchmark::State& state) { for (auto _ : state) { ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Evaluate(&arena, activation)); - ASSERT_TRUE(InstanceOf(result)); - ASSERT_THAT(Cast(result).Size(), IsOkAndHolds(len)); + ASSERT_TRUE(result.Is()); + ASSERT_THAT(result.Get().Size(), IsOkAndHolds(len)); } } @@ -1399,7 +1397,7 @@ void BM_ComprehensionCpp(benchmark::State& state) { auto op = [&list]() { int sum = 0; for (const auto& value : list) { - sum += Cast(value).NativeValue(); + sum += value.Get().NativeValue(); } return sum; }; @@ -1460,8 +1458,8 @@ void BM_MapTransformComprehension(benchmark::State& state) { for (auto _ : state) { ASSERT_OK_AND_ASSIGN(cel::Value result, cel_expr->Evaluate(&arena, activation)); - ASSERT_TRUE(InstanceOf(result)); - ASSERT_THAT(Cast(result).Size(), IsOkAndHolds(len)); + ASSERT_TRUE(result.Is()); + ASSERT_THAT(result.Get().Size(), IsOkAndHolds(len)); } } diff --git a/extensions/BUILD b/extensions/BUILD index 743d32a9b..908cacb18 100644 --- a/extensions/BUILD +++ b/extensions/BUILD @@ -76,7 +76,6 @@ cc_library( hdrs = ["math_ext.h"], deps = [ ":math_ext_decls", - "//common:casting", "//common:value", "//eval/public:cel_function_registry", "//eval/public:cel_number", @@ -327,7 +326,6 @@ cc_library( "//base:builtins", "//common:ast", "//common:ast_rewrite", - "//common:casting", "//common:constant", "//common:expr", "//common:function_descriptor", diff --git a/extensions/math_ext.cc b/extensions/math_ext.cc index 78c066f9f..49a0d2b38 100644 --- a/extensions/math_ext.cc +++ b/extensions/math_ext.cc @@ -25,7 +25,6 @@ #include "absl/status/statusor.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" -#include "common/casting.h" #include "common/value.h" #include "eval/public/cel_function_registry.h" #include "eval/public/cel_number.h" @@ -62,13 +61,13 @@ Value NumberToValue(CelNumber number) { absl::StatusOr ValueToNumber(const Value& value, absl::string_view function) { - if (auto int_value = As(value); int_value) { + if (auto int_value = value.AsInt(); int_value) { return CelNumber::FromInt64(int_value->NativeValue()); } - if (auto uint_value = As(value); uint_value) { + if (auto uint_value = value.AsUint(); uint_value) { return CelNumber::FromUint64(uint_value->NativeValue()); } - if (auto double_value = As(value); double_value) { + if (auto double_value = value.AsDouble(); double_value) { return CelNumber::FromDouble(double_value->NativeValue()); } return absl::InvalidArgumentError( diff --git a/extensions/protobuf/BUILD b/extensions/protobuf/BUILD index 002da4764..eae7a7b68 100644 --- a/extensions/protobuf/BUILD +++ b/extensions/protobuf/BUILD @@ -118,7 +118,6 @@ cc_test( deps = [ ":value", "//base:attributes", - "//common:casting", "//common:value", "//common:value_kind", "//common:value_testing", @@ -166,7 +165,6 @@ cc_library( srcs = ["bind_proto_to_activation.cc"], hdrs = ["bind_proto_to_activation.h"], deps = [ - "//common:casting", "//common:value", "//internal:status_macros", "//runtime:activation", @@ -184,14 +182,12 @@ cc_test( srcs = ["bind_proto_to_activation_test.cc"], deps = [ ":bind_proto_to_activation", - "//common:casting", "//common:value", "//common:value_testing", "//internal:testing", "//runtime:activation", "@com_google_absl//absl/status", "@com_google_absl//absl/status:status_matchers", - "@com_google_absl//absl/types:optional", "@com_google_cel_spec//proto/cel/expr/conformance/proto2:test_all_types_cc_proto", "@com_google_protobuf//:protobuf", "@com_google_protobuf//:wrappers_cc_proto", diff --git a/extensions/protobuf/bind_proto_to_activation_test.cc b/extensions/protobuf/bind_proto_to_activation_test.cc index 680b4b353..a0ab69123 100644 --- a/extensions/protobuf/bind_proto_to_activation_test.cc +++ b/extensions/protobuf/bind_proto_to_activation_test.cc @@ -17,8 +17,6 @@ #include "google/protobuf/wrappers.pb.h" #include "absl/status/status.h" #include "absl/status/status_matchers.h" -#include "absl/types/optional.h" -#include "common/casting.h" #include "common/value.h" #include "common/value_testing.h" #include "internal/testing.h" @@ -123,7 +121,7 @@ TEST_F(BindProtoToActivationTest, BindProtoToActivationDefaultAny) { MATCHER_P(IsListValueOfSize, size, "") { const Value& v = arg; - auto value = As(v); + auto value = v.AsList(); if (!value) { return false; } @@ -185,7 +183,7 @@ TEST_F(BindProtoToActivationTest, BindProtoToActivationRepeatedComplex) { MATCHER_P(IsMapValueOfSize, size, "") { const Value& v = arg; - auto value = As(v); + auto value = v.AsMap(); if (!value) { return false; } diff --git a/extensions/protobuf/value_test.cc b/extensions/protobuf/value_test.cc index b43c65968..712745c36 100644 --- a/extensions/protobuf/value_test.cc +++ b/extensions/protobuf/value_test.cc @@ -32,7 +32,6 @@ #include "absl/strings/string_view.h" #include "absl/time/time.h" #include "base/attribute.h" -#include "common/casting.h" #include "common/value.h" #include "common/value_kind.h" #include "common/value_testing.h" @@ -267,7 +266,7 @@ TEST_F(ProtoValueWrapTest, GetFieldNoSuchField) { descriptor_pool(), message_factory(), arena())); ASSERT_THAT(value, StructValueIs(_)); - StructValue struct_value = Cast(value); + StructValue struct_value = value.GetStruct(); EXPECT_THAT(struct_value.GetFieldByName("does_not_exist", descriptor_pool(), message_factory(), arena()), IsOkAndHolds(ErrorValueIs(StatusIs(absl::StatusCode::kNotFound, @@ -289,7 +288,7 @@ TEST_F(ProtoValueWrapTest, GetFieldByNumber) { single_bytes: "foo")pb"), descriptor_pool(), message_factory(), arena())); EXPECT_THAT(value, StructValueIs(_)); - StructValue struct_value = Cast(value); + StructValue struct_value = value.GetStruct(); EXPECT_THAT(struct_value.GetFieldByNumber( TestAllTypes::kSingleInt32FieldNumber, descriptor_pool(), @@ -349,7 +348,7 @@ TEST_F(ProtoValueWrapTest, GetFieldByNumberNoSuchField) { single_bytes: "foo")pb"), descriptor_pool(), message_factory(), arena())); EXPECT_THAT(value, StructValueIs(_)); - StructValue struct_value = Cast(value); + StructValue struct_value = value.GetStruct(); EXPECT_THAT(struct_value.GetFieldByNumber(999, descriptor_pool(), message_factory(), arena()), @@ -370,7 +369,7 @@ TEST_F(ProtoValueWrapTest, HasFieldByNumber) { single_int64: 2)pb"), descriptor_pool(), message_factory(), arena())); EXPECT_THAT(value, StructValueIs(_)); - StructValue struct_value = Cast(value); + StructValue struct_value = value.GetStruct(); EXPECT_THAT( struct_value.HasFieldByNumber(TestAllTypes::kSingleInt32FieldNumber), @@ -393,7 +392,7 @@ TEST_F(ProtoValueWrapTest, HasFieldByNumberNoSuchField) { single_int64: 2)pb"), descriptor_pool(), message_factory(), arena())); EXPECT_THAT(value, StructValueIs(_)); - StructValue struct_value = Cast(value); + StructValue struct_value = value.GetStruct(); // Has returns a status directly instead of a CEL error as in Get. EXPECT_THAT( @@ -450,7 +449,7 @@ TEST_F(ProtoValueWrapTest, ProtoMessageForEachField) { )pb"), descriptor_pool(), message_factory(), arena())); EXPECT_THAT(value, StructValueIs(_)); - StructValue struct_value = Cast(value); + StructValue struct_value = value.GetStruct(); std::vector fields; auto cb = [&fields](absl::string_view field, @@ -473,7 +472,7 @@ TEST_F(ProtoValueWrapTest, ProtoMessageQualify) { )pb"), descriptor_pool(), message_factory(), arena())); EXPECT_THAT(value, StructValueIs(_)); - StructValue struct_value = Cast(value); + StructValue struct_value = value.GetStruct(); std::vector qualifiers{ FieldSpecifier{TestAllTypes::kStandaloneMessageFieldNumber, @@ -500,7 +499,7 @@ TEST_F(ProtoValueWrapTest, ProtoMessageQualifyHas) { )pb"), descriptor_pool(), message_factory(), arena())); EXPECT_THAT(value, StructValueIs(_)); - StructValue struct_value = Cast(value); + StructValue struct_value = value.GetStruct(); std::vector qualifiers{ FieldSpecifier{TestAllTypes::kStandaloneMessageFieldNumber, @@ -525,15 +524,15 @@ TEST_F(ProtoValueWrapTest, ProtoInt64MapListKeys) { R"pb( map_int64_int64 { key: 10 value: 20 })pb"), descriptor_pool(), message_factory(), arena())); - ASSERT_OK_AND_ASSIGN(auto map_value, Cast(value).GetFieldByName( + ASSERT_OK_AND_ASSIGN(auto map_value, value.GetStruct().GetFieldByName( "map_int64_int64", descriptor_pool(), message_factory(), arena())); ASSERT_THAT(map_value, MapValueIs(_)); ASSERT_OK_AND_ASSIGN(ListValue key_set, - Cast(map_value).ListKeys( - descriptor_pool(), message_factory(), arena())); + map_value.GetMap().ListKeys(descriptor_pool(), + message_factory(), arena())); EXPECT_THAT(key_set.Size(), IsOkAndHolds(1)); @@ -550,15 +549,15 @@ TEST_F(ProtoValueWrapTest, ProtoInt32MapListKeys) { R"pb( map_int32_int64 { key: 10 value: 20 })pb"), descriptor_pool(), message_factory(), arena())); - ASSERT_OK_AND_ASSIGN(auto map_value, Cast(value).GetFieldByName( + ASSERT_OK_AND_ASSIGN(auto map_value, value.GetStruct().GetFieldByName( "map_int32_int64", descriptor_pool(), message_factory(), arena())); ASSERT_THAT(map_value, MapValueIs(_)); ASSERT_OK_AND_ASSIGN(ListValue key_set, - Cast(map_value).ListKeys( - descriptor_pool(), message_factory(), arena())); + map_value.GetMap().ListKeys(descriptor_pool(), + message_factory(), arena())); EXPECT_THAT(key_set.Size(), IsOkAndHolds(1)); @@ -575,15 +574,15 @@ TEST_F(ProtoValueWrapTest, ProtoBoolMapListKeys) { R"pb( map_bool_int64 { key: false value: 20 })pb"), descriptor_pool(), message_factory(), arena())); - ASSERT_OK_AND_ASSIGN(auto map_value, Cast(value).GetFieldByName( + ASSERT_OK_AND_ASSIGN(auto map_value, value.GetStruct().GetFieldByName( "map_bool_int64", descriptor_pool(), message_factory(), arena())); ASSERT_THAT(map_value, MapValueIs(_)); ASSERT_OK_AND_ASSIGN(ListValue key_set, - Cast(map_value).ListKeys( - descriptor_pool(), message_factory(), arena())); + map_value.GetMap().ListKeys(descriptor_pool(), + message_factory(), arena())); EXPECT_THAT(key_set.Size(), IsOkAndHolds(1)); @@ -602,14 +601,14 @@ TEST_F(ProtoValueWrapTest, ProtoUint32MapListKeys) { descriptor_pool(), message_factory(), arena())); ASSERT_OK_AND_ASSIGN( auto map_value, - Cast(value).GetFieldByName( - "map_uint32_int64", descriptor_pool(), message_factory(), arena())); + value.GetStruct().GetFieldByName("map_uint32_int64", descriptor_pool(), + message_factory(), arena())); ASSERT_THAT(map_value, MapValueIs(_)); ASSERT_OK_AND_ASSIGN(ListValue key_set, - Cast(map_value).ListKeys( - descriptor_pool(), message_factory(), arena())); + map_value.GetMap().ListKeys(descriptor_pool(), + message_factory(), arena())); EXPECT_THAT(key_set.Size(), IsOkAndHolds(1)); @@ -628,14 +627,14 @@ TEST_F(ProtoValueWrapTest, ProtoUint64MapListKeys) { descriptor_pool(), message_factory(), arena())); ASSERT_OK_AND_ASSIGN( auto map_value, - Cast(value).GetFieldByName( - "map_uint64_int64", descriptor_pool(), message_factory(), arena())); + value.GetStruct().GetFieldByName("map_uint64_int64", descriptor_pool(), + message_factory(), arena())); ASSERT_THAT(map_value, MapValueIs(_)); ASSERT_OK_AND_ASSIGN(ListValue key_set, - Cast(map_value).ListKeys( - descriptor_pool(), message_factory(), arena())); + map_value.GetMap().ListKeys(descriptor_pool(), + message_factory(), arena())); EXPECT_THAT(key_set.Size(), IsOkAndHolds(1)); @@ -655,14 +654,14 @@ TEST_F(ProtoValueWrapTest, ProtoStringMapListKeys) { descriptor_pool(), message_factory(), arena())); ASSERT_OK_AND_ASSIGN( auto map_value, - Cast(value).GetFieldByName( - "map_string_int64", descriptor_pool(), message_factory(), arena())); + value.GetStruct().GetFieldByName("map_string_int64", descriptor_pool(), + message_factory(), arena())); ASSERT_THAT(map_value, MapValueIs(_)); ASSERT_OK_AND_ASSIGN(ListValue key_set, - Cast(map_value).ListKeys( - descriptor_pool(), message_factory(), arena())); + map_value.GetMap().ListKeys(descriptor_pool(), + message_factory(), arena())); EXPECT_THAT(key_set.Size(), IsOkAndHolds(1)); @@ -683,12 +682,12 @@ TEST_F(ProtoValueWrapTest, ProtoMapIterator) { descriptor_pool(), message_factory(), arena())); ASSERT_OK_AND_ASSIGN( auto field_value, - Cast(value).GetFieldByName( - "map_int64_int64", descriptor_pool(), message_factory(), arena())); + value.GetStruct().GetFieldByName("map_int64_int64", descriptor_pool(), + message_factory(), arena())); ASSERT_THAT(field_value, MapValueIs(_)); - MapValue map_value = Cast(field_value); + MapValue map_value = field_value.GetMap(); std::vector keys; @@ -714,12 +713,12 @@ TEST_F(ProtoValueWrapTest, ProtoMapForEach) { descriptor_pool(), message_factory(), arena())); ASSERT_OK_AND_ASSIGN( auto field_value, - Cast(value).GetFieldByName( - "map_int64_int64", descriptor_pool(), message_factory(), arena())); + value.GetStruct().GetFieldByName("map_int64_int64", descriptor_pool(), + message_factory(), arena())); ASSERT_THAT(field_value, MapValueIs(_)); - MapValue map_value = Cast(field_value); + MapValue map_value = field_value.GetMap(); std::vector> pairs; @@ -747,12 +746,12 @@ TEST_F(ProtoValueWrapTest, ProtoListIterator) { descriptor_pool(), message_factory(), arena())); ASSERT_OK_AND_ASSIGN( auto field_value, - Cast(value).GetFieldByName( - "repeated_int64", descriptor_pool(), message_factory(), arena())); + value.GetStruct().GetFieldByName("repeated_int64", descriptor_pool(), + message_factory(), arena())); ASSERT_THAT(field_value, ListValueIs(_)); - ListValue list_value = Cast(field_value); + ListValue list_value = field_value.GetList(); std::vector elements; @@ -777,12 +776,12 @@ TEST_F(ProtoValueWrapTest, ProtoListForEachWithIndex) { descriptor_pool(), message_factory(), arena())); ASSERT_OK_AND_ASSIGN( auto field_value, - Cast(value).GetFieldByName( - "repeated_int64", descriptor_pool(), message_factory(), arena())); + value.GetStruct().GetFieldByName("repeated_int64", descriptor_pool(), + message_factory(), arena())); ASSERT_THAT(field_value, ListValueIs(_)); - ListValue list_value = Cast(field_value); + ListValue list_value = field_value.GetList(); std::vector> elements; diff --git a/extensions/select_optimization.cc b/extensions/select_optimization.cc index 8e1a95440..07e0ae4c0 100644 --- a/extensions/select_optimization.cc +++ b/extensions/select_optimization.cc @@ -39,7 +39,6 @@ #include "base/builtins.h" #include "common/ast.h" #include "common/ast_rewrite.h" -#include "common/casting.h" #include "common/constant.h" #include "common/expr.h" #include "common/function_descriptor.h" @@ -853,7 +852,7 @@ absl::Status RecursiveImpl::Evaluate(ExecutionFrameBase& frame, Value& result, AttributeTrail& attribute) const { CEL_RETURN_IF_ERROR(operand_->Evaluate(frame, result, attribute)); - if (InstanceOf(result) || InstanceOf(result)) { + if (result.IsError() || result.IsUnknown()) { // Just forward. return absl::OkStatus(); } @@ -868,12 +867,11 @@ absl::Status RecursiveImpl::Evaluate(ExecutionFrameBase& frame, Value& result, } } - if (!InstanceOf(result)) { + if (!result.IsStruct()) { return absl::InvalidArgumentError( "Expected struct type for select optimization"); } - CEL_ASSIGN_OR_RETURN(result, - impl_.ApplySelect(frame, Cast(result))); + CEL_ASSIGN_OR_RETURN(result, impl_.ApplySelect(frame, result.GetStruct())); return absl::OkStatus(); } diff --git a/runtime/BUILD b/runtime/BUILD index 57bdb1a63..84cf40e75 100644 --- a/runtime/BUILD +++ b/runtime/BUILD @@ -133,13 +133,11 @@ cc_test( deps = [ ":activation", ":bind_proto_to_activation", - "//common:casting", "//common:value", "//common:value_testing", "//internal:testing", "@com_google_absl//absl/status", "@com_google_absl//absl/status:status_matchers", - "@com_google_absl//absl/types:optional", "@com_google_cel_spec//proto/cel/expr/conformance/proto2:test_all_types_cc_proto", "@com_google_protobuf//:protobuf", "@com_google_protobuf//:wrappers_cc_proto", @@ -580,7 +578,6 @@ cc_library( ":runtime_builder", ":runtime_options", "//base:function_adapter", - "//common:casting", "//common:type", "//common:value", "//internal:casts", diff --git a/runtime/bind_proto_to_activation_test.cc b/runtime/bind_proto_to_activation_test.cc index e7935b92a..e28fbef1b 100644 --- a/runtime/bind_proto_to_activation_test.cc +++ b/runtime/bind_proto_to_activation_test.cc @@ -14,11 +14,11 @@ #include "runtime/bind_proto_to_activation.h" +#include + #include "google/protobuf/wrappers.pb.h" #include "absl/status/status.h" #include "absl/status/status_matchers.h" -#include "absl/types/optional.h" -#include "common/casting.h" #include "common/value.h" #include "common/value_testing.h" #include "internal/testing.h" @@ -122,7 +122,7 @@ TEST_F(BindProtoToActivationTest, BindProtoToActivationDefaultAny) { MATCHER_P(IsListValueOfSize, size, "") { const Value& v = arg; - auto value = As(v); + auto value = v.AsList(); if (!value) { return false; } @@ -184,7 +184,7 @@ TEST_F(BindProtoToActivationTest, BindProtoToActivationRepeatedComplex) { MATCHER_P(IsMapValueOfSize, size, "") { const Value& v = arg; - auto value = As(v); + auto value = v.AsMap(); if (!value) { return false; } diff --git a/runtime/internal/BUILD b/runtime/internal/BUILD index c5475a024..28703ef3b 100644 --- a/runtime/internal/BUILD +++ b/runtime/internal/BUILD @@ -138,7 +138,6 @@ cc_library( "function_adapter.h", ], deps = [ - "//common:casting", "//common:kind", "//common:value", "@com_google_absl//absl/status", diff --git a/runtime/internal/function_adapter.h b/runtime/internal/function_adapter.h index 1ff497486..3a0ed1beb 100644 --- a/runtime/internal/function_adapter.h +++ b/runtime/internal/function_adapter.h @@ -24,7 +24,6 @@ #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/time/time.h" -#include "common/casting.h" #include "common/kind.h" #include "common/value.h" @@ -162,24 +161,24 @@ struct ValueToAdaptedVisitor { template absl::Status operator()(T* out) const { - if (!InstanceOf>(input)) { + if (!input.Is>()) { return absl::InvalidArgumentError( absl::StrCat("expected ", ValueKindToString(T::kKind), " value")); } - *out = Cast>(input); + *out = input.Get>(); return absl::OkStatus(); } template absl::Status operator()(T** out) const { - if (!InstanceOf>(input)) { + if (!input.Is>()) { return absl::InvalidArgumentError( absl::StrCat("expected ", ValueKindToString(T::kKind), " value")); } static_assert(std::is_lvalue_reference_v< - decltype(Cast>(input))>, + decltype(input.Get>())>, "expected l-value reference return type for Cast."); - *out = &Cast>(input); + *out = &input.Get>(); return absl::OkStatus(); } diff --git a/runtime/optional_types.cc b/runtime/optional_types.cc index 6e5dbc12b..91e5b0543 100644 --- a/runtime/optional_types.cc +++ b/runtime/optional_types.cc @@ -27,7 +27,6 @@ #include "absl/strings/string_view.h" #include "absl/types/optional.h" #include "base/function_adapter.h" -#include "common/casting.h" #include "common/type.h" #include "common/value.h" #include "internal/casts.h" @@ -148,7 +147,7 @@ absl::StatusOr MapOptIndexOptionalValue( google::protobuf::MessageFactory* absl_nonnull message_factory, google::protobuf::Arena* absl_nonnull arena) { absl::optional value; - if (auto double_key = cel::As(key); double_key) { + if (auto double_key = key.AsDouble(); double_key) { // Try int/uint. auto number = internal::Number::FromDouble(double_key->NativeValue()); if (number.LosslessConvertibleToInt()) { @@ -217,17 +216,17 @@ absl::StatusOr OptionalOptIndexOptionalValue( const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, google::protobuf::MessageFactory* absl_nonnull message_factory, google::protobuf::Arena* absl_nonnull arena) { - if (auto optional_value = As(opaque_value); optional_value) { + if (auto optional_value = opaque_value.AsOptional(); optional_value) { if (!optional_value->HasValue()) { return OptionalValue::None(); } auto container = optional_value->Value(); - if (auto map_value = cel::As(container); map_value) { + if (auto map_value = container.AsMap(); map_value) { return MapOptIndexOptionalValue(*map_value, key, descriptor_pool, message_factory, arena); } - if (auto list_value = cel::As(container); list_value) { - if (auto int_value = cel::As(key); int_value) { + if (auto list_value = container.AsList(); list_value) { + if (auto int_value = key.AsInt(); int_value) { return ListOptIndexOptionalInt(*list_value, int_value->NativeValue(), descriptor_pool, message_factory, arena); } diff --git a/runtime/standard/container_membership_functions.cc b/runtime/standard/container_membership_functions.cc index cc0638429..26c115103 100644 --- a/runtime/standard/container_membership_functions.cc +++ b/runtime/standard/container_membership_functions.cc @@ -16,7 +16,6 @@ #include #include -#include #include "absl/base/nullability.h" #include "absl/status/status.h" @@ -50,7 +49,7 @@ bool ValueEquals(const Value& value, T other); template <> bool ValueEquals(const Value& value, bool other) { - if (auto bool_value = As(value); bool_value) { + if (auto bool_value = value.As(); bool_value) { return bool_value->NativeValue() == other; } return false; @@ -58,7 +57,7 @@ bool ValueEquals(const Value& value, bool other) { template <> bool ValueEquals(const Value& value, int64_t other) { - if (auto int_value = As(value); int_value) { + if (auto int_value = value.As(); int_value) { return int_value->NativeValue() == other; } return false; @@ -66,7 +65,7 @@ bool ValueEquals(const Value& value, int64_t other) { template <> bool ValueEquals(const Value& value, uint64_t other) { - if (auto uint_value = As(value); uint_value) { + if (auto uint_value = value.As(); uint_value) { return uint_value->NativeValue() == other; } return false; @@ -74,7 +73,7 @@ bool ValueEquals(const Value& value, uint64_t other) { template <> bool ValueEquals(const Value& value, double other) { - if (auto double_value = As(value); double_value) { + if (auto double_value = value.As(); double_value) { return double_value->NativeValue() == other; } return false; @@ -82,7 +81,7 @@ bool ValueEquals(const Value& value, double other) { template <> bool ValueEquals(const Value& value, const StringValue& other) { - if (auto string_value = As(value); string_value) { + if (auto string_value = value.As(); string_value) { return string_value->Equals(other); } return false; @@ -90,7 +89,7 @@ bool ValueEquals(const Value& value, const StringValue& other) { template <> bool ValueEquals(const Value& value, const BytesValue& other) { - if (auto bytes_value = As(value); bytes_value) { + if (auto bytes_value = value.As(); bytes_value) { return bytes_value->Equals(other); } return false;