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