diff options
Diffstat (limited to 'src/google/protobuf/util/field_mask_util.cc')
-rw-r--r-- | src/google/protobuf/util/field_mask_util.cc | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/src/google/protobuf/util/field_mask_util.cc b/src/google/protobuf/util/field_mask_util.cc index 8a413498..a2e2a388 100644 --- a/src/google/protobuf/util/field_mask_util.cc +++ b/src/google/protobuf/util/field_mask_util.cc @@ -31,6 +31,7 @@ #include <google/protobuf/util/field_mask_util.h> #include <google/protobuf/stubs/strutil.h> + #include <google/protobuf/stubs/map_util.h> namespace google { @@ -131,27 +132,27 @@ bool FieldMaskUtil::FromJsonString(StringPiece str, FieldMask* out) { bool FieldMaskUtil::GetFieldDescriptors( const Descriptor* descriptor, StringPiece path, std::vector<const FieldDescriptor*>* field_descriptors) { - if (field_descriptors != NULL) { + if (field_descriptors != nullptr) { field_descriptors->clear(); } std::vector<string> parts = Split(path, "."); for (int i = 0; i < parts.size(); ++i) { const string& field_name = parts[i]; - if (descriptor == NULL) { + if (descriptor == nullptr) { return false; } const FieldDescriptor* field = descriptor->FindFieldByName(field_name); - if (field == NULL) { + if (field == nullptr) { return false; } - if (field_descriptors != NULL) { + if (field_descriptors != nullptr) { field_descriptors->push_back(field); } if (!field->is_repeated() && field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { descriptor = field->message_type(); } else { - descriptor = NULL; + descriptor = nullptr; } } return true; @@ -342,6 +343,12 @@ void FieldMaskTree::AddPath(const string& path) { void FieldMaskTree::RemovePath(const string& path, const Descriptor* descriptor) { + if (root_.children.empty()) { + // Nothing to be removed from an empty tree. We shortcut it here so an empty + // tree won't be interpreted as a field mask containing all fields by the + // code below. + return; + } std::vector<string> parts = Split(path, "."); if (parts.empty()) { return; @@ -349,16 +356,16 @@ void FieldMaskTree::RemovePath(const string& path, std::vector<Node*> nodes(parts.size()); Node* node = &root_; const Descriptor* current_descriptor = descriptor; - Node* new_branch_node = NULL; + Node* new_branch_node = nullptr; for (int i = 0; i < parts.size(); ++i) { nodes[i] = node; const FieldDescriptor* field_descriptor = current_descriptor->FindFieldByName(parts[i]); - if (field_descriptor == NULL || + if (field_descriptor == nullptr || (field_descriptor->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE && i != parts.size() - 1)) { // Invalid path. - if (new_branch_node != NULL) { + if (new_branch_node != nullptr) { // If add any new nodes, cleanup. new_branch_node->ClearChildren(); } @@ -366,7 +373,7 @@ void FieldMaskTree::RemovePath(const string& path, } if (node->children.empty()) { - if (new_branch_node == NULL) { + if (new_branch_node == nullptr) { new_branch_node = node; } for (int i = 0; i < current_descriptor->field_count(); ++i) { @@ -542,7 +549,7 @@ void FieldMaskTree::AddRequiredFieldPath( if (field->is_required()) { const string& node_name = field->name(); Node*& child = node->children[node_name]; - if (child == NULL) { + if (child == nullptr) { // Add required field path to the tree child = new Node(); } else if (child->children.empty()){ |