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 #include <google/protobuf/util/field_mask_util.h>
32
33 #include <google/protobuf/stubs/strutil.h>
34 #include <google/protobuf/stubs/map_util.h>
35
36 #include <google/protobuf/port_def.inc>
37
38 namespace google {
39 namespace protobuf {
40 namespace util {
41
42 using google::protobuf::FieldMask;
43
ToString(const FieldMask & mask)44 std::string FieldMaskUtil::ToString(const FieldMask& mask) {
45 return Join(mask.paths(), ",");
46 }
47
FromString(StringPiece str,FieldMask * out)48 void FieldMaskUtil::FromString(StringPiece str, FieldMask* out) {
49 out->Clear();
50 std::vector<std::string> paths = Split(str, ",");
51 for (int i = 0; i < paths.size(); ++i) {
52 if (paths[i].empty()) continue;
53 out->add_paths(paths[i]);
54 }
55 }
56
SnakeCaseToCamelCase(StringPiece input,std::string * output)57 bool FieldMaskUtil::SnakeCaseToCamelCase(StringPiece input,
58 std::string* output) {
59 output->clear();
60 bool after_underscore = false;
61 for (int i = 0; i < input.size(); ++i) {
62 if (input[i] >= 'A' && input[i] <= 'Z') {
63 // The field name must not contain uppercase letters.
64 return false;
65 }
66 if (after_underscore) {
67 if (input[i] >= 'a' && input[i] <= 'z') {
68 output->push_back(input[i] + 'A' - 'a');
69 after_underscore = false;
70 } else {
71 // The character after a "_" must be a lowercase letter.
72 return false;
73 }
74 } else if (input[i] == '_') {
75 after_underscore = true;
76 } else {
77 output->push_back(input[i]);
78 }
79 }
80 if (after_underscore) {
81 // Trailing "_".
82 return false;
83 }
84 return true;
85 }
86
CamelCaseToSnakeCase(StringPiece input,std::string * output)87 bool FieldMaskUtil::CamelCaseToSnakeCase(StringPiece input,
88 std::string* output) {
89 output->clear();
90 for (int i = 0; i < input.size(); ++i) {
91 if (input[i] == '_') {
92 // The field name must not contain "_"s.
93 return false;
94 }
95 if (input[i] >= 'A' && input[i] <= 'Z') {
96 output->push_back('_');
97 output->push_back(input[i] + 'a' - 'A');
98 } else {
99 output->push_back(input[i]);
100 }
101 }
102 return true;
103 }
104
ToJsonString(const FieldMask & mask,std::string * out)105 bool FieldMaskUtil::ToJsonString(const FieldMask& mask, std::string* out) {
106 out->clear();
107 for (int i = 0; i < mask.paths_size(); ++i) {
108 const std::string& path = mask.paths(i);
109 std::string camelcase_path;
110 if (!SnakeCaseToCamelCase(path, &camelcase_path)) {
111 return false;
112 }
113 if (i > 0) {
114 out->push_back(',');
115 }
116 out->append(camelcase_path);
117 }
118 return true;
119 }
120
FromJsonString(StringPiece str,FieldMask * out)121 bool FieldMaskUtil::FromJsonString(StringPiece str, FieldMask* out) {
122 out->Clear();
123 std::vector<std::string> paths = Split(str, ",");
124 for (int i = 0; i < paths.size(); ++i) {
125 if (paths[i].empty()) continue;
126 std::string snakecase_path;
127 if (!CamelCaseToSnakeCase(paths[i], &snakecase_path)) {
128 return false;
129 }
130 out->add_paths(snakecase_path);
131 }
132 return true;
133 }
134
GetFieldDescriptors(const Descriptor * descriptor,StringPiece path,std::vector<const FieldDescriptor * > * field_descriptors)135 bool FieldMaskUtil::GetFieldDescriptors(
136 const Descriptor* descriptor, StringPiece path,
137 std::vector<const FieldDescriptor*>* field_descriptors) {
138 if (field_descriptors != nullptr) {
139 field_descriptors->clear();
140 }
141 std::vector<std::string> parts = Split(path, ".");
142 for (int i = 0; i < parts.size(); ++i) {
143 const std::string& field_name = parts[i];
144 if (descriptor == nullptr) {
145 return false;
146 }
147 const FieldDescriptor* field = descriptor->FindFieldByName(field_name);
148 if (field == nullptr) {
149 return false;
150 }
151 if (field_descriptors != nullptr) {
152 field_descriptors->push_back(field);
153 }
154 if (!field->is_repeated() &&
155 field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
156 descriptor = field->message_type();
157 } else {
158 descriptor = nullptr;
159 }
160 }
161 return true;
162 }
163
GetFieldMaskForAllFields(const Descriptor * descriptor,FieldMask * out)164 void FieldMaskUtil::GetFieldMaskForAllFields(const Descriptor* descriptor,
165 FieldMask* out) {
166 for (int i = 0; i < descriptor->field_count(); ++i) {
167 out->add_paths(descriptor->field(i)->name());
168 }
169 }
170
171 namespace {
172 // A FieldMaskTree represents a FieldMask in a tree structure. For example,
173 // given a FieldMask "foo.bar,foo.baz,bar.baz", the FieldMaskTree will be:
174 //
175 // [root] -+- foo -+- bar
176 // | |
177 // | +- baz
178 // |
179 // +- bar --- baz
180 //
181 // In the tree, each leaf node represents a field path.
182 class FieldMaskTree {
183 public:
184 FieldMaskTree();
185 ~FieldMaskTree();
186
187 void MergeFromFieldMask(const FieldMask& mask);
188 void MergeToFieldMask(FieldMask* mask);
189
190 // Add a field path into the tree. In a FieldMask, each field path matches
191 // the specified field and also all its sub-fields. If the field path to
192 // add is a sub-path of an existing field path in the tree (i.e., a leaf
193 // node), it means the tree already matches the given path so nothing will
194 // be added to the tree. If the path matches an existing non-leaf node in the
195 // tree, that non-leaf node will be turned into a leaf node with all its
196 // children removed because the path matches all the node's children.
197 void AddPath(const std::string& path);
198
199 // Remove a path from the tree.
200 // If the path is a sub-path of an existing field path in the tree, it means
201 // we need remove the existing fied path and add all sub-paths except
202 // specified path. If the path matches an existing node in the tree, this node
203 // will be moved.
204 void RemovePath(const std::string& path, const Descriptor* descriptor);
205
206 // Calculate the intersection part of a field path with this tree and add
207 // the intersection field path into out.
208 void IntersectPath(const std::string& path, FieldMaskTree* out);
209
210 // Merge all fields specified by this tree from one message to another.
MergeMessage(const Message & source,const FieldMaskUtil::MergeOptions & options,Message * destination)211 void MergeMessage(const Message& source,
212 const FieldMaskUtil::MergeOptions& options,
213 Message* destination) {
214 // Do nothing if the tree is empty.
215 if (root_.children.empty()) {
216 return;
217 }
218 MergeMessage(&root_, source, options, destination);
219 }
220
221 // Add required field path of the message to this tree based on current tree
222 // structure. If a message is present in the tree, add the path of its
223 // required field to the tree. This is to make sure that after trimming a
224 // message with required fields are set, check IsInitialized() will not fail.
AddRequiredFieldPath(const Descriptor * descriptor)225 void AddRequiredFieldPath(const Descriptor* descriptor) {
226 // Do nothing if the tree is empty.
227 if (root_.children.empty()) {
228 return;
229 }
230 AddRequiredFieldPath(&root_, descriptor);
231 }
232
233 // Trims all fields not specified by this tree from the given message.
234 // Returns true if the message is modified.
TrimMessage(Message * message)235 bool TrimMessage(Message* message) {
236 // Do nothing if the tree is empty.
237 if (root_.children.empty()) {
238 return false;
239 }
240 return TrimMessage(&root_, message);
241 }
242
243 private:
244 struct Node {
Nodegoogle::protobuf::util::__anon08d4a52e0111::FieldMaskTree::Node245 Node() {}
246
~Nodegoogle::protobuf::util::__anon08d4a52e0111::FieldMaskTree::Node247 ~Node() { ClearChildren(); }
248
ClearChildrengoogle::protobuf::util::__anon08d4a52e0111::FieldMaskTree::Node249 void ClearChildren() {
250 for (std::map<std::string, Node*>::iterator it = children.begin();
251 it != children.end(); ++it) {
252 delete it->second;
253 }
254 children.clear();
255 }
256
257 std::map<std::string, Node*> children;
258
259 private:
260 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Node);
261 };
262
263 // Merge a sub-tree to mask. This method adds the field paths represented
264 // by all leaf nodes descended from "node" to mask.
265 void MergeToFieldMask(const std::string& prefix, const Node* node,
266 FieldMask* out);
267
268 // Merge all leaf nodes of a sub-tree to another tree.
269 void MergeLeafNodesToTree(const std::string& prefix, const Node* node,
270 FieldMaskTree* out);
271
272 // Merge all fields specified by a sub-tree from one message to another.
273 void MergeMessage(const Node* node, const Message& source,
274 const FieldMaskUtil::MergeOptions& options,
275 Message* destination);
276
277 // Add required field path of the message to this tree based on current tree
278 // structure. If a message is present in the tree, add the path of its
279 // required field to the tree. This is to make sure that after trimming a
280 // message with required fields are set, check IsInitialized() will not fail.
281 void AddRequiredFieldPath(Node* node, const Descriptor* descriptor);
282
283 // Trims all fields not specified by this sub-tree from the given message.
284 // Returns true if the message is actually modified
285 bool TrimMessage(const Node* node, Message* message);
286
287 Node root_;
288
289 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldMaskTree);
290 };
291
FieldMaskTree()292 FieldMaskTree::FieldMaskTree() {}
293
~FieldMaskTree()294 FieldMaskTree::~FieldMaskTree() {}
295
MergeFromFieldMask(const FieldMask & mask)296 void FieldMaskTree::MergeFromFieldMask(const FieldMask& mask) {
297 for (int i = 0; i < mask.paths_size(); ++i) {
298 AddPath(mask.paths(i));
299 }
300 }
301
MergeToFieldMask(FieldMask * mask)302 void FieldMaskTree::MergeToFieldMask(FieldMask* mask) {
303 MergeToFieldMask("", &root_, mask);
304 }
305
MergeToFieldMask(const std::string & prefix,const Node * node,FieldMask * out)306 void FieldMaskTree::MergeToFieldMask(const std::string& prefix,
307 const Node* node, FieldMask* out) {
308 if (node->children.empty()) {
309 if (prefix.empty()) {
310 // This is the root node.
311 return;
312 }
313 out->add_paths(prefix);
314 return;
315 }
316 for (std::map<std::string, Node*>::const_iterator it = node->children.begin();
317 it != node->children.end(); ++it) {
318 std::string current_path =
319 prefix.empty() ? it->first : prefix + "." + it->first;
320 MergeToFieldMask(current_path, it->second, out);
321 }
322 }
323
AddPath(const std::string & path)324 void FieldMaskTree::AddPath(const std::string& path) {
325 std::vector<std::string> parts = Split(path, ".");
326 if (parts.empty()) {
327 return;
328 }
329 bool new_branch = false;
330 Node* node = &root_;
331 for (int i = 0; i < parts.size(); ++i) {
332 if (!new_branch && node != &root_ && node->children.empty()) {
333 // Path matches an existing leaf node. This means the path is already
334 // coverred by this tree (for example, adding "foo.bar.baz" to a tree
335 // which already contains "foo.bar").
336 return;
337 }
338 const std::string& node_name = parts[i];
339 Node*& child = node->children[node_name];
340 if (child == NULL) {
341 new_branch = true;
342 child = new Node();
343 }
344 node = child;
345 }
346 if (!node->children.empty()) {
347 node->ClearChildren();
348 }
349 }
350
RemovePath(const std::string & path,const Descriptor * descriptor)351 void FieldMaskTree::RemovePath(const std::string& path,
352 const Descriptor* descriptor) {
353 if (root_.children.empty()) {
354 // Nothing to be removed from an empty tree. We shortcut it here so an empty
355 // tree won't be interpreted as a field mask containing all fields by the
356 // code below.
357 return;
358 }
359 std::vector<std::string> parts = Split(path, ".");
360 if (parts.empty()) {
361 return;
362 }
363 std::vector<Node*> nodes(parts.size());
364 Node* node = &root_;
365 const Descriptor* current_descriptor = descriptor;
366 Node* new_branch_node = nullptr;
367 for (int i = 0; i < parts.size(); ++i) {
368 nodes[i] = node;
369 const FieldDescriptor* field_descriptor =
370 current_descriptor->FindFieldByName(parts[i]);
371 if (field_descriptor == nullptr ||
372 (field_descriptor->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE &&
373 i != parts.size() - 1)) {
374 // Invalid path.
375 if (new_branch_node != nullptr) {
376 // If add any new nodes, cleanup.
377 new_branch_node->ClearChildren();
378 }
379 return;
380 }
381
382 if (node->children.empty()) {
383 if (new_branch_node == nullptr) {
384 new_branch_node = node;
385 }
386 for (int i = 0; i < current_descriptor->field_count(); ++i) {
387 node->children[current_descriptor->field(i)->name()] = new Node();
388 }
389 }
390 if (ContainsKey(node->children, parts[i])) {
391 node = node->children[parts[i]];
392 if (field_descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
393 current_descriptor = field_descriptor->message_type();
394 }
395 } else {
396 // Path does not exist.
397 return;
398 }
399 }
400 // Remove path.
401 for (int i = parts.size() - 1; i >= 0; i--) {
402 delete nodes[i]->children[parts[i]];
403 nodes[i]->children.erase(parts[i]);
404 if (!nodes[i]->children.empty()) {
405 break;
406 }
407 }
408 }
409
IntersectPath(const std::string & path,FieldMaskTree * out)410 void FieldMaskTree::IntersectPath(const std::string& path, FieldMaskTree* out) {
411 std::vector<std::string> parts = Split(path, ".");
412 if (parts.empty()) {
413 return;
414 }
415 const Node* node = &root_;
416 for (int i = 0; i < parts.size(); ++i) {
417 if (node->children.empty()) {
418 if (node != &root_) {
419 out->AddPath(path);
420 }
421 return;
422 }
423 const std::string& node_name = parts[i];
424 const Node* result = FindPtrOrNull(node->children, node_name);
425 if (result == NULL) {
426 // No intersection found.
427 return;
428 }
429 node = result;
430 }
431 // Now we found a matching node with the given path. Add all leaf nodes
432 // to out.
433 MergeLeafNodesToTree(path, node, out);
434 }
435
MergeLeafNodesToTree(const std::string & prefix,const Node * node,FieldMaskTree * out)436 void FieldMaskTree::MergeLeafNodesToTree(const std::string& prefix,
437 const Node* node, FieldMaskTree* out) {
438 if (node->children.empty()) {
439 out->AddPath(prefix);
440 }
441 for (std::map<std::string, Node*>::const_iterator it = node->children.begin();
442 it != node->children.end(); ++it) {
443 std::string current_path =
444 prefix.empty() ? it->first : prefix + "." + it->first;
445 MergeLeafNodesToTree(current_path, it->second, out);
446 }
447 }
448
MergeMessage(const Node * node,const Message & source,const FieldMaskUtil::MergeOptions & options,Message * destination)449 void FieldMaskTree::MergeMessage(const Node* node, const Message& source,
450 const FieldMaskUtil::MergeOptions& options,
451 Message* destination) {
452 GOOGLE_DCHECK(!node->children.empty());
453 const Reflection* source_reflection = source.GetReflection();
454 const Reflection* destination_reflection = destination->GetReflection();
455 const Descriptor* descriptor = source.GetDescriptor();
456 for (std::map<std::string, Node*>::const_iterator it = node->children.begin();
457 it != node->children.end(); ++it) {
458 const std::string& field_name = it->first;
459 const Node* child = it->second;
460 const FieldDescriptor* field = descriptor->FindFieldByName(field_name);
461 if (field == NULL) {
462 GOOGLE_LOG(ERROR) << "Cannot find field \"" << field_name << "\" in message "
463 << descriptor->full_name();
464 continue;
465 }
466 if (!child->children.empty()) {
467 // Sub-paths are only allowed for singular message fields.
468 if (field->is_repeated() ||
469 field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) {
470 GOOGLE_LOG(ERROR) << "Field \"" << field_name << "\" in message "
471 << descriptor->full_name()
472 << " is not a singular message field and cannot "
473 << "have sub-fields.";
474 continue;
475 }
476 MergeMessage(child, source_reflection->GetMessage(source, field), options,
477 destination_reflection->MutableMessage(destination, field));
478 continue;
479 }
480 if (!field->is_repeated()) {
481 switch (field->cpp_type()) {
482 #define COPY_VALUE(TYPE, Name) \
483 case FieldDescriptor::CPPTYPE_##TYPE: { \
484 if (source_reflection->HasField(source, field)) { \
485 destination_reflection->Set##Name( \
486 destination, field, source_reflection->Get##Name(source, field)); \
487 } else { \
488 destination_reflection->ClearField(destination, field); \
489 } \
490 break; \
491 }
492 COPY_VALUE(BOOL, Bool)
493 COPY_VALUE(INT32, Int32)
494 COPY_VALUE(INT64, Int64)
495 COPY_VALUE(UINT32, UInt32)
496 COPY_VALUE(UINT64, UInt64)
497 COPY_VALUE(FLOAT, Float)
498 COPY_VALUE(DOUBLE, Double)
499 COPY_VALUE(ENUM, Enum)
500 COPY_VALUE(STRING, String)
501 #undef COPY_VALUE
502 case FieldDescriptor::CPPTYPE_MESSAGE: {
503 if (options.replace_message_fields()) {
504 destination_reflection->ClearField(destination, field);
505 }
506 if (source_reflection->HasField(source, field)) {
507 destination_reflection->MutableMessage(destination, field)
508 ->MergeFrom(source_reflection->GetMessage(source, field));
509 }
510 break;
511 }
512 }
513 } else {
514 if (options.replace_repeated_fields()) {
515 destination_reflection->ClearField(destination, field);
516 }
517 switch (field->cpp_type()) {
518 #define COPY_REPEATED_VALUE(TYPE, Name) \
519 case FieldDescriptor::CPPTYPE_##TYPE: { \
520 int size = source_reflection->FieldSize(source, field); \
521 for (int i = 0; i < size; ++i) { \
522 destination_reflection->Add##Name( \
523 destination, field, \
524 source_reflection->GetRepeated##Name(source, field, i)); \
525 } \
526 break; \
527 }
528 COPY_REPEATED_VALUE(BOOL, Bool)
529 COPY_REPEATED_VALUE(INT32, Int32)
530 COPY_REPEATED_VALUE(INT64, Int64)
531 COPY_REPEATED_VALUE(UINT32, UInt32)
532 COPY_REPEATED_VALUE(UINT64, UInt64)
533 COPY_REPEATED_VALUE(FLOAT, Float)
534 COPY_REPEATED_VALUE(DOUBLE, Double)
535 COPY_REPEATED_VALUE(ENUM, Enum)
536 COPY_REPEATED_VALUE(STRING, String)
537 #undef COPY_REPEATED_VALUE
538 case FieldDescriptor::CPPTYPE_MESSAGE: {
539 int size = source_reflection->FieldSize(source, field);
540 for (int i = 0; i < size; ++i) {
541 destination_reflection->AddMessage(destination, field)
542 ->MergeFrom(
543 source_reflection->GetRepeatedMessage(source, field, i));
544 }
545 break;
546 }
547 }
548 }
549 }
550 }
551
AddRequiredFieldPath(Node * node,const Descriptor * descriptor)552 void FieldMaskTree::AddRequiredFieldPath(Node* node,
553 const Descriptor* descriptor) {
554 const int32 field_count = descriptor->field_count();
555 for (int index = 0; index < field_count; ++index) {
556 const FieldDescriptor* field = descriptor->field(index);
557 if (field->is_required()) {
558 const std::string& node_name = field->name();
559 Node*& child = node->children[node_name];
560 if (child == nullptr) {
561 // Add required field path to the tree
562 child = new Node();
563 } else if (child->children.empty()) {
564 // If the required field is in the tree and does not have any children,
565 // do nothing.
566 continue;
567 }
568 // Add required field in the children to the tree if the field is message.
569 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
570 AddRequiredFieldPath(child, field->message_type());
571 }
572 } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
573 std::map<std::string, Node*>::const_iterator it =
574 node->children.find(field->name());
575 if (it != node->children.end()) {
576 // Add required fields in the children to the
577 // tree if the field is a message and present in the tree.
578 Node* child = it->second;
579 if (!child->children.empty()) {
580 AddRequiredFieldPath(child, field->message_type());
581 }
582 }
583 }
584 }
585 }
586
TrimMessage(const Node * node,Message * message)587 bool FieldMaskTree::TrimMessage(const Node* node, Message* message) {
588 GOOGLE_DCHECK(!node->children.empty());
589 const Reflection* reflection = message->GetReflection();
590 const Descriptor* descriptor = message->GetDescriptor();
591 const int32 field_count = descriptor->field_count();
592 bool modified = false;
593 for (int index = 0; index < field_count; ++index) {
594 const FieldDescriptor* field = descriptor->field(index);
595 std::map<std::string, Node*>::const_iterator it =
596 node->children.find(field->name());
597 if (it == node->children.end()) {
598 if (field->is_repeated()) {
599 if (reflection->FieldSize(*message, field) != 0) {
600 modified = true;
601 }
602 } else {
603 if (reflection->HasField(*message, field)) {
604 modified = true;
605 }
606 }
607 reflection->ClearField(message, field);
608 } else {
609 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
610 Node* child = it->second;
611 if (!child->children.empty() && reflection->HasField(*message, field)) {
612 bool nestedMessageChanged =
613 TrimMessage(child, reflection->MutableMessage(message, field));
614 modified = nestedMessageChanged || modified;
615 }
616 }
617 }
618 }
619 return modified;
620 }
621
622 } // namespace
623
ToCanonicalForm(const FieldMask & mask,FieldMask * out)624 void FieldMaskUtil::ToCanonicalForm(const FieldMask& mask, FieldMask* out) {
625 FieldMaskTree tree;
626 tree.MergeFromFieldMask(mask);
627 out->Clear();
628 tree.MergeToFieldMask(out);
629 }
630
Union(const FieldMask & mask1,const FieldMask & mask2,FieldMask * out)631 void FieldMaskUtil::Union(const FieldMask& mask1, const FieldMask& mask2,
632 FieldMask* out) {
633 FieldMaskTree tree;
634 tree.MergeFromFieldMask(mask1);
635 tree.MergeFromFieldMask(mask2);
636 out->Clear();
637 tree.MergeToFieldMask(out);
638 }
639
Intersect(const FieldMask & mask1,const FieldMask & mask2,FieldMask * out)640 void FieldMaskUtil::Intersect(const FieldMask& mask1, const FieldMask& mask2,
641 FieldMask* out) {
642 FieldMaskTree tree, intersection;
643 tree.MergeFromFieldMask(mask1);
644 for (int i = 0; i < mask2.paths_size(); ++i) {
645 tree.IntersectPath(mask2.paths(i), &intersection);
646 }
647 out->Clear();
648 intersection.MergeToFieldMask(out);
649 }
650
Subtract(const Descriptor * descriptor,const FieldMask & mask1,const FieldMask & mask2,FieldMask * out)651 void FieldMaskUtil::Subtract(const Descriptor* descriptor,
652 const FieldMask& mask1, const FieldMask& mask2,
653 FieldMask* out) {
654 if (mask1.paths().empty()) {
655 out->Clear();
656 return;
657 }
658 FieldMaskTree tree;
659 tree.MergeFromFieldMask(mask1);
660 for (int i = 0; i < mask2.paths_size(); ++i) {
661 tree.RemovePath(mask2.paths(i), descriptor);
662 }
663 out->Clear();
664 tree.MergeToFieldMask(out);
665 }
666
IsPathInFieldMask(StringPiece path,const FieldMask & mask)667 bool FieldMaskUtil::IsPathInFieldMask(StringPiece path,
668 const FieldMask& mask) {
669 for (int i = 0; i < mask.paths_size(); ++i) {
670 const std::string& mask_path = mask.paths(i);
671 if (path == mask_path) {
672 return true;
673 } else if (mask_path.length() < path.length()) {
674 // Also check whether mask.paths(i) is a prefix of path.
675 if (path.substr(0, mask_path.length() + 1).compare(mask_path + ".") ==
676 0) {
677 return true;
678 }
679 }
680 }
681 return false;
682 }
683
MergeMessageTo(const Message & source,const FieldMask & mask,const MergeOptions & options,Message * destination)684 void FieldMaskUtil::MergeMessageTo(const Message& source, const FieldMask& mask,
685 const MergeOptions& options,
686 Message* destination) {
687 GOOGLE_CHECK(source.GetDescriptor() == destination->GetDescriptor());
688 // Build a FieldMaskTree and walk through the tree to merge all specified
689 // fields.
690 FieldMaskTree tree;
691 tree.MergeFromFieldMask(mask);
692 tree.MergeMessage(source, options, destination);
693 }
694
TrimMessage(const FieldMask & mask,Message * message)695 bool FieldMaskUtil::TrimMessage(const FieldMask& mask, Message* message) {
696 // Build a FieldMaskTree and walk through the tree to merge all specified
697 // fields.
698 FieldMaskTree tree;
699 tree.MergeFromFieldMask(mask);
700 return tree.TrimMessage(GOOGLE_CHECK_NOTNULL(message));
701 }
702
TrimMessage(const FieldMask & mask,Message * message,const TrimOptions & options)703 bool FieldMaskUtil::TrimMessage(const FieldMask& mask, Message* message,
704 const TrimOptions& options) {
705 // Build a FieldMaskTree and walk through the tree to merge all specified
706 // fields.
707 FieldMaskTree tree;
708 tree.MergeFromFieldMask(mask);
709 // If keep_required_fields is true, implicitely add required fields of
710 // a message present in the tree to prevent from trimming.
711 if (options.keep_required_fields()) {
712 tree.AddRequiredFieldPath(GOOGLE_CHECK_NOTNULL(message->GetDescriptor()));
713 }
714 return tree.TrimMessage(GOOGLE_CHECK_NOTNULL(message));
715 }
716
717 } // namespace util
718 } // namespace protobuf
719 } // namespace google
720