Skip to content

Commit 008b0a0

Browse files
author
dgronskiy
committed
refactor grpc: link userver::grpc to userver::protobuf, move descriptor collector internals to protobuf/
Changelog entry (build): * `userver::grpc` now links to `userver::protobuf`. commit_hash:c12ac0812c76ba454fb01c023e3ea77344e29956
1 parent 8534fc4 commit 008b0a0

12 files changed

Lines changed: 75 additions & 67 deletions

File tree

.mapping.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2634,7 +2634,6 @@
26342634
"grpc/include/userver/ugrpc/impl/completion_queue_pool_base.hpp":"taxi/uservices/userver/grpc/include/userver/ugrpc/impl/completion_queue_pool_base.hpp",
26352635
"grpc/include/userver/ugrpc/impl/event_base.hpp":"taxi/uservices/userver/grpc/include/userver/ugrpc/impl/event_base.hpp",
26362636
"grpc/include/userver/ugrpc/impl/maybe_owned_string.hpp":"taxi/uservices/userver/grpc/include/userver/ugrpc/impl/maybe_owned_string.hpp",
2637-
"grpc/include/userver/ugrpc/impl/protobuf_collector.hpp":"taxi/uservices/userver/grpc/include/userver/ugrpc/impl/protobuf_collector.hpp",
26382637
"grpc/include/userver/ugrpc/impl/queue_runner.hpp":"taxi/uservices/userver/grpc/include/userver/ugrpc/impl/queue_runner.hpp",
26392638
"grpc/include/userver/ugrpc/impl/static_service_metadata.hpp":"taxi/uservices/userver/grpc/include/userver/ugrpc/impl/static_service_metadata.hpp",
26402639
"grpc/include/userver/ugrpc/impl/statistics.hpp":"taxi/uservices/userver/grpc/include/userver/ugrpc/impl/statistics.hpp",
@@ -2786,7 +2785,6 @@
27862785
"grpc/src/ugrpc/impl/grpc_string_logging.hpp":"taxi/uservices/userver/grpc/src/ugrpc/impl/grpc_string_logging.hpp",
27872786
"grpc/src/ugrpc/impl/logging.cpp":"taxi/uservices/userver/grpc/src/ugrpc/impl/logging.cpp",
27882787
"grpc/src/ugrpc/impl/logging.hpp":"taxi/uservices/userver/grpc/src/ugrpc/impl/logging.hpp",
2789-
"grpc/src/ugrpc/impl/protobuf_collector.cpp":"taxi/uservices/userver/grpc/src/ugrpc/impl/protobuf_collector.cpp",
27902788
"grpc/src/ugrpc/impl/queue_runner.cpp":"taxi/uservices/userver/grpc/src/ugrpc/impl/queue_runner.cpp",
27912789
"grpc/src/ugrpc/impl/rpc_metadata.cpp":"taxi/uservices/userver/grpc/src/ugrpc/impl/rpc_metadata.cpp",
27922790
"grpc/src/ugrpc/impl/rpc_metadata.hpp":"taxi/uservices/userver/grpc/src/ugrpc/impl/rpc_metadata.hpp",
@@ -3224,11 +3222,13 @@
32243222
"libraries/protobuf/CMakeLists.txt":"taxi/uservices/userver/libraries/protobuf/CMakeLists.txt",
32253223
"libraries/protobuf/include/userver/protobuf/datetime.hpp":"taxi/uservices/userver/libraries/protobuf/include/userver/protobuf/datetime.hpp",
32263224
"libraries/protobuf/include/userver/protobuf/exceptions.hpp":"taxi/uservices/userver/libraries/protobuf/include/userver/protobuf/exceptions.hpp",
3225+
"libraries/protobuf/include/userver/protobuf/impl/descriptor_collector.hpp":"taxi/uservices/userver/libraries/protobuf/include/userver/protobuf/impl/descriptor_collector.hpp",
32273226
"libraries/protobuf/include/userver/protobuf/json/convert.hpp":"taxi/uservices/userver/libraries/protobuf/include/userver/protobuf/json/convert.hpp",
32283227
"libraries/protobuf/include/userver/protobuf/json/convert_options.hpp":"taxi/uservices/userver/libraries/protobuf/include/userver/protobuf/json/convert_options.hpp",
32293228
"libraries/protobuf/include/userver/protobuf/json/exceptions.hpp":"taxi/uservices/userver/libraries/protobuf/include/userver/protobuf/json/exceptions.hpp",
32303229
"libraries/protobuf/include/userver/protobuf/string.hpp":"taxi/uservices/userver/libraries/protobuf/include/userver/protobuf/string.hpp",
32313230
"libraries/protobuf/src/protobuf/datetime.cpp":"taxi/uservices/userver/libraries/protobuf/src/protobuf/datetime.cpp",
3231+
"libraries/protobuf/src/protobuf/impl/descriptor_collector.cpp":"taxi/uservices/userver/libraries/protobuf/src/protobuf/impl/descriptor_collector.cpp",
32323232
"libraries/protobuf/src/protobuf/json/convert.cpp":"taxi/uservices/userver/libraries/protobuf/src/protobuf/json/convert.cpp",
32333233
"libraries/protobuf/src/protobuf/json/exceptions.cpp":"taxi/uservices/userver/libraries/protobuf/src/protobuf/json/exceptions.cpp",
32343234
"libraries/protobuf/src/protobuf/json/impl/convert_utils.cpp":"taxi/uservices/userver/libraries/protobuf/src/protobuf/json/impl/convert_utils.cpp",

