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 * Instances of the class [Request] represent a request that was received. | 10 * Instances of the class [Request] represent a request that was received. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 * 'params': { | 62 * 'params': { |
63 * paramter_name: value | 63 * paramter_name: value |
64 * } | 64 * } |
65 * } | 65 * } |
66 * | 66 * |
67 * where the parameters are optional and can contain any number of name/value | 67 * where the parameters are optional and can contain any number of name/value |
68 * pairs. | 68 * pairs. |
69 */ | 69 */ |
70 factory Request.fromString(String data) { | 70 factory Request.fromString(String data) { |
71 try { | 71 try { |
72 var result = DECODER.convert(data); | 72 dynamic result = DECODER.convert(data); |
Brian Wilkerson
2014/05/24 17:36:52
Hm. It might be better to leave 'var' in places wh
scheglov
2014/05/24 17:45:50
I rolled back places where we use "dynamic" type.
| |
73 if (result is! Map) { | 73 if (result is! Map) { |
74 return null; | 74 return null; |
75 } | 75 } |
76 var id = result[Request.ID]; | 76 dynamic id = result[Request.ID]; |
77 var method = result[Request.METHOD]; | 77 dynamic method = result[Request.METHOD]; |
78 if (id is! String || method is! String) { | 78 if (id is! String || method is! String) { |
79 return null; | 79 return null; |
80 } | 80 } |
81 var params = result[Request.PARAMS]; | 81 dynamic params = result[Request.PARAMS]; |
82 Request request = new Request(id, method); | 82 Request request = new Request(id, method); |
83 if (params is Map) { | 83 if (params is Map) { |
84 params.forEach((String key, Object value) { | 84 params.forEach((String key, Object value) { |
85 request.setParameter(key, value); | 85 request.setParameter(key, value); |
86 }); | 86 }); |
87 } else if (params != null) { | 87 } else if (params != null) { |
88 return null; | 88 return null; |
89 } | 89 } |
90 return request; | 90 return request; |
91 } catch (exception) { | 91 } catch (exception) { |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 return datum; | 256 return datum; |
257 } | 257 } |
258 | 258 |
259 /** | 259 /** |
260 * Determine if the datum is a list of strings. | 260 * Determine if the datum is a list of strings. |
261 */ | 261 */ |
262 bool isStringList() { | 262 bool isStringList() { |
263 if (datum is! List) { | 263 if (datum is! List) { |
264 return false; | 264 return false; |
265 } | 265 } |
266 for (var element in datum) { | 266 for (dynamic element in datum) { |
267 if (element is! String) { | 267 if (element is! String) { |
268 return false; | 268 return false; |
269 } | 269 } |
270 } | 270 } |
271 return true; | 271 return true; |
272 } | 272 } |
273 | 273 |
274 /** | 274 /** |
275 * Validate that the datum is a list of strings, and return it. | 275 * Validate that the datum is a list of strings, and return it. |
276 */ | 276 */ |
277 List<String> asStringList() { | 277 List<String> asStringList() { |
278 if (!isStringList()) { | 278 if (!isStringList()) { |
279 throw new RequestFailure(new Response.invalidParameter(request, path, | 279 throw new RequestFailure(new Response.invalidParameter(request, path, |
280 "be a list of strings")); | 280 "be a list of strings")); |
281 } | 281 } |
282 return datum; | 282 return datum; |
283 } | 283 } |
284 | 284 |
285 /** | 285 /** |
286 * Determine if the datum is a map whose values are all strings. | 286 * Determine if the datum is a map whose values are all strings. |
287 * | 287 * |
288 * Note: we can safely assume that the keys are all strings, since JSON maps | 288 * Note: we can safely assume that the keys are all strings, since JSON maps |
289 * cannot have any other key type. | 289 * cannot have any other key type. |
290 */ | 290 */ |
291 bool isStringMap() { | 291 bool isStringMap() { |
292 if (datum is! Map) { | 292 if (datum is! Map) { |
293 return false; | 293 return false; |
294 } | 294 } |
295 for (var value in datum.values) { | 295 for (dynamic value in datum.values) { |
296 if (value is! String) { | 296 if (value is! String) { |
297 return false; | 297 return false; |
298 } | 298 } |
299 } | 299 } |
300 return true; | 300 return true; |
301 } | 301 } |
302 | 302 |
303 /** | 303 /** |
304 * Validate that the datum is a map from strings to strings, and return it. | 304 * Validate that the datum is a map from strings to strings, and return it. |
305 */ | 305 */ |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
405 : this(request.id, new RequestError(-7, 'Unknown request')); | 405 : this(request.id, new RequestError(-7, 'Unknown request')); |
406 | 406 |
407 Response.contextAlreadyExists(Request request) | 407 Response.contextAlreadyExists(Request request) |
408 : this(request.id, new RequestError(-8, 'Context already exists')); | 408 : this(request.id, new RequestError(-8, 'Context already exists')); |
409 | 409 |
410 /** | 410 /** |
411 * Initialize a newly created instance based upon the given JSON data | 411 * Initialize a newly created instance based upon the given JSON data |
412 */ | 412 */ |
413 factory Response.fromJson(Map<String, Object> json) { | 413 factory Response.fromJson(Map<String, Object> json) { |
414 try { | 414 try { |
415 var id = json[Response.ID]; | 415 Object id = json[Response.ID]; |
416 if (id is! String) { | 416 if (id is! String) { |
417 return null; | 417 return null; |
418 } | 418 } |
419 var error = json[Response.ERROR]; | 419 Object error = json[Response.ERROR]; |
420 var result = json[Response.RESULT]; | 420 Object result = json[Response.RESULT]; |
421 Response response; | 421 Response response; |
422 if (error is Map) { | 422 if (error is Map) { |
423 response = new Response(id, new RequestError.fromJson(error)); | 423 response = new Response(id, new RequestError.fromJson(error)); |
424 } else { | 424 } else { |
425 response = new Response(id); | 425 response = new Response(id); |
426 } | 426 } |
427 if (result is Map) { | 427 if (result is Map) { |
428 result.forEach((String key, Object value) { | 428 result.forEach((String key, Object value) { |
429 response.setResult(key, value); | 429 response.setResult(key, value); |
430 }); | 430 }); |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
667 * Initialize a newly created [Notification] to have the given [event] name. | 667 * Initialize a newly created [Notification] to have the given [event] name. |
668 */ | 668 */ |
669 Notification(this.event); | 669 Notification(this.event); |
670 | 670 |
671 /** | 671 /** |
672 * Initialize a newly created instance based upon the given JSON data | 672 * Initialize a newly created instance based upon the given JSON data |
673 */ | 673 */ |
674 factory Notification.fromJson(Map<String, Object> json) { | 674 factory Notification.fromJson(Map<String, Object> json) { |
675 try { | 675 try { |
676 String event = json[Notification.EVENT]; | 676 String event = json[Notification.EVENT]; |
677 var params = json[Notification.PARAMS]; | 677 Object params = json[Notification.PARAMS]; |
678 Notification notification = new Notification(event); | 678 Notification notification = new Notification(event); |
679 if (params is Map) { | 679 if (params is Map) { |
680 params.forEach((String key, Object value) { | 680 params.forEach((String key, Object value) { |
681 notification.setParameter(key, value); | 681 notification.setParameter(key, value); |
682 }); | 682 }); |
683 } | 683 } |
684 return notification; | 684 return notification; |
685 } catch (exception) { | 685 } catch (exception) { |
686 return null; | 686 return null; |
687 } | 687 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
737 /** | 737 /** |
738 * The response to be returned as a result of the failure. | 738 * The response to be returned as a result of the failure. |
739 */ | 739 */ |
740 final Response response; | 740 final Response response; |
741 | 741 |
742 /** | 742 /** |
743 * Initialize a newly created exception to return the given reponse. | 743 * Initialize a newly created exception to return the given reponse. |
744 */ | 744 */ |
745 RequestFailure(this.response); | 745 RequestFailure(this.response); |
746 } | 746 } |
OLD | NEW |