field_mask_utility.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/internal/field_mask_utility.h>
  31. #include <google/protobuf/util/internal/utility.h>
  32. #include <google/protobuf/stubs/strutil.h>
  33. #include <google/protobuf/stubs/status_macros.h>
  34. namespace google {
  35. namespace protobuf {
  36. namespace util {
  37. namespace converter {
  38. namespace {
  39. inline util::Status CallPathSink(PathSinkCallback path_sink,
  40. StringPiece arg) {
  41. return path_sink->Run(arg);
  42. }
  43. // Appends a FieldMask path segment to a prefix.
  44. string AppendPathSegmentToPrefix(StringPiece prefix, StringPiece segment) {
  45. if (prefix.empty()) {
  46. return segment.ToString();
  47. }
  48. if (segment.empty()) {
  49. return prefix.ToString();
  50. }
  51. // If the segment is a map key, appends it to the prefix without the ".".
  52. if (StringStartsWith(segment, "[\"")) {
  53. return StrCat(prefix, segment);
  54. }
  55. return StrCat(prefix, ".", segment);
  56. }
  57. } // namespace
  58. string ConvertFieldMaskPath(const StringPiece path,
  59. ConverterCallback converter) {
  60. string result;
  61. result.reserve(path.size() << 1);
  62. bool is_quoted = false;
  63. bool is_escaping = false;
  64. int current_segment_start = 0;
  65. // Loops until 1 passed the end of the input to make handling the last
  66. // segment easier.
  67. for (size_t i = 0; i <= path.size(); ++i) {
  68. // Outputs quoted string as-is.
  69. if (is_quoted) {
  70. if (i == path.size()) {
  71. break;
  72. }
  73. result.push_back(path[i]);
  74. if (is_escaping) {
  75. is_escaping = false;
  76. } else if (path[i] == '\\') {
  77. is_escaping = true;
  78. } else if (path[i] == '\"') {
  79. current_segment_start = i + 1;
  80. is_quoted = false;
  81. }
  82. continue;
  83. }
  84. if (i == path.size() || path[i] == '.' || path[i] == '(' ||
  85. path[i] == ')' || path[i] == '\"') {
  86. result += converter(
  87. path.substr(current_segment_start, i - current_segment_start));
  88. if (i < path.size()) {
  89. result.push_back(path[i]);
  90. }
  91. current_segment_start = i + 1;
  92. }
  93. if (i < path.size() && path[i] == '\"') {
  94. is_quoted = true;
  95. }
  96. }
  97. return result;
  98. }
  99. util::Status DecodeCompactFieldMaskPaths(StringPiece paths,
  100. PathSinkCallback path_sink) {
  101. std::stack<string> prefix;
  102. int length = paths.length();
  103. int previous_position = 0;
  104. bool in_map_key = false;
  105. bool is_escaping = false;
  106. // Loops until 1 passed the end of the input to make the handle of the last
  107. // segment easier.
  108. for (int i = 0; i <= length; ++i) {
  109. if (i != length) {
  110. // Skips everything in a map key until we hit the end of it, which is
  111. // marked by an un-escaped '"' immediately followed by a ']'.
  112. if (in_map_key) {
  113. if (is_escaping) {
  114. is_escaping = false;
  115. continue;
  116. }
  117. if (paths[i] == '\\') {
  118. is_escaping = true;
  119. continue;
  120. }
  121. if (paths[i] != '\"') {
  122. continue;
  123. }
  124. // Un-escaped '"' must be followed with a ']'.
  125. if (i >= length - 1 || paths[i + 1] != ']') {
  126. return util::Status(
  127. util::error::INVALID_ARGUMENT,
  128. StrCat("Invalid FieldMask '", paths,
  129. "'. Map keys should be represented as [\"some_key\"]."));
  130. }
  131. // The end of the map key ("\"]") has been found.
  132. in_map_key = false;
  133. // Skips ']'.
  134. i++;
  135. // Checks whether the key ends at the end of a path segment.
  136. if (i < length - 1 && paths[i + 1] != '.' && paths[i + 1] != ',' &&
  137. paths[i + 1] != ')' && paths[i + 1] != '(') {
  138. return util::Status(
  139. util::error::INVALID_ARGUMENT,
  140. StrCat("Invalid FieldMask '", paths,
  141. "'. Map keys should be at the end of a path segment."));
  142. }
  143. is_escaping = false;
  144. continue;
  145. }
  146. // We are not in a map key, look for the start of one.
  147. if (paths[i] == '[') {
  148. if (i >= length - 1 || paths[i + 1] != '\"') {
  149. return util::Status(
  150. util::error::INVALID_ARGUMENT,
  151. StrCat("Invalid FieldMask '", paths,
  152. "'. Map keys should be represented as [\"some_key\"]."));
  153. }
  154. // "[\"" starts a map key.
  155. in_map_key = true;
  156. i++; // Skips the '\"'.
  157. continue;
  158. }
  159. // If the current character is not a special character (',', '(' or ')'),
  160. // continue to the next.
  161. if (paths[i] != ',' && paths[i] != ')' && paths[i] != '(') {
  162. continue;
  163. }
  164. }
  165. // Gets the current segment - sub-string between previous position (after
  166. // '(', ')', ',', or the beginning of the input) and the current position.
  167. StringPiece segment =
  168. paths.substr(previous_position, i - previous_position);
  169. string current_prefix = prefix.empty() ? "" : prefix.top();
  170. if (i < length && paths[i] == '(') {
  171. // Builds a prefix and save it into the stack.
  172. prefix.push(AppendPathSegmentToPrefix(current_prefix, segment));
  173. } else if (!segment.empty()) {
  174. // When the current charactor is ')', ',' or the current position has
  175. // passed the end of the input, builds and outputs a new paths by
  176. // concatenating the last prefix with the current segment.
  177. RETURN_IF_ERROR(CallPathSink(
  178. path_sink, AppendPathSegmentToPrefix(current_prefix, segment)));
  179. }
  180. // Removes the last prefix after seeing a ')'.
  181. if (i < length && paths[i] == ')') {
  182. if (prefix.empty()) {
  183. return util::Status(
  184. util::error::INVALID_ARGUMENT,
  185. StrCat("Invalid FieldMask '", paths,
  186. "'. Cannot find matching '(' for all ')'."));
  187. }
  188. prefix.pop();
  189. }
  190. previous_position = i + 1;
  191. }
  192. if (in_map_key) {
  193. return util::Status(util::error::INVALID_ARGUMENT,
  194. StrCat("Invalid FieldMask '", paths,
  195. "'. Cannot find matching ']' for all '['."));
  196. }
  197. if (!prefix.empty()) {
  198. return util::Status(util::error::INVALID_ARGUMENT,
  199. StrCat("Invalid FieldMask '", paths,
  200. "'. Cannot find matching ')' for all '('."));
  201. }
  202. return util::Status();
  203. }
  204. } // namespace converter
  205. } // namespace util
  206. } // namespace protobuf
  207. } // namespace google