21#include "absl/strings/str_cat.h"
22#include "google/protobuf/descriptor.h"
23#include "google/protobuf/message.h"
24#include "google/protobuf/text_format.h"
29using ::google::protobuf::Descriptor;
30using ::google::protobuf::FieldDescriptor;
31using ::google::protobuf::Reflection;
32using ::google::protobuf::TextFormat;
34void WriteFullProtocolMessage(
const google::protobuf::Message& message,
35 int indent_level, std::string* out) {
36 std::string temp_string;
37 const std::string indent(indent_level * 2,
' ');
38 const Descriptor* desc = message.GetDescriptor();
39 const Reflection* refl = message.GetReflection();
40 for (
int i = 0;
i < desc->field_count(); ++
i) {
41 const FieldDescriptor* fd = desc->field(i);
42 const bool repeated = fd->is_repeated();
43 const int start = repeated ? 0 : -1;
44 const int limit = repeated ? refl->FieldSize(message, fd) : 0;
45 for (
int j = start; j < limit; ++j) {
46 absl::StrAppend(out, indent, fd->name());
47 if (fd->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
48 absl::StrAppend(out,
" {\n");
49 const google::protobuf::Message& nested_message =
50 repeated ? refl->GetRepeatedMessage(message, fd, j)
51 : refl->GetMessage(message, fd);
52 WriteFullProtocolMessage(nested_message, indent_level + 1, out);
53 absl::StrAppend(out, indent,
"}\n");
55 TextFormat::PrintFieldValueToString(message, fd, j, &temp_string);
56 absl::StrAppend(out,
": ", temp_string,
"\n");
64 const google::protobuf::Message& message,
int indent_level) {
65 std::string message_str;
66 WriteFullProtocolMessage(message, indent_level, &message_str);
std::string FullProtocolMessageAsString(const google::protobuf::Message &message, int indent_level)