Skip to content

Commit 5916159

Browse files
jnthntatumcopybara-github
authored andcommitted
Misc consistency/style fixes for Struct types:
- add helper for exposing operations on wrapped legacy value messages - avoid a string copy in AttibuteTrail::Step when empty - add accessors for ParsedMessage::message ::descriptor - update legacy type provider to report typed fields for normal (no custom accesor impl) protos - test helper for working with dynamic messages in the hermetic test environment No functional changes PiperOrigin-RevId: 952165947
1 parent 5354722 commit 5916159

19 files changed

Lines changed: 335 additions & 64 deletions

common/descriptor_pool_type_introspector.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ FindStructTypeFieldByNameDirectly(
4444
if (descriptor == nullptr) {
4545
return std::nullopt;
4646
}
47-
const google::protobuf::FieldDescriptor* absl_nullable field =
48-
descriptor->FindFieldByName(name);
47+
const google::protobuf::FieldDescriptor* field = descriptor->FindFieldByName(name);
4948
if (field != nullptr) {
5049
return StructTypeField(MessageTypeField(field));
5150
}

common/legacy_value.cc

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,15 @@ namespace cel {
7171

7272
namespace {
7373

74-
using google::api::expr::runtime::CelList;
75-
using google::api::expr::runtime::CelMap;
76-
using google::api::expr::runtime::CelValue;
77-
using google::api::expr::runtime::FieldBackedListImpl;
78-
using google::api::expr::runtime::FieldBackedMapImpl;
79-
using google::api::expr::runtime::GetGenericProtoTypeInfoInstance;
80-
using google::api::expr::runtime::LegacyTypeInfoApis;
81-
using google::api::expr::runtime::MessageWrapper;
74+
using ::google::api::expr::runtime::CelList;
75+
using ::google::api::expr::runtime::CelMap;
76+
using ::google::api::expr::runtime::CelValue;
77+
using ::google::api::expr::runtime::CreateCelValueFromField;
78+
using ::google::api::expr::runtime::FieldBackedListImpl;
79+
using ::google::api::expr::runtime::FieldBackedMapImpl;
80+
using ::google::api::expr::runtime::GetGenericProtoTypeInfoInstance;
81+
using ::google::api::expr::runtime::LegacyTypeInfoApis;
82+
using ::google::api::expr::runtime::MessageWrapper;
8283
using ::google::api::expr::runtime::internal::MaybeWrapValueToMessage;
8384

8485
absl::Status InvalidMapKeyTypeError(ValueKind kind) {
@@ -1288,6 +1289,36 @@ TypeValue CreateTypeValueFromView(google::protobuf::Arena* arena,
12881289
return TypeValue(common_internal::LegacyRuntimeType(input));
12891290
}
12901291

1292+
const google::protobuf::Message* absl_nullable GetLegacyMessage(const Value& value) {
1293+
if (!common_internal::IsLegacyStructValue(value)) {
1294+
return nullptr;
1295+
}
1296+
1297+
auto legacy = common_internal::GetLegacyStructValue(value);
1298+
const auto* legacy_type_info = legacy.legacy_type_info();
1299+
if (legacy_type_info == nullptr) {
1300+
return nullptr;
1301+
}
1302+
if (legacy_type_info != &GetGenericProtoTypeInfoInstance()) {
1303+
return nullptr;
1304+
}
1305+
if (IsWellKnownMessageType(legacy.message_ptr()->GetDescriptor())) {
1306+
return nullptr;
1307+
}
1308+
return legacy.message_ptr();
1309+
}
1310+
1311+
absl::Status WrapLegacyMessageField(
1312+
const google::protobuf::Message* absl_nonnull message,
1313+
const google::protobuf::FieldDescriptor* absl_nonnull field_descriptor,
1314+
ProtoWrapperTypeOptions unboxing_option, google::protobuf::Arena* arena,
1315+
Value* absl_nonnull out) {
1316+
CEL_ASSIGN_OR_RETURN(CelValue result,
1317+
CreateCelValueFromField(message, field_descriptor,
1318+
unboxing_option, arena));
1319+
return ModernValue(arena, result, *out);
1320+
}
1321+
12911322
} // namespace interop_internal
12921323

12931324
} // namespace cel

common/legacy_value.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
#include "common/value.h"
2828
#include "eval/public/cel_value.h"
2929
#include "internal/status_macros.h"
30+
#include "runtime/runtime_options.h"
3031
#include "google/protobuf/arena.h"
32+
#include "google/protobuf/descriptor.h"
3133

3234
namespace cel {
3335

@@ -59,6 +61,19 @@ google::api::expr::runtime::CelValue UnsafeLegacyValue(
5961

6062
namespace cel::interop_internal {
6163

64+
// Returns the underlying `google::protobuf::Message` of a `cel::Value` if it is a legacy
65+
// message with the default type info, or `nullptr` otherwise.
66+
const google::protobuf::Message* absl_nullable GetLegacyMessage(const Value& value);
67+
68+
// Access a field on a legacy message value, writing the result to `out`.
69+
// Prefers wrapping legacy values instead of using the modern value
70+
// representation.
71+
absl::Status WrapLegacyMessageField(
72+
const google::protobuf::Message* absl_nonnull message,
73+
const google::protobuf::FieldDescriptor* absl_nonnull field_descriptor,
74+
ProtoWrapperTypeOptions unboxing_option, google::protobuf::Arena* arena,
75+
Value* absl_nonnull out);
76+
6277
absl::StatusOr<Value> FromLegacyValue(
6378
google::protobuf::Arena* arena,
6479
const google::api::expr::runtime::CelValue& legacy_value,

common/type.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,11 @@ absl::optional<MessageTypeField> StructTypeField::AsMessage() const {
606606
return std::nullopt;
607607
}
608608

609+
MessageTypeField StructTypeField::GetMessage() const {
610+
ABSL_DCHECK(IsMessage());
611+
return absl::get<MessageTypeField>(variant_);
612+
}
613+
609614
StructTypeField::operator MessageTypeField() const {
610615
ABSL_DCHECK(IsMessage());
611616
return absl::get<MessageTypeField>(variant_);

common/type.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,7 @@ class StructTypeField final {
11401140
}
11411141

11421142
absl::optional<MessageTypeField> AsMessage() const;
1143+
MessageTypeField GetMessage() const;
11431144

11441145
explicit operator MessageTypeField() const;
11451146

common/types/message_type.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ class MessageType final {
8787
return descriptor_;
8888
}
8989

90+
const google::protobuf::Descriptor* absl_nonnull descriptor() const {
91+
ABSL_DCHECK(*this);
92+
return descriptor_;
93+
}
94+
9095
explicit operator bool() const { return descriptor_ != nullptr; }
9196

9297
private:
@@ -166,8 +171,12 @@ class MessageTypeField final {
166171
return *descriptor_;
167172
}
168173

169-
const google::protobuf::FieldDescriptor* absl_nonnull operator->() const
170-
ABSL_ATTRIBUTE_LIFETIME_BOUND {
174+
const google::protobuf::FieldDescriptor* absl_nonnull operator->() const {
175+
ABSL_DCHECK(*this);
176+
return descriptor_;
177+
}
178+
179+
const google::protobuf::FieldDescriptor* absl_nonnull descriptor() const {
171180
ABSL_DCHECK(*this);
172181
return descriptor_;
173182
}

common/value.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <cstddef>
1818
#include <cstdint>
1919
#include <memory>
20+
#include <optional>
2021
#include <ostream>
2122
#include <string>
2223
#include <type_traits>

common/values/parsed_message_value.h

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,8 @@ class ParsedMessageValue final
9999
return *value_;
100100
}
101101

102-
const google::protobuf::Message* absl_nonnull operator->() const
103-
ABSL_ATTRIBUTE_LIFETIME_BOUND {
104-
return value_;
105-
}
102+
const google::protobuf::Message* absl_nonnull operator->() const { return value_; }
103+
const google::protobuf::Message* absl_nonnull message() const { return value_; }
106104

107105
bool IsZeroValue() const;
108106

@@ -175,6 +173,15 @@ class ParsedMessageValue final
175173
swap(lhs.arena_, rhs.arena_);
176174
}
177175

176+
absl::Status GetField(
177+
const google::protobuf::FieldDescriptor* absl_nonnull field,
178+
ProtoWrapperTypeOptions unboxing_options,
179+
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
180+
google::protobuf::MessageFactory* absl_nonnull message_factory,
181+
google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const;
182+
183+
bool HasField(const google::protobuf::FieldDescriptor* absl_nonnull field) const;
184+
178185
private:
179186
friend std::pointer_traits<ParsedMessageValue>;
180187
friend class StructValue;
@@ -203,15 +210,6 @@ class ParsedMessageValue final
203210
return absl::OkStatus();
204211
}
205212

206-
absl::Status GetField(
207-
const google::protobuf::FieldDescriptor* absl_nonnull field,
208-
ProtoWrapperTypeOptions unboxing_options,
209-
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
210-
google::protobuf::MessageFactory* absl_nonnull message_factory,
211-
google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const;
212-
213-
bool HasField(const google::protobuf::FieldDescriptor* absl_nonnull field) const;
214-
215213
const google::protobuf::Message* absl_nonnull value_;
216214
// Arena that is attributed as owning the value. May be null to indicate that
217215
// the value is managed externally.

eval/eval/attribute_trail.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class AttributeTrail {
4747

4848
// Creates AttributeTrail with attribute path incremented by "qualifier".
4949
AttributeTrail Step(const std::string* qualifier) const {
50+
if (empty()) return AttributeTrail();
5051
return Step(cel::AttributeQualifier::OfString(*qualifier));
5152
}
5253

eval/public/structs/BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ cc_test(
325325
":proto_message_type_adapter",
326326
"//base:attributes",
327327
"//common:value",
328+
"//common:value_testing",
328329
"//eval/public:cel_value",
329330
"//eval/public:message_wrapper",
330331
"//eval/public/containers:container_backed_list_impl",
@@ -365,10 +366,13 @@ cc_test(
365366
deps = [
366367
":legacy_type_info_apis",
367368
":protobuf_descriptor_type_provider",
369+
"//common:type",
368370
"//eval/public:cel_value",
369371
"//eval/public/testing:matchers",
370372
"//extensions/protobuf:memory_manager",
371373
"//internal:testing",
374+
"@com_google_absl//absl/status:status_matchers",
375+
"@com_google_cel_spec//proto/cel/expr/conformance/proto3:test_all_types_cc_proto",
372376
"@com_google_protobuf//:protobuf",
373377
"@com_google_protobuf//:wrappers_cc_proto",
374378
],
@@ -414,7 +418,9 @@ cc_test(
414418
deps = [
415419
":legacy_type_info_apis",
416420
":legacy_type_provider",
421+
"//common:type",
417422
"//internal:testing",
423+
"@com_google_absl//absl/status:status_matchers",
418424
"@com_google_absl//absl/strings:string_view",
419425
],
420426
)

0 commit comments

Comments
 (0)