FieldMaskTree.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2015 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using System.Collections;
  33. using System.Collections.Generic;
  34. using System.Diagnostics;
  35. using Google.Protobuf.Reflection;
  36. using Google.Protobuf.WellKnownTypes;
  37. namespace Google.Protobuf
  38. {
  39. /// <summary>
  40. /// <para>A tree representation of a FieldMask. Each leaf node in this tree represent
  41. /// a field path in the FieldMask.</para>
  42. ///
  43. /// <para>For example, FieldMask "foo.bar,foo.baz,bar.baz" as a tree will be:</para>
  44. /// <code>
  45. /// [root] -+- foo -+- bar
  46. /// | |
  47. /// | +- baz
  48. /// |
  49. /// +- bar --- baz
  50. /// </code>
  51. ///
  52. /// <para>By representing FieldMasks with this tree structure we can easily convert
  53. /// a FieldMask to a canonical form, merge two FieldMasks, calculate the
  54. /// intersection to two FieldMasks and traverse all fields specified by the
  55. /// FieldMask in a message tree.</para>
  56. /// </summary>
  57. internal sealed class FieldMaskTree
  58. {
  59. private const char FIELD_PATH_SEPARATOR = '.';
  60. internal sealed class Node
  61. {
  62. public Dictionary<string, Node> Children { get; } = new Dictionary<string, Node>();
  63. }
  64. private readonly Node root = new Node();
  65. /// <summary>
  66. /// Creates an empty FieldMaskTree.
  67. /// </summary>
  68. public FieldMaskTree()
  69. {
  70. }
  71. /// <summary>
  72. /// Creates a FieldMaskTree for a given FieldMask.
  73. /// </summary>
  74. public FieldMaskTree(FieldMask mask)
  75. {
  76. MergeFromFieldMask(mask);
  77. }
  78. public override string ToString()
  79. {
  80. return ToFieldMask().ToString();
  81. }
  82. /// <summary>
  83. /// Adds a field path to the tree. In a FieldMask, every field path matches the
  84. /// specified field as well as all its sub-fields. For example, a field path
  85. /// "foo.bar" matches field "foo.bar" and also "foo.bar.baz", etc. When adding
  86. /// a field path to the tree, redundant sub-paths will be removed. That is,
  87. /// after adding "foo.bar" to the tree, "foo.bar.baz" will be removed if it
  88. /// exists, which will turn the tree node for "foo.bar" to a leaf node.
  89. /// Likewise, if the field path to add is a sub-path of an existing leaf node,
  90. /// nothing will be changed in the tree.
  91. /// </summary>
  92. public FieldMaskTree AddFieldPath(string path)
  93. {
  94. var parts = path.Split(FIELD_PATH_SEPARATOR);
  95. if (parts.Length == 0)
  96. {
  97. return this;
  98. }
  99. var node = root;
  100. var createNewBranch = false;
  101. // Find the matching node in the tree.
  102. foreach (var part in parts)
  103. {
  104. // Check whether the path matches an existing leaf node.
  105. if (!createNewBranch
  106. && node != root
  107. && node.Children.Count == 0)
  108. {
  109. // The path to add is a sub-path of an existing leaf node.
  110. return this;
  111. }
  112. Node childNode;
  113. if (!node.Children.TryGetValue(part, out childNode))
  114. {
  115. createNewBranch = true;
  116. childNode = new Node();
  117. node.Children.Add(part, childNode);
  118. }
  119. node = childNode;
  120. }
  121. // Turn the matching node into a leaf node (i.e., remove sub-paths).
  122. node.Children.Clear();
  123. return this;
  124. }
  125. /// <summary>
  126. /// Merges all field paths in a FieldMask into this tree.
  127. /// </summary>
  128. public FieldMaskTree MergeFromFieldMask(FieldMask mask)
  129. {
  130. foreach (var path in mask.Paths)
  131. {
  132. AddFieldPath(path);
  133. }
  134. return this;
  135. }
  136. /// <summary>
  137. /// Converts this tree to a FieldMask.
  138. /// </summary>
  139. public FieldMask ToFieldMask()
  140. {
  141. var mask = new FieldMask();
  142. if (root.Children.Count != 0)
  143. {
  144. var paths = new List<string>();
  145. GetFieldPaths(root, "", paths);
  146. mask.Paths.AddRange(paths);
  147. }
  148. return mask;
  149. }
  150. /// <summary>
  151. /// Gathers all field paths in a sub-tree.
  152. /// </summary>
  153. private void GetFieldPaths(Node node, string path, List<string> paths)
  154. {
  155. if (node.Children.Count == 0)
  156. {
  157. paths.Add(path);
  158. return;
  159. }
  160. foreach (var entry in node.Children)
  161. {
  162. var childPath = path.Length == 0 ? entry.Key : path + "." + entry.Key;
  163. GetFieldPaths(entry.Value, childPath, paths);
  164. }
  165. }
  166. /// <summary>
  167. /// Adds the intersection of this tree with the given <paramref name="path"/> to <paramref name="output"/>.
  168. /// </summary>
  169. public void IntersectFieldPath(string path, FieldMaskTree output)
  170. {
  171. if (root.Children.Count == 0)
  172. {
  173. return;
  174. }
  175. var parts = path.Split(FIELD_PATH_SEPARATOR);
  176. if (parts.Length == 0)
  177. {
  178. return;
  179. }
  180. var node = root;
  181. foreach (var part in parts)
  182. {
  183. if (node != root
  184. && node.Children.Count == 0)
  185. {
  186. // The given path is a sub-path of an existing leaf node in the tree.
  187. output.AddFieldPath(path);
  188. return;
  189. }
  190. if (!node.Children.TryGetValue(part, out node))
  191. {
  192. return;
  193. }
  194. }
  195. // We found a matching node for the path. All leaf children of this matching
  196. // node is in the intersection.
  197. var paths = new List<string>();
  198. GetFieldPaths(node, path, paths);
  199. foreach (var value in paths)
  200. {
  201. output.AddFieldPath(value);
  202. }
  203. }
  204. /// <summary>
  205. /// Merges all fields specified by this FieldMaskTree from <paramref name="source"/> to <paramref name="destination"/>.
  206. /// </summary>
  207. public void Merge(IMessage source, IMessage destination, FieldMask.MergeOptions options)
  208. {
  209. if (source.Descriptor != destination.Descriptor)
  210. {
  211. throw new InvalidProtocolBufferException("Cannot merge messages of different types.");
  212. }
  213. if (root.Children.Count == 0)
  214. {
  215. return;
  216. }
  217. Merge(root, "", source, destination, options);
  218. }
  219. /// <summary>
  220. /// Merges all fields specified by a sub-tree from <paramref name="source"/> to <paramref name="destination"/>.
  221. /// </summary>
  222. private void Merge(
  223. Node node,
  224. string path,
  225. IMessage source,
  226. IMessage destination,
  227. FieldMask.MergeOptions options)
  228. {
  229. if (source.Descriptor != destination.Descriptor)
  230. {
  231. throw new InvalidProtocolBufferException($"source ({source.Descriptor}) and destination ({destination.Descriptor}) descriptor must be equal");
  232. }
  233. var descriptor = source.Descriptor;
  234. foreach (var entry in node.Children)
  235. {
  236. var field = descriptor.FindFieldByName(entry.Key);
  237. if (field == null)
  238. {
  239. Debug.WriteLine($"Cannot find field \"{entry.Key}\" in message type \"{descriptor.FullName}\"");
  240. continue;
  241. }
  242. if (entry.Value.Children.Count != 0)
  243. {
  244. if (field.IsRepeated
  245. || field.FieldType != FieldType.Message)
  246. {
  247. Debug.WriteLine($"Field \"{field.FullName}\" is not a singular message field and cannot have sub-fields.");
  248. continue;
  249. }
  250. var sourceField = field.Accessor.GetValue(source);
  251. var destinationField = field.Accessor.GetValue(destination);
  252. if (sourceField == null
  253. && destinationField == null)
  254. {
  255. // If the message field is not present in both source and destination, skip recursing
  256. // so we don't create unnecessary empty messages.
  257. continue;
  258. }
  259. if (destinationField == null)
  260. {
  261. // If we have to merge but the destination does not contain the field, create it.
  262. destinationField = field.MessageType.Parser.CreateTemplate();
  263. field.Accessor.SetValue(destination, destinationField);
  264. }
  265. var childPath = path.Length == 0 ? entry.Key : path + "." + entry.Key;
  266. Merge(entry.Value, childPath, (IMessage)sourceField, (IMessage)destinationField, options);
  267. continue;
  268. }
  269. if (field.IsRepeated)
  270. {
  271. if (options.ReplaceRepeatedFields)
  272. {
  273. field.Accessor.Clear(destination);
  274. }
  275. var sourceField = (IList)field.Accessor.GetValue(source);
  276. var destinationField = (IList)field.Accessor.GetValue(destination);
  277. foreach (var element in sourceField)
  278. {
  279. destinationField.Add(element);
  280. }
  281. }
  282. else
  283. {
  284. var sourceField = field.Accessor.GetValue(source);
  285. if (field.FieldType == FieldType.Message)
  286. {
  287. if (options.ReplaceMessageFields)
  288. {
  289. if (sourceField == null)
  290. {
  291. field.Accessor.Clear(destination);
  292. }
  293. else
  294. {
  295. field.Accessor.SetValue(destination, sourceField);
  296. }
  297. }
  298. else
  299. {
  300. if (sourceField != null)
  301. {
  302. var sourceByteString = ((IMessage)sourceField).ToByteString();
  303. var destinationValue = (IMessage)field.Accessor.GetValue(destination);
  304. if (destinationValue != null)
  305. {
  306. destinationValue.MergeFrom(sourceByteString);
  307. }
  308. else
  309. {
  310. field.Accessor.SetValue(destination, field.MessageType.Parser.ParseFrom(sourceByteString));
  311. }
  312. }
  313. }
  314. }
  315. else
  316. {
  317. if (sourceField != null
  318. || !options.ReplacePrimitiveFields)
  319. {
  320. field.Accessor.SetValue(destination, sourceField);
  321. }
  322. else
  323. {
  324. field.Accessor.Clear(destination);
  325. }
  326. }
  327. }
  328. }
  329. }
  330. }
  331. }