Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: pkg/analysis_server/lib/src/protocol.dart

Issue 342383002: Allow "null" as a synonym for [] or {} in the analysis server API. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/analysis_server/test/protocol_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library protocol; 5 library protocol;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 import 'dart:convert' show JsonDecoder; 8 import 'dart:convert' show JsonDecoder;
9 9
10 /** 10 /**
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 } catch (exception) { 131 } catch (exception) {
132 return null; 132 return null;
133 } 133 }
134 } 134 }
135 135
136 /** 136 /**
137 * Return the value of the parameter with the given [name], or [defaultValue] 137 * Return the value of the parameter with the given [name], or [defaultValue]
138 * if there is no such parameter associated with this request. 138 * if there is no such parameter associated with this request.
139 */ 139 */
140 RequestDatum getParameter(String name, defaultValue) { 140 RequestDatum getParameter(String name, defaultValue) {
141 Object value = params[name]; 141 if (!params.containsKey(name)) {
142 if (value == null) {
143 return new RequestDatum(this, "default for $name", defaultValue); 142 return new RequestDatum(this, "default for $name", defaultValue);
144 } 143 }
144 Object value = params[name];
Brian Wilkerson 2014/06/21 03:24:51 Remove this line, given that 'value' is never acce
Paul Berry 2014/06/23 16:51:45 Done.
145 return new RequestDatum(this, name, params[name]); 145 return new RequestDatum(this, name, params[name]);
146 } 146 }
147 147
148 /** 148 /**
149 * Return the value of the parameter with the given [name], or throw a 149 * Return the value of the parameter with the given [name], or throw a
150 * [RequestFailure] exception with an appropriate error message if there is no 150 * [RequestFailure] exception with an appropriate error message if there is no
151 * such parameter associated with this request. 151 * such parameter associated with this request.
152 */ 152 */
153 RequestDatum getRequiredParameter(String name) { 153 RequestDatum getRequiredParameter(String name) {
154 Object value = params[name]; 154 if (!params.containsKey(name)) {
155 if (value == null) {
156 throw new RequestFailure(new Response.missingRequiredParameter(this, name) ); 155 throw new RequestFailure(new Response.missingRequiredParameter(this, name) );
157 } 156 }
157 Object value = params[name];
Brian Wilkerson 2014/06/21 03:24:51 Ditto.
Paul Berry 2014/06/23 16:51:45 Done.
158 return new RequestDatum(this, name, value); 158 return new RequestDatum(this, name, value);
159 } 159 }
160 160
161 /** 161 /**
162 * Set the value of the parameter with the given [name] to the given [value]. 162 * Set the value of the parameter with the given [name] to the given [value].
163 */ 163 */
164 void setParameter(String name, Object value) { 164 void setParameter(String name, Object value) {
165 params[name] = value; 165 params[name] = value;
166 } 166 }
167 167
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 * Create a RequestDatum for decoding and validating [datum], which refers to 206 * Create a RequestDatum for decoding and validating [datum], which refers to
207 * [request] in any errors it reports. 207 * [request] in any errors it reports.
208 */ 208 */
209 RequestDatum(this.request, this.path, this.datum); 209 RequestDatum(this.request, this.path, this.datum);
210 210
211 /** 211 /**
212 * Validate that the datum is a Map containing the given [key], and return 212 * Validate that the datum is a Map containing the given [key], and return
213 * a [RequestDatum] containing the corresponding value. 213 * a [RequestDatum] containing the corresponding value.
214 */ 214 */
215 RequestDatum operator [](String key) { 215 RequestDatum operator [](String key) {
216 if (datum is! Map) { 216 Map<String, Object> map = _asMap();
217 throw new RequestFailure(new Response.invalidParameter(request, path, 217 if (!map.containsKey(key)) {
218 "be a map"));
219 }
220 if (!datum.containsKey(key)) {
221 throw new RequestFailure(new Response.invalidParameter(request, path, 218 throw new RequestFailure(new Response.invalidParameter(request, path,
222 "contain key '$key'")); 219 "contain key '$key'"));
223 } 220 }
224 return new RequestDatum(request, "$path.$key", datum[key]); 221 return new RequestDatum(request, "$path.$key", map[key]);
225 } 222 }
226 223
227 /** 224 /**
228 * Return `true` if the datum is a Map containing the given [key]. 225 * Return `true` if the datum is a Map containing the given [key].
229 */ 226 */
230 bool hasKey(String key) { 227 bool hasKey(String key) {
231 if (datum is! Map) { 228 return _asMap().containsKey(key);
232 throw new RequestFailure(new Response.invalidParameter(request, path,
233 "be a map"));
234 }
235 return datum.containsKey(key);
236 } 229 }
237 230
238 /** 231 /**
239 * Validate that the datum is a Map whose keys are strings, and call [f] on 232 * Validate that the datum is a Map whose keys are strings, and call [f] on
240 * each key/value pair in the map. 233 * each key/value pair in the map.
241 */ 234 */
242 void forEachMap(void f(String key, RequestDatum value)) { 235 void forEachMap(void f(String key, RequestDatum value)) {
243 if (datum is! Map) { 236 _asMap().forEach((String key, value) {
244 throw new RequestFailure(new Response.invalidParameter(request, path,
245 "be a map"));
246 }
247 datum.forEach((String key, value) {
248 f(key, new RequestDatum(request, "$path.$key", value)); 237 f(key, new RequestDatum(request, "$path.$key", value));
249 }); 238 });
250 } 239 }
251 240
252 /** 241 /**
253 * Validate that the datum is an integer (or a string that can be parsed 242 * Validate that the datum is an integer (or a string that can be parsed
254 * as an integer), and return the int. 243 * as an integer), and return the int.
255 */ 244 */
256 int asInt() { 245 int asInt() {
257 if (datum is int) { 246 if (datum is int) {
(...skipping 21 matching lines...) Expand all
279 } else if (datum == 'true') { 268 } else if (datum == 'true') {
280 return true; 269 return true;
281 } else if (datum == 'false') { 270 } else if (datum == 'false') {
282 return false; 271 return false;
283 } 272 }
284 throw new RequestFailure(new Response.invalidParameter(request, datum, 273 throw new RequestFailure(new Response.invalidParameter(request, datum,
285 "be a boolean")); 274 "be a boolean"));
286 } 275 }
287 276
288 /** 277 /**
278 * Determine if the datum is a list. Note: null is considered a synonym for
279 * the empty list.
280 */
281 bool get isList {
282 return datum == null || datum is List;
283 }
284
285 /**
286 * Validate that the datum is a list, and return it in raw form.
287 */
288 List _asList() {
289 if (!isList) {
290 throw new RequestFailure(new Response.invalidParameter(request, path,
291 "be a list"));
292 }
293 if (datum == null) {
294 return [];
295 } else {
296 return datum;
297 }
298 }
299
300 /**
289 * Validate that the datum is a list, and return a list where each element in 301 * Validate that the datum is a list, and return a list where each element in
290 * the datum has been converted using the provided function. 302 * the datum has been converted using the provided function.
291 */ 303 */
292 List asList(elementConverter(RequestDatum datum)) { 304 List asList(elementConverter(RequestDatum datum)) {
293 if (datum is! List) {
294 throw new RequestFailure(new Response.invalidParameter(request, path,
295 "be a list"));
296 }
297 List list = datum as List;
298 List result = []; 305 List result = [];
306 List list = _asList();
299 for (int i = 0; i < list.length; i++) { 307 for (int i = 0; i < list.length; i++) {
300 result.add(elementConverter(new RequestDatum(request, "$path.$i", list[i]) )); 308 result.add(elementConverter(new RequestDatum(request, "$path.$i", list[i]) ));
301 } 309 }
302 return result; 310 return result;
303 } 311 }
304 312
305 /** 313 /**
306 * Validate that the datum is a string, and return it. 314 * Validate that the datum is a string, and return it.
307 */ 315 */
308 String asString() { 316 String asString() {
309 if (datum is! String) { 317 if (datum is! String) {
310 throw new RequestFailure(new Response.invalidParameter(request, path, 318 throw new RequestFailure(new Response.invalidParameter(request, path,
311 "be a string")); 319 "be a string"));
312 } 320 }
313 return datum; 321 return datum;
314 } 322 }
315 323
316 /** 324 /**
317 * Determine if the datum is a list of strings. 325 * Determine if the datum is a list of strings. Note: null is considered a
326 * synonym for the empty list.
318 */ 327 */
319 bool get isStringList { 328 bool get isStringList {
320 if (datum is! List) { 329 if (!isList) {
321 return false; 330 return false;
322 } 331 }
323 for (var element in datum) { 332 for (var element in _asList()) {
324 if (element is! String) { 333 if (element is! String) {
325 return false; 334 return false;
326 } 335 }
327 } 336 }
328 return true; 337 return true;
329 } 338 }
330 339
331 /** 340 /**
332 * Validate that the datum is a list of strings, and return it. 341 * Validate that the datum is a list of strings, and return it. Note: null
342 * is considered a synonym for the empty list.
333 */ 343 */
334 List<String> asStringList() { 344 List<String> asStringList() {
335 if (!isStringList) { 345 if (!isStringList) {
336 throw new RequestFailure(new Response.invalidParameter(request, path, 346 throw new RequestFailure(new Response.invalidParameter(request, path,
337 "be a list of strings")); 347 "be a list of strings"));
338 } 348 }
339 return datum; 349 return _asList();
340 } 350 }
341 351
342 /** 352 /**
343 * Validate that the datum is a list of strings, and convert it into [Enum]s. 353 * Validate that the datum is a list of strings, and convert it into [Enum]s.
344 */ 354 */
345 Set<Enum2> asEnumSet(List<Enum2> allValues) { 355 Set<Enum2> asEnumSet(List<Enum2> allValues) {
346 Set values = new Set(); 356 Set values = new Set();
347 for (String name in asStringList()) { 357 for (String name in asStringList()) {
348 Enum2 value = Enum2.valueOf(allValues, name); 358 Enum2 value = Enum2.valueOf(allValues, name);
349 if (value == null) { 359 if (value == null) {
350 throw new RequestFailure(new Response.invalidParameter(request, path, 360 throw new RequestFailure(new Response.invalidParameter(request, path,
351 "be a list of names from the list $allValues")); 361 "be a list of names from the list $allValues"));
352 } 362 }
353 values.add(value); 363 values.add(value);
354 } 364 }
355 return values; 365 return values;
356 } 366 }
357 367
358 /** 368 /**
359 * Determine if the datum is a map whose values are all strings. 369 * Determine if the datum is a map. Note: null is considered a synonym for
370 * the empty map.
371 */
372 bool get isMap {
373 return datum == null || datum is Map;
374 }
375
376 /**
377 * Validate that the datum is a map, and return it in raw form.
378 */
379 Map<String, Object> _asMap() {
380 if (!isMap) {
381 throw new RequestFailure(new Response.invalidParameter(request, path,
382 "be a map"));
383 }
384 if (datum == null) {
385 return {};
386 } else {
387 return datum;
388 }
389 }
390
391 /**
392 * Determine if the datum is a map whose values are all strings. Note: null
393 * is considered a synonym for the empty map.
360 * 394 *
361 * Note: we can safely assume that the keys are all strings, since JSON maps 395 * Note: we can safely assume that the keys are all strings, since JSON maps
362 * cannot have any other key type. 396 * cannot have any other key type.
363 */ 397 */
364 bool get isStringMap { 398 bool get isStringMap {
365 if (datum is! Map) { 399 if (!isMap) {
366 return false; 400 return false;
367 } 401 }
368 for (var value in datum.values) { 402 for (var value in _asMap().values) {
369 if (value is! String) { 403 if (value is! String) {
370 return false; 404 return false;
371 } 405 }
372 } 406 }
373 return true; 407 return true;
374 } 408 }
375 409
376 /** 410 /**
377 * Validate that the datum is a map from strings to strings, and return it. 411 * Validate that the datum is a map from strings to strings, and return it.
378 */ 412 */
379 Map<String, String> asStringMap() { 413 Map<String, String> asStringMap() {
380 if (!isStringMap) { 414 if (!isStringMap) {
381 throw new RequestFailure(new Response.invalidParameter(request, path, 415 throw new RequestFailure(new Response.invalidParameter(request, path,
382 "be a string map")); 416 "be a string map"));
383 } 417 }
384 return datum; 418 return _asMap();
385 } 419 }
386 420
387 /** 421 /**
388 * Determine if the datum is a map whose values are all string lists. 422 * Determine if the datum is a map whose values are all string lists. Note:
423 * null is considered a synonym for the empty map.
389 * 424 *
390 * Note: we can safely assume that the keys are all strings, since JSON maps 425 * Note: we can safely assume that the keys are all strings, since JSON maps
391 * cannot have any other key type. 426 * cannot have any other key type.
392 */ 427 */
393 bool isStringListMap() { 428 bool get isStringListMap {
394 if (datum is! Map) { 429 if (!isMap) {
395 return false; 430 return false;
396 } 431 }
397 for (var value in datum.values) { 432 for (var value in _asMap().values) {
398 if (value is! List) { 433 if (value is! List) {
399 return false; 434 return false;
400 } 435 }
401 for (var listItem in value) { 436 for (var listItem in value) {
402 if (listItem is! String) { 437 if (listItem is! String) {
403 return false; 438 return false;
404 } 439 }
405 } 440 }
406 } 441 }
407 return true; 442 return true;
408 } 443 }
409 444
410 /** 445 /**
411 * Validate that the datum is a map from strings to string listss, and return 446 * Validate that the datum is a map from strings to string lists, and return
412 * it. 447 * it.
413 */ 448 */
414 Map<String, List<String>> asStringListMap() { 449 Map<String, List<String>> asStringListMap() {
415 if (!isStringListMap()) { 450 if (!isStringListMap) {
416 throw new RequestFailure(new Response.invalidParameter(request, path, 451 throw new RequestFailure(new Response.invalidParameter(request, path,
417 "be a string list map")); 452 "be a string list map"));
418 } 453 }
419 return datum; 454 return _asMap();
420 } 455 }
421 456
422 bool get isNull => datum == null; 457 bool get isNull => datum == null;
423 } 458 }
424 459
425 /** 460 /**
426 * Instances of the class [Response] represent a response to a request. 461 * Instances of the class [Response] represent a response to a request.
427 */ 462 */
428 class Response { 463 class Response {
429 /** 464 /**
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after
877 /** 912 /**
878 * The response to be returned as a result of the failure. 913 * The response to be returned as a result of the failure.
879 */ 914 */
880 final Response response; 915 final Response response;
881 916
882 /** 917 /**
883 * Initialize a newly created exception to return the given reponse. 918 * Initialize a newly created exception to return the given reponse.
884 */ 919 */
885 RequestFailure(this.response); 920 RequestFailure(this.response);
886 } 921 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analysis_server/test/protocol_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698