| 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:collection'; | 7 import 'dart:collection'; |
| 8 import 'dart:convert' show JsonDecoder; | 8 import 'dart:convert' show JsonDecoder; |
| 9 | 9 |
| 10 import 'package:analysis_server/src/services/json.dart'; | 10 import 'package:analysis_server/src/services/json.dart'; |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * An abstract enumeration. | |
| 14 */ | |
| 15 abstract class Enum2<E extends Enum2> implements Comparable<E> { | |
| 16 /** | |
| 17 * The name of this enum constant, as declared in the enum declaration. | |
| 18 */ | |
| 19 final String name; | |
| 20 | |
| 21 /** | |
| 22 * The position in the enum declaration. | |
| 23 */ | |
| 24 final int ordinal; | |
| 25 | |
| 26 const Enum2(this.name, this.ordinal); | |
| 27 | |
| 28 @override | |
| 29 int get hashCode => ordinal; | |
| 30 | |
| 31 @override | |
| 32 String toString() => name; | |
| 33 | |
| 34 int compareTo(E other) => ordinal - other.ordinal; | |
| 35 | |
| 36 /** | |
| 37 * Returns the enum constant with the given [name], `null` if not found. | |
| 38 */ | |
| 39 static Enum2 valueOf(List<Enum2> values, String name) { | |
| 40 for (int i = 0; i < values.length; i++) { | |
| 41 Enum2 value = values[i]; | |
| 42 if (value.name == name) { | |
| 43 return value; | |
| 44 } | |
| 45 } | |
| 46 return null; | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 | |
| 51 /** | |
| 52 * Instances of the class [Request] represent a request that was received. | 13 * Instances of the class [Request] represent a request that was received. |
| 53 */ | 14 */ |
| 54 class Request { | 15 class Request { |
| 55 /** | 16 /** |
| 56 * The name of the JSON attribute containing the id of the request. | 17 * The name of the JSON attribute containing the id of the request. |
| 57 */ | 18 */ |
| 58 static const String ID = 'id'; | 19 static const String ID = 'id'; |
| 59 | 20 |
| 60 /** | 21 /** |
| 61 * The name of the JSON attribute containing the name of the request. | 22 * The name of the JSON attribute containing the name of the request. |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 } else if (params != null) { | 90 } else if (params != null) { |
| 130 return null; | 91 return null; |
| 131 } | 92 } |
| 132 return request; | 93 return request; |
| 133 } catch (exception) { | 94 } catch (exception) { |
| 134 return null; | 95 return null; |
| 135 } | 96 } |
| 136 } | 97 } |
| 137 | 98 |
| 138 /** | 99 /** |
| 139 * Return the value of the parameter with the given [name], or [defaultValue] | |
| 140 * if there is no such parameter associated with this request. | |
| 141 */ | |
| 142 RequestDatum getParameter(String name, defaultValue) { | |
| 143 if (!params.containsKey(name)) { | |
| 144 return new RequestDatum(this, "default for $name", defaultValue); | |
| 145 } | |
| 146 return new RequestDatum(this, name, params[name]); | |
| 147 } | |
| 148 | |
| 149 /** | |
| 150 * Return the value of the parameter with the given [name], or throw a | 100 * Return the value of the parameter with the given [name], or throw a |
| 151 * [RequestFailure] exception with an appropriate error message if there is no | 101 * [RequestFailure] exception with an appropriate error message if there is no |
| 152 * such parameter associated with this request. | 102 * such parameter associated with this request. |
| 153 */ | 103 */ |
| 154 RequestDatum getRequiredParameter(String name) { | 104 RequestDatum getRequiredParameter(String name) { |
| 155 if (!params.containsKey(name)) { | 105 if (!params.containsKey(name)) { |
| 156 throw new RequestFailure(new Response.missingRequiredParameter(this, name)
); | 106 throw new RequestFailure(new Response.missingRequiredParameter(this, name)
); |
| 157 } | 107 } |
| 158 return new RequestDatum(this, name, params[name]); | 108 return new RequestDatum(this, name, params[name]); |
| 159 } | 109 } |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 */ | 298 */ |
| 349 List<String> asStringList() { | 299 List<String> asStringList() { |
| 350 if (!isStringList) { | 300 if (!isStringList) { |
| 351 throw new RequestFailure(new Response.invalidParameter(request, path, | 301 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 352 "be a list of strings")); | 302 "be a list of strings")); |
| 353 } | 303 } |
| 354 return _asList(); | 304 return _asList(); |
| 355 } | 305 } |
| 356 | 306 |
| 357 /** | 307 /** |
| 358 * Validate that the datum is a list of strings, and convert it into [Enum]s. | |
| 359 */ | |
| 360 Set<Enum2> asEnumSet(List<Enum2> allValues) { | |
| 361 Set values = new Set(); | |
| 362 for (String name in asStringList()) { | |
| 363 Enum2 value = Enum2.valueOf(allValues, name); | |
| 364 if (value == null) { | |
| 365 throw new RequestFailure(new Response.invalidParameter(request, path, | |
| 366 "be a list of names from the list $allValues")); | |
| 367 } | |
| 368 values.add(value); | |
| 369 } | |
| 370 return values; | |
| 371 } | |
| 372 | |
| 373 /** | |
| 374 * Determine if the datum is a map. Note: null is considered a synonym for | 308 * Determine if the datum is a map. Note: null is considered a synonym for |
| 375 * the empty map. | 309 * the empty map. |
| 376 */ | 310 */ |
| 377 bool get isMap { | 311 bool get isMap { |
| 378 return datum == null || datum is Map; | 312 return datum == null || datum is Map; |
| 379 } | 313 } |
| 380 | 314 |
| 381 /** | 315 /** |
| 382 * Validate that the datum is a map, and return it in raw form. | 316 * Validate that the datum is a map, and return it in raw form. |
| 383 */ | 317 */ |
| (...skipping 571 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 955 */ | 889 */ |
| 956 _toJson(Object value) { | 890 _toJson(Object value) { |
| 957 if (value is HasToJson) { | 891 if (value is HasToJson) { |
| 958 return value.toJson(); | 892 return value.toJson(); |
| 959 } | 893 } |
| 960 if (value is Iterable) { | 894 if (value is Iterable) { |
| 961 return value.map((item) => _toJson(item)).toList(); | 895 return value.map((item) => _toJson(item)).toList(); |
| 962 } | 896 } |
| 963 return value; | 897 return value; |
| 964 } | 898 } |
| OLD | NEW |