Skip to content

Commit f1f04e7

Browse files
jnthntatumcopybara-github
authored andcommitted
Restrict usages of TrivialTypeInfo to the singleton.
PiperOrigin-RevId: 962442370
1 parent 7341cac commit f1f04e7

4 files changed

Lines changed: 20 additions & 10 deletions

File tree

eval/public/BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ licenses(["notice"])
2929

3030
exports_files(["LICENSE"])
3131

32+
# No new users should depend on MessageWrapper using legacy APIs.
33+
# This was added as a temporary workaround for implementing custom reflection operations
34+
# on messages that were compiled with the LITE_RUNTIME (MessageLite) option.
35+
# Internally, we now assume legacy Message types are always normal (Message) protobuf messages with
36+
# standard reflection operations.
37+
# Custom structs (including MessageLite protos) should use the modern API (see
38+
# `cel::CustomStructValue`).
3239
cc_library(
3340
name = "message_wrapper",
3441
hdrs = [

eval/public/structs/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ cc_library(
374374
":legacy_type_info_apis",
375375
"//eval/public:message_wrapper",
376376
"@com_google_absl//absl/base:no_destructor",
377+
"@com_google_absl//absl/base:nullability",
377378
"@com_google_absl//absl/strings:string_view",
378379
],
379380
)

eval/public/structs/trivial_legacy_type_info.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <string>
1919

2020
#include "absl/base/no_destructor.h"
21+
#include "absl/base/nullability.h"
2122
#include "absl/strings/string_view.h"
2223
#include "eval/public/message_wrapper.h"
2324
#include "eval/public/structs/legacy_type_info_apis.h"
@@ -27,7 +28,12 @@ namespace google::api::expr::runtime {
2728
// Implementation of type info APIs suitable for testing where no message
2829
// operations need to be supported.
2930
class TrivialTypeInfo : public LegacyTypeInfoApis {
31+
private:
32+
struct Key {};
33+
3034
public:
35+
explicit TrivialTypeInfo(Key&) {}
36+
3137
absl::string_view GetTypename(const MessageWrapper& wrapper) const override {
3238
return "opaque";
3339
}
@@ -43,10 +49,14 @@ class TrivialTypeInfo : public LegacyTypeInfoApis {
4349
return nullptr;
4450
}
4551

46-
static const TrivialTypeInfo* GetInstance() {
47-
static absl::NoDestructor<TrivialTypeInfo> kInstance;
52+
static const TrivialTypeInfo* absl_nonnull GetInstance() {
53+
static absl::NoDestructor<Key> kKey;
54+
static absl::NoDestructor<TrivialTypeInfo> kInstance(*kKey);
4855
return &*kInstance;
4956
}
57+
58+
private:
59+
TrivialTypeInfo() = default;
5060
};
5161

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

eval/public/structs/trivial_legacy_type_info_test.cc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,27 @@ namespace google::api::expr::runtime {
2121
namespace {
2222

2323
TEST(TrivialTypeInfo, GetTypename) {
24-
TrivialTypeInfo info;
2524
MessageWrapper wrapper;
2625

27-
EXPECT_EQ(info.GetTypename(wrapper), "opaque");
2826
EXPECT_EQ(TrivialTypeInfo::GetInstance()->GetTypename(wrapper), "opaque");
2927
}
3028

3129
TEST(TrivialTypeInfo, DebugString) {
32-
TrivialTypeInfo info;
3330
MessageWrapper wrapper;
3431

35-
EXPECT_EQ(info.DebugString(wrapper), "opaque");
3632
EXPECT_EQ(TrivialTypeInfo::GetInstance()->DebugString(wrapper), "opaque");
3733
}
3834

3935
TEST(TrivialTypeInfo, GetAccessApis) {
40-
TrivialTypeInfo info;
4136
MessageWrapper wrapper;
4237

43-
EXPECT_EQ(info.GetAccessApis(wrapper), nullptr);
4438
EXPECT_EQ(TrivialTypeInfo::GetInstance()->GetAccessApis(wrapper), nullptr);
4539
}
4640

4741

4842
TEST(TrivialTypeInfo, FindFieldByName) {
49-
TrivialTypeInfo info;
5043
MessageWrapper wrapper;
5144

52-
EXPECT_EQ(info.FindFieldByName("foo"), std::nullopt);
5345
EXPECT_EQ(TrivialTypeInfo::GetInstance()->FindFieldByName("foo"),
5446
std::nullopt);
5547
}

0 commit comments

Comments
 (0)