diff options
Diffstat (limited to 'src/google/protobuf/util/internal')
14 files changed, 168 insertions, 59 deletions
diff --git a/src/google/protobuf/util/internal/default_value_objectwriter.h b/src/google/protobuf/util/internal/default_value_objectwriter.h index 6e71f9c8..7d245c9c 100644 --- a/src/google/protobuf/util/internal/default_value_objectwriter.h +++ b/src/google/protobuf/util/internal/default_value_objectwriter.h @@ -83,37 +83,37 @@ class LIBPROTOBUF_EXPORT DefaultValueObjectWriter : public ObjectWriter { virtual ~DefaultValueObjectWriter(); // ObjectWriter methods. - virtual DefaultValueObjectWriter* StartObject(StringPiece name); + virtual DefaultValueObjectWriter* StartObject(StringPiece name) override; - virtual DefaultValueObjectWriter* EndObject(); + virtual DefaultValueObjectWriter* EndObject() override; - virtual DefaultValueObjectWriter* StartList(StringPiece name); + virtual DefaultValueObjectWriter* StartList(StringPiece name) override; - virtual DefaultValueObjectWriter* EndList(); + virtual DefaultValueObjectWriter* EndList() override; - virtual DefaultValueObjectWriter* RenderBool(StringPiece name, bool value); + virtual DefaultValueObjectWriter* RenderBool(StringPiece name, bool value) override; - virtual DefaultValueObjectWriter* RenderInt32(StringPiece name, int32 value); + virtual DefaultValueObjectWriter* RenderInt32(StringPiece name, int32 value) override; virtual DefaultValueObjectWriter* RenderUint32(StringPiece name, - uint32 value); + uint32 value) override; - virtual DefaultValueObjectWriter* RenderInt64(StringPiece name, int64 value); + virtual DefaultValueObjectWriter* RenderInt64(StringPiece name, int64 value) override; virtual DefaultValueObjectWriter* RenderUint64(StringPiece name, - uint64 value); + uint64 value) override; virtual DefaultValueObjectWriter* RenderDouble(StringPiece name, - double value); + double value) override; - virtual DefaultValueObjectWriter* RenderFloat(StringPiece name, float value); + virtual DefaultValueObjectWriter* RenderFloat(StringPiece name, float value) override; virtual DefaultValueObjectWriter* RenderString(StringPiece name, - StringPiece value); + StringPiece value) override; virtual DefaultValueObjectWriter* RenderBytes(StringPiece name, - StringPiece value); + StringPiece value) override; - virtual DefaultValueObjectWriter* RenderNull(StringPiece name); + virtual DefaultValueObjectWriter* RenderNull(StringPiece name) override; // Register the callback for scrubbing of fields. Owership of // field_scrub_callback pointer is also transferred to this class diff --git a/src/google/protobuf/util/internal/error_listener.h b/src/google/protobuf/util/internal/error_listener.h index a19bd3f7..e3baa224 100644 --- a/src/google/protobuf/util/internal/error_listener.h +++ b/src/google/protobuf/util/internal/error_listener.h @@ -76,16 +76,16 @@ class LIBPROTOBUF_EXPORT ErrorListener { class LIBPROTOBUF_EXPORT NoopErrorListener : public ErrorListener { public: NoopErrorListener() {} - virtual ~NoopErrorListener() {} + virtual ~NoopErrorListener() override {} virtual void InvalidName(const LocationTrackerInterface& loc, - StringPiece invalid_name, StringPiece message) {} + StringPiece invalid_name, StringPiece message) override {} - virtual void InvalidValue(const LocationTrackerInterface& loc, - StringPiece type_name, StringPiece value) {} + virtual void InvalidValue(const LocationTrackerInterface &loc, StringPiece type_name, + StringPiece value) override {} - virtual void MissingField(const LocationTrackerInterface& loc, - StringPiece missing_name) {} + virtual void MissingField(const LocationTrackerInterface &loc, + StringPiece missing_name) override {} private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(NoopErrorListener); diff --git a/src/google/protobuf/util/internal/json_objectwriter.h b/src/google/protobuf/util/internal/json_objectwriter.h index 81644dab..4c25b465 100644 --- a/src/google/protobuf/util/internal/json_objectwriter.h +++ b/src/google/protobuf/util/internal/json_objectwriter.h @@ -94,20 +94,20 @@ class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter { virtual ~JsonObjectWriter(); // ObjectWriter methods. - virtual JsonObjectWriter* StartObject(StringPiece name); - virtual JsonObjectWriter* EndObject(); - virtual JsonObjectWriter* StartList(StringPiece name); - virtual JsonObjectWriter* EndList(); - virtual JsonObjectWriter* RenderBool(StringPiece name, bool value); - virtual JsonObjectWriter* RenderInt32(StringPiece name, int32 value); - virtual JsonObjectWriter* RenderUint32(StringPiece name, uint32 value); - virtual JsonObjectWriter* RenderInt64(StringPiece name, int64 value); - virtual JsonObjectWriter* RenderUint64(StringPiece name, uint64 value); - virtual JsonObjectWriter* RenderDouble(StringPiece name, double value); - virtual JsonObjectWriter* RenderFloat(StringPiece name, float value); - virtual JsonObjectWriter* RenderString(StringPiece name, StringPiece value); - virtual JsonObjectWriter* RenderBytes(StringPiece name, StringPiece value); - virtual JsonObjectWriter* RenderNull(StringPiece name); + virtual JsonObjectWriter* StartObject(StringPiece name) override; + virtual JsonObjectWriter* EndObject() override; + virtual JsonObjectWriter* StartList(StringPiece name) override; + virtual JsonObjectWriter* EndList() override; + virtual JsonObjectWriter* RenderBool(StringPiece name, bool value) override; + virtual JsonObjectWriter* RenderInt32(StringPiece name, int32 value) override; + virtual JsonObjectWriter* RenderUint32(StringPiece name, uint32 value) override; + virtual JsonObjectWriter* RenderInt64(StringPiece name, int64 value) override; + virtual JsonObjectWriter* RenderUint64(StringPiece name, uint64 value) override; + virtual JsonObjectWriter* RenderDouble(StringPiece name, double value) override; + virtual JsonObjectWriter* RenderFloat(StringPiece name, float value) override; + virtual JsonObjectWriter* RenderString(StringPiece name, StringPiece value) override; + virtual JsonObjectWriter* RenderBytes(StringPiece name, StringPiece value) override; + virtual JsonObjectWriter* RenderNull(StringPiece name) override; virtual JsonObjectWriter* RenderNullAsEmpty(StringPiece name); void set_use_websafe_base64_for_bytes(bool value) { @@ -143,17 +143,17 @@ class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter { GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(Element); }; - virtual Element* element() { return element_.get(); } + Element* element() override { return element_.get(); } private: class LIBPROTOBUF_EXPORT ByteSinkWrapper : public strings::ByteSink { public: explicit ByteSinkWrapper(google::protobuf::io::CodedOutputStream* stream) : stream_(stream) {} - virtual ~ByteSinkWrapper() {} + ~ByteSinkWrapper() override {} // ByteSink methods. - virtual void Append(const char* bytes, size_t n) { + void Append(const char* bytes, size_t n) override { stream_->WriteRaw(bytes, n); } diff --git a/src/google/protobuf/util/internal/object_location_tracker.h b/src/google/protobuf/util/internal/object_location_tracker.h index 8586cecc..f9b90bc4 100644 --- a/src/google/protobuf/util/internal/object_location_tracker.h +++ b/src/google/protobuf/util/internal/object_location_tracker.h @@ -47,10 +47,10 @@ class ObjectLocationTracker : public LocationTrackerInterface { // Creates an empty location tracker. ObjectLocationTracker() {} - virtual ~ObjectLocationTracker() {} + ~ObjectLocationTracker() override {} // Returns empty because nothing is tracked. - virtual string ToString() const { return ""; } + string ToString() const override { return ""; } private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ObjectLocationTracker); diff --git a/src/google/protobuf/util/internal/proto_writer.h b/src/google/protobuf/util/internal/proto_writer.h index 28496963..5bdafcc9 100644 --- a/src/google/protobuf/util/internal/proto_writer.h +++ b/src/google/protobuf/util/internal/proto_writer.h @@ -78,7 +78,7 @@ class LIBPROTOBUF_EXPORT ProtoWriter : public StructuredObjectWriter { // Constructor. Does not take ownership of any parameter passed in. ProtoWriter(TypeResolver* type_resolver, const google::protobuf::Type& type, strings::ByteSink* output, ErrorListener* listener); - virtual ~ProtoWriter(); + virtual ~ProtoWriter() override; // ObjectWriter methods. ProtoWriter* StartObject(StringPiece name) override; @@ -110,7 +110,7 @@ class LIBPROTOBUF_EXPORT ProtoWriter : public StructuredObjectWriter { return RenderDataPiece(name, DataPiece(value, use_strict_base64_decoding())); } - virtual ProtoWriter* RenderBytes(StringPiece name, StringPiece value) { + ProtoWriter* RenderBytes(StringPiece name, StringPiece value) override { return RenderDataPiece( name, DataPiece(value, false, use_strict_base64_decoding())); } @@ -163,7 +163,7 @@ class LIBPROTOBUF_EXPORT ProtoWriter : public StructuredObjectWriter { ProtoElement(ProtoElement* parent, const google::protobuf::Field* field, const google::protobuf::Type& type, bool is_list); - virtual ~ProtoElement() {} + virtual ~ProtoElement() override {} // Called just before the destructor for clean up: // - reports any missing required fields @@ -183,9 +183,9 @@ class LIBPROTOBUF_EXPORT ProtoWriter : public StructuredObjectWriter { void RegisterField(const google::protobuf::Field* field); // To report location on error messages. - virtual string ToString() const; + virtual string ToString() const override; - virtual ProtoElement* parent() const { + virtual ProtoElement* parent() const override { return static_cast<ProtoElement*>(BaseElement::parent()); } diff --git a/src/google/protobuf/util/internal/protostream_objectsource.cc b/src/google/protobuf/util/internal/protostream_objectsource.cc index 56e6db12..b0d86c17 100644 --- a/src/google/protobuf/util/internal/protostream_objectsource.cc +++ b/src/google/protobuf/util/internal/protostream_objectsource.cc @@ -648,6 +648,13 @@ Status ProtoStreamObjectSource::RenderAny(const ProtoStreamObjectSource* os, // using a nested ProtoStreamObjectSource using our nested type information. ProtoStreamObjectSource nested_os(&in_stream, os->typeinfo_, *nested_type); + // TODO(htuch): This is somewhat fragile, since new options may be omitted. + // We should probably do this via the constructor or some object grouping + // options. + nested_os.set_use_lower_camel_for_enums(os->use_lower_camel_for_enums_); + nested_os.set_use_ints_for_enums(os->use_ints_for_enums_); + nested_os.set_preserve_proto_field_names(os->preserve_proto_field_names_); + // We manually call start and end object here so we can inject the @type. ow->StartObject(field_name); ow->RenderString("@type", type_url); diff --git a/src/google/protobuf/util/internal/protostream_objectsource.h b/src/google/protobuf/util/internal/protostream_objectsource.h index b56efdf4..acd081d9 100644 --- a/src/google/protobuf/util/internal/protostream_objectsource.h +++ b/src/google/protobuf/util/internal/protostream_objectsource.h @@ -78,9 +78,9 @@ class LIBPROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource { TypeResolver* type_resolver, const google::protobuf::Type& type); - virtual ~ProtoStreamObjectSource(); + virtual ~ProtoStreamObjectSource() override; - virtual util::Status NamedWriteTo(StringPiece name, ObjectWriter* ow) const; + virtual util::Status NamedWriteTo(StringPiece name, ObjectWriter* ow) const override; // Sets whether or not to use lowerCamelCase casing for enum values. If set to // false, enum values are output without any case conversions. diff --git a/src/google/protobuf/util/internal/protostream_objectsource_test.cc b/src/google/protobuf/util/internal/protostream_objectsource_test.cc index df790728..4d86b856 100644 --- a/src/google/protobuf/util/internal/protostream_objectsource_test.cc +++ b/src/google/protobuf/util/internal/protostream_objectsource_test.cc @@ -100,6 +100,7 @@ class ProtostreamObjectSourceTest ow_(&mock_), use_lower_camel_for_enums_(false), use_ints_for_enums_(false), + use_preserve_proto_field_names_(false), add_trailing_zeros_(false), render_unknown_enum_values_(true) { helper_.ResetTypeInfo(Book::descriptor(), Proto3Message::descriptor()); @@ -123,6 +124,7 @@ class ProtostreamObjectSourceTest helper_.NewProtoSource(&in_stream, GetTypeUrl(descriptor))); if (use_lower_camel_for_enums_) os->set_use_lower_camel_for_enums(true); if (use_ints_for_enums_) os->set_use_ints_for_enums(true); + if (use_preserve_proto_field_names_) os->set_preserve_proto_field_names(true); os->set_max_recursion_depth(64); return os->WriteTo(&mock_); } @@ -272,6 +274,8 @@ class ProtostreamObjectSourceTest void UseIntsForEnums() { use_ints_for_enums_ = true; } + void UsePreserveProtoFieldNames() { use_preserve_proto_field_names_ = true; } + void AddTrailingZeros() { add_trailing_zeros_ = true; } void SetRenderUnknownEnumValues(bool value) { @@ -284,6 +288,7 @@ class ProtostreamObjectSourceTest ExpectingObjectWriter ow_; bool use_lower_camel_for_enums_; bool use_ints_for_enums_; + bool use_preserve_proto_field_names_; bool add_trailing_zeros_; bool render_unknown_enum_values_; }; @@ -536,6 +541,16 @@ TEST_P(ProtostreamObjectSourceTest, UseIntsForEnumsTest) { DoTest(book, Book::descriptor()); } +TEST_P(ProtostreamObjectSourceTest, UsePreserveProtoFieldNames) { + Book book; + book.set_snake_field("foo"); + + UsePreserveProtoFieldNames(); + + ow_.StartObject("")->RenderString("snake_field", "foo")->EndObject(); + DoTest(book, Book::descriptor()); +} + TEST_P(ProtostreamObjectSourceTest, UnknownEnumAreDroppedWhenRenderUnknownEnumValuesIsUnset) { Proto3Message message; @@ -769,6 +784,69 @@ TEST_P(ProtostreamObjectSourceAnysTest, BasicAny) { DoTest(out, AnyOut::descriptor()); } +TEST_P(ProtostreamObjectSourceAnysTest, LowerCamelEnumOutputSnakeCase) { + AnyOut out; + ::google::protobuf::Any* any = out.mutable_any(); + + Book book; + book.set_type(Book::arts_and_photography); + any->PackFrom(book); + + UseLowerCamelForEnums(); + + ow_.StartObject("") + ->StartObject("any") + ->RenderString("@type", + "type.googleapis.com/google.protobuf.testing.Book") + ->RenderString("type", "artsAndPhotography") + ->EndObject() + ->EndObject(); + + DoTest(out, AnyOut::descriptor()); +} + +TEST_P(ProtostreamObjectSourceAnysTest, UseIntsForEnumsTest) { + AnyOut out; + ::google::protobuf::Any* any = out.mutable_any(); + + Book book; + book.set_type(Book::ACTION_AND_ADVENTURE); + any->PackFrom(book); + + UseIntsForEnums(); + + ow_.StartObject("") + ->StartObject("any") + ->RenderString("@type", + "type.googleapis.com/google.protobuf.testing.Book") + ->RenderInt32("type", 3) + ->EndObject() + ->EndObject(); + + DoTest(out, AnyOut::descriptor()); +} + +TEST_P(ProtostreamObjectSourceAnysTest, UsePreserveProtoFieldNames) { + AnyOut out; + ::google::protobuf::Any* any = out.mutable_any(); + + Book book; + book.set_snake_field("foo"); + any->PackFrom(book); + + UsePreserveProtoFieldNames(); + + ow_.StartObject("") + ->StartObject("any") + ->RenderString("@type", + "type.googleapis.com/google.protobuf.testing.Book") + ->RenderString("snake_field", "foo") + ->EndObject() + ->EndObject(); + + DoTest(out, AnyOut::descriptor()); +} + TEST_P(ProtostreamObjectSourceAnysTest, RecursiveAny) { AnyOut out; ::google::protobuf::Any* any = out.mutable_any(); diff --git a/src/google/protobuf/util/internal/protostream_objectwriter.cc b/src/google/protobuf/util/internal/protostream_objectwriter.cc index 2edfd075..a1a7030a 100644 --- a/src/google/protobuf/util/internal/protostream_objectwriter.cc +++ b/src/google/protobuf/util/internal/protostream_objectwriter.cc @@ -534,7 +534,7 @@ ProtoStreamObjectWriter* ProtoStreamObjectWriter::StartObject( Push("", Item::MESSAGE, false, false); ProtoWriter::RenderDataPiece("key", DataPiece(name, use_strict_base64_decoding())); - Push("value", Item::MESSAGE, true, false); + Push("value", IsAny(*Lookup("value")) ? Item::ANY : Item::MESSAGE, true, false); // Make sure we are valid so far after starting map fields. if (invalid_depth() > 0) return this; diff --git a/src/google/protobuf/util/internal/protostream_objectwriter.h b/src/google/protobuf/util/internal/protostream_objectwriter.h index c33a4639..d9bb432e 100644 --- a/src/google/protobuf/util/internal/protostream_objectwriter.h +++ b/src/google/protobuf/util/internal/protostream_objectwriter.h @@ -112,18 +112,18 @@ class LIBPROTOBUF_EXPORT ProtoStreamObjectWriter : public ProtoWriter { strings::ByteSink* output, ErrorListener* listener, const ProtoStreamObjectWriter::Options& options = ProtoStreamObjectWriter::Options::Defaults()); - virtual ~ProtoStreamObjectWriter(); + virtual ~ProtoStreamObjectWriter() override; // ObjectWriter methods. - virtual ProtoStreamObjectWriter* StartObject(StringPiece name); - virtual ProtoStreamObjectWriter* EndObject(); - virtual ProtoStreamObjectWriter* StartList(StringPiece name); - virtual ProtoStreamObjectWriter* EndList(); + virtual ProtoStreamObjectWriter* StartObject(StringPiece name) override; + virtual ProtoStreamObjectWriter* EndObject() override; + virtual ProtoStreamObjectWriter* StartList(StringPiece name) override; + virtual ProtoStreamObjectWriter* EndList() override; // Renders a DataPiece 'value' into a field whose wire type is determined // from the given field 'name'. virtual ProtoStreamObjectWriter* RenderDataPiece(StringPiece name, - const DataPiece& value); + const DataPiece& value) override; protected: // Function that renders a well known type with modified behavior. @@ -263,7 +263,7 @@ class LIBPROTOBUF_EXPORT ProtoStreamObjectWriter : public ProtoWriter { // Constructor for a field of a message. Item(Item* parent, ItemType item_type, bool is_placeholder, bool is_list); - virtual ~Item() {} + virtual ~Item() override {} // These functions return true if the element type is corresponding to the // type in function name. @@ -272,7 +272,7 @@ class LIBPROTOBUF_EXPORT ProtoStreamObjectWriter : public ProtoWriter { AnyWriter* any() const { return any_.get(); } - virtual Item* parent() const { + virtual Item* parent() const override { return static_cast<Item*>(BaseElement::parent()); } diff --git a/src/google/protobuf/util/internal/protostream_objectwriter_test.cc b/src/google/protobuf/util/internal/protostream_objectwriter_test.cc index 7f0df567..9c50e7dd 100644 --- a/src/google/protobuf/util/internal/protostream_objectwriter_test.cc +++ b/src/google/protobuf/util/internal/protostream_objectwriter_test.cc @@ -1679,6 +1679,22 @@ TEST_P(ProtoStreamObjectWriterMapTest, RepeatedMapKeyTest) { ->EndObject(); } +TEST_P(ProtoStreamObjectWriterMapTest, AnyInMap) { + MapIn mm; + google::protobuf::DoubleValue d; + d.set_value(40.2); + (*mm.mutable_map_any())["foo"].PackFrom(d); + ow_->StartObject("") + ->StartObject("map_any") + ->StartObject("foo") + ->RenderString("@type", "type.googleapis.com/google.protobuf.DoubleValue") + ->RenderDouble("value", 40.2) + ->EndObject() + ->EndObject() + ->EndObject(); + CheckOutput(mm); +} + class ProtoStreamObjectWriterAnyTest : public BaseProtoStreamObjectWriterTest { protected: ProtoStreamObjectWriterAnyTest() { diff --git a/src/google/protobuf/util/internal/testdata/books.proto b/src/google/protobuf/util/internal/testdata/books.proto index 5630cc78..5e08a291 100644 --- a/src/google/protobuf/util/internal/testdata/books.proto +++ b/src/google/protobuf/util/internal/testdata/books.proto @@ -69,6 +69,9 @@ message Book { } optional Type type = 11; + // Useful for testing JSON snake/camel-case conversions. + optional string snake_field = 12; + extensions 200 to 499; } diff --git a/src/google/protobuf/util/internal/testdata/maps.proto b/src/google/protobuf/util/internal/testdata/maps.proto index 0f381b32..765254ea 100644 --- a/src/google/protobuf/util/internal/testdata/maps.proto +++ b/src/google/protobuf/util/internal/testdata/maps.proto @@ -32,6 +32,8 @@ syntax = "proto3"; package google.protobuf.testing; +import "google/protobuf/any.proto"; + // Top-level test cases proto used by MarshallingTest. See description // at the top of the class MarshallingTest for details on how to write // test cases. @@ -103,6 +105,7 @@ message MapIn { string other = 1; repeated string things = 2; map<string, string> map_input = 3; + map<string, google.protobuf.Any> map_any = 4; } message MapOut { diff --git a/src/google/protobuf/util/internal/type_info.cc b/src/google/protobuf/util/internal/type_info.cc index 3847b179..e32e71d0 100644 --- a/src/google/protobuf/util/internal/type_info.cc +++ b/src/google/protobuf/util/internal/type_info.cc @@ -59,7 +59,7 @@ class TypeInfoForTypeResolver : public TypeInfo { } virtual util::StatusOr<const google::protobuf::Type*> ResolveTypeUrl( - StringPiece type_url) const { + StringPiece type_url) const override { std::map<StringPiece, StatusOrType>::iterator it = cached_types_.find(type_url); if (it != cached_types_.end()) { @@ -79,13 +79,13 @@ class TypeInfoForTypeResolver : public TypeInfo { } virtual const google::protobuf::Type* GetTypeByTypeUrl( - StringPiece type_url) const { + StringPiece type_url) const override { StatusOrType result = ResolveTypeUrl(type_url); return result.ok() ? result.ValueOrDie() : NULL; } virtual const google::protobuf::Enum* GetEnumByTypeUrl( - StringPiece type_url) const { + StringPiece type_url) const override { std::map<StringPiece, StatusOrEnum>::iterator it = cached_enums_.find(type_url); if (it != cached_enums_.end()) { @@ -105,8 +105,10 @@ class TypeInfoForTypeResolver : public TypeInfo { return result.ok() ? result.ValueOrDie() : NULL; } + virtual const google::protobuf::Field* FindField( - const google::protobuf::Type* type, StringPiece camel_case_name) const { + const google::protobuf::Type* type, + StringPiece camel_case_name) const override { std::map<const google::protobuf::Type*, CamelCaseNameTable>::const_iterator it = indexed_types_.find(type); const CamelCaseNameTable& camel_case_name_table = |