grpc/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ target_link_libraries(${PROJECT_NAME}-proto PUBLIC ${PROJECT_NAME}-deps)
9494
target_include_directories(${PROJECT_NAME}-proto SYSTEM PUBLIC $<BUILD_INTERFACE:${proto_include_paths}>)
9595
_userver_install_targets(COMPONENT grpc TARGETS ${PROJECT_NAME}-proto)
9696

97-
target_link_libraries(${PROJECT_NAME}-internal PUBLIC userver-core ${PROJECT_NAME}-proto)
97+
target_link_libraries(${PROJECT_NAME}-internal PUBLIC userver-core userver-protobuf ${PROJECT_NAME}-proto)
9898

9999
set(CHANNELZ_MIN_VERSION "1.17.0")
100100
if(${gRPC_VERSION} VERSION_GREATER_EQUAL ${CHANNELZ_MIN_VERSION} AND NOT GRPC_USE_SYSTEM_PACKAGE)

grpc/include/userver/ugrpc/client/impl/codegen_definitions.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <string>
1010
#include <utility>
1111

12+
#include <userver/protobuf/impl/descriptor_collector.hpp>
1213
#include <userver/ugrpc/client/impl/call_params.hpp>
1314
#include <userver/ugrpc/client/impl/client_data.hpp>
1415
#include <userver/ugrpc/client/impl/perform_unary_call.hpp>
15-
#include <userver/ugrpc/impl/protobuf_collector.hpp>

grpc/include/userver/ugrpc/server/impl/codegen_definitions.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
#include <string_view>
1010
#include <utility>
1111

12-
#include <userver/ugrpc/impl/protobuf_collector.hpp>
12+
#include <userver/protobuf/impl/descriptor_collector.hpp>
1313
#include <userver/ugrpc/server/impl/service_internals.hpp>
1414
#include <userver/ugrpc/server/impl/service_worker_impl.hpp>

grpc/src/ugrpc/impl/protobuf_collector.cpp

Lines changed: 0 additions & 45 deletions
This file was deleted.

grpc/src/ugrpc/protobuf_visit.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <userver/utils/assert.hpp>
88
#include <userver/utils/impl/internal_tag.hpp>
99

10-
#include <userver/ugrpc/impl/protobuf_collector.hpp>
10+
#include <userver/protobuf/impl/descriptor_collector.hpp>
1111
#include <userver/ugrpc/protobuf_logging.hpp>
1212

1313
USERVER_NAMESPACE_BEGIN
@@ -154,13 +154,7 @@ DescriptorList GetNestedMessageDescriptors(const google::protobuf::Descriptor& d
154154
}
155155

156156
const google::protobuf::Descriptor* FindGeneratedMessage(std::string_view name) {
157-
const google::protobuf::DescriptorPool* pool = google::protobuf::DescriptorPool::generated_pool();
158-
UINVARIANT(pool, "pool is nullptr");
159-
#if GOOGLE_PROTOBUF_VERSION >= 4022000
160-
return pool->FindMessageTypeByName(name);
161-
#else
162-
return pool->FindMessageTypeByName(std::string(name));
163-
#endif
157+
return protobuf::impl::FindGeneratedMessage(name);
164158
}
165159

166160
const google::protobuf::FieldDescriptor* FindField(
@@ -234,7 +228,7 @@ void VisitorCompiler::Compile(const DescriptorList& descriptors) {
234228
}
235229
}
236230

237-
void VisitorCompiler::CompileAllGenerated() { Compile(impl::GetGeneratedMessages()); }
231+
void VisitorCompiler::CompileAllGenerated() { Compile(protobuf::impl::GetGeneratedMessages()); }
238232

239233
void VisitorCompiler::CompileGenerated(std::string_view message_name) { Compile(FindGeneratedMessage(message_name)); }
240234

grpc/tests/protobuf_collector_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <userver/ugrpc/impl/protobuf_collector.hpp>
1+
#include <userver/protobuf/impl/descriptor_collector.hpp>
22

33
#include <cstddef>
44
#include <vector>
@@ -12,7 +12,7 @@
1212
USERVER_NAMESPACE_BEGIN
1313

