Chromium Code Reviews| 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 /// An abstract enumeration. | |
| 10 abstract class Enum2<E extends Enum2> implements Comparable<E> { | |
| 11 /// The name of this enum constant, as declared in the enum declaration. | |
| 12 final String name; | |
| 13 | |
| 14 /// The position in the enum declaration. | |
| 15 final int ordinal; | |
| 16 | |
| 17 const Enum2(this.name, this.ordinal); | |
| 18 | |
| 19 int get hashCode => ordinal; | |
| 20 | |
| 21 String toString() => name; | |
| 22 | |
| 23 int compareTo(E other) => ordinal - other.ordinal; | |
| 24 | |
| 25 /// Returns the enum constant with the given [name], `null` if not found. | |
| 26 static Enum2 valueOf(List<Enum2> values, String name) { | |
| 27 for (var e in values) { | |
|
Brian Wilkerson
2014/05/24 15:19:49
The body of the method should be indented.
We sho
scheglov
2014/05/24 15:34:42
Done.
| |
| 28 if (e.name == name) { | |
| 29 return e; | |
| 30 } | |
| 31 } | |
| 32 return null; | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 | |
| 9 /** | 37 /** |
| 10 * Instances of the class [Request] represent a request that was received. | 38 * Instances of the class [Request] represent a request that was received. |
| 11 */ | 39 */ |
| 12 class Request { | 40 class Request { |
| 13 /** | 41 /** |
| 14 * The name of the JSON attribute containing the id of the request. | 42 * The name of the JSON attribute containing the id of the request. |
| 15 */ | 43 */ |
| 16 static const String ID = 'id'; | 44 static const String ID = 'id'; |
| 17 | 45 |
| 18 /** | 46 /** |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 276 */ | 304 */ |
| 277 List<String> asStringList() { | 305 List<String> asStringList() { |
| 278 if (!isStringList()) { | 306 if (!isStringList()) { |
| 279 throw new RequestFailure(new Response.invalidParameter(request, path, | 307 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 280 "be a list of strings")); | 308 "be a list of strings")); |
| 281 } | 309 } |
| 282 return datum; | 310 return datum; |
| 283 } | 311 } |
| 284 | 312 |
| 285 /** | 313 /** |
| 314 * Validate that the datum is a list of strings, and convert it into [Enum]s. | |
| 315 */ | |
| 316 Set<Enum2> asEnumSet(List<Enum2> allValues) { | |
| 317 var values = new Set(); | |
| 318 for (var name in asStringList()) { | |
| 319 var value = Enum2.valueOf(allValues, name); | |
| 320 if (value == null) { | |
| 321 throw new RequestFailure(new Response.invalidParameter(request, path, | |
| 322 "be a list of names the enumeration values $allValues")); | |
|
Brian Wilkerson
2014/05/24 15:19:49
"names the enumeration values" --> "names from the
scheglov
2014/05/24 15:34:42
Done.
| |
| 323 } | |
| 324 values.add(value); | |
| 325 } | |
| 326 return values; | |
| 327 } | |
| 328 | |
| 329 /** | |
| 286 * Determine if the datum is a map whose values are all strings. | 330 * Determine if the datum is a map whose values are all strings. |
| 287 * | 331 * |
| 288 * Note: we can safely assume that the keys are all strings, since JSON maps | 332 * Note: we can safely assume that the keys are all strings, since JSON maps |
| 289 * cannot have any other key type. | 333 * cannot have any other key type. |
| 290 */ | 334 */ |
| 291 bool isStringMap() { | 335 bool isStringMap() { |
| 292 if (datum is! Map) { | 336 if (datum is! Map) { |
| 293 return false; | 337 return false; |
| 294 } | 338 } |
| 295 for (var value in datum.values) { | 339 for (var value in datum.values) { |
| (...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 737 /** | 781 /** |
| 738 * The response to be returned as a result of the failure. | 782 * The response to be returned as a result of the failure. |
| 739 */ | 783 */ |
| 740 final Response response; | 784 final Response response; |
| 741 | 785 |
| 742 /** | 786 /** |
| 743 * Initialize a newly created exception to return the given reponse. | 787 * Initialize a newly created exception to return the given reponse. |
| 744 */ | 788 */ |
| 745 RequestFailure(this.response); | 789 RequestFailure(this.response); |
| 746 } | 790 } |
| OLD | NEW |