6
6
#include < string>
7
7
#include < vector>
8
8
9
- #include " google/protobuf/descriptor.h"
10
- #include " google/protobuf/message.h"
11
- #include " google/protobuf/unknown_field_set.h"
12
9
#include " absl/container/flat_hash_map.h"
13
10
#include " absl/container/flat_hash_set.h"
14
11
#include " absl/strings/str_cat.h"
15
12
#include " absl/strings/str_join.h"
16
13
#include " absl/strings/string_view.h"
17
14
#include " absl/synchronization/mutex.h"
15
+ #include " google/protobuf/descriptor.h"
16
+ #include " google/protobuf/message.h"
17
+ #include " google/protobuf/unknown_field_set.h"
18
+ #include " python/google/protobuf/proto_api.h"
18
19
19
20
namespace pybind11_protobuf ::check_unknown_fields {
20
21
namespace {
21
22
22
23
using AllowListSet = absl::flat_hash_set<std::string>;
23
24
using MayContainExtensionsMap =
24
- absl::flat_hash_map<const :: google::protobuf::Descriptor*, bool >;
25
+ absl::flat_hash_map<const google::protobuf::Descriptor*, bool >;
25
26
26
27
AllowListSet* GetAllowList () {
27
28
static auto * allow_list = new AllowListSet ();
@@ -37,7 +38,7 @@ std::string MakeAllowListKey(
37
38
38
39
// / Recurses through the message Descriptor class looking for valid extensions.
39
40
// / Stores the result to `memoized`.
40
- bool MessageMayContainExtensionsRecursive (const :: google::protobuf::Descriptor* descriptor,
41
+ bool MessageMayContainExtensionsRecursive (const google::protobuf::Descriptor* descriptor,
41
42
MayContainExtensionsMap* memoized) {
42
43
if (descriptor->extension_range_count () > 0 ) return true ;
43
44
@@ -48,7 +49,7 @@ bool MessageMayContainExtensionsRecursive(const ::google::protobuf::Descriptor*
48
49
49
50
for (int i = 0 ; i < descriptor->field_count (); i++) {
50
51
auto * fd = descriptor->field (i);
51
- if (fd->cpp_type () != :: google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE) continue ;
52
+ if (fd->cpp_type () != google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE) continue ;
52
53
if (MessageMayContainExtensionsRecursive (fd->message_type (), memoized)) {
53
54
(*memoized)[descriptor] = true ;
54
55
return true ;
@@ -58,16 +59,16 @@ bool MessageMayContainExtensionsRecursive(const ::google::protobuf::Descriptor*
58
59
return false ;
59
60
}
60
61
61
- bool MessageMayContainExtensionsMemoized (const :: google::protobuf::Descriptor* descriptor) {
62
+ bool MessageMayContainExtensionsMemoized (const google::protobuf::Descriptor* descriptor) {
62
63
static auto * memoized = new MayContainExtensionsMap ();
63
64
static absl::Mutex lock;
64
65
absl::MutexLock l (&lock);
65
66
return MessageMayContainExtensionsRecursive (descriptor, memoized);
66
67
}
67
68
68
69
struct HasUnknownFields {
69
- HasUnknownFields (const :: google::protobuf::python::PyProto_API* py_proto_api,
70
- const :: google::protobuf::Descriptor* root_descriptor)
70
+ HasUnknownFields (const google::protobuf::python::PyProto_API* py_proto_api,
71
+ const google::protobuf::Descriptor* root_descriptor)
71
72
: py_proto_api(py_proto_api), root_descriptor(root_descriptor) {}
72
73
73
74
std::string FieldFQN () const { return absl::StrJoin (field_fqn_parts, " ." ); }
@@ -77,34 +78,33 @@ struct HasUnknownFields {
77
78
: absl::StrCat (FieldFQN (), " ." , unknown_field_number);
78
79
}
79
80
80
- bool FindUnknownFieldsRecursive (const :: google::protobuf::Message* sub_message,
81
+ bool FindUnknownFieldsRecursive (const google::protobuf::Message* sub_message,
81
82
uint32_t depth);
82
83
83
84
std::string BuildErrorMessage () const ;
84
85
85
- const :: google::protobuf::python::PyProto_API* py_proto_api;
86
- const :: google::protobuf::Descriptor* root_descriptor = nullptr ;
87
- const :: google::protobuf::Descriptor* unknown_field_parent_descriptor = nullptr ;
86
+ const google::protobuf::python::PyProto_API* py_proto_api;
87
+ const google::protobuf::Descriptor* root_descriptor = nullptr ;
88
+ const google::protobuf::Descriptor* unknown_field_parent_descriptor = nullptr ;
88
89
std::vector<std::string> field_fqn_parts;
89
90
int unknown_field_number;
90
91
};
91
92
92
93
// / Recurses through the message fields class looking for UnknownFields.
93
94
bool HasUnknownFields::FindUnknownFieldsRecursive (
94
- const :: google::protobuf::Message* sub_message, uint32_t depth) {
95
- const :: google::protobuf::Reflection& reflection = *sub_message->GetReflection ();
95
+ const google::protobuf::Message* sub_message, uint32_t depth) {
96
+ const google::protobuf::Reflection& reflection = *sub_message->GetReflection ();
96
97
97
98
// If there are unknown fields, stop searching.
98
- const :: google::protobuf::UnknownFieldSet& unknown_field_set =
99
+ const google::protobuf::UnknownFieldSet& unknown_field_set =
99
100
reflection.GetUnknownFields (*sub_message);
100
101
if (!unknown_field_set.empty ()) {
101
102
unknown_field_parent_descriptor = sub_message->GetDescriptor ();
102
103
unknown_field_number = unknown_field_set.field (0 ).number ();
103
104
104
105
// Stop only if the extension is known by Python.
105
106
if (py_proto_api->GetDefaultDescriptorPool ()->FindExtensionByNumber (
106
- unknown_field_parent_descriptor,
107
- unknown_field_number)) {
107
+ unknown_field_parent_descriptor, unknown_field_number)) {
108
108
field_fqn_parts.resize (depth);
109
109
return true ;
110
110
}
@@ -118,11 +118,11 @@ bool HasUnknownFields::FindUnknownFieldsRecursive(
118
118
119
119
// Otherwise the method has to check all present fields, including
120
120
// extensions to determine if they include unknown fields.
121
- std::vector<const :: google::protobuf::FieldDescriptor*> present_fields;
121
+ std::vector<const google::protobuf::FieldDescriptor*> present_fields;
122
122
reflection.ListFields (*sub_message, &present_fields);
123
123
124
124
for (const auto * field : present_fields) {
125
- if (field->cpp_type () != :: google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
125
+ if (field->cpp_type () != google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
126
126
continue ;
127
127
}
128
128
if (field->is_repeated ()) {
@@ -182,8 +182,8 @@ void AllowUnknownFieldsFor(absl::string_view top_message_descriptor_full_name,
182
182
}
183
183
184
184
std::optional<std::string> CheckRecursively (
185
- const :: google::protobuf::python::PyProto_API* py_proto_api,
186
- const :: google::protobuf::Message* message) {
185
+ const google::protobuf::python::PyProto_API* py_proto_api,
186
+ const google::protobuf::Message* message) {
187
187
const auto * root_descriptor = message->GetDescriptor ();
188
188
HasUnknownFields search{py_proto_api, root_descriptor};
189
189
if (!search.FindUnknownFieldsRecursive (message, 0u )) {
0 commit comments