| OLD | NEW |
| 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:convert' show JsonDecoder; | 7 import 'dart:convert' show JsonDecoder; |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * An abstract enumeration. | 10 * An abstract enumeration. |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 /** | 358 /** |
| 359 * Validate that the datum is a map from strings to strings, and return it. | 359 * Validate that the datum is a map from strings to strings, and return it. |
| 360 */ | 360 */ |
| 361 Map<String, String> asStringMap() { | 361 Map<String, String> asStringMap() { |
| 362 if (!isStringMap) { | 362 if (!isStringMap) { |
| 363 throw new RequestFailure(new Response.invalidParameter(request, path, | 363 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 364 "be a string map")); | 364 "be a string map")); |
| 365 } | 365 } |
| 366 return datum; | 366 return datum; |
| 367 } | 367 } |
| 368 |
| 369 /** |
| 370 * Determine if the datum is a map whose values are all string lists. |
| 371 * |
| 372 * Note: we can safely assume that the keys are all strings, since JSON maps |
| 373 * cannot have any other key type. |
| 374 */ |
| 375 bool isStringListMap() { |
| 376 if (datum is! Map) { |
| 377 return false; |
| 378 } |
| 379 for (var value in datum.values) { |
| 380 if (value is! List) { |
| 381 return false; |
| 382 } |
| 383 for (var listItem in value) { |
| 384 if (listItem is! String) { |
| 385 return false; |
| 386 } |
| 387 } |
| 388 } |
| 389 return true; |
| 390 } |
| 391 |
| 392 /** |
| 393 * Validate that the datum is a map from strings to string listss, and return |
| 394 * it. |
| 395 */ |
| 396 Map<String, List<String>> asStringListMap() { |
| 397 if (!isStringListMap()) { |
| 398 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 399 "be a string list map")); |
| 400 } |
| 401 return datum; |
| 402 } |
| 368 } | 403 } |
| 369 | 404 |
| 370 /** | 405 /** |
| 371 * Instances of the class [Response] represent a response to a request. | 406 * Instances of the class [Response] represent a response to a request. |
| 372 */ | 407 */ |
| 373 class Response { | 408 class Response { |
| 374 /** | 409 /** |
| 375 * The name of the JSON attribute containing the id of the request for which | 410 * The name of the JSON attribute containing the id of the request for which |
| 376 * this is a response. | 411 * this is a response. |
| 377 */ | 412 */ |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 Response.unknownRequest(Request request) | 494 Response.unknownRequest(Request request) |
| 460 : this(request.id, new RequestError(-7, 'Unknown request')); | 495 : this(request.id, new RequestError(-7, 'Unknown request')); |
| 461 | 496 |
| 462 Response.contextAlreadyExists(Request request) | 497 Response.contextAlreadyExists(Request request) |
| 463 : this(request.id, new RequestError(-8, 'Context already exists')); | 498 : this(request.id, new RequestError(-8, 'Context already exists')); |
| 464 | 499 |
| 465 Response.unsupportedFeature(String requestId, String message) | 500 Response.unsupportedFeature(String requestId, String message) |
| 466 : this(requestId, new RequestError(-9, message)); | 501 : this(requestId, new RequestError(-9, message)); |
| 467 | 502 |
| 468 /** | 503 /** |
| 504 * Initialize a newly created instance to represent an error condition caused |
| 505 * by a `analysis.setSubscriptions` [request] that includes an unknown |
| 506 * analysis service name. |
| 507 */ |
| 508 Response.unknownAnalysisService(Request request, String name) |
| 509 : this(request.id, new RequestError(-10, 'Unknown analysis service: "$name"'
)); |
| 510 |
| 511 /** |
| 469 * Initialize a newly created instance based upon the given JSON data | 512 * Initialize a newly created instance based upon the given JSON data |
| 470 */ | 513 */ |
| 471 factory Response.fromJson(Map<String, Object> json) { | 514 factory Response.fromJson(Map<String, Object> json) { |
| 472 try { | 515 try { |
| 473 Object id = json[Response.ID]; | 516 Object id = json[Response.ID]; |
| 474 if (id is! String) { | 517 if (id is! String) { |
| 475 return null; | 518 return null; |
| 476 } | 519 } |
| 477 Object error = json[Response.ERROR]; | 520 Object error = json[Response.ERROR]; |
| 478 Object result = json[Response.RESULT]; | 521 Object result = json[Response.RESULT]; |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 798 /** | 841 /** |
| 799 * The response to be returned as a result of the failure. | 842 * The response to be returned as a result of the failure. |
| 800 */ | 843 */ |
| 801 final Response response; | 844 final Response response; |
| 802 | 845 |
| 803 /** | 846 /** |
| 804 * Initialize a newly created exception to return the given reponse. | 847 * Initialize a newly created exception to return the given reponse. |
| 805 */ | 848 */ |
| 806 RequestFailure(this.response); | 849 RequestFailure(this.response); |
| 807 } | 850 } |
| OLD | NEW |