| OLD | NEW |
| (Empty) | |
| 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ |
| 4 // |
| 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are |
| 7 // met: |
| 8 // |
| 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. |
| 11 // * Redistributions in binary form must reproduce the above |
| 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the |
| 14 // distribution. |
| 15 // * Neither the name of Google Inc. nor the names of its |
| 16 // contributors may be used to endorse or promote products derived from |
| 17 // this software without specific prior written permission. |
| 18 // |
| 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
| 31 #ifndef GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__ |
| 32 #define GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__ |
| 33 |
| 34 #include <string> |
| 35 |
| 36 #include <google/protobuf/descriptor.h> |
| 37 #include <google/protobuf/field_mask.pb.h> |
| 38 #include <google/protobuf/stubs/stringpiece.h> |
| 39 |
| 40 namespace google { |
| 41 namespace protobuf { |
| 42 namespace util { |
| 43 |
| 44 class LIBPROTOBUF_EXPORT FieldMaskUtil { |
| 45 typedef google::protobuf::FieldMask FieldMask; |
| 46 |
| 47 public: |
| 48 // Converts FieldMask to/from string, formatted according to proto3 JSON |
| 49 // spec for FieldMask (e.g., "foo,bar,baz.quz"). |
| 50 static string ToString(const FieldMask& mask); |
| 51 static void FromString(StringPiece str, FieldMask* out); |
| 52 |
| 53 // Checks whether the given path is valid for type T. |
| 54 template <typename T> |
| 55 static bool IsValidPath(StringPiece path) { |
| 56 return InternalIsValidPath(T::descriptor(), path); |
| 57 } |
| 58 |
| 59 // Checks whether the given FieldMask is valid for type T. |
| 60 template <typename T> |
| 61 static bool IsValidFieldMask(const FieldMask& mask) { |
| 62 for (int i = 0; i < mask.paths_size(); ++i) { |
| 63 if (!InternalIsValidPath(T::descriptor(), mask.paths(i))) return false; |
| 64 } |
| 65 return true; |
| 66 } |
| 67 |
| 68 // Adds a path to FieldMask after checking whether the given path is valid. |
| 69 // This method check-fails if the path is not a valid path for type T. |
| 70 template <typename T> |
| 71 static void AddPathToFieldMask(StringPiece path, FieldMask* mask) { |
| 72 GOOGLE_CHECK(IsValidPath<T>(path)); |
| 73 mask->add_paths(path); |
| 74 } |
| 75 |
| 76 // Creates a FieldMask with all fields of type T. This FieldMask only |
| 77 // contains fields of T but not any sub-message fields. |
| 78 template <typename T> |
| 79 static void GetFieldMaskForAllFields(FieldMask* out) { |
| 80 InternalGetFieldMaskForAllFields(T::descriptor(), out); |
| 81 } |
| 82 |
| 83 // Converts a FieldMask to the canonical form. It will: |
| 84 // 1. Remove paths that are covered by another path. For example, |
| 85 // "foo.bar" is covered by "foo" and will be removed if "foo" |
| 86 // is also in the FieldMask. |
| 87 // 2. Sort all paths in alphabetical order. |
| 88 static void ToCanonicalForm(const FieldMask& mask, FieldMask* out); |
| 89 |
| 90 // Creates an union of two FieldMasks. |
| 91 static void Union(const FieldMask& mask1, const FieldMask& mask2, |
| 92 FieldMask* out); |
| 93 |
| 94 // Creates an intersection of two FieldMasks. |
| 95 static void Intersect(const FieldMask& mask1, const FieldMask& mask2, |
| 96 FieldMask* out); |
| 97 |
| 98 // Returns true if path is covered by the given FieldMask. Note that path |
| 99 // "foo.bar" covers all paths like "foo.bar.baz", "foo.bar.quz.x", etc. |
| 100 static bool IsPathInFieldMask(StringPiece path, const FieldMask& mask); |
| 101 |
| 102 class MergeOptions; |
| 103 // Merges fields specified in a FieldMask into another message. |
| 104 static void MergeMessageTo(const Message& source, const FieldMask& mask, |
| 105 const MergeOptions& options, Message* destination); |
| 106 |
| 107 private: |
| 108 static bool InternalIsValidPath(const Descriptor* descriptor, |
| 109 StringPiece path); |
| 110 |
| 111 static void InternalGetFieldMaskForAllFields(const Descriptor* descriptor, |
| 112 FieldMask* out); |
| 113 }; |
| 114 |
| 115 class LIBPROTOBUF_EXPORT FieldMaskUtil::MergeOptions { |
| 116 public: |
| 117 MergeOptions() |
| 118 : replace_message_fields_(false), replace_repeated_fields_(false) {} |
| 119 // When merging message fields, the default behavior is to merge the |
| 120 // content of two message fields together. If you instead want to use |
| 121 // the field from the source message to replace the corresponding field |
| 122 // in the destination message, set this flag to true. When this flag is set, |
| 123 // specified submessage fields that are missing in source will be cleared in |
| 124 // destination. |
| 125 void set_replace_message_fields(bool value) { |
| 126 replace_message_fields_ = value; |
| 127 } |
| 128 bool replace_message_fields() const { return replace_message_fields_; } |
| 129 // The default merging behavior will append entries from the source |
| 130 // repeated field to the destination repeated field. If you only want |
| 131 // to keep the entries from the source repeated field, set this flag |
| 132 // to true. |
| 133 void set_replace_repeated_fields(bool value) { |
| 134 replace_repeated_fields_ = value; |
| 135 } |
| 136 bool replace_repeated_fields() const { return replace_repeated_fields_; } |
| 137 |
| 138 private: |
| 139 bool replace_message_fields_; |
| 140 bool replace_repeated_fields_; |
| 141 }; |
| 142 |
| 143 } // namespace util |
| 144 } // namespace protobuf |
| 145 |
| 146 } // namespace google |
| 147 #endif // GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__ |
| OLD | NEW |