| 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 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 if (datum is! String) { | 291 if (datum is! String) { |
| 292 throw new RequestFailure(new Response.invalidParameter(request, path, | 292 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 293 "be a string")); | 293 "be a string")); |
| 294 } | 294 } |
| 295 return datum; | 295 return datum; |
| 296 } | 296 } |
| 297 | 297 |
| 298 /** | 298 /** |
| 299 * Determine if the datum is a list of strings. | 299 * Determine if the datum is a list of strings. |
| 300 */ | 300 */ |
| 301 bool isStringList() { | 301 bool get isStringList { |
| 302 if (datum is! List) { | 302 if (datum is! List) { |
| 303 return false; | 303 return false; |
| 304 } | 304 } |
| 305 for (var element in datum) { | 305 for (var element in datum) { |
| 306 if (element is! String) { | 306 if (element is! String) { |
| 307 return false; | 307 return false; |
| 308 } | 308 } |
| 309 } | 309 } |
| 310 return true; | 310 return true; |
| 311 } | 311 } |
| 312 | 312 |
| 313 /** | 313 /** |
| 314 * Validate that the datum is a list of strings, and return it. | 314 * Validate that the datum is a list of strings, and return it. |
| 315 */ | 315 */ |
| 316 List<String> asStringList() { | 316 List<String> asStringList() { |
| 317 if (!isStringList()) { | 317 if (!isStringList) { |
| 318 throw new RequestFailure(new Response.invalidParameter(request, path, | 318 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 319 "be a list of strings")); | 319 "be a list of strings")); |
| 320 } | 320 } |
| 321 return datum; | 321 return datum; |
| 322 } | 322 } |
| 323 | 323 |
| 324 /** | 324 /** |
| 325 * Validate that the datum is a list of strings, and convert it into [Enum]s. | 325 * Validate that the datum is a list of strings, and convert it into [Enum]s. |
| 326 */ | 326 */ |
| 327 Set<Enum2> asEnumSet(List<Enum2> allValues) { | 327 Set<Enum2> asEnumSet(List<Enum2> allValues) { |
| 328 Set values = new Set(); | 328 Set values = new Set(); |
| 329 for (String name in asStringList()) { | 329 for (String name in asStringList()) { |
| 330 Enum2 value = Enum2.valueOf(allValues, name); | 330 Enum2 value = Enum2.valueOf(allValues, name); |
| 331 if (value == null) { | 331 if (value == null) { |
| 332 throw new RequestFailure(new Response.invalidParameter(request, path, | 332 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 333 "be a list of names from the list $allValues")); | 333 "be a list of names from the list $allValues")); |
| 334 } | 334 } |
| 335 values.add(value); | 335 values.add(value); |
| 336 } | 336 } |
| 337 return values; | 337 return values; |
| 338 } | 338 } |
| 339 | 339 |
| 340 /** | 340 /** |
| 341 * Determine if the datum is a map whose values are all strings. | 341 * Determine if the datum is a map whose values are all strings. |
| 342 * | 342 * |
| 343 * Note: we can safely assume that the keys are all strings, since JSON maps | 343 * Note: we can safely assume that the keys are all strings, since JSON maps |
| 344 * cannot have any other key type. | 344 * cannot have any other key type. |
| 345 */ | 345 */ |
| 346 bool isStringMap() { | 346 bool get isStringMap { |
| 347 if (datum is! Map) { | 347 if (datum is! Map) { |
| 348 return false; | 348 return false; |
| 349 } | 349 } |
| 350 for (var value in datum.values) { | 350 for (var value in datum.values) { |
| 351 if (value is! String) { | 351 if (value is! String) { |
| 352 return false; | 352 return false; |
| 353 } | 353 } |
| 354 } | 354 } |
| 355 return true; | 355 return true; |
| 356 } | 356 } |
| 357 | 357 |
| 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 } | 368 } |
| 369 | 369 |
| 370 /** | 370 /** |
| 371 * Instances of the class [Response] represent a response to a request. | 371 * Instances of the class [Response] represent a response to a request. |
| 372 */ | 372 */ |
| (...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 798 /** | 798 /** |
| 799 * The response to be returned as a result of the failure. | 799 * The response to be returned as a result of the failure. |
| 800 */ | 800 */ |
| 801 final Response response; | 801 final Response response; |
| 802 | 802 |
| 803 /** | 803 /** |
| 804 * Initialize a newly created exception to return the given reponse. | 804 * Initialize a newly created exception to return the given reponse. |
| 805 */ | 805 */ |
| 806 RequestFailure(this.response); | 806 RequestFailure(this.response); |
| 807 } | 807 } |
| OLD | NEW |