24 #include <unordered_set>
26 #include "absl/memory/memory.h"
27 #include "absl/strings/str_split.h"
38 std::replace(clean_input.begin(), clean_input.end(),
'/',
'.');
39 if (clean_input.size() > full_name.size()) {
42 return full_name.compare(full_name.size() - clean_input.size(),
43 clean_input.size(), clean_input) == 0;
53 std::ostringstream oss;
54 oss <<
"error " <<
filename <<
" " <<
line <<
" " << column <<
" "
61 std::cerr <<
"warning " <<
filename <<
" " <<
line <<
" " << column <<
" "
74 std::vector<std::string> service_list;
77 absl::make_unique<grpc::ProtoReflectionDescriptorDatabase>(
channel);
81 std::unordered_set<std::string> known_services;
82 if (!protofiles.empty()) {
88 importer_ = absl::make_unique<protobuf::compiler::Importer>(
92 std::stringstream ss(protofiles);
93 while (std::getline(ss, file_name,
',')) {
94 const auto* file_desc =
importer_->Import(file_name);
96 for (
int i = 0;
i < file_desc->service_count();
i++) {
98 known_services.insert(file_desc->service(
i)->full_name());
101 std::cerr << file_name <<
" not found" << std::endl;
106 absl::make_unique<protobuf::DescriptorPoolDatabase>(*
importer_->pool());
110 LogError(
"No available proto database");
119 desc_db_ = absl::make_unique<protobuf::MergedDescriptorDatabase>(
125 for (
auto it = service_list.begin();
it != service_list.end();
it++) {
126 if (known_services.find(*
it) == known_services.end()) {
130 known_services.insert(*
it);
148 const auto* service_desc = *
it;
149 for (
int j = 0; j < service_desc->method_count(); j++) {
150 const auto* method_desc = service_desc->method(j);
151 if (MethodNameMatch(method_desc->full_name(),
method)) {
152 if (method_descriptor) {
153 std::ostringstream error_stream;
154 error_stream <<
"Ambiguous method names: ";
155 error_stream << method_descriptor->
full_name() <<
" ";
156 error_stream << method_desc->full_name();
159 method_descriptor = method_desc;
163 if (!method_descriptor) {
181 size_t last_dot = formatted_method_name.find_last_of(
'.');
182 if (last_dot != std::string::npos) {
183 formatted_method_name[last_dot] =
'/';
185 formatted_method_name.insert(formatted_method_name.begin(),
'/');
186 return formatted_method_name;
197 desc_pool_->FindMethodByName(full_method_name);
216 desc_pool_->FindMethodByName(full_method_name);
228 bool is_request,
bool is_json_format) {
240 bool is_request,
bool is_json_format) {
252 bool is_json_format) {
256 desc_pool_->FindMessageTypeByName(message_type_name);
261 std::unique_ptr<grpc::protobuf::Message>
msg(
265 if (is_json_format) {
269 LogError(
"Failed to convert json format to proto.");
275 LogError(
"Failed to convert text format to proto.");
280 ok =
msg->SerializeToString(&serialized);
282 LogError(
"Failed to serialize proto.");
290 bool is_json_format) {
293 desc_pool_->FindMessageTypeByName(message_type_name);
298 std::unique_ptr<grpc::protobuf::Message>
msg(
300 if (!
msg->ParseFromString(serialized_proto)) {
301 LogError(
"Failed to deserialize proto.");
306 if (is_json_format) {
307 grpc::protobuf::json::JsonPrintOptions jsonPrintOptions;
308 jsonPrintOptions.add_whitespace =
true;
312 LogError(
"Failed to print proto message to json format");
317 LogError(
"Failed to print proto message to text format");
321 return formatted_string;
325 if (!error_msg.empty()) {
326 std::cerr << error_msg << std::endl;