OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 10 matching lines...) Expand all Loading... |
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
| 31 // Defines utilities for the FieldMask well known type. |
| 32 |
31 #ifndef GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__ | 33 #ifndef GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__ |
32 #define GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__ | 34 #define GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__ |
33 | 35 |
34 #include <string> | 36 #include <string> |
35 | 37 |
36 #include <google/protobuf/descriptor.h> | 38 #include <google/protobuf/descriptor.h> |
37 #include <google/protobuf/field_mask.pb.h> | 39 #include <google/protobuf/field_mask.pb.h> |
38 #include <google/protobuf/stubs/stringpiece.h> | 40 #include <google/protobuf/stubs/stringpiece.h> |
39 | 41 |
40 namespace google { | 42 namespace google { |
41 namespace protobuf { | 43 namespace protobuf { |
42 namespace util { | 44 namespace util { |
43 | 45 |
44 class LIBPROTOBUF_EXPORT FieldMaskUtil { | 46 class LIBPROTOBUF_EXPORT FieldMaskUtil { |
45 typedef google::protobuf::FieldMask FieldMask; | 47 typedef google::protobuf::FieldMask FieldMask; |
46 | 48 |
47 public: | 49 public: |
48 // Converts FieldMask to/from string, formatted by separating each path | 50 // Converts FieldMask to/from string, formatted by separating each path |
49 // with a comma (e.g., "foo_bar,baz.quz"). | 51 // with a comma (e.g., "foo_bar,baz.quz"). |
50 static string ToString(const FieldMask& mask); | 52 static string ToString(const FieldMask& mask); |
51 static void FromString(StringPiece str, FieldMask* out); | 53 static void FromString(StringPiece str, FieldMask* out); |
52 | 54 |
53 // Converts FieldMask to/from string, formatted according to proto3 JSON | 55 // Converts FieldMask to/from string, formatted according to proto3 JSON |
54 // spec for FieldMask (e.g., "fooBar,baz.quz"). If the field name is not | 56 // spec for FieldMask (e.g., "fooBar,baz.quz"). If the field name is not |
55 // style conforming (i.e., not snake_case when converted to string, or not | 57 // style conforming (i.e., not snake_case when converted to string, or not |
56 // camelCase when converted from string), the conversion will fail. | 58 // camelCase when converted from string), the conversion will fail. |
57 static bool ToJsonString(const FieldMask& mask, string* out); | 59 static bool ToJsonString(const FieldMask& mask, string* out); |
58 static bool FromJsonString(StringPiece str, FieldMask* out); | 60 static bool FromJsonString(StringPiece str, FieldMask* out); |
59 | 61 |
| 62 // Get the descriptors of the fields which the given path from the message |
| 63 // descriptor traverses, if field_descriptors is not null. |
| 64 // Return false if the path is not valid, and the content of field_descriptors |
| 65 // is unspecified. |
| 66 static bool GetFieldDescriptors( |
| 67 const Descriptor* descriptor, StringPiece path, |
| 68 std::vector<const FieldDescriptor*>* field_descriptors); |
| 69 |
60 // Checks whether the given path is valid for type T. | 70 // Checks whether the given path is valid for type T. |
61 template <typename T> | 71 template <typename T> |
62 static bool IsValidPath(StringPiece path) { | 72 static bool IsValidPath(StringPiece path) { |
63 return InternalIsValidPath(T::descriptor(), path); | 73 return GetFieldDescriptors(T::descriptor(), path, NULL); |
64 } | 74 } |
65 | 75 |
66 // Checks whether the given FieldMask is valid for type T. | 76 // Checks whether the given FieldMask is valid for type T. |
67 template <typename T> | 77 template <typename T> |
68 static bool IsValidFieldMask(const FieldMask& mask) { | 78 static bool IsValidFieldMask(const FieldMask& mask) { |
69 for (int i = 0; i < mask.paths_size(); ++i) { | 79 for (int i = 0; i < mask.paths_size(); ++i) { |
70 if (!InternalIsValidPath(T::descriptor(), mask.paths(i))) return false; | 80 if (!GetFieldDescriptors(T::descriptor(), mask.paths(i), NULL)) |
| 81 return false; |
71 } | 82 } |
72 return true; | 83 return true; |
73 } | 84 } |
74 | 85 |
75 // Adds a path to FieldMask after checking whether the given path is valid. | 86 // Adds a path to FieldMask after checking whether the given path is valid. |
76 // This method check-fails if the path is not a valid path for type T. | 87 // This method check-fails if the path is not a valid path for type T. |
77 template <typename T> | 88 template <typename T> |
78 static void AddPathToFieldMask(StringPiece path, FieldMask* mask) { | 89 static void AddPathToFieldMask(StringPiece path, FieldMask* mask) { |
79 GOOGLE_CHECK(IsValidPath<T>(path)); | 90 GOOGLE_CHECK(IsValidPath<T>(path)); |
80 mask->add_paths(path); | 91 mask->add_paths(path); |
(...skipping 19 matching lines...) Expand all Loading... |
100 | 111 |
101 // Creates an intersection of two FieldMasks. | 112 // Creates an intersection of two FieldMasks. |
102 static void Intersect(const FieldMask& mask1, const FieldMask& mask2, | 113 static void Intersect(const FieldMask& mask1, const FieldMask& mask2, |
103 FieldMask* out); | 114 FieldMask* out); |
104 | 115 |
105 // Returns true if path is covered by the given FieldMask. Note that path | 116 // Returns true if path is covered by the given FieldMask. Note that path |
106 // "foo.bar" covers all paths like "foo.bar.baz", "foo.bar.quz.x", etc. | 117 // "foo.bar" covers all paths like "foo.bar.baz", "foo.bar.quz.x", etc. |
107 static bool IsPathInFieldMask(StringPiece path, const FieldMask& mask); | 118 static bool IsPathInFieldMask(StringPiece path, const FieldMask& mask); |
108 | 119 |
109 class MergeOptions; | 120 class MergeOptions; |
110 // Merges fields specified in a FieldMask into another message. | 121 // Merges fields specified in a FieldMask into another message. See the |
| 122 // comments in MergeOptions regarding compatibility with |
| 123 // google/protobuf/field_mask.proto |
111 static void MergeMessageTo(const Message& source, const FieldMask& mask, | 124 static void MergeMessageTo(const Message& source, const FieldMask& mask, |
112 const MergeOptions& options, Message* destination); | 125 const MergeOptions& options, Message* destination); |
113 | 126 |
| 127 // Removes from 'message' any field that is not represented in the given |
| 128 // FieldMask. If the FieldMask is empty, does nothing. |
| 129 static void TrimMessage(const FieldMask& mask, Message* message); |
| 130 |
114 private: | 131 private: |
115 friend class SnakeCaseCamelCaseTest; | 132 friend class SnakeCaseCamelCaseTest; |
116 // Converts a field name from snake_case to camelCase: | 133 // Converts a field name from snake_case to camelCase: |
117 // 1. Every character after "_" will be converted to uppercase. | 134 // 1. Every character after "_" will be converted to uppercase. |
118 // 2. All "_"s are removed. | 135 // 2. All "_"s are removed. |
119 // The conversion will fail if: | 136 // The conversion will fail if: |
120 // 1. The field name contains uppercase letters. | 137 // 1. The field name contains uppercase letters. |
121 // 2. Any character after a "_" is not a lowercase letter. | 138 // 2. Any character after a "_" is not a lowercase letter. |
122 // If the conversion succeeds, it's guaranteed that the resulted | 139 // If the conversion succeeds, it's guaranteed that the resulted |
123 // camelCase name will yield the original snake_case name when | 140 // camelCase name will yield the original snake_case name when |
(...skipping 10 matching lines...) Expand all Loading... |
134 // 1. The field name contains "_"s. | 151 // 1. The field name contains "_"s. |
135 // If the conversion succeeds, it's guaranteed that the resulted | 152 // If the conversion succeeds, it's guaranteed that the resulted |
136 // snake_case name will yield the original camelCase name when | 153 // snake_case name will yield the original camelCase name when |
137 // converted using SnakeCaseToCamelCase(). | 154 // converted using SnakeCaseToCamelCase(). |
138 // | 155 // |
139 // Note that the input can contain characters not allowed in C identifiers. | 156 // Note that the input can contain characters not allowed in C identifiers. |
140 // For example, "fooBar,bazQuz" will be converted to "foo_bar,baz_quz" | 157 // For example, "fooBar,bazQuz" will be converted to "foo_bar,baz_quz" |
141 // successfully. | 158 // successfully. |
142 static bool CamelCaseToSnakeCase(StringPiece input, string* output); | 159 static bool CamelCaseToSnakeCase(StringPiece input, string* output); |
143 | 160 |
144 static bool InternalIsValidPath(const Descriptor* descriptor, | |
145 StringPiece path); | |
146 | |
147 static void InternalGetFieldMaskForAllFields(const Descriptor* descriptor, | 161 static void InternalGetFieldMaskForAllFields(const Descriptor* descriptor, |
148 FieldMask* out); | 162 FieldMask* out); |
149 }; | 163 }; |
150 | 164 |
| 165 // Note that for compatibility with the defined behaviour for FieldMask in |
| 166 // google/protobuf/field_mask.proto, set replace_message_fields and |
| 167 // replace_repeated_fields to 'true'. The default options are not compatible |
| 168 // with google/protobuf/field_mask.proto. |
151 class LIBPROTOBUF_EXPORT FieldMaskUtil::MergeOptions { | 169 class LIBPROTOBUF_EXPORT FieldMaskUtil::MergeOptions { |
152 public: | 170 public: |
153 MergeOptions() | 171 MergeOptions() |
154 : replace_message_fields_(false), replace_repeated_fields_(false) {} | 172 : replace_message_fields_(false), replace_repeated_fields_(false) {} |
155 // When merging message fields, the default behavior is to merge the | 173 // When merging message fields, the default behavior is to merge the |
156 // content of two message fields together. If you instead want to use | 174 // content of two message fields together. If you instead want to use |
157 // the field from the source message to replace the corresponding field | 175 // the field from the source message to replace the corresponding field |
158 // in the destination message, set this flag to true. When this flag is set, | 176 // in the destination message, set this flag to true. When this flag is set, |
159 // specified submessage fields that are missing in source will be cleared in | 177 // specified submessage fields that are missing in source will be cleared in |
160 // destination. | 178 // destination. |
(...skipping 13 matching lines...) Expand all Loading... |
174 private: | 192 private: |
175 bool replace_message_fields_; | 193 bool replace_message_fields_; |
176 bool replace_repeated_fields_; | 194 bool replace_repeated_fields_; |
177 }; | 195 }; |
178 | 196 |
179 } // namespace util | 197 } // namespace util |
180 } // namespace protobuf | 198 } // namespace protobuf |
181 | 199 |
182 } // namespace google | 200 } // namespace google |
183 #endif // GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__ | 201 #endif // GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__ |
OLD | NEW |