1414
TEST(GetGeneratedMessages, Ok) {
15-
const ugrpc::DescriptorList generated_message = ugrpc::impl::GetGeneratedMessages();
15+
const ugrpc::DescriptorList generated_message = protobuf::impl::GetGeneratedMessages();
1616

1717
// Please adjust this number if new test proto schemas are added.
1818
#if GOOGLE_PROTOBUF_VERSION >= 4022000

libraries/grpc-protovalidate/src/grpc-protovalidate/impl/utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include <fmt/format.h>
77

8-
#include <userver/ugrpc/impl/protobuf_collector.hpp>
8+
#include <userver/protobuf/impl/descriptor_collector.hpp>
99
#include <userver/utils/assert.hpp>
1010

1111
namespace {
@@ -95,7 +95,7 @@ std::unique_ptr<buf::validate::ValidatorFactory> CreateProtoValidatorFactory() {
9595
auto result = buf::validate::ValidatorFactory::New();
9696
UINVARIANT(result.ok(), "Failed to create validator factory");
9797
std::unique_ptr<buf::validate::ValidatorFactory> factory = std::move(result).value();
98-
for (const google::protobuf::Descriptor* descriptor : ugrpc::impl::GetGeneratedMessages()) {
98+
for (const google::protobuf::Descriptor* descriptor : protobuf::impl::GetGeneratedMessages()) {
9999
factory->Add(descriptor);
100100
}
101101
return factory;

grpc/include/userver/ugrpc/impl/protobuf_collector.hpp renamed to libraries/protobuf/include/userver/protobuf/impl/descriptor_collector.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <initializer_list>
44
#include <string>
5+
#include <string_view>
56
#include <vector>
67

78
namespace google::protobuf {
@@ -12,16 +13,19 @@ class Descriptor;
1213

1314
USERVER_NAMESPACE_BEGIN
1415

15-
namespace ugrpc::impl {
16+
namespace protobuf::impl {
1617

1718
/// @brief Registers multiple message types during static initialization time
1819
void RegisterMessageTypes(std::initializer_list<std::string> type_names);
1920

21+
/// @brief Find a generated message descriptor by fully-qualified name
22+
const google::protobuf::Descriptor* FindGeneratedMessage(std::string_view name);
23+
2024
/// @brief Find all known messages
2125
///
2226
/// @warning This is probably not an exhaustive list!
2327
std::vector<const google::protobuf::Descriptor*> GetGeneratedMessages();
2428

25-
} // namespace ugrpc::impl
29+
} // namespace protobuf::impl
2630

2731
USERVER_NAMESPACE_END
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <userver/protobuf/impl/descriptor_collector.hpp>
2+
3+
#include <unordered_set>
4+
5+
#include <google/protobuf/descriptor.h>
6+
#include <google/protobuf/descriptor_database.h>
7+
8+
#include <userver/utils/assert.hpp>
9+
#include <userver/utils/impl/static_registration.hpp>
10+
11+
USERVER_NAMESPACE_BEGIN
12+
13+
namespace protobuf::impl {
14+
15+
namespace {
16+
17+
std::unordered_set<std::string>& GetGeneratedMessagesImpl() {
18+
static std::unordered_set<std::string> messages;
19+
return messages;
20+
}
21+
22+
} // namespace
23+
24+
void RegisterMessageTypes(std::initializer_list<std::string> type_names) {
25+
utils::impl::AssertStaticRegistrationAllowed("Calling protobuf::impl::RegisterMessageTypes()");
26+
GetGeneratedMessagesImpl().merge(std::unordered_set<std::string>(type_names));
27+
}
28+
29+
const google::protobuf::Descriptor* FindGeneratedMessage(std::string_view name) {
30+
const google::protobuf::DescriptorPool* pool = google::protobuf::DescriptorPool::generated_pool();
31+
UINVARIANT(pool, "pool is nullptr");
32+
#if GOOGLE_PROTOBUF_VERSION >= 4022000
33+
return pool->FindMessageTypeByName(name);
34+
#else
35+
return pool->FindMessageTypeByName(std::string(name));
36+
#endif
37+
}
38+
39+
std::vector<const google::protobuf::Descriptor*> GetGeneratedMessages() {
40+
utils::impl::AssertStaticRegistrationFinished();
41+
42+
const auto& generated_messages = GetGeneratedMessagesImpl();
43+
std::vector<const google::protobuf::Descriptor*> result;
44+
result.reserve(generated_messages.size());
45+
for (const std::string& message_name : generated_messages) {
46+
const google::protobuf::Descriptor* descriptor = FindGeneratedMessage(message_name);
47+
UINVARIANT(descriptor, "descriptor is nullptr");
48+
result.push_back(descriptor);
49+
}
50+
return result;
51+
}
52+
53+
} // namespace protobuf::impl
54+
55+
USERVER_NAMESPACE_END

0 commit comments

Comments
 (0)