On Tue, Nov 23, 2010 at 5:04 PM, Brenden Matthews wrote:
> Greetings,
>
> One of the best features of protocol buffers is the notion of presence.
> Another great feature is the ability to do 'message.PrintDebugString()'.
> One problem, however, is that fields which are not present do not get
> displayed when printing the debug string.
>
> Proposed solution: add an optional parameter to display optional fields and
> their default values (if applicable) even when they aren't present.
>
> Attached is a patch which implements this.
>
> Please provide comments/suggestions, and let me know if this is the right
> place for this.
>
I also rushed things a bit, and the first patch has a mistake. Here is a
fixed version of it.
--
You received this message because you are subscribed to the Google Groups
"Protocol Buffers" group.
To post to this group, send email to proto...@googlegroups.com.
To unsubscribe from this group, send email to
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/protobuf?hl=en.
From edf13be13766caa1a496c9feeced8d9d24de0c10 Mon Sep 17 00:00:00 2001
From: Brenden Matthews
Date: Tue, 23 Nov 2010 17:01:01 -0800
Subject: [PATCH] Display non-present fields with TextFormat.
This allows for the non-presence of optional fields to be represented in
string output for a message. This greatly aids in debugging
data-intensive applications which use protobuf.
---
protobuf/src/google/protobuf/descriptor.cc |2 +-
.../protobuf/generated_message_reflection.cc |9 +--
.../google/protobuf/generated_message_reflection.h |4 +-
protobuf/src/google/protobuf/message.h | 14 ++--
protobuf/src/google/protobuf/text_format.cc| 74
protobuf/src/google/protobuf/text_format.h | 26 ---
6 files changed, 76 insertions(+), 53 deletions(-)
diff --git a/protobuf/src/google/protobuf/descriptor.cc b/protobuf/src/google/protobuf/descriptor.cc
index b6f276d..9e8aa51 100644
--- a/protobuf/src/google/protobuf/descriptor.cc
+++ b/protobuf/src/google/protobuf/descriptor.cc
@@ -1507,7 +1507,7 @@ bool RetrieveOptions(const Message &options, vector *option_entries) {
for (int j = 0; j < count; j++) {
string fieldval;
TextFormat::PrintFieldValueToString(options, fields[i],
- repeated ? count : -1, &fieldval);
+ repeated ? count : -1, &fieldval, false);
option_entries->push_back(fields[i]->name() + " = " + fieldval);
}
}
diff --git a/protobuf/src/google/protobuf/generated_message_reflection.cc b/protobuf/src/google/protobuf/generated_message_reflection.cc
index 8428b75..aa7e628 100644
--- a/protobuf/src/google/protobuf/generated_message_reflection.cc
+++ b/protobuf/src/google/protobuf/generated_message_reflection.cc
@@ -640,7 +640,8 @@ struct FieldNumberSorter {
void GeneratedMessageReflection::ListFields(
const Message& message,
-vector* output) const {
+vector* output,
+const bool include_missing) const {
output->clear();
// Optimization: The default instance never has any fields set.
@@ -652,10 +653,8 @@ void GeneratedMessageReflection::ListFields(
if (FieldSize(message, field) > 0) {
output->push_back(field);
}
-} else {
- if (HasBit(message, field)) {
-output->push_back(field);
- }
+} else if (include_missing || HasBit(message, field)) {
+ output->push_back(field);
}
}
diff --git a/protobuf/src/google/protobuf/generated_message_reflection.h b/protobuf/src/google/protobuf/generated_message_reflection.h
index b545fa1..1475f9e 100644
--- a/protobuf/src/google/protobuf/generated_message_reflection.h
+++ b/protobuf/src/google/protobuf/generated_message_reflection.h
@@ -144,8 +144,8 @@ class LIBPROTOBUF_EXPORT GeneratedMessageReflection : public Reflection {
void Swap(Message* message1, Message* message2) const;
void SwapElements(Message* message, const FieldDescriptor* field,
int index1, int index2) const;
- void ListFields(const Message& message,
- vector* output) const;
+ void ListFields(const Message& message, vector*
+ output, const bool include_missing = false) const;
int32 GetInt32 (const Message& message,
const FieldDescriptor* field) const;
diff --git a/protobuf/src/google/protobuf/message.h b/protobuf/src/google/protobuf/message.h
index c0062f9..988f50c 100644
--- a/protobuf/src/google/protobuf/message.h
+++ b/protobuf/src/google/protobuf/message.h
@@ -243,13 +243,13 @@ class LIBPROTOBUF_EXPORT Message : public MessageLite {
// Generates a human readable form of this message, useful for debugging
// and other purposes.
- string DebugString() const;
+ string DebugString(const bool show_missing = false) const;
// Like DebugString(), but with less whitespace.
string ShortDebugString() con