aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/util
diff options
context:
space:
mode:
authorGravatar Adam Cozzette <acozzette@google.com>2017-12-01 10:05:10 -0800
committerGravatar Adam Cozzette <acozzette@google.com>2017-12-01 10:05:10 -0800
commit92a7e778e7394386f413cec28d67a07630f784b1 (patch)
treecb5673c7c09b0d3905b48a24765d07e423bc5b30 /src/google/protobuf/util
parentce0a53273a400369932ba788d17500336a6ecaad (diff)
Integrated internal changes from Google
Diffstat (limited to 'src/google/protobuf/util')
-rw-r--r--src/google/protobuf/util/field_comparator.cc10
-rw-r--r--src/google/protobuf/util/field_comparator.h11
-rw-r--r--src/google/protobuf/util/field_mask_util.h9
-rw-r--r--src/google/protobuf/util/field_mask_util_test.cc6
-rw-r--r--src/google/protobuf/util/internal/json_escaping.h8
-rw-r--r--src/google/protobuf/util/internal/json_objectwriter.cc13
-rw-r--r--src/google/protobuf/util/internal/json_objectwriter.h42
-rw-r--r--src/google/protobuf/util/internal/json_objectwriter_test.cc6
-rw-r--r--src/google/protobuf/util/internal/object_writer.h7
-rw-r--r--src/google/protobuf/util/internal/protostream_objectsource.cc2
-rw-r--r--src/google/protobuf/util/internal/utility.cc5
-rw-r--r--src/google/protobuf/util/message_differencer.cc10
-rw-r--r--src/google/protobuf/util/message_differencer.h14
13 files changed, 86 insertions, 57 deletions
diff --git a/src/google/protobuf/util/field_comparator.cc b/src/google/protobuf/util/field_comparator.cc
index a1a56ee6..86ddf06a 100644
--- a/src/google/protobuf/util/field_comparator.cc
+++ b/src/google/protobuf/util/field_comparator.cc
@@ -36,6 +36,7 @@
#include <google/protobuf/descriptor.h>
#include <google/protobuf/message.h>
+#include <google/protobuf/util/message_differencer.h>
#include <google/protobuf/stubs/map_util.h>
#include <google/protobuf/stubs/mathlimits.h>
#include <google/protobuf/stubs/mathutil.h>
@@ -130,6 +131,15 @@ FieldComparator::ComparisonResult DefaultFieldComparator::Compare(
}
}
+bool DefaultFieldComparator::Compare(
+ MessageDifferencer* differencer,
+ const Message& message1,
+ const Message& message2,
+ const google::protobuf::util::FieldContext* field_context) {
+ return differencer->Compare(
+ message1, message2, field_context->parent_fields());
+}
+
void DefaultFieldComparator::SetDefaultFractionAndMargin(double fraction,
double margin) {
default_tolerance_ = Tolerance(fraction, margin);
diff --git a/src/google/protobuf/util/field_comparator.h b/src/google/protobuf/util/field_comparator.h
index 26a7ba4d..27ef4c77 100644
--- a/src/google/protobuf/util/field_comparator.h
+++ b/src/google/protobuf/util/field_comparator.h
@@ -35,6 +35,7 @@
#include <map>
#include <string>
+#include <vector>
#include <google/protobuf/stubs/common.h>
@@ -48,6 +49,7 @@ class FieldDescriptor;
namespace util {
class FieldContext;
+class MessageDifferencer;
// Base class specifying the interface for comparing protocol buffer fields.
// Regular users should consider using or subclassing DefaultFieldComparator
@@ -153,6 +155,15 @@ class LIBPROTOBUF_EXPORT DefaultFieldComparator : public FieldComparator {
// REQUIRES: float_comparison_ == APPROXIMATE
void SetDefaultFractionAndMargin(double fraction, double margin);
+ protected:
+ // Compare using the provided message_differencer. For example, a subclass can
+ // use this method to compare some field in a certain way using the same
+ // message_differencer instance and the field context.
+ bool Compare(MessageDifferencer* differencer,
+ const google::protobuf::Message& message1,
+ const google::protobuf::Message& message2,
+ const google::protobuf::util::FieldContext* field_context);
+
private:
// Defines the tolerance for floating point comparison (fraction and margin).
struct Tolerance {
diff --git a/src/google/protobuf/util/field_mask_util.h b/src/google/protobuf/util/field_mask_util.h
index 01642c6f..91787bd5 100644
--- a/src/google/protobuf/util/field_mask_util.h
+++ b/src/google/protobuf/util/field_mask_util.h
@@ -94,6 +94,13 @@ class LIBPROTOBUF_EXPORT FieldMaskUtil {
// Creates a FieldMask with all fields of type T. This FieldMask only
// contains fields of T but not any sub-message fields.
template <typename T>
+ static FieldMask GetFieldMaskForAllFields() {
+ FieldMask out;
+ InternalGetFieldMaskForAllFields(T::descriptor(), &out);
+ return out;
+ }
+ template <typename T>
+ PROTOBUF_RUNTIME_DEPRECATED("Use *out = GetFieldMaskForAllFields() instead")
static void GetFieldMaskForAllFields(FieldMask* out) {
InternalGetFieldMaskForAllFields(T::descriptor(), out);
}
@@ -122,6 +129,8 @@ class LIBPROTOBUF_EXPORT FieldMaskUtil {
// Returns true if path is covered by the given FieldMask. Note that path
// "foo.bar" covers all paths like "foo.bar.baz", "foo.bar.quz.x", etc.
+ // Also note that parent paths are not covered by explicit child path, i.e.
+ // "foo.bar" does NOT cover "foo", even if "bar" is the only child.
static bool IsPathInFieldMask(StringPiece path, const FieldMask& mask);
class MergeOptions;
diff --git a/src/google/protobuf/util/field_mask_util_test.cc b/src/google/protobuf/util/field_mask_util_test.cc
index 7939f733..0664d9e5 100644
--- a/src/google/protobuf/util/field_mask_util_test.cc
+++ b/src/google/protobuf/util/field_mask_util_test.cc
@@ -206,12 +206,12 @@ TEST(FieldMaskUtilTest, TestIsValidFieldMask) {
TEST(FieldMaskUtilTest, TestGetFieldMaskForAllFields) {
FieldMask mask;
- FieldMaskUtil::GetFieldMaskForAllFields<TestAllTypes::NestedMessage>(&mask);
+ mask = FieldMaskUtil::GetFieldMaskForAllFields<TestAllTypes::NestedMessage>();
EXPECT_EQ(1, mask.paths_size());
EXPECT_TRUE(FieldMaskUtil::IsPathInFieldMask("bb", mask));
- FieldMaskUtil::GetFieldMaskForAllFields<TestAllTypes>(&mask);
- EXPECT_EQ(76, mask.paths_size());
+ mask = FieldMaskUtil::GetFieldMaskForAllFields<TestAllTypes>();
+ EXPECT_EQ(75, mask.paths_size());
EXPECT_TRUE(FieldMaskUtil::IsPathInFieldMask("optional_int32", mask));
EXPECT_TRUE(FieldMaskUtil::IsPathInFieldMask("optional_int64", mask));
EXPECT_TRUE(FieldMaskUtil::IsPathInFieldMask("optional_uint32", mask));
diff --git a/src/google/protobuf/util/internal/json_escaping.h b/src/google/protobuf/util/internal/json_escaping.h
index 9b8b2afd..5495c57f 100644
--- a/src/google/protobuf/util/internal/json_escaping.h
+++ b/src/google/protobuf/util/internal/json_escaping.h
@@ -28,8 +28,8 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#ifndef NET_PROTO2_UTIL_CONVERTER_STRINGS_JSON_ESCAPING_H_
-#define NET_PROTO2_UTIL_CONVERTER_STRINGS_JSON_ESCAPING_H_
+#ifndef GOOGLE_PROTOBUF_UTIL_INTERNAL__JSON_ESCAPING_H__
+#define GOOGLE_PROTOBUF_UTIL_INTERNAL__JSON_ESCAPING_H__
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/bytestream.h>
@@ -86,6 +86,6 @@ class JsonEscaping {
} // namespace converter
} // namespace util
} // namespace protobuf
-} // namespace google
-#endif // NET_PROTO2_UTIL_CONVERTER_STRINGS_JSON_ESCAPING_H_
+} // namespace google
+#endif // GOOGLE_PROTOBUF_UTIL_INTERNAL__JSON_ESCAPING_H__
diff --git a/src/google/protobuf/util/internal/json_objectwriter.cc b/src/google/protobuf/util/internal/json_objectwriter.cc
index 6e4edd88..a431177a 100644
--- a/src/google/protobuf/util/internal/json_objectwriter.cc
+++ b/src/google/protobuf/util/internal/json_objectwriter.cc
@@ -57,7 +57,7 @@ JsonObjectWriter::~JsonObjectWriter() {
JsonObjectWriter* JsonObjectWriter::StartObject(StringPiece name) {
WritePrefix(name);
WriteChar('{');
- Push();
+ PushObject();
return this;
}
@@ -71,7 +71,7 @@ JsonObjectWriter* JsonObjectWriter::EndObject() {
JsonObjectWriter* JsonObjectWriter::StartList(StringPiece name) {
WritePrefix(name);
WriteChar('[');
- Push();
+ PushArray();
return this;
}
@@ -172,8 +172,7 @@ void JsonObjectWriter::WritePrefix(StringPiece name) {
bool not_first = !element()->is_first();
if (not_first) WriteChar(',');
if (not_first || !element()->is_root()) NewLine();
- bool empty_key_ok = GetAndResetEmptyKeyOk();
- if (!name.empty() || empty_key_ok) {
+ if (!name.empty() || element()->is_json_object()) {
WriteChar('"');
if (!name.empty()) {
ArrayByteSource source(name);
@@ -184,12 +183,6 @@ void JsonObjectWriter::WritePrefix(StringPiece name) {
}
}
-bool JsonObjectWriter::GetAndResetEmptyKeyOk() {
- bool retval = empty_name_ok_for_next_key_;
- empty_name_ok_for_next_key_ = false;
- return retval;
-}
-
} // namespace converter
} // namespace util
} // namespace protobuf
diff --git a/src/google/protobuf/util/internal/json_objectwriter.h b/src/google/protobuf/util/internal/json_objectwriter.h
index 31edc292..81da0500 100644
--- a/src/google/protobuf/util/internal/json_objectwriter.h
+++ b/src/google/protobuf/util/internal/json_objectwriter.h
@@ -89,12 +89,11 @@ class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter {
public:
JsonObjectWriter(StringPiece indent_string,
google::protobuf::io::CodedOutputStream* out)
- : element_(new Element(NULL)),
+ : element_(new Element(/*parent=*/NULL, /*is_json_object=*/false)),
stream_(out),
sink_(out),
indent_string_(indent_string.ToString()),
- use_websafe_base64_for_bytes_(false),
- empty_name_ok_for_next_key_(false) {}
+ use_websafe_base64_for_bytes_(false) {}
virtual ~JsonObjectWriter();
// ObjectWriter methods.
@@ -118,17 +117,13 @@ class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter {
use_websafe_base64_for_bytes_ = value;
}
- // Whether empty strings should be rendered for the next JSON key. This
- // setting is only valid until the next key is rendered, after which it gets
- // reset to false.
- virtual void empty_name_ok_for_next_key() {
- empty_name_ok_for_next_key_ = true;
- }
-
protected:
class LIBPROTOBUF_EXPORT Element : public BaseElement {
public:
- explicit Element(Element* parent) : BaseElement(parent), is_first_(true) {}
+ Element(Element* parent, bool is_json_object)
+ : BaseElement(parent),
+ is_first_(true),
+ is_json_object_(is_json_object) {}
// Called before each field of the Element is to be processed.
// Returns true if this is the first call (processing the first field).
@@ -140,8 +135,13 @@ class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter {
return false;
}
+ // Whether we are currently renderring inside a JSON object (i.e., between
+ // StartObject() and EndObject()).
+ bool is_json_object() const { return is_json_object_; }
+
private:
bool is_first_;
+ bool is_json_object_;
GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(Element);
};
@@ -175,8 +175,15 @@ class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter {
return this;
}
- // Pushes a new element to the stack.
- void Push() { element_.reset(new Element(element_.release())); }
+ // Pushes a new JSON array element to the stack.
+ void PushArray() {
+ element_.reset(new Element(element_.release(), /*is_json_object=*/false));
+ }
+
+ // Pushes a new JSON object element to the stack.
+ void PushObject() {
+ element_.reset(new Element(element_.release(), /*is_json_object=*/true));
+ }
// Pops an element off of the stack and deletes the popped element.
void Pop() {
@@ -204,10 +211,6 @@ class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter {
// Writes an individual character to the output.
void WriteChar(const char c) { stream_->WriteRaw(&c, sizeof(c)); }
- // Returns the current value of empty_name_ok_for_next_key_ and resets it to
- // false.
- bool GetAndResetEmptyKeyOk();
-
google::protobuf::scoped_ptr<Element> element_;
google::protobuf::io::CodedOutputStream* stream_;
ByteSinkWrapper sink_;
@@ -217,11 +220,6 @@ class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter {
// to regular base64 encoding.
bool use_websafe_base64_for_bytes_;
- // Whether empty strings should be rendered for the next JSON key. This
- // setting is only valid until the next key is rendered, after which it gets
- // reset to false.
- bool empty_name_ok_for_next_key_;
-
GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(JsonObjectWriter);
};
diff --git a/src/google/protobuf/util/internal/json_objectwriter_test.cc b/src/google/protobuf/util/internal/json_objectwriter_test.cc
index bbd9d78a..8cc588a6 100644
--- a/src/google/protobuf/util/internal/json_objectwriter_test.cc
+++ b/src/google/protobuf/util/internal/json_objectwriter_test.cc
@@ -95,6 +95,12 @@ TEST_F(JsonObjectWriterTest, EmptyList) {
output_.substr(0, out_stream_->ByteCount()));
}
+TEST_F(JsonObjectWriterTest, EmptyObjectKey) {
+ ow_ = new JsonObjectWriter("", out_stream_);
+ ow_->StartObject("")->RenderString("", "value")->EndObject();
+ EXPECT_EQ("{\"\":\"value\"}", output_.substr(0, out_stream_->ByteCount()));
+}
+
TEST_F(JsonObjectWriterTest, ObjectInObject) {
ow_ = new JsonObjectWriter("", out_stream_);
ow_->StartObject("")
diff --git a/src/google/protobuf/util/internal/object_writer.h b/src/google/protobuf/util/internal/object_writer.h
index b6fbd19b..5781aa1e 100644
--- a/src/google/protobuf/util/internal/object_writer.h
+++ b/src/google/protobuf/util/internal/object_writer.h
@@ -119,13 +119,6 @@ class LIBPROTOBUF_EXPORT ObjectWriter {
return use_strict_base64_decoding_;
}
- // Whether empty strings should be rendered for the next name for Start/Render
- // calls. This setting is only valid until the next key is rendered, after
- // which it gets reset.
- // It is up to the derived classes to interpret this and render accordingly.
- // Default implementation ignores this setting.
- virtual void empty_name_ok_for_next_key() {}
-
protected:
ObjectWriter() : use_strict_base64_decoding_(true) {}
diff --git a/src/google/protobuf/util/internal/protostream_objectsource.cc b/src/google/protobuf/util/internal/protostream_objectsource.cc
index 02360a1a..72ad5a7a 100644
--- a/src/google/protobuf/util/internal/protostream_objectsource.cc
+++ b/src/google/protobuf/util/internal/protostream_objectsource.cc
@@ -300,8 +300,6 @@ StatusOr<uint32> ProtoStreamObjectSource::RenderMap(
return Status(util::error::INTERNAL, "Invalid map entry.");
}
ASSIGN_OR_RETURN(map_key, MapKeyDefaultValueAsString(*key_field));
- // Key is empty, force it to render as empty (for string values).
- ow->empty_name_ok_for_next_key();
}
RETURN_IF_ERROR(RenderField(field, map_key, ow));
} else {
diff --git a/src/google/protobuf/util/internal/utility.cc b/src/google/protobuf/util/internal/utility.cc
index 11780ee8..b1007765 100644
--- a/src/google/protobuf/util/internal/utility.cc
+++ b/src/google/protobuf/util/internal/utility.cc
@@ -353,8 +353,6 @@ bool IsMap(const google::protobuf::Field& field,
google::protobuf::Field_Cardinality_CARDINALITY_REPEATED &&
(GetBoolOptionOrDefault(type.options(), "map_entry", false) ||
GetBoolOptionOrDefault(type.options(),
- "google.protobuf.MessageOptions.map_entry", false) ||
- GetBoolOptionOrDefault(type.options(),
"google.protobuf.MessageOptions.map_entry",
false));
}
@@ -362,9 +360,6 @@ bool IsMap(const google::protobuf::Field& field,
bool IsMessageSetWireFormat(const google::protobuf::Type& type) {
return GetBoolOptionOrDefault(type.options(), "message_set_wire_format",
false) ||
- GetBoolOptionOrDefault(type.options(),
- "google.protobuf.MessageOptions.message_set_wire_format",
- false) ||
GetBoolOptionOrDefault(
type.options(),
"google.protobuf.MessageOptions.message_set_wire_format", false);
diff --git a/src/google/protobuf/util/message_differencer.cc b/src/google/protobuf/util/message_differencer.cc
index e964e041..c32e941c 100644
--- a/src/google/protobuf/util/message_differencer.cc
+++ b/src/google/protobuf/util/message_differencer.cc
@@ -363,9 +363,6 @@ void MessageDifferencer::TreatAsMapUsingKeyComparator(
const MapKeyComparator* key_comparator) {
GOOGLE_CHECK(field->is_repeated()) << "Field must be repeated: "
<< field->full_name();
- GOOGLE_CHECK_EQ(FieldDescriptor::CPPTYPE_MESSAGE, field->cpp_type())
- << "Field has to be message type. Field name is: "
- << field->full_name();
GOOGLE_CHECK(set_fields_.find(field) == set_fields_.end())
<< "Cannot treat this repeated field as both Map and Set for "
<< "comparison.";
@@ -1751,6 +1748,13 @@ void MessageDifferencer::StreamReporter::ReportUnknownFieldIgnored(
printer_->Print("\n"); // Print for newlines.
}
+MessageDifferencer::MapKeyComparator*
+MessageDifferencer::CreateMultipleFieldsMapKeyComparator(
+ const std::vector<std::vector<const FieldDescriptor*> >& key_field_paths) {
+ return new MultipleFieldsMapKeyComparator(this, key_field_paths);
+}
+
+
} // namespace util
} // namespace protobuf
} // namespace google
diff --git a/src/google/protobuf/util/message_differencer.h b/src/google/protobuf/util/message_differencer.h
index b7d4de00..11d6cc03 100644
--- a/src/google/protobuf/util/message_differencer.h
+++ b/src/google/protobuf/util/message_differencer.h
@@ -64,6 +64,7 @@ class Printer;
namespace util {
+class DefaultFieldComparator;
class FieldContext; // declared below MessageDifferencer
// A basic differencer that can be used to determine
@@ -372,7 +373,7 @@ class LIBPROTOBUF_EXPORT MessageDifferencer {
// repeated fields have different numbers of elements, the
// unpaired elements are reported using ReportAdded() or
// ReportDeleted().
- AS_SET, // Treat all the repeated fields as sets by default.
+ AS_SET, // Treat all the repeated fields as sets.
// See TreatAsSet(), as below.
};
@@ -386,6 +387,11 @@ class LIBPROTOBUF_EXPORT MessageDifferencer {
// the only differences between the compared messages is that some fields
// have been moved, then the comparison returns true.
//
+ // Note that despite the name of this method, this is really
+ // comparison as multisets: if one side of the comparison has a duplicate
+ // in the repeated field but the other side doesn't, this will count as
+ // a mismatch.
+ //
// If the scope of comparison is set to PARTIAL, then in addition to what's
// above, extra values added to repeated fields of the second message will
// not cause the comparison to fail.
@@ -470,6 +476,10 @@ class LIBPROTOBUF_EXPORT MessageDifferencer {
const FieldDescriptor* field,
const MapKeyComparator* key_comparator);
+ // Initiates and returns a new instance of MultipleFieldsMapKeyComparator.
+ MapKeyComparator* CreateMultipleFieldsMapKeyComparator(
+ const std::vector<std::vector<const FieldDescriptor*> >& key_field_paths);
+
// Add a custom ignore criteria that is evaluated in addition to the
// ignored fields added with IgnoreField.
// Takes ownership of ignore_criteria.
@@ -660,6 +670,8 @@ class LIBPROTOBUF_EXPORT MessageDifferencer {
};
private:
+ friend class DefaultFieldComparator;
+
// A MapKeyComparator to be used in TreatAsMapUsingKeyComparator.
// Implementation of this class needs to do field value comparison which
// relies on some private methods of MessageDifferencer. That's why this