PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/protobuf-2.5.0/src/google/protobuf/util/field_mask_util.cc

https://gitlab.com/f1ssi0n/sniffles
C++ | 418 lines | 314 code | 39 blank | 65 comment | 62 complexity | e944c47eaaee43d0c2372bc05559b3d1 MD5 | raw file
  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. #include <google/protobuf/util/field_mask_util.h>
  31. #include <google/protobuf/stubs/strutil.h>
  32. #include <google/protobuf/stubs/map_util.h>
  33. namespace google {
  34. namespace protobuf {
  35. namespace util {
  36. using google::protobuf::FieldMask;
  37. string FieldMaskUtil::ToString(const FieldMask& mask) {
  38. return Join(mask.paths(), ",");
  39. }
  40. void FieldMaskUtil::FromString(StringPiece str, FieldMask* out) {
  41. out->Clear();
  42. vector<string> paths = Split(str, ",");
  43. for (int i = 0; i < paths.size(); ++i) {
  44. if (paths[i].empty()) continue;
  45. out->add_paths(paths[i]);
  46. }
  47. }
  48. bool FieldMaskUtil::InternalIsValidPath(const Descriptor* descriptor,
  49. StringPiece path) {
  50. vector<string> parts = Split(path, ".");
  51. for (int i = 0; i < parts.size(); ++i) {
  52. const string& field_name = parts[i];
  53. if (descriptor == NULL) {
  54. return false;
  55. }
  56. const FieldDescriptor* field = descriptor->FindFieldByName(field_name);
  57. if (field == NULL) {
  58. return false;
  59. }
  60. if (!field->is_repeated() &&
  61. field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
  62. descriptor = field->message_type();
  63. } else {
  64. descriptor = NULL;
  65. }
  66. }
  67. return true;
  68. }
  69. void FieldMaskUtil::InternalGetFieldMaskForAllFields(
  70. const Descriptor* descriptor, FieldMask* out) {
  71. for (int i = 0; i < descriptor->field_count(); ++i) {
  72. out->add_paths(descriptor->field(i)->name());
  73. }
  74. }
  75. namespace {
  76. // A FieldMaskTree represents a FieldMask in a tree structure. For example,
  77. // given a FieldMask "foo.bar,foo.baz,bar.baz", the FieldMaskTree will be:
  78. //
  79. // [root] -+- foo -+- bar
  80. // | |
  81. // | +- baz
  82. // |
  83. // +- bar --- baz
  84. //
  85. // In the tree, each leaf node represents a field path.
  86. class FieldMaskTree {
  87. public:
  88. FieldMaskTree();
  89. ~FieldMaskTree();
  90. void MergeFromFieldMask(const FieldMask& mask);
  91. void MergeToFieldMask(FieldMask* mask);
  92. // Add a field path into the tree. In a FieldMask, each field path matches
  93. // the specified field and also all its sub-fields. If the field path to
  94. // add is a sub-path of an existing field path in the tree (i.e., a leaf
  95. // node), it means the tree already matchesthe the given path so nothing will
  96. // be added to the tree. If the path matches an existing non-leaf node in the
  97. // tree, that non-leaf node will be turned into a leaf node with all its
  98. // children removed because the path matches all the node's children.
  99. void AddPath(const string& path);
  100. // Calculate the intersection part of a field path with this tree and add
  101. // the intersection field path into out.
  102. void IntersectPath(const string& path, FieldMaskTree* out);
  103. // Merge all fields specified by this tree from one message to another.
  104. void MergeMessage(const Message& source,
  105. const FieldMaskUtil::MergeOptions& options,
  106. Message* destination) {
  107. // Do nothing if the tree is empty.
  108. if (root_.children.empty()) {
  109. return;
  110. }
  111. MergeMessage(&root_, source, options, destination);
  112. }
  113. private:
  114. struct Node {
  115. Node() {}
  116. ~Node() { ClearChildren(); }
  117. void ClearChildren() {
  118. for (map<string, Node*>::iterator it = children.begin();
  119. it != children.end(); ++it) {
  120. delete it->second;
  121. }
  122. children.clear();
  123. }
  124. map<string, Node*> children;
  125. private:
  126. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Node);
  127. };
  128. // Merge a sub-tree to mask. This method adds the field paths represented
  129. // by all leaf nodes descended from "node" to mask.
  130. void MergeToFieldMask(const string& prefix, const Node* node, FieldMask* out);
  131. // Merge all leaf nodes of a sub-tree to another tree.
  132. void MergeLeafNodesToTree(const string& prefix, const Node* node,
  133. FieldMaskTree* out);
  134. // Merge all fields specified by a sub-tree from one message to another.
  135. void MergeMessage(const Node* node, const Message& source,
  136. const FieldMaskUtil::MergeOptions& options,
  137. Message* destination);
  138. Node root_;
  139. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldMaskTree);
  140. };
  141. FieldMaskTree::FieldMaskTree() {}
  142. FieldMaskTree::~FieldMaskTree() {}
  143. void FieldMaskTree::MergeFromFieldMask(const FieldMask& mask) {
  144. for (int i = 0; i < mask.paths_size(); ++i) {
  145. AddPath(mask.paths(i));
  146. }
  147. }
  148. void FieldMaskTree::MergeToFieldMask(FieldMask* mask) {
  149. MergeToFieldMask("", &root_, mask);
  150. }
  151. void FieldMaskTree::MergeToFieldMask(const string& prefix, const Node* node,
  152. FieldMask* out) {
  153. if (node->children.empty()) {
  154. if (prefix.empty()) {
  155. // This is the root node.
  156. return;
  157. }
  158. out->add_paths(prefix);
  159. return;
  160. }
  161. for (map<string, Node*>::const_iterator it = node->children.begin();
  162. it != node->children.end(); ++it) {
  163. string current_path = prefix.empty() ? it->first : prefix + "." + it->first;
  164. MergeToFieldMask(current_path, it->second, out);
  165. }
  166. }
  167. void FieldMaskTree::AddPath(const string& path) {
  168. vector<string> parts = Split(path, ".");
  169. if (parts.empty()) {
  170. return;
  171. }
  172. bool new_branch = false;
  173. Node* node = &root_;
  174. for (int i = 0; i < parts.size(); ++i) {
  175. if (!new_branch && node != &root_ && node->children.empty()) {
  176. // Path matches an existing leaf node. This means the path is already
  177. // coverred by this tree (for example, adding "foo.bar.baz" to a tree
  178. // which already contains "foo.bar").
  179. return;
  180. }
  181. const string& node_name = parts[i];
  182. Node*& child = node->children[node_name];
  183. if (child == NULL) {
  184. new_branch = true;
  185. child = new Node();
  186. }
  187. node = child;
  188. }
  189. if (!node->children.empty()) {
  190. node->ClearChildren();
  191. }
  192. }
  193. void FieldMaskTree::IntersectPath(const string& path, FieldMaskTree* out) {
  194. vector<string> parts = Split(path, ".");
  195. if (parts.empty()) {
  196. return;
  197. }
  198. const Node* node = &root_;
  199. for (int i = 0; i < parts.size(); ++i) {
  200. if (node->children.empty()) {
  201. if (node != &root_) {
  202. out->AddPath(path);
  203. }
  204. return;
  205. }
  206. const string& node_name = parts[i];
  207. const Node* result = FindPtrOrNull(node->children, node_name);
  208. if (result == NULL) {
  209. // No intersection found.
  210. return;
  211. }
  212. node = result;
  213. }
  214. // Now we found a matching node with the given path. Add all leaf nodes
  215. // to out.
  216. MergeLeafNodesToTree(path, node, out);
  217. }
  218. void FieldMaskTree::MergeLeafNodesToTree(const string& prefix, const Node* node,
  219. FieldMaskTree* out) {
  220. if (node->children.empty()) {
  221. out->AddPath(prefix);
  222. }
  223. for (map<string, Node*>::const_iterator it = node->children.begin();
  224. it != node->children.end(); ++it) {
  225. string current_path = prefix.empty() ? it->first : prefix + "." + it->first;
  226. MergeLeafNodesToTree(current_path, it->second, out);
  227. }
  228. }
  229. void FieldMaskTree::MergeMessage(const Node* node, const Message& source,
  230. const FieldMaskUtil::MergeOptions& options,
  231. Message* destination) {
  232. GOOGLE_DCHECK(!node->children.empty());
  233. const Reflection* source_reflection = source.GetReflection();
  234. const Reflection* destination_reflection = destination->GetReflection();
  235. const Descriptor* descriptor = source.GetDescriptor();
  236. for (map<string, Node*>::const_iterator it = node->children.begin();
  237. it != node->children.end(); ++it) {
  238. const string& field_name = it->first;
  239. const Node* child = it->second;
  240. const FieldDescriptor* field = descriptor->FindFieldByName(field_name);
  241. if (field == NULL) {
  242. GOOGLE_LOG(ERROR) << "Cannot find field \"" << field_name << "\" in message "
  243. << descriptor->full_name();
  244. continue;
  245. }
  246. if (!child->children.empty()) {
  247. // Sub-paths are only allowed for singular message fields.
  248. if (field->is_repeated() ||
  249. field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) {
  250. GOOGLE_LOG(ERROR) << "Field \"" << field_name << "\" in message "
  251. << descriptor->full_name()
  252. << " is not a singular message field and cannot "
  253. << "have sub-fields.";
  254. continue;
  255. }
  256. MergeMessage(child, source_reflection->GetMessage(source, field), options,
  257. destination_reflection->MutableMessage(destination, field));
  258. continue;
  259. }
  260. if (!field->is_repeated()) {
  261. switch (field->cpp_type()) {
  262. #define COPY_VALUE(TYPE, Name) \
  263. case FieldDescriptor::CPPTYPE_##TYPE: { \
  264. destination_reflection->Set##Name( \
  265. destination, field, source_reflection->Get##Name(source, field)); \
  266. break; \
  267. }
  268. COPY_VALUE(BOOL, Bool)
  269. COPY_VALUE(INT32, Int32)
  270. COPY_VALUE(INT64, Int64)
  271. COPY_VALUE(UINT32, UInt32)
  272. COPY_VALUE(UINT64, UInt64)
  273. COPY_VALUE(FLOAT, Float)
  274. COPY_VALUE(DOUBLE, Double)
  275. COPY_VALUE(ENUM, Enum)
  276. COPY_VALUE(STRING, String)
  277. #undef COPY_VALUE
  278. case FieldDescriptor::CPPTYPE_MESSAGE: {
  279. if (options.replace_message_fields()) {
  280. destination_reflection->ClearField(destination, field);
  281. }
  282. if (source_reflection->HasField(source, field)) {
  283. destination_reflection->MutableMessage(destination, field)
  284. ->MergeFrom(source_reflection->GetMessage(source, field));
  285. }
  286. break;
  287. }
  288. }
  289. } else {
  290. if (options.replace_repeated_fields()) {
  291. destination_reflection->ClearField(destination, field);
  292. }
  293. switch (field->cpp_type()) {
  294. #define COPY_REPEATED_VALUE(TYPE, Name) \
  295. case FieldDescriptor::CPPTYPE_##TYPE: { \
  296. int size = source_reflection->FieldSize(source, field); \
  297. for (int i = 0; i < size; ++i) { \
  298. destination_reflection->Add##Name( \
  299. destination, field, \
  300. source_reflection->GetRepeated##Name(source, field, i)); \
  301. } \
  302. break; \
  303. }
  304. COPY_REPEATED_VALUE(BOOL, Bool)
  305. COPY_REPEATED_VALUE(INT32, Int32)
  306. COPY_REPEATED_VALUE(INT64, Int64)
  307. COPY_REPEATED_VALUE(UINT32, UInt32)
  308. COPY_REPEATED_VALUE(UINT64, UInt64)
  309. COPY_REPEATED_VALUE(FLOAT, Float)
  310. COPY_REPEATED_VALUE(DOUBLE, Double)
  311. COPY_REPEATED_VALUE(ENUM, Enum)
  312. COPY_REPEATED_VALUE(STRING, String)
  313. #undef COPY_REPEATED_VALUE
  314. case FieldDescriptor::CPPTYPE_MESSAGE: {
  315. int size = source_reflection->FieldSize(source, field);
  316. for (int i = 0; i < size; ++i) {
  317. destination_reflection->AddMessage(destination, field)
  318. ->MergeFrom(
  319. source_reflection->GetRepeatedMessage(source, field, i));
  320. }
  321. break;
  322. }
  323. }
  324. }
  325. }
  326. }
  327. } // namespace
  328. void FieldMaskUtil::ToCanonicalForm(const FieldMask& mask, FieldMask* out) {
  329. FieldMaskTree tree;
  330. tree.MergeFromFieldMask(mask);
  331. out->Clear();
  332. tree.MergeToFieldMask(out);
  333. }
  334. void FieldMaskUtil::Union(const FieldMask& mask1, const FieldMask& mask2,
  335. FieldMask* out) {
  336. FieldMaskTree tree;
  337. tree.MergeFromFieldMask(mask1);
  338. tree.MergeFromFieldMask(mask2);
  339. out->Clear();
  340. tree.MergeToFieldMask(out);
  341. }
  342. void FieldMaskUtil::Intersect(const FieldMask& mask1, const FieldMask& mask2,
  343. FieldMask* out) {
  344. FieldMaskTree tree, intersection;
  345. tree.MergeFromFieldMask(mask1);
  346. for (int i = 0; i < mask2.paths_size(); ++i) {
  347. tree.IntersectPath(mask2.paths(i), &intersection);
  348. }
  349. out->Clear();
  350. intersection.MergeToFieldMask(out);
  351. }
  352. bool FieldMaskUtil::IsPathInFieldMask(StringPiece path, const FieldMask& mask) {
  353. for (int i = 0; i < mask.paths_size(); ++i) {
  354. const string& mask_path = mask.paths(i);
  355. if (path == mask_path) {
  356. return true;
  357. } else if (mask_path.length() < path.length()) {
  358. // Also check whether mask.paths(i) is a prefix of path.
  359. if (path.substr(0, mask_path.length() + 1).compare(mask_path + ".") ==
  360. 0) {
  361. return true;
  362. }
  363. }
  364. }
  365. return false;
  366. }
  367. void FieldMaskUtil::MergeMessageTo(const Message& source, const FieldMask& mask,
  368. const MergeOptions& options,
  369. Message* destination) {
  370. GOOGLE_CHECK(source.GetDescriptor() == destination->GetDescriptor());
  371. // Build a FieldMaskTree and walk through the tree to merge all specified
  372. // fields.
  373. FieldMaskTree tree;
  374. tree.MergeFromFieldMask(mask);
  375. tree.MergeMessage(source, options, destination);
  376. }
  377. } // namespace util
  378. } // namespace protobuf
  379. } // namespace google