diff options
author | Jisi Liu <jisi.liu@gmail.com> | 2017-07-18 15:38:30 -0700 |
---|---|---|
committer | Jisi Liu <jisi.liu@gmail.com> | 2017-07-18 15:38:30 -0700 |
commit | 09354db1434859a31a3c81abebcc4018d42f2715 (patch) | |
tree | b87c7cdc2255e6c8062ab92b4082665cd698d753 /src/google/protobuf/util/internal | |
parent | 9053033a5076f82cf18b823c31f352e95e5bfd8d (diff) |
Merge from Google internal for 3.4 release
Diffstat (limited to 'src/google/protobuf/util/internal')
7 files changed, 49 insertions, 18 deletions
diff --git a/src/google/protobuf/util/internal/default_value_objectwriter.cc b/src/google/protobuf/util/internal/default_value_objectwriter.cc index 5763d0c6..95b3a17d 100644 --- a/src/google/protobuf/util/internal/default_value_objectwriter.cc +++ b/src/google/protobuf/util/internal/default_value_objectwriter.cc @@ -637,6 +637,7 @@ void DefaultValueObjectWriter::RenderDataPiece(StringPiece name, current_->AddChild(node.release()); } else { child->set_data(data); + child->set_is_placeholder(false); } } diff --git a/src/google/protobuf/util/internal/default_value_objectwriter.h b/src/google/protobuf/util/internal/default_value_objectwriter.h index ef2cc981..09c6d23f 100644 --- a/src/google/protobuf/util/internal/default_value_objectwriter.h +++ b/src/google/protobuf/util/internal/default_value_objectwriter.h @@ -172,7 +172,7 @@ class LIBPROTOBUF_EXPORT DefaultValueObjectWriter : public ObjectWriter { // If this node is a leaf (has data), writes the current node to the // ObjectWriter; if not, then recursively writes the children to the // ObjectWriter. - void WriteTo(ObjectWriter* ow); + virtual void WriteTo(ObjectWriter* ow); // Accessors const string& name() const { return name_; } @@ -262,6 +262,10 @@ class LIBPROTOBUF_EXPORT DefaultValueObjectWriter : public ObjectWriter { static DataPiece CreateDefaultDataPieceForField( const google::protobuf::Field& field, const TypeInfo* typeinfo); + protected: + // Returns a pointer to current Node in tree. + Node* current() { return current_; } + private: // Populates children of "node" if it is an "any" Node and its real type has // been given. diff --git a/src/google/protobuf/util/internal/protostream_objectsource.cc b/src/google/protobuf/util/internal/protostream_objectsource.cc index 025fbd40..02360a1a 100644 --- a/src/google/protobuf/util/internal/protostream_objectsource.cc +++ b/src/google/protobuf/util/internal/protostream_objectsource.cc @@ -125,6 +125,7 @@ ProtoStreamObjectSource::ProtoStreamObjectSource( recursion_depth_(0), max_recursion_depth_(kDefaultMaxRecursionDepth), render_unknown_fields_(false), + render_unknown_enum_values_(true), add_trailing_zeros_for_timestamp_and_duration_(false) { GOOGLE_LOG_IF(DFATAL, stream == NULL) << "Input stream is NULL."; } @@ -142,6 +143,7 @@ ProtoStreamObjectSource::ProtoStreamObjectSource( recursion_depth_(0), max_recursion_depth_(kDefaultMaxRecursionDepth), render_unknown_fields_(false), + render_unknown_enum_values_(true), add_trailing_zeros_for_timestamp_and_duration_(false) { GOOGLE_LOG_IF(DFATAL, stream == NULL) << "Input stream is NULL."; } @@ -866,12 +868,6 @@ Status ProtoStreamObjectSource::RenderNonMessageField( break; } - // No need to lookup enum type if we need to render int. - if (use_ints_for_enums_) { - ow->RenderInt32(field_name, buffer32); - break; - } - // Get the nested enum type for this field. // TODO(skarvaje): Avoid string manipulation. Find ways to speed this // up. @@ -883,14 +879,17 @@ Status ProtoStreamObjectSource::RenderNonMessageField( const google::protobuf::EnumValue* enum_value = FindEnumValueByNumber(*en, buffer32); if (enum_value != NULL) { - if (use_lower_camel_for_enums_) + if (use_ints_for_enums_) { + ow->RenderInt32(field_name, buffer32); + } else if (use_lower_camel_for_enums_) { ow->RenderString(field_name, ToCamelCase(enum_value->name())); - else + } else { ow->RenderString(field_name, enum_value->name()); - } else { + } + } else if (render_unknown_enum_values_) { ow->RenderInt32(field_name, buffer32); } - } else { + } else if (render_unknown_enum_values_) { ow->RenderInt32(field_name, buffer32); } break; diff --git a/src/google/protobuf/util/internal/protostream_objectsource.h b/src/google/protobuf/util/internal/protostream_objectsource.h index 58d77c2c..b56efdf4 100644 --- a/src/google/protobuf/util/internal/protostream_objectsource.h +++ b/src/google/protobuf/util/internal/protostream_objectsource.h @@ -309,6 +309,9 @@ class LIBPROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource { // Whether to render unknown fields. bool render_unknown_fields_; + // Whether to render unknown enum values. + bool render_unknown_enum_values_; + // Whether to add trailing zeros for timestamp and duration. bool add_trailing_zeros_for_timestamp_and_duration_; diff --git a/src/google/protobuf/util/internal/protostream_objectsource_test.cc b/src/google/protobuf/util/internal/protostream_objectsource_test.cc index 06c9bb6d..36bb1ba9 100644 --- a/src/google/protobuf/util/internal/protostream_objectsource_test.cc +++ b/src/google/protobuf/util/internal/protostream_objectsource_test.cc @@ -103,7 +103,8 @@ class ProtostreamObjectSourceTest ow_(&mock_), use_lower_camel_for_enums_(false), use_ints_for_enums_(false), - add_trailing_zeros_(false) { + add_trailing_zeros_(false), + render_unknown_enum_values_(true) { helper_.ResetTypeInfo(Book::descriptor(), Proto3Message::descriptor()); } @@ -276,6 +277,10 @@ class ProtostreamObjectSourceTest void AddTrailingZeros() { add_trailing_zeros_ = true; } + void SetRenderUnknownEnumValues(bool value) { + render_unknown_enum_values_ = value; + } + testing::TypeInfoTestHelper helper_; ::testing::NiceMock<MockObjectWriter> mock_; @@ -283,6 +288,7 @@ class ProtostreamObjectSourceTest bool use_lower_camel_for_enums_; bool use_ints_for_enums_; bool add_trailing_zeros_; + bool render_unknown_enum_values_; }; INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, @@ -513,12 +519,27 @@ TEST_P(ProtostreamObjectSourceTest, UseIntsForEnumsTest) { DoTest(book, Book::descriptor()); } -TEST_P(ProtostreamObjectSourceTest, UnknownEnum) { +TEST_P(ProtostreamObjectSourceTest, + UnknownEnumAreDroppedWhenRenderUnknownEnumValuesIsUnset) { Proto3Message message; message.set_enum_value(static_cast<Proto3Message::NestedEnum>(1234)); - ow_.StartObject("") - ->RenderInt32("enumValue", 1234) - ->EndObject(); + + SetRenderUnknownEnumValues(false); + + // Unknown enum values are not output. + ow_.StartObject("")->EndObject(); + DoTest(message, Proto3Message::descriptor()); +} + +TEST_P(ProtostreamObjectSourceTest, + UnknownEnumAreOutputWhenRenderUnknownEnumValuesIsSet) { + Proto3Message message; + message.set_enum_value(static_cast<Proto3Message::NestedEnum>(1234)); + + SetRenderUnknownEnumValues(true); + + // Unknown enum values are output. + ow_.StartObject("")->RenderInt32("enumValue", 1234)->EndObject(); DoTest(message, Proto3Message::descriptor()); } diff --git a/src/google/protobuf/util/internal/protostream_objectwriter.cc b/src/google/protobuf/util/internal/protostream_objectwriter.cc index d4e15bca..97f96819 100644 --- a/src/google/protobuf/util/internal/protostream_objectwriter.cc +++ b/src/google/protobuf/util/internal/protostream_objectwriter.cc @@ -962,7 +962,7 @@ Status ProtoStreamObjectWriter::RenderFieldMask(ProtoStreamObjectWriter* ow, // conversions as much as possible. Because ToSnakeCase sometimes returns the // wrong value. google::protobuf::scoped_ptr<ResultCallback1<util::Status, StringPiece> > callback( - NewPermanentCallback(&RenderOneFieldPath, ow)); + ::google::protobuf::NewPermanentCallback(&RenderOneFieldPath, ow)); return DecodeCompactFieldMaskPaths(data.str(), callback.get()); } diff --git a/src/google/protobuf/util/internal/utility.cc b/src/google/protobuf/util/internal/utility.cc index 8cf42e49..11780ee8 100644 --- a/src/google/protobuf/util/internal/utility.cc +++ b/src/google/protobuf/util/internal/utility.cc @@ -124,7 +124,10 @@ const StringPiece GetTypeWithoutUrl(StringPiece type_url) { return type_url.substr(kTypeUrlSize + 1); } else { size_t idx = type_url.rfind('/'); - return type_url.substr(idx + 1); + if (idx != type_url.npos) { + type_url.remove_prefix(idx + 1); + } + return type_url; } } |