Skip to content

Commit a2d5240

Browse files
jnthntatumcopybara-github
authored andcommitted
Prevent new subclasses of legacy TypeInfoApis.
This was a work around for custom reflection operations on protos while the cel::Value work was in progress. Stopping new usages now that custom structs can be introduced via cel::CustomStructValue. PiperOrigin-RevId: 959771840
1 parent 72e3616 commit a2d5240

11 files changed

Lines changed: 174 additions & 348 deletions

eval/eval/BUILD

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -789,9 +789,7 @@ cc_test(
789789
deps = [
790790
":attribute_trail",
791791
":cel_expression_flat_impl",
792-
":compiler_constant_step",
793792
":const_value_step",
794-
":create_map_step",
795793
":evaluator_core",
796794
":ident_step",
797795
":select_step",
@@ -810,9 +808,7 @@ cc_test(
810808
"//eval/public:unknown_set",
811809
"//eval/public/containers:container_backed_map_impl",
812810
"//eval/public/structs:cel_proto_wrapper",
813-
"//eval/public/structs:legacy_type_adapter",
814811
"//eval/public/structs:trivial_legacy_type_info",
815-
"//eval/public/testing:matchers",
816812
"//eval/testutil:test_extensions_cc_proto",
817813
"//eval/testutil:test_message_cc_proto",
818814
"//extensions/protobuf:value",

eval/eval/select_step_test.cc

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,15 @@
2424
#include "common/value_testing.h"
2525
#include "eval/eval/attribute_trail.h"
2626
#include "eval/eval/cel_expression_flat_impl.h"
27-
#include "eval/eval/compiler_constant_step.h"
2827
#include "eval/eval/const_value_step.h"
29-
#include "eval/eval/create_map_step.h"
3028
#include "eval/eval/evaluator_core.h"
3129
#include "eval/eval/ident_step.h"
3230
#include "eval/public/activation.h"
3331
#include "eval/public/cel_attribute.h"
3432
#include "eval/public/cel_value.h"
3533
#include "eval/public/containers/container_backed_map_impl.h"
3634
#include "eval/public/structs/cel_proto_wrapper.h"
37-
#include "eval/public/structs/legacy_type_adapter.h"
3835
#include "eval/public/structs/trivial_legacy_type_info.h"
39-
#include "eval/public/testing/matchers.h"
4036
#include "eval/public/unknown_attribute_set.h"
4137
#include "eval/public/unknown_set.h"
4238
#include "eval/testutil/test_extensions.pb.h"
@@ -81,43 +77,15 @@ using ::cel::internal::test::EqualsProto;
8177
using ::cel::runtime_internal::NewTestingRuntimeEnv;
8278
using ::cel::runtime_internal::RuntimeEnv;
8379
using ::cel::test::IntValueIs;
84-
using ::testing::_;
8580
using ::testing::Eq;
8681
using ::testing::HasSubstr;
87-
using ::testing::Return;
8882
using ::testing::UnorderedElementsAre;
8983

9084
struct RunExpressionOptions {
9185
bool enable_unknowns = false;
9286
bool enable_wrapper_type_null_unboxing = false;
9387
};
9488

95-
// Simple implementation LegacyTypeAccessApis / LegacyTypeInfoApis that allows
96-
// mocking for getters/setters.
97-
class MockAccessor : public LegacyTypeAccessApis, public LegacyTypeInfoApis {
98-
public:
99-
MOCK_METHOD(absl::StatusOr<bool>, HasField,
100-
(absl::string_view field_name,
101-
const CelValue::MessageWrapper& value),
102-
(const, override));
103-
MOCK_METHOD(absl::StatusOr<CelValue>, GetField,
104-
(absl::string_view field_name,
105-
const CelValue::MessageWrapper& instance,
106-
ProtoWrapperTypeOptions unboxing_option,
107-
cel::MemoryManagerRef memory_manager),
108-
(const, override));
109-
MOCK_METHOD(absl::string_view, GetTypename,
110-
(const CelValue::MessageWrapper& instance), (const, override));
111-
MOCK_METHOD(std::string, DebugString,
112-
(const CelValue::MessageWrapper& instance), (const, override));
113-
MOCK_METHOD(std::vector<absl::string_view>, ListFields,
114-
(const CelValue::MessageWrapper& value), (const, override));
115-
const LegacyTypeAccessApis* GetAccessApis(
116-
const CelValue::MessageWrapper& instance) const override {
117-
return this;
118-
}
119-
};
120-
12189
class SelectStepTest : public testing::Test {
12290
public:
12391
SelectStepTest() : env_(NewTestingRuntimeEnv()) {}
@@ -702,68 +670,6 @@ TEST_P(SelectStepConformanceTest, NullMessageAccessor) {
702670
EXPECT_THAT(*result.ErrorOrDie(), StatusIs(absl::StatusCode::kNotFound));
703671
}
704672

705-
TEST_P(SelectStepConformanceTest, CustomAccessor) {
706-
TestMessage message;
707-
TestMessage* message2 = message.mutable_message_value();
708-
message2->set_int32_value(1);
709-
message2->set_string_value("test");
710-
RunExpressionOptions options;
711-
options.enable_unknowns = GetParam();
712-
testing::NiceMock<MockAccessor> accessor;
713-
CelValue value = CelValue::CreateMessageWrapper(
714-
CelValue::MessageWrapper(&message, &accessor));
715-
716-
ON_CALL(accessor, GetField(_, _, _, _))
717-
.WillByDefault(Return(CelValue::CreateInt64(2)));
718-
ON_CALL(accessor, HasField(_, _)).WillByDefault(Return(false));
719-
720-
ASSERT_OK_AND_ASSIGN(CelValue result,
721-
RunExpression(value, "message_value",
722-
/*test=*/false,
723-
/*unknown_path=*/"", options));
724-
725-
EXPECT_THAT(result, test::IsCelInt64(2));
726-
727-
// testonly select (has)
728-
ASSERT_OK_AND_ASSIGN(result, RunExpression(value, "message_value",
729-
/*test=*/true,
730-
/*unknown_path=*/"", options));
731-
732-
EXPECT_THAT(result, test::IsCelBool(false));
733-
}
734-
735-
TEST_P(SelectStepConformanceTest, CustomAccessorErrorHandling) {
736-
TestMessage message;
737-
TestMessage* message2 = message.mutable_message_value();
738-
message2->set_int32_value(1);
739-
message2->set_string_value("test");
740-
RunExpressionOptions options;
741-
options.enable_unknowns = GetParam();
742-
testing::NiceMock<MockAccessor> accessor;
743-
CelValue value = CelValue::CreateMessageWrapper(
744-
CelValue::MessageWrapper(&message, &accessor));
745-
746-
ON_CALL(accessor, GetField(_, _, _, _))
747-
.WillByDefault(Return(absl::InternalError("bad data")));
748-
ON_CALL(accessor, HasField(_, _))
749-
.WillByDefault(Return(absl::NotFoundError("not found")));
750-
751-
// For get field, implementation may return an error-type cel value or a
752-
// status (e.g. broken assumption using a core type).
753-
ASSERT_OK_AND_ASSIGN(CelValue result,
754-
RunExpression(value, "message_value",
755-
/*test=*/false,
756-
/*unknown_path=*/"", options));
757-
EXPECT_THAT(result, test::IsCelError(StatusIs(absl::StatusCode::kInternal)));
758-
759-
// testonly select (has) errors are coerced to CelError.
760-
ASSERT_OK_AND_ASSIGN(result, RunExpression(value, "message_value",
761-
/*test=*/true,
762-
/*unknown_path=*/"", options));
763-
764-
EXPECT_THAT(result, test::IsCelError(StatusIs(absl::StatusCode::kNotFound)));
765-
}
766-
767673
TEST_P(SelectStepConformanceTest, SimpleEnumTest) {
768674
TestMessage message;
769675
message.set_enum_value(TestMessage::TEST_ENUM_1);

eval/public/structs/BUILD

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,9 @@ cc_test(
272272
srcs = ["legacy_type_adapter_test.cc"],
273273
deps = [
274274
":legacy_type_adapter",
275-
":trivial_legacy_type_info",
276-
"//eval/public:cel_value",
277-
"//eval/public/testing:matchers",
275+
":proto_message_type_adapter",
278276
"//eval/testutil:test_message_cc_proto",
279-
"//extensions/protobuf:memory_manager",
280-
"//internal:status_macros",
281277
"//internal:testing",
282-
"@com_google_protobuf//:protobuf",
283278
],
284279
)
285280

@@ -301,15 +296,13 @@ cc_library(
301296
"//eval/public/containers:internal_field_backed_map_impl",
302297
"//extensions/protobuf:memory_manager",
303298
"//extensions/protobuf/internal:qualify",
304-
"//internal:casts",
305299
"//internal:status_macros",
306300
"@com_google_absl//absl/base:no_destructor",
307301
"@com_google_absl//absl/base:nullability",
308302
"@com_google_absl//absl/log:absl_check",
309303
"@com_google_absl//absl/status",
310304
"@com_google_absl//absl/status:statusor",
311305
"@com_google_absl//absl/strings",
312-
"@com_google_absl//absl/types:optional",
313306
"@com_google_absl//absl/types:span",
314307
"@com_google_protobuf//:differencer",
315308
"@com_google_protobuf//:protobuf",
@@ -386,7 +379,6 @@ cc_library(
386379
deps = [
387380
"//eval/public:message_wrapper",
388381
"@com_google_absl//absl/base:nullability",
389-
"@com_google_absl//absl/status",
390382
"@com_google_absl//absl/strings:string_view",
391383
"@com_google_protobuf//:protobuf",
392384
],
@@ -418,11 +410,14 @@ cc_test(
418410
name = "legacy_type_provider_test",
419411
srcs = ["legacy_type_provider_test.cc"],
420412
deps = [
413+
":legacy_type_adapter",
421414
":legacy_type_info_apis",
422415
":legacy_type_provider",
416+
":proto_message_type_adapter",
417+
":trivial_legacy_type_info",
423418
"//common:type",
419+
"//eval/testutil:test_message_cc_proto",
424420
"//internal:testing",
425-
"@com_google_absl//absl/status:status_matchers",
426421
"@com_google_absl//absl/strings:string_view",
427422
],
428423
)

eval/public/structs/legacy_type_adapter.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232

3333
namespace google::api::expr::runtime {
3434

35+
// Forward declare permitted subclasses.
36+
class DucktypedMessageAdapter;
37+
class ProtoMessageTypeAdapter;
38+
3539
// Interface for mutation apis.
3640
// Note: in the new type system, a type provider represents this by returning
3741
// a cel::Type and cel::ValueManager for the type.
@@ -71,6 +75,13 @@ class LegacyTypeMutationApis {
7175
CelValue::MessageWrapper::Builder& instance [[maybe_unused]]) const {
7276
return absl::UnimplementedError("SetFieldByNumber is not yet implemented");
7377
}
78+
79+
private:
80+
// This class should only be implemented by CEL. Custom structs are only
81+
// supported using the cel::Value APIs.
82+
friend class ProtoMessageTypeAdapter;
83+
84+
LegacyTypeMutationApis() = default;
7485
};
7586

7687
// Interface for access apis.
@@ -138,6 +149,14 @@ class LegacyTypeAccessApis {
138149

139150
virtual std::vector<absl::string_view> ListFields(
140151
const CelValue::MessageWrapper& instance) const = 0;
152+
153+
private:
154+
// This class should only be implemented by CEL. Custom structs are only
155+
// supported using the cel::Value APIs.
156+
friend class DucktypedMessageAdapter;
157+
friend class ProtoMessageTypeAdapter;
158+
159+
LegacyTypeAccessApis() = default;
141160
};
142161

143162
// Type information about a legacy Struct type.

eval/public/structs/legacy_type_adapter_test.cc

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,19 @@
1414

1515
#include "eval/public/structs/legacy_type_adapter.h"
1616

17-
#include <vector>
18-
19-
#include "eval/public/cel_value.h"
20-
#include "eval/public/structs/trivial_legacy_type_info.h"
21-
#include "eval/public/testing/matchers.h"
17+
#include "eval/public/structs/proto_message_type_adapter.h"
2218
#include "eval/testutil/test_message.pb.h"
23-
#include "extensions/protobuf/memory_manager.h"
24-
#include "internal/status_macros.h"
2519
#include "internal/testing.h"
2620

2721
namespace google::api::expr::runtime {
2822
namespace {
2923

30-
class TestAccessApiImpl : public LegacyTypeAccessApis {
31-
public:
32-
TestAccessApiImpl() {}
33-
absl::StatusOr<bool> HasField(
34-
absl::string_view field_name,
35-
const CelValue::MessageWrapper& value) const override {
36-
return absl::UnimplementedError("Not implemented");
37-
}
38-
39-
absl::StatusOr<CelValue> GetField(
40-
absl::string_view field_name, const CelValue::MessageWrapper& instance,
41-
ProtoWrapperTypeOptions unboxing_option,
42-
cel::MemoryManagerRef memory_manager) const override {
43-
return absl::UnimplementedError("Not implemented");
44-
}
45-
46-
std::vector<absl::string_view> ListFields(
47-
const CelValue::MessageWrapper& instance) const override {
48-
return std::vector<absl::string_view>();
49-
}
50-
};
51-
52-
TEST(LegacyTypeAdapterAccessApis, DefaultAlwaysInequal) {
53-
TestMessage message;
54-
MessageWrapper wrapper(&message, nullptr);
55-
MessageWrapper wrapper2(&message, nullptr);
56-
57-
TestAccessApiImpl impl;
24+
TEST(LegacyTypeAdapter, Basic) {
25+
ProtoMessageTypeAdapter adapter(TestMessage::descriptor(), nullptr);
26+
LegacyTypeAdapter type_adapter(&adapter, &adapter);
5827

59-
EXPECT_FALSE(impl.IsEqualTo(wrapper, wrapper2));
28+
EXPECT_EQ(type_adapter.access_apis(), &adapter);
29+
EXPECT_EQ(type_adapter.mutation_apis(), &adapter);
6030
}
6131

6232
} // namespace

eval/public/structs/legacy_type_info_apis.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
#ifndef THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_STRUCTS_LEGACY_TYPE_INFO_APIS_H_
1616
#define THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_STRUCTS_LEGACY_TYPE_INFO_APIS_H_
1717

18+
#include <optional>
1819
#include <string>
1920

2021
#include "absl/base/nullability.h"
21-
#include "absl/status/status.h"
2222
#include "absl/strings/string_view.h"
2323
#include "eval/public/message_wrapper.h"
2424
#include "google/protobuf/descriptor.h"
@@ -29,6 +29,11 @@ namespace google::api::expr::runtime {
2929
class LegacyTypeAccessApis;
3030
class LegacyTypeMutationApis;
3131

32+
// Forward declare permitted subclasses.
33+
class DucktypedMessageAdapter;
34+
class ProtoMessageTypeAdapter;
35+
class TrivialTypeInfo;
36+
3237
// Interface for providing type info from a user defined type (represented as a
3338
// message).
3439
//
@@ -97,10 +102,19 @@ class LegacyTypeInfoApis {
97102
//
98103
// The underlying string is expected to remain valid as long as the
99104
// LegacyTypeInfoApis instance.
100-
virtual absl::optional<FieldDescription> FindFieldByName(
105+
virtual std::optional<FieldDescription> FindFieldByName(
101106
absl::string_view name [[maybe_unused]]) const {
102-
return absl::nullopt;
107+
return std::nullopt;
103108
}
109+
110+
private:
111+
// This class should only be implemented by CEL. Custom structs are only
112+
// supported using the cel::Value APIs.
113+
friend class DucktypedMessageAdapter;
114+
friend class ProtoMessageTypeAdapter;
115+
friend class TrivialTypeInfo;
116+
117+
LegacyTypeInfoApis() = default;
104118
};
105119

106120
} // namespace google::api::expr::runtime

eval/public/structs/legacy_type_provider.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
#ifndef THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_STRUCTS_TYPE_PROVIDER_H_
1616
#define THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_STRUCTS_TYPE_PROVIDER_H_
1717

18+
#include <optional>
19+
1820
#include "absl/base/attributes.h"
1921
#include "absl/base/nullability.h"
2022
#include "absl/status/statusor.h"
2123
#include "absl/strings/string_view.h"
22-
#include "absl/types/optional.h"
2324
#include "common/type.h"
2425
#include "common/type_reflector.h"
2526
#include "common/value.h"
@@ -45,7 +46,7 @@ class LegacyTypeProvider : public cel::TypeReflector {
4546
// Returned non-null pointers from the adapter implemententation must remain
4647
// valid as long as the type provider.
4748
// TODO(uncreated-issue/3): add alternative for new type system.
48-
virtual absl::optional<LegacyTypeAdapter> ProvideLegacyType(
49+
virtual std::optional<LegacyTypeAdapter> ProvideLegacyType(
4950
absl::string_view name) const = 0;
5051

5152
// Return LegacyTypeInfoApis for the fully qualified type name if available.
@@ -55,9 +56,9 @@ class LegacyTypeProvider : public cel::TypeReflector {
5556
// Since custom type providers should create values compatible with evaluator
5657
// created ones, the TypeInfoApis returned from this method should be the same
5758
// as the ones used in value creation.
58-
virtual absl::optional<const LegacyTypeInfoApis*> ProvideLegacyTypeInfo(
59+
virtual std::optional<const LegacyTypeInfoApis*> ProvideLegacyTypeInfo(
5960
ABSL_ATTRIBUTE_UNUSED absl::string_view name) const {
60-
return absl::nullopt;
61+
return std::nullopt;
6162
}
6263

6364
absl::StatusOr<absl_nullable cel::ValueBuilderPtr> NewValueBuilder(
@@ -66,10 +67,10 @@ class LegacyTypeProvider : public cel::TypeReflector {
6667
google::protobuf::Arena* absl_nonnull arena) const final;
6768

6869
protected:
69-
absl::StatusOr<absl::optional<cel::Type>> FindTypeImpl(
70+
absl::StatusOr<std::optional<cel::Type>> FindTypeImpl(
7071
absl::string_view name) const final;
7172

72-
absl::StatusOr<absl::optional<cel::StructTypeField>>
73+
absl::StatusOr<std::optional<cel::StructTypeField>>
7374
FindStructTypeFieldByNameImpl(absl::string_view type,
7475
absl::string_view name) const final;
7576
};

0 commit comments

Comments
 (0)