57 const google::protobuf::Reflection* reflection = msg.GetReflection();
58 const google::protobuf::Descriptor* descriptor = msg.GetDescriptor();
59 std::vector<const google::protobuf::FieldDescriptor*> fields;
60 if (descriptor->options().map_entry()) {
61 fields.push_back(descriptor->field(0));
62 fields.push_back(descriptor->field(1));
64 reflection->ListFields(msg, &fields);
67 auto fsize = fields.size();
69 for (
decltype(fsize) i = 0; i < fsize; ++i) {
77 const google::protobuf::Message& msg,
int screen_width,
78 const google::protobuf::FieldDescriptor* field,
79 const google::protobuf::Reflection* reflection,
bool is_folded) {
81 if (!is_folded && field->is_repeated()) {
82 int size = reflection->FieldSize(msg, field);
83 for (
int i = 0; i < size; ++i) {
84 switch (field->cpp_type()) {
85 case google::protobuf::FieldDescriptor::CPPTYPE_STRING: {
87 const std::string& value =
88 reflection->GetRepeatedStringReference(msg, field, i, &scratch);
89 ret += CalculateStringLines(value, screen_width);
93 case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
94 const google::protobuf::Message& child_msg =
95 reflection->GetRepeatedMessage(msg, field, i);
96 ret +=
LineCount(child_msg, screen_width);
106 if (!field->is_repeated()) {
107 switch (field->cpp_type()) {
108 case google::protobuf::FieldDescriptor::CPPTYPE_STRING: {
110 const std::string& value =
111 reflection->GetStringReference(msg, field, &scratch);
112 ret += CalculateStringLines(value, screen_width);
116 case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
117 const google::protobuf::Message& child_msg =
118 reflection->GetMessage(msg, field);
119 ret +=
LineCount(child_msg, screen_width);
132 const google::protobuf::Message& msg,
133 int* jump_lines,
const Screen* s,
134 int* line_no,
int indent) {
135 const google::protobuf::Reflection* reflection = msg.GetReflection();
136 const google::protobuf::Descriptor* descriptor = msg.GetDescriptor();
137 std::vector<const google::protobuf::FieldDescriptor*> fields;
138 if (descriptor->options().map_entry()) {
139 fields.push_back(descriptor->field(0));
140 fields.push_back(descriptor->field(1));
142 reflection->ListFields(msg, &fields);
144 for (std::size_t i = 0; i < fields.size(); ++i) {
148 const google::protobuf::FieldDescriptor* field = fields[i];
149 if (field->is_repeated()) {
153 std::ostringstream out_str;
154 const std::string& fieldName = field->name();
155 out_str << fieldName <<
": ";
156 out_str <<
"+[" << reflection->FieldSize(msg, field) <<
" items]";
170 const google::protobuf::UnknownFieldSet& unknown_fields =
171 reflection->GetUnknownFields(msg);
172 if (!unknown_fields.empty()) {
184 int* jump_lines,
const Screen* s,
int* line_no,
int indent,
185 const google::protobuf::Reflection* ref,
186 const google::protobuf::FieldDescriptor* field,
int index) {
187 std::ostringstream out_str;
188 std::ios_base::fmtflags old_flags;
190 switch (field->cpp_type()) {
191#define OUTPUT_FIELD(CPPTYPE, METHOD, PRECISION) \
192 case google::protobuf::FieldDescriptor::CPPTYPE_##CPPTYPE: \
196 const std::string& fieldName = field->name(); \
197 out_str << fieldName << ": "; \
198 if (field->is_repeated()) { \
199 out_str << "[" << index << "] "; \
201 old_flags = out_str.flags(); \
202 out_str << std::fixed << std::setprecision(PRECISION) \
203 << (field->is_repeated() \
204 ? ref->GetRepeated##METHOD(msg, field, index) \
205 : ref->Get##METHOD(msg, field)); \
206 out_str.flags(old_flags); \
207 s->AddStr(indent, (*line_no)++, out_str.str().c_str()); \
220 case google::protobuf::FieldDescriptor::CPPTYPE_STRING: {
222 const std::string& str =
224 ? ref->GetRepeatedStringReference(msg, field, index, &scratch)
225 : ref->GetStringReference(msg, field, &scratch);
230 for (; i < str.size() && *jump_lines > 0; ++i) {
231 if (str[i] ==
'\n' || str[i] ==
'\r') {
236 if (line_width == s->
Width()) {
243 if (*jump_lines == 0) {
245 unsigned line_count = 1;
247 const std::string& fieldName = field->name();
248 out_str << fieldName <<
": ";
249 if (field->is_repeated()) {
250 out_str <<
"[" << index <<
"] ";
253 for (; i < str.size(); ++i) {
255 if (str[i] ==
'\n' || str[i] ==
'\r') {
261 if (line_width == s->
Width()) {
270 (*line_no) += line_count;
277 case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: {
281 const std::string& fieldName = field->name();
282 out_str << fieldName <<
": ";
283 if (field->is_repeated()) {
284 out_str <<
"[" << index <<
"] ";
286 int enum_value = field->is_repeated()
287 ? ref->GetRepeatedEnumValue(msg, field, index)
288 : ref->GetEnumValue(msg, field);
289 const google::protobuf::EnumValueDescriptor* enum_desc =
290 field->enum_type()->FindValueByNumber(enum_value);
291 if (enum_desc !=
nullptr) {
292 out_str << enum_desc->name();
294 out_str << enum_value;
301 case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
303 const std::string& fieldName = field->name();
304 out_str << fieldName;
305 if (!field->is_map()) {
307 if (field->is_repeated()) {
308 out_str <<
"[" << index <<
"] ";
317 field->is_repeated() ? ref->GetRepeatedMessage(msg, field, index)
318 : ref->GetMessage(msg, field),
319 jump_lines, s,
line_no, indent + 2);
static void PrintField(GeneralMessageBase *baseMsg, const google::protobuf::Message &msg, int *jump_lines, const Screen *s, int *line_no, int indent, const google::protobuf::Reflection *ref, const google::protobuf::FieldDescriptor *field, int index)