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