-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathparse_text_proto.h
More file actions
121 lines (112 loc) · 5.41 KB
/
Copy pathparse_text_proto.h
File metadata and controls
121 lines (112 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2024 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_INTERNAL_PARSE_TEXT_PROTO_H_
#define THIRD_PARTY_CEL_CPP_INTERNAL_PARSE_TEXT_PROTO_H_
#include <type_traits>
#include "absl/base/nullability.h"
#include "absl/log/absl_check.h"
#include "absl/log/die_if_null.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h"
#include "common/memory.h"
#include "internal/message_type_name.h"
#include "internal/testing_descriptor_pool.h"
#include "internal/testing_message_factory.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"
#include "google/protobuf/message_lite.h"
#include "google/protobuf/text_format.h"
namespace cel::internal {
// `GeneratedParseTextProto` parses the text format protocol buffer message as
// the message with the same name as `T`, looked up in the provided descriptor
// pool, returning as the generated message. This works regardless of whether
// all messages are built with the lite runtime or not.
template <typename T>
std::enable_if_t<std::is_base_of_v<google::protobuf::Message, T>, T* absl_nonnull>
GeneratedParseTextProto(
google::protobuf::Arena* absl_nonnull arena, absl::string_view text,
const google::protobuf::DescriptorPool* absl_nonnull pool =
GetTestingDescriptorPool(),
google::protobuf::MessageFactory* absl_nonnull factory = GetTestingMessageFactory()) {
// Full runtime.
const auto* descriptor = ABSL_DIE_IF_NULL( // Crash OK
pool->FindMessageTypeByName(MessageTypeNameFor<T>()));
const auto* dynamic_message_prototype =
ABSL_DIE_IF_NULL(factory->GetPrototype(descriptor)); // Crash OK
auto* dynamic_message = dynamic_message_prototype->New(arena);
ABSL_CHECK( // Crash OK
google::protobuf::TextFormat::ParseFromString(text, dynamic_message));
if (auto* generated_message = google::protobuf::DynamicCastMessage<T>(dynamic_message);
generated_message != nullptr) {
// Same thing, no need to serialize and parse.
return generated_message;
}
auto* message = google::protobuf::Arena::Create<T>(arena);
absl::Cord serialized_message;
ABSL_CHECK( // Crash OK
dynamic_message->SerializeToCord(&serialized_message));
ABSL_CHECK(message->ParseFromCord(serialized_message)); // Crash OK
return message;
}
// `GeneratedParseTextProto` parses the text format protocol buffer message as
// the message with the same name as `T`, looked up in the provided descriptor
// pool, returning as the generated message. This works regardless of whether
// all messages are built with the lite runtime or not.
template <typename T>
std::enable_if_t<
std::conjunction_v<std::is_base_of<google::protobuf::MessageLite, T>,
std::negation<std::is_base_of<google::protobuf::Message, T>>>,
T* absl_nonnull>
GeneratedParseTextProto(
google::protobuf::Arena* absl_nonnull arena, absl::string_view text,
const google::protobuf::DescriptorPool* absl_nonnull pool =
GetTestingDescriptorPool(),
google::protobuf::MessageFactory* absl_nonnull factory = GetTestingMessageFactory()) {
// Lite runtime.
const auto* descriptor = ABSL_DIE_IF_NULL( // Crash OK
pool->FindMessageTypeByName(MessageTypeNameFor<T>()));
const auto* dynamic_message_prototype =
ABSL_DIE_IF_NULL(factory->GetPrototype(descriptor)); // Crash OK
auto* dynamic_message = dynamic_message_prototype->New(arena);
ABSL_CHECK( // Crash OK
google::protobuf::TextFormat::ParseFromString(text, dynamic_message));
auto* message = google::protobuf::Arena::Create<T>(arena);
absl::Cord serialized_message;
ABSL_CHECK( // Crash OK
dynamic_message->SerializeToCord(&serialized_message));
ABSL_CHECK(message->ParseFromCord(serialized_message)); // Crash OK
return message;
}
// `DynamicParseTextProto` parses the text format protocol buffer message as the
// dynamic message with the same name as `T`, looked up in the provided
// descriptor pool, returning the dynamic message.
template <typename T>
google::protobuf::Message* absl_nonnull DynamicParseTextProto(
google::protobuf::Arena* absl_nonnull arena, absl::string_view text,
const google::protobuf::DescriptorPool* absl_nonnull pool =
GetTestingDescriptorPool(),
google::protobuf::MessageFactory* absl_nonnull factory = GetTestingMessageFactory()) {
static_assert(std::is_base_of_v<google::protobuf::MessageLite, T>);
const auto* descriptor = ABSL_DIE_IF_NULL( // Crash OK
pool->FindMessageTypeByName(MessageTypeNameFor<T>()));
const auto* dynamic_message_prototype =
ABSL_DIE_IF_NULL(factory->GetPrototype(descriptor)); // Crash OK
auto* dynamic_message = dynamic_message_prototype->New(arena);
ABSL_CHECK(google::protobuf::TextFormat::ParseFromString( // Crash OK
text, cel::to_address(dynamic_message)));
return dynamic_message;
}
} // namespace cel::internal
#endif // THIRD_PARTY_CEL_CPP_INTERNAL_PARSE_TEXT_PROTO_H_