| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library protocol; | |
| 6 | |
| 7 import 'dart:collection'; | |
| 8 import 'dart:convert'; | |
| 9 | |
| 10 import 'json.dart'; | |
| 11 | |
| 12 part 'generated_protocol.dart'; | |
| 13 | |
| 14 | |
| 15 final Map<String, RefactoringKind> REQUEST_ID_REFACTORING_KINDS = | |
| 16 new HashMap<String, RefactoringKind>(); | |
| 17 | |
| 18 /** | |
| 19 * Translate the input [map], applying [keyCallback] to all its keys, and | |
| 20 * [valueCallback] to all its values. | |
| 21 */ | |
| 22 mapMap(Map map, {dynamic keyCallback(key), dynamic valueCallback(value)}) { | |
| 23 Map result = {}; | |
| 24 map.forEach((key, value) { | |
| 25 if (keyCallback != null) { | |
| 26 key = keyCallback(key); | |
| 27 } | |
| 28 if (valueCallback != null) { | |
| 29 value = valueCallback(value); | |
| 30 } | |
| 31 result[key] = value; | |
| 32 }); | |
| 33 return result; | |
| 34 } | |
| 35 | |
| 36 /** | |
| 37 * Adds the given [sourceEdits] to the list in [sourceFileEdit]. | |
| 38 */ | |
| 39 void _addAllEditsForSource(SourceFileEdit sourceFileEdit, | |
| 40 Iterable<SourceEdit> edits) { | |
| 41 edits.forEach(sourceFileEdit.add); | |
| 42 } | |
| 43 | |
| 44 /** | |
| 45 * Adds the given [sourceEdit] to the list in [sourceFileEdit]. | |
| 46 */ | |
| 47 void _addEditForSource(SourceFileEdit sourceFileEdit, SourceEdit sourceEdit) { | |
| 48 List<SourceEdit> edits = sourceFileEdit.edits; | |
| 49 int index = 0; | |
| 50 while (index < edits.length && edits[index].offset > sourceEdit.offset) { | |
| 51 index++; | |
| 52 } | |
| 53 edits.insert(index, sourceEdit); | |
| 54 } | |
| 55 | |
| 56 /** | |
| 57 * Adds [edit] to the [FileEdit] for the given [file]. | |
| 58 */ | |
| 59 void _addEditToSourceChange(SourceChange change, String file, int fileStamp, | |
| 60 SourceEdit edit) { | |
| 61 SourceFileEdit fileEdit = change.getFileEdit(file); | |
| 62 if (fileEdit == null) { | |
| 63 fileEdit = new SourceFileEdit(file, fileStamp); | |
| 64 change.addFileEdit(fileEdit); | |
| 65 } | |
| 66 fileEdit.add(edit); | |
| 67 } | |
| 68 | |
| 69 /** | |
| 70 * Get the result of applying the edit to the given [code]. Access via | |
| 71 * SourceEdit.apply(). | |
| 72 */ | |
| 73 String _applyEdit(String code, SourceEdit edit) { | |
| 74 if (edit.length < 0) { | |
| 75 throw new RangeError('length is negative'); | |
| 76 } | |
| 77 return code.substring(0, edit.offset) + | |
| 78 edit.replacement + | |
| 79 code.substring(edit.end); | |
| 80 } | |
| 81 | |
| 82 /** | |
| 83 * Get the result of applying a set of [edits] to the given [code]. Edits | |
| 84 * are applied in the order they appear in [edits]. Access via | |
| 85 * SourceEdit.applySequence(). | |
| 86 */ | |
| 87 String _applySequence(String code, Iterable<SourceEdit> edits) { | |
| 88 edits.forEach((SourceEdit edit) { | |
| 89 code = edit.apply(code); | |
| 90 }); | |
| 91 return code; | |
| 92 } | |
| 93 | |
| 94 /** | |
| 95 * Returns the [FileEdit] for the given [file], maybe `null`. | |
| 96 */ | |
| 97 SourceFileEdit _getChangeFileEdit(SourceChange change, String file) { | |
| 98 for (SourceFileEdit fileEdit in change.edits) { | |
| 99 if (fileEdit.file == file) { | |
| 100 return fileEdit; | |
| 101 } | |
| 102 } | |
| 103 return null; | |
| 104 } | |
| 105 | |
| 106 /** | |
| 107 * Compare the lists [listA] and [listB], using [itemEqual] to compare | |
| 108 * list elements. | |
| 109 */ | |
| 110 bool _listEqual(List listA, List listB, bool itemEqual(a, b)) { | |
| 111 if (listA.length != listB.length) { | |
| 112 return false; | |
| 113 } | |
| 114 for (int i = 0; i < listA.length; i++) { | |
| 115 if (!itemEqual(listA[i], listB[i])) { | |
| 116 return false; | |
| 117 } | |
| 118 } | |
| 119 return true; | |
| 120 } | |
| 121 | |
| 122 /** | |
| 123 * Compare the maps [mapA] and [mapB], using [valueEqual] to compare map | |
| 124 * values. | |
| 125 */ | |
| 126 bool _mapEqual(Map mapA, Map mapB, bool valueEqual(a, b)) { | |
| 127 if (mapA.length != mapB.length) { | |
| 128 return false; | |
| 129 } | |
| 130 for (var key in mapA.keys) { | |
| 131 if (!mapB.containsKey(key)) { | |
| 132 return false; | |
| 133 } | |
| 134 if (!valueEqual(mapA[key], mapB[key])) { | |
| 135 return false; | |
| 136 } | |
| 137 } | |
| 138 return true; | |
| 139 } | |
| 140 | |
| 141 RefactoringProblemSeverity | |
| 142 _maxRefactoringProblemSeverity(RefactoringProblemSeverity a, | |
| 143 RefactoringProblemSeverity b) { | |
| 144 if (b == null) { | |
| 145 return a; | |
| 146 } | |
| 147 if (a == null) { | |
| 148 return b; | |
| 149 } else if (a == RefactoringProblemSeverity.INFO) { | |
| 150 return b; | |
| 151 } else if (a == RefactoringProblemSeverity.WARNING) { | |
| 152 if (b == RefactoringProblemSeverity.ERROR || | |
| 153 b == RefactoringProblemSeverity.FATAL) { | |
| 154 return b; | |
| 155 } | |
| 156 } else if (a == RefactoringProblemSeverity.ERROR) { | |
| 157 if (b == RefactoringProblemSeverity.FATAL) { | |
| 158 return b; | |
| 159 } | |
| 160 } | |
| 161 return a; | |
| 162 } | |
| 163 | |
| 164 /** | |
| 165 * Create a [RefactoringFeedback] corresponding the given [kind]. | |
| 166 */ | |
| 167 RefactoringFeedback _refactoringFeedbackFromJson(JsonDecoder jsonDecoder, | |
| 168 String jsonPath, Object json, Map feedbackJson) { | |
| 169 String requestId; | |
| 170 if (jsonDecoder is ResponseDecoder) { | |
| 171 requestId = jsonDecoder.response.id; | |
| 172 } | |
| 173 RefactoringKind kind = REQUEST_ID_REFACTORING_KINDS.remove(requestId); | |
| 174 if (kind == RefactoringKind.EXTRACT_LOCAL_VARIABLE) { | |
| 175 return new ExtractLocalVariableFeedback.fromJson( | |
| 176 jsonDecoder, | |
| 177 jsonPath, | |
| 178 json); | |
| 179 } | |
| 180 if (kind == RefactoringKind.EXTRACT_METHOD) { | |
| 181 return new ExtractMethodFeedback.fromJson(jsonDecoder, jsonPath, json); | |
| 182 } | |
| 183 if (kind == RefactoringKind.INLINE_LOCAL_VARIABLE) { | |
| 184 return new InlineLocalVariableFeedback.fromJson( | |
| 185 jsonDecoder, | |
| 186 jsonPath, | |
| 187 json); | |
| 188 } | |
| 189 if (kind == RefactoringKind.INLINE_METHOD) { | |
| 190 return new InlineMethodFeedback.fromJson(jsonDecoder, jsonPath, json); | |
| 191 } | |
| 192 if (kind == RefactoringKind.RENAME) { | |
| 193 return new RenameFeedback.fromJson(jsonDecoder, jsonPath, json); | |
| 194 } | |
| 195 return null; | |
| 196 } | |
| 197 | |
| 198 | |
| 199 /** | |
| 200 * Create a [RefactoringOptions] corresponding the given [kind]. | |
| 201 */ | |
| 202 RefactoringOptions _refactoringOptionsFromJson(JsonDecoder jsonDecoder, | |
| 203 String jsonPath, Object json, RefactoringKind kind) { | |
| 204 if (kind == RefactoringKind.EXTRACT_LOCAL_VARIABLE) { | |
| 205 return new ExtractLocalVariableOptions.fromJson( | |
| 206 jsonDecoder, | |
| 207 jsonPath, | |
| 208 json); | |
| 209 } | |
| 210 if (kind == RefactoringKind.EXTRACT_METHOD) { | |
| 211 return new ExtractMethodOptions.fromJson(jsonDecoder, jsonPath, json); | |
| 212 } | |
| 213 if (kind == RefactoringKind.INLINE_METHOD) { | |
| 214 return new InlineMethodOptions.fromJson(jsonDecoder, jsonPath, json); | |
| 215 } | |
| 216 if (kind == RefactoringKind.RENAME) { | |
| 217 return new RenameOptions.fromJson(jsonDecoder, jsonPath, json); | |
| 218 } | |
| 219 return null; | |
| 220 } | |
| 221 | |
| 222 /** | |
| 223 * Type of callbacks used to decode parts of JSON objects. [jsonPath] is a | |
| 224 * string describing the part of the JSON object being decoded, and [value] is | |
| 225 * the part to decode. | |
| 226 */ | |
| 227 typedef Object JsonDecoderCallback(String jsonPath, Object value); | |
| 228 | |
| 229 /** | |
| 230 * Base class for decoding JSON objects. The derived class must implement | |
| 231 * error reporting logic. | |
| 232 */ | |
| 233 abstract class JsonDecoder { | |
| 234 /** | |
| 235 * Create an exception to throw if the JSON object at [jsonPath] fails to | |
| 236 * match the API definition of [expected]. | |
| 237 */ | |
| 238 dynamic mismatch(String jsonPath, String expected); | |
| 239 | |
| 240 /** | |
| 241 * Create an exception to throw if the JSON object at [jsonPath] is missing | |
| 242 * the key [key]. | |
| 243 */ | |
| 244 dynamic missingKey(String jsonPath, String key); | |
| 245 | |
| 246 /** | |
| 247 * Decode a JSON object that is expected to be a boolean. The strings "true" | |
| 248 * and "false" are also accepted. | |
| 249 */ | |
| 250 bool _decodeBool(String jsonPath, Object json) { | |
| 251 if (json is bool) { | |
| 252 return json; | |
| 253 } else if (json == 'true') { | |
| 254 return true; | |
| 255 } else if (json == 'false') { | |
| 256 return false; | |
| 257 } | |
| 258 throw mismatch(jsonPath, 'bool'); | |
| 259 } | |
| 260 | |
| 261 /** | |
| 262 * Decode a JSON object that is expected to be an integer. A string | |
| 263 * representation of an integer is also accepted. | |
| 264 */ | |
| 265 int _decodeInt(String jsonPath, Object json) { | |
| 266 if (json is int) { | |
| 267 return json; | |
| 268 } else if (json is String) { | |
| 269 return int.parse(json, onError: (String value) { | |
| 270 throw mismatch(jsonPath, 'int'); | |
| 271 }); | |
| 272 } | |
| 273 throw mismatch(jsonPath, 'int'); | |
| 274 } | |
| 275 | |
| 276 /** | |
| 277 * Decode a JSON object that is expected to be a List. [decoder] is used to | |
| 278 * decode the items in the list. | |
| 279 */ | |
| 280 List _decodeList(String jsonPath, Object json, | |
| 281 [JsonDecoderCallback decoder]) { | |
| 282 if (json == null) { | |
| 283 return []; | |
| 284 } else if (json is List) { | |
| 285 List result = []; | |
| 286 for (int i = 0; i < json.length; i++) { | |
| 287 result.add(decoder('$jsonPath[$i]', json[i])); | |
| 288 } | |
| 289 return result; | |
| 290 } else { | |
| 291 throw mismatch(jsonPath, 'List'); | |
| 292 } | |
| 293 } | |
| 294 | |
| 295 /** | |
| 296 * Decode a JSON object that is expected to be a Map. [keyDecoder] is used | |
| 297 * to decode the keys, and [valueDecoder] is used to decode the values. | |
| 298 */ | |
| 299 Map _decodeMap(String jsonPath, Object json, {JsonDecoderCallback keyDecoder, | |
| 300 JsonDecoderCallback valueDecoder}) { | |
| 301 if (json == null) { | |
| 302 return {}; | |
| 303 } else if (json is Map) { | |
| 304 Map result = {}; | |
| 305 json.forEach((String key, value) { | |
| 306 Object decodedKey; | |
| 307 if (keyDecoder != null) { | |
| 308 decodedKey = keyDecoder('$jsonPath.key', key); | |
| 309 } else { | |
| 310 decodedKey = key; | |
| 311 } | |
| 312 if (valueDecoder != null) { | |
| 313 value = valueDecoder('$jsonPath[${JSON.encode(key)}]', value); | |
| 314 } | |
| 315 result[decodedKey] = value; | |
| 316 }); | |
| 317 return result; | |
| 318 } else { | |
| 319 throw mismatch(jsonPath, 'Map'); | |
| 320 } | |
| 321 } | |
| 322 | |
| 323 /** | |
| 324 * Decode a JSON object that is expected to be a string. | |
| 325 */ | |
| 326 String _decodeString(String jsonPath, Object json) { | |
| 327 if (json is String) { | |
| 328 return json; | |
| 329 } else { | |
| 330 throw mismatch(jsonPath, 'String'); | |
| 331 } | |
| 332 } | |
| 333 | |
| 334 /** | |
| 335 * Decode a JSON object that is expected to be one of several choices, | |
| 336 * where the choices are disambiguated by the contents of the field [field]. | |
| 337 * [decoders] is a map from each possible string in the field to the decoder | |
| 338 * that should be used to decode the JSON object. | |
| 339 */ | |
| 340 Object _decodeUnion(String jsonPath, Map json, String field, Map<String, | |
| 341 JsonDecoderCallback> decoders) { | |
| 342 if (json is Map) { | |
| 343 if (!json.containsKey(field)) { | |
| 344 throw missingKey(jsonPath, field); | |
| 345 } | |
| 346 var disambiguatorPath = '$jsonPath[${JSON.encode(field)}]'; | |
| 347 String disambiguator = _decodeString(disambiguatorPath, json[field]); | |
| 348 if (!decoders.containsKey(disambiguator)) { | |
| 349 throw mismatch(disambiguatorPath, 'One of: ${decoders.keys.toList()}'); | |
| 350 } | |
| 351 return decoders[disambiguator](jsonPath, json); | |
| 352 } else { | |
| 353 throw mismatch(jsonPath, 'Map'); | |
| 354 } | |
| 355 } | |
| 356 } | |
| 357 | |
| 358 | |
| 359 /** | |
| 360 * Instances of the class [Notification] represent a notification from the | |
| 361 * server about an event that occurred. | |
| 362 */ | |
| 363 class Notification { | |
| 364 /** | |
| 365 * The name of the JSON attribute containing the name of the event that | |
| 366 * triggered the notification. | |
| 367 */ | |
| 368 static const String EVENT = 'event'; | |
| 369 | |
| 370 /** | |
| 371 * The name of the JSON attribute containing the result values. | |
| 372 */ | |
| 373 static const String PARAMS = 'params'; | |
| 374 | |
| 375 /** | |
| 376 * The name of the event that triggered the notification. | |
| 377 */ | |
| 378 final String event; | |
| 379 | |
| 380 /** | |
| 381 * A table mapping the names of notification parameters to their values, or | |
| 382 * null if there are no notification parameters. | |
| 383 */ | |
| 384 Map<String, Object> _params; | |
| 385 | |
| 386 /** | |
| 387 * Initialize a newly created [Notification] to have the given [event] name. | |
| 388 * If [_params] is provided, it will be used as the params; otherwise no | |
| 389 * params will be used. | |
| 390 */ | |
| 391 Notification(this.event, [this._params]); | |
| 392 | |
| 393 /** | |
| 394 * Initialize a newly created instance based upon the given JSON data | |
| 395 */ | |
| 396 factory Notification.fromJson(Map<String, Object> json) { | |
| 397 return new Notification( | |
| 398 json[Notification.EVENT], | |
| 399 json[Notification.PARAMS]); | |
| 400 } | |
| 401 | |
| 402 /** | |
| 403 * Return a table representing the structure of the Json object that will be | |
| 404 * sent to the client to represent this response. | |
| 405 */ | |
| 406 Map<String, Object> toJson() { | |
| 407 Map<String, Object> jsonObject = {}; | |
| 408 jsonObject[EVENT] = event; | |
| 409 if (_params != null) { | |
| 410 jsonObject[PARAMS] = _params; | |
| 411 } | |
| 412 return jsonObject; | |
| 413 } | |
| 414 } | |
| 415 | |
| 416 | |
| 417 /** | |
| 418 * Instances of the class [Request] represent a request that was received. | |
| 419 */ | |
| 420 class Request { | |
| 421 /** | |
| 422 * The name of the JSON attribute containing the id of the request. | |
| 423 */ | |
| 424 static const String ID = 'id'; | |
| 425 | |
| 426 /** | |
| 427 * The name of the JSON attribute containing the name of the request. | |
| 428 */ | |
| 429 static const String METHOD = 'method'; | |
| 430 | |
| 431 /** | |
| 432 * The name of the JSON attribute containing the request parameters. | |
| 433 */ | |
| 434 static const String PARAMS = 'params'; | |
| 435 | |
| 436 /** | |
| 437 * The unique identifier used to identify this request. | |
| 438 */ | |
| 439 final String id; | |
| 440 | |
| 441 /** | |
| 442 * The method being requested. | |
| 443 */ | |
| 444 final String method; | |
| 445 | |
| 446 /** | |
| 447 * A table mapping the names of request parameters to their values. | |
| 448 */ | |
| 449 final Map<String, Object> _params; | |
| 450 | |
| 451 /** | |
| 452 * Initialize a newly created [Request] to have the given [id] and [method] | |
| 453 * name. If [params] is supplied, it is used as the "params" map for the | |
| 454 * request. Otherwise an empty "params" map is allocated. | |
| 455 */ | |
| 456 Request(this.id, this.method, [Map<String, Object> params]) | |
| 457 : _params = params != null ? params : new HashMap<String, Object>(); | |
| 458 | |
| 459 /** | |
| 460 * Return a request parsed from the given [data], or `null` if the [data] is | |
| 461 * not a valid json representation of a request. The [data] is expected to | |
| 462 * have the following format: | |
| 463 * | |
| 464 * { | |
| 465 * 'id': String, | |
| 466 * 'method': methodName, | |
| 467 * 'params': { | |
| 468 * paramter_name: value | |
| 469 * } | |
| 470 * } | |
| 471 * | |
| 472 * where the parameters are optional and can contain any number of name/value | |
| 473 * pairs. | |
| 474 */ | |
| 475 factory Request.fromString(String data) { | |
| 476 try { | |
| 477 var result = JSON.decode(data); | |
| 478 if (result is! Map) { | |
| 479 return null; | |
| 480 } | |
| 481 var id = result[Request.ID]; | |
| 482 var method = result[Request.METHOD]; | |
| 483 if (id is! String || method is! String) { | |
| 484 return null; | |
| 485 } | |
| 486 var params = result[Request.PARAMS]; | |
| 487 if (params is Map || params == null) { | |
| 488 return new Request(id, method, params); | |
| 489 } else { | |
| 490 return null; | |
| 491 } | |
| 492 } catch (exception) { | |
| 493 return null; | |
| 494 } | |
| 495 } | |
| 496 | |
| 497 /** | |
| 498 * Return a table representing the structure of the Json object that will be | |
| 499 * sent to the client to represent this response. | |
| 500 */ | |
| 501 Map<String, Object> toJson() { | |
| 502 Map<String, Object> jsonObject = new HashMap<String, Object>(); | |
| 503 jsonObject[ID] = id; | |
| 504 jsonObject[METHOD] = method; | |
| 505 if (_params.isNotEmpty) { | |
| 506 jsonObject[PARAMS] = _params; | |
| 507 } | |
| 508 return jsonObject; | |
| 509 } | |
| 510 } | |
| 511 | |
| 512 /** | |
| 513 * JsonDecoder for decoding requests. Errors are reporting by throwing a | |
| 514 * [RequestFailure]. | |
| 515 */ | |
| 516 class RequestDecoder extends JsonDecoder { | |
| 517 /** | |
| 518 * The request being deserialized. | |
| 519 */ | |
| 520 final Request _request; | |
| 521 | |
| 522 RequestDecoder(this._request); | |
| 523 | |
| 524 @override | |
| 525 dynamic mismatch(String jsonPath, String expected) { | |
| 526 return new RequestFailure( | |
| 527 new Response.invalidParameter(_request, jsonPath, 'be $expected')); | |
| 528 } | |
| 529 | |
| 530 @override | |
| 531 dynamic missingKey(String jsonPath, String key) { | |
| 532 return new RequestFailure( | |
| 533 new Response.invalidParameter( | |
| 534 _request, | |
| 535 jsonPath, | |
| 536 'contain key ${JSON.encode(key)}')); | |
| 537 } | |
| 538 } | |
| 539 | |
| 540 | |
| 541 /** | |
| 542 * Instances of the class [RequestFailure] represent an exception that occurred | |
| 543 * during the handling of a request that requires that an error be returned to | |
| 544 * the client. | |
| 545 */ | |
| 546 class RequestFailure implements Exception { | |
| 547 /** | |
| 548 * The response to be returned as a result of the failure. | |
| 549 */ | |
| 550 final Response response; | |
| 551 | |
| 552 /** | |
| 553 * Initialize a newly created exception to return the given reponse. | |
| 554 */ | |
| 555 RequestFailure(this.response); | |
| 556 } | |
| 557 | |
| 558 /** | |
| 559 * Instances of the class [RequestHandler] implement a handler that can handle | |
| 560 * requests and produce responses for them. | |
| 561 */ | |
| 562 abstract class RequestHandler { | |
| 563 /** | |
| 564 * Attempt to handle the given [request]. If the request is not recognized by | |
| 565 * this handler, return `null` so that other handlers will be given a chance | |
| 566 * to handle it. Otherwise, return the response that should be passed back to | |
| 567 * the client. | |
| 568 */ | |
| 569 Response handleRequest(Request request); | |
| 570 } | |
| 571 | |
| 572 /** | |
| 573 * Instances of the class [Response] represent a response to a request. | |
| 574 */ | |
| 575 class Response { | |
| 576 /** | |
| 577 * The [Response] instance that is returned when a real [Response] cannot | |
| 578 * be provided at the moment. | |
| 579 */ | |
| 580 static final Response DELAYED_RESPONSE = new Response('DELAYED_RESPONSE'); | |
| 581 | |
| 582 /** | |
| 583 * The name of the JSON attribute containing the id of the request for which | |
| 584 * this is a response. | |
| 585 */ | |
| 586 static const String ID = 'id'; | |
| 587 | |
| 588 /** | |
| 589 * The name of the JSON attribute containing the error message. | |
| 590 */ | |
| 591 static const String ERROR = 'error'; | |
| 592 | |
| 593 /** | |
| 594 * The name of the JSON attribute containing the result values. | |
| 595 */ | |
| 596 static const String RESULT = 'result'; | |
| 597 | |
| 598 /** | |
| 599 * The unique identifier used to identify the request that this response is | |
| 600 * associated with. | |
| 601 */ | |
| 602 final String id; | |
| 603 | |
| 604 /** | |
| 605 * The error that was caused by attempting to handle the request, or `null` if | |
| 606 * there was no error. | |
| 607 */ | |
| 608 final RequestError error; | |
| 609 | |
| 610 /** | |
| 611 * A table mapping the names of result fields to their values. Should be | |
| 612 * null if there is no result to send. | |
| 613 */ | |
| 614 Map<String, Object> _result; | |
| 615 | |
| 616 /** | |
| 617 * Initialize a newly created instance to represent a response to a request | |
| 618 * with the given [id]. If [_result] is provided, it will be used as the | |
| 619 * result; otherwise an empty result will be used. If an [error] is provided | |
| 620 * then the response will represent an error condition. | |
| 621 */ | |
| 622 Response(this.id, {Map<String, Object> result, this.error}) | |
| 623 : _result = result; | |
| 624 | |
| 625 /** | |
| 626 * Initialize a newly created instance based upon the given JSON data | |
| 627 */ | |
| 628 factory Response.fromJson(Map<String, Object> json) { | |
| 629 try { | |
| 630 Object id = json[Response.ID]; | |
| 631 if (id is! String) { | |
| 632 return null; | |
| 633 } | |
| 634 Object error = json[Response.ERROR]; | |
| 635 RequestError decodedError; | |
| 636 if (error is Map) { | |
| 637 decodedError = | |
| 638 new RequestError.fromJson(new ResponseDecoder(null), '.error', error
); | |
| 639 } | |
| 640 Object result = json[Response.RESULT]; | |
| 641 Map<String, Object> decodedResult; | |
| 642 if (result is Map) { | |
| 643 decodedResult = result; | |
| 644 } | |
| 645 return new Response(id, error: decodedError, result: decodedResult); | |
| 646 } catch (exception) { | |
| 647 return null; | |
| 648 } | |
| 649 } | |
| 650 | |
| 651 /** | |
| 652 * Initialize a newly created instance to represent the | |
| 653 * GET_ERRORS_INVALID_FILE error condition. | |
| 654 */ | |
| 655 Response.getErrorsInvalidFile(Request request) | |
| 656 : this( | |
| 657 request.id, | |
| 658 error: new RequestError( | |
| 659 RequestErrorCode.GET_ERRORS_INVALID_FILE, | |
| 660 'Error during `analysis.getErrors`: invalid file.')); | |
| 661 | |
| 662 /** | |
| 663 * Initialize a newly created instance to represent an error condition caused | |
| 664 * by a [request] that had invalid parameter. [path] is the path to the | |
| 665 * invalid parameter, in Javascript notation (e.g. "foo.bar" means that the | |
| 666 * parameter "foo" contained a key "bar" whose value was the wrong type). | |
| 667 * [expectation] is a description of the type of data that was expected. | |
| 668 */ | |
| 669 Response.invalidParameter(Request request, String path, String expectation) | |
| 670 : this( | |
| 671 request.id, | |
| 672 error: new RequestError( | |
| 673 RequestErrorCode.INVALID_PARAMETER, | |
| 674 "Expected parameter $path to $expectation")); | |
| 675 | |
| 676 /** | |
| 677 * Initialize a newly created instance to represent an error condition caused | |
| 678 * by a malformed request. | |
| 679 */ | |
| 680 Response.invalidRequestFormat() | |
| 681 : this( | |
| 682 '', | |
| 683 error: new RequestError(RequestErrorCode.INVALID_REQUEST, 'Invalid req
uest')); | |
| 684 | |
| 685 /** | |
| 686 * Initialize a newly created instance to represent an error condition caused | |
| 687 * by a `analysis.setPriorityFiles` [request] that includes one or more files | |
| 688 * that are not being analyzed. | |
| 689 */ | |
| 690 Response.unanalyzedPriorityFiles(Request request, String fileNames) | |
| 691 : this( | |
| 692 request.id, | |
| 693 error: new RequestError( | |
| 694 RequestErrorCode.UNANALYZED_PRIORITY_FILES, | |
| 695 "Unanalyzed files cannot be a priority: '$fileNames'")); | |
| 696 | |
| 697 /** | |
| 698 * Initialize a newly created instance to represent an error condition caused | |
| 699 * by a [request] that cannot be handled by any known handlers. | |
| 700 */ | |
| 701 Response.unknownRequest(Request request) | |
| 702 : this( | |
| 703 request.id, | |
| 704 error: new RequestError(RequestErrorCode.UNKNOWN_REQUEST, 'Unknown req
uest')); | |
| 705 | |
| 706 Response.unsupportedFeature(String requestId, String message) | |
| 707 : this( | |
| 708 requestId, | |
| 709 error: new RequestError(RequestErrorCode.UNSUPPORTED_FEATURE, message)
); | |
| 710 | |
| 711 /** | |
| 712 * Return a table representing the structure of the Json object that will be | |
| 713 * sent to the client to represent this response. | |
| 714 */ | |
| 715 Map<String, Object> toJson() { | |
| 716 Map<String, Object> jsonObject = new HashMap<String, Object>(); | |
| 717 jsonObject[ID] = id; | |
| 718 if (error != null) { | |
| 719 jsonObject[ERROR] = error.toJson(); | |
| 720 } | |
| 721 if (_result != null) { | |
| 722 jsonObject[RESULT] = _result; | |
| 723 } | |
| 724 return jsonObject; | |
| 725 } | |
| 726 } | |
| 727 | |
| 728 /** | |
| 729 * JsonDecoder for decoding responses from the server. This is intended to be | |
| 730 * used only for testing. Errors are reported using bare [Exception] objects. | |
| 731 */ | |
| 732 class ResponseDecoder extends JsonDecoder { | |
| 733 final Response response; | |
| 734 | |
| 735 ResponseDecoder(this.response); | |
| 736 | |
| 737 @override | |
| 738 dynamic mismatch(String jsonPath, String expected) { | |
| 739 return new Exception('Expected $expected at $jsonPath'); | |
| 740 } | |
| 741 | |
| 742 @override | |
| 743 dynamic missingKey(String jsonPath, String key) { | |
| 744 return new Exception('Missing key $key at $jsonPath'); | |
| 745 } | |
| 746 } | |
| 747 | |
| 748 /** | |
| 749 * Jenkins hash function, optimized for small integers. Borrowed from | |
| 750 * sdk/lib/math/jenkins_smi_hash.dart. | |
| 751 * | |
| 752 * TODO(paulberry): Move to somewhere that can be shared with other code. | |
| 753 */ | |
| 754 class _JenkinsSmiHash { | |
| 755 static int combine(int hash, int value) { | |
| 756 hash = 0x1fffffff & (hash + value); | |
| 757 hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); | |
| 758 return hash ^ (hash >> 6); | |
| 759 } | |
| 760 | |
| 761 static int finish(int hash) { | |
| 762 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); | |
| 763 hash = hash ^ (hash >> 11); | |
| 764 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); | |
| 765 } | |
| 766 | |
| 767 static int hash2(a, b) => finish(combine(combine(0, a), b)); | |
| 768 | |
| 769 static int hash4(a, b, c, d) => | |
| 770 finish(combine(combine(combine(combine(0, a), b), c), d)); | |
| 771 } | |
| OLD | NEW |