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 | |
33 #ifndef GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__ | 31 #ifndef GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__ |
34 #define GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__ | 32 #define GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__ |
35 | 33 |
36 #include <string> | 34 #include <string> |
37 | 35 |
38 #include <google/protobuf/descriptor.h> | 36 #include <google/protobuf/descriptor.h> |
39 #include <google/protobuf/field_mask.pb.h> | 37 #include <google/protobuf/field_mask.pb.h> |
40 #include <google/protobuf/stubs/stringpiece.h> | 38 #include <google/protobuf/stubs/stringpiece.h> |
41 | 39 |
42 namespace google { | 40 namespace google { |
43 namespace protobuf { | 41 namespace protobuf { |
44 namespace util { | 42 namespace util { |
45 | 43 |
46 class LIBPROTOBUF_EXPORT FieldMaskUtil { | 44 class LIBPROTOBUF_EXPORT FieldMaskUtil { |
47 typedef google::protobuf::FieldMask FieldMask; | 45 typedef google::protobuf::FieldMask FieldMask; |
48 | 46 |
49 public: | 47 public: |
50 // Converts FieldMask to/from string, formatted by separating each path | 48 // Converts FieldMask to/from string, formatted by separating each path |
51 // with a comma (e.g., "foo_bar,baz.quz"). | 49 // with a comma (e.g., "foo_bar,baz.quz"). |
52 static string ToString(const FieldMask& mask); | 50 static string ToString(const FieldMask& mask); |
53 static void FromString(StringPiece str, FieldMask* out); | 51 static void FromString(StringPiece str, FieldMask* out); |
54 | 52 |
55 // Converts FieldMask to/from string, formatted according to proto3 JSON | 53 // Converts FieldMask to/from string, formatted according to proto3 JSON |
56 // spec for FieldMask (e.g., "fooBar,baz.quz"). If the field name is not | 54 // spec for FieldMask (e.g., "fooBar,baz.quz"). If the field name is not |
57 // style conforming (i.e., not snake_case when converted to string, or not | 55 // style conforming (i.e., not snake_case when converted to string, or not |
58 // camelCase when converted from string), the conversion will fail. | 56 // camelCase when converted from string), the conversion will fail. |
59 static bool ToJsonString(const FieldMask& mask, string* out); | 57 static bool ToJsonString(const FieldMask& mask, string* out); |
60 static bool FromJsonString(StringPiece str, FieldMask* out); | 58 static bool FromJsonString(StringPiece str, FieldMask* out); |
61 | 59 |
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 | |
70 // Checks whether the given path is valid for type T. | 60 // Checks whether the given path is valid for type T. |
71 template <typename T> | 61 template <typename T> |
72 static bool IsValidPath(StringPiece path) { | 62 static bool IsValidPath(StringPiece path) { |
73 return GetFieldDescriptors(T::descriptor(), path, NULL); | 63 return InternalIsValidPath(T::descriptor(), path); |
74 } | 64 } |
75 | 65 |
76 // Checks whether the given FieldMask is valid for type T. | 66 // Checks whether the given FieldMask is valid for type T. |
77 template <typename T> | 67 template <typename T> |
78 static bool IsValidFieldMask(const FieldMask& mask) { | 68 static bool IsValidFieldMask(const FieldMask& mask) { |
79 for (int i = 0; i < mask.paths_size(); ++i) { | 69 for (int i = 0; i < mask.paths_size(); ++i) { |
80 if (!GetFieldDescriptors(T::descriptor(), mask.paths(i), NULL)) | 70 if (!InternalIsValidPath(T::descriptor(), mask.paths(i))) return false; |
81 return false; | |
82 } | 71 } |
83 return true; | 72 return true; |
84 } | 73 } |
85 | 74 |
86 // Adds a path to FieldMask after checking whether the given path is valid. | 75 // Adds a path to FieldMask after checking whether the given path is valid. |
87 // This method check-fails if the path is not a valid path for type T. | 76 // This method check-fails if the path is not a valid path for type T. |
88 template <typename T> | 77 template <typename T> |
89 static void AddPathToFieldMask(StringPiece path, FieldMask* mask) { | 78 static void AddPathToFieldMask(StringPiece path, FieldMask* mask) { |
90 GOOGLE_CHECK(IsValidPath<T>(path)); | 79 GOOGLE_CHECK(IsValidPath<T>(path)); |
91 mask->add_paths(path); | 80 mask->add_paths(path); |
(...skipping 19 matching lines...) Expand all Loading... |
111 | 100 |
112 // Creates an intersection of two FieldMasks. | 101 // Creates an intersection of two FieldMasks. |
113 static void Intersect(const FieldMask& mask1, const FieldMask& mask2, | 102 static void Intersect(const FieldMask& mask1, const FieldMask& mask2, |
114 FieldMask* out); | 103 FieldMask* out); |
115 | 104 |
116 // Returns true if path is covered by the given FieldMask. Note that path | 105 // Returns true if path is covered by the given FieldMask. Note that path |
117 // "foo.bar" covers all paths like "foo.bar.baz", "foo.bar.quz.x", etc. | 106 // "foo.bar" covers all paths like "foo.bar.baz", "foo.bar.quz.x", etc. |
118 static bool IsPathInFieldMask(StringPiece path, const FieldMask& mask); | 107 static bool IsPathInFieldMask(StringPiece path, const FieldMask& mask); |
119 | 108 |
120 class MergeOptions; | 109 class MergeOptions; |
121 // Merges fields specified in a FieldMask into another message. See the | 110 // Merges fields specified in a FieldMask into another message. |
122 // comments in MergeOptions regarding compatibility with | |
123 // google/protobuf/field_mask.proto | |
124 static void MergeMessageTo(const Message& source, const FieldMask& mask, | 111 static void MergeMessageTo(const Message& source, const FieldMask& mask, |
125 const MergeOptions& options, Message* destination); | 112 const MergeOptions& options, Message* destination); |
126 | 113 |
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 | |
131 private: | 114 private: |
132 friend class SnakeCaseCamelCaseTest; | 115 friend class SnakeCaseCamelCaseTest; |
133 // Converts a field name from snake_case to camelCase: | 116 // Converts a field name from snake_case to camelCase: |
134 // 1. Every character after "_" will be converted to uppercase. | 117 // 1. Every character after "_" will be converted to uppercase. |
135 // 2. All "_"s are removed. | 118 // 2. All "_"s are removed. |
136 // The conversion will fail if: | 119 // The conversion will fail if: |
137 // 1. The field name contains uppercase letters. | 120 // 1. The field name contains uppercase letters. |
138 // 2. Any character after a "_" is not a lowercase letter. | 121 // 2. Any character after a "_" is not a lowercase letter. |
139 // If the conversion succeeds, it's guaranteed that the resulted | 122 // If the conversion succeeds, it's guaranteed that the resulted |
140 // camelCase name will yield the original snake_case name when | 123 // camelCase name will yield the original snake_case name when |
(...skipping 10 matching lines...) Expand all Loading... |
151 // 1. The field name contains "_"s. | 134 // 1. The field name contains "_"s. |
152 // If the conversion succeeds, it's guaranteed that the resulted | 135 // If the conversion succeeds, it's guaranteed that the resulted |
153 // snake_case name will yield the original camelCase name when | 136 // snake_case name will yield the original camelCase name when |
154 // converted using SnakeCaseToCamelCase(). | 137 // converted using SnakeCaseToCamelCase(). |
155 // | 138 // |
156 // Note that the input can contain characters not allowed in C identifiers. | 139 // Note that the input can contain characters not allowed in C identifiers. |
157 // For example, "fooBar,bazQuz" will be converted to "foo_bar,baz_quz" | 140 // For example, "fooBar,bazQuz" will be converted to "foo_bar,baz_quz" |
158 // successfully. | 141 // successfully. |
159 static bool CamelCaseToSnakeCase(StringPiece input, string* output); | 142 static bool CamelCaseToSnakeCase(StringPiece input, string* output); |
160 | 143 |
| 144 static bool InternalIsValidPath(const Descriptor* descriptor, |
| 145 StringPiece path); |
| 146 |
161 static void InternalGetFieldMaskForAllFields(const Descriptor* descriptor, | 147 static void InternalGetFieldMaskForAllFields(const Descriptor* descriptor, |
162 FieldMask* out); | 148 FieldMask* out); |
163 }; | 149 }; |
164 | 150 |
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. | |
169 class LIBPROTOBUF_EXPORT FieldMaskUtil::MergeOptions { | 151 class LIBPROTOBUF_EXPORT FieldMaskUtil::MergeOptions { |
170 public: | 152 public: |
171 MergeOptions() | 153 MergeOptions() |
172 : replace_message_fields_(false), replace_repeated_fields_(false) {} | 154 : replace_message_fields_(false), replace_repeated_fields_(false) {} |
173 // When merging message fields, the default behavior is to merge the | 155 // When merging message fields, the default behavior is to merge the |
174 // content of two message fields together. If you instead want to use | 156 // content of two message fields together. If you instead want to use |
175 // the field from the source message to replace the corresponding field | 157 // the field from the source message to replace the corresponding field |
176 // in the destination message, set this flag to true. When this flag is set, | 158 // in the destination message, set this flag to true. When this flag is set, |
177 // specified submessage fields that are missing in source will be cleared in | 159 // specified submessage fields that are missing in source will be cleared in |
178 // destination. | 160 // destination. |
(...skipping 13 matching lines...) Expand all Loading... |
192 private: | 174 private: |
193 bool replace_message_fields_; | 175 bool replace_message_fields_; |
194 bool replace_repeated_fields_; | 176 bool replace_repeated_fields_; |
195 }; | 177 }; |
196 | 178 |
197 } // namespace util | 179 } // namespace util |
198 } // namespace protobuf | 180 } // namespace protobuf |
199 | 181 |
200 } // namespace google | 182 } // namespace google |
201 #endif // GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__ | 183 #endif // GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__ |
OLD | NEW |