| 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'; |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 * by a `analysis.updateOptions` [request] that includes an unknown analysis | 235 * by a `analysis.updateOptions` [request] that includes an unknown analysis |
| 236 * option. | 236 * option. |
| 237 */ | 237 */ |
| 238 Response.unknownOptionName(Request request, String optionName) | 238 Response.unknownOptionName(Request request, String optionName) |
| 239 : this(request.id, error: new RequestError('UNKNOWN_OPTION_NAME', 'Unknown a
nalysis option: "$optionName"')); | 239 : this(request.id, error: new RequestError('UNKNOWN_OPTION_NAME', 'Unknown a
nalysis option: "$optionName"')); |
| 240 | 240 |
| 241 /** | 241 /** |
| 242 * Initialize a newly created instance to represent an error condition caused | 242 * Initialize a newly created instance to represent an error condition caused |
| 243 * by an error during `analysis.getErrors`. | 243 * by an error during `analysis.getErrors`. |
| 244 */ | 244 */ |
| 245 Response.getErrorsError(Request request, String message) | 245 Response.getErrorsError(Request request, String message, |
| 246 Map<String, Object> result) |
| 246 : this( | 247 : this( |
| 247 request.id, | 248 request.id, |
| 248 error: new RequestError('GET_ERRORS_ERROR', 'Error during `analysis.getE
rrors`: $message.')); | 249 error: new RequestError('GET_ERRORS_ERROR', 'Error during `analysis.getE
rrors`: $message.'), |
| 250 result: result); |
| 249 | 251 |
| 250 /** | 252 /** |
| 251 * Initialize a newly created instance based upon the given JSON data | 253 * Initialize a newly created instance based upon the given JSON data |
| 252 */ | 254 */ |
| 253 factory Response.fromJson(Map<String, Object> json) { | 255 factory Response.fromJson(Map<String, Object> json) { |
| 254 try { | 256 try { |
| 255 Object id = json[Response.ID]; | 257 Object id = json[Response.ID]; |
| 256 if (id is! String) { | 258 if (id is! String) { |
| 257 return null; | 259 return null; |
| 258 } | 260 } |
| 259 Object error = json[Response.ERROR]; | 261 Object error = json[Response.ERROR]; |
| 262 RequestError decodedError; |
| 263 if (error is Map) { |
| 264 decodedError = new RequestError.fromJson(error); |
| 265 } |
| 260 Object result = json[Response.RESULT]; | 266 Object result = json[Response.RESULT]; |
| 261 Response response; | 267 Map<String, Object> decodedResult; |
| 262 if (error is Map) { | 268 if (result is Map) { |
| 263 response = new Response(id, error: new RequestError.fromJson(error)); | 269 decodedResult = result; |
| 264 } else { | |
| 265 response = new Response(id); | |
| 266 } | 270 } |
| 267 if (result is Map) { | 271 return new Response(id, error: decodedError, |
| 268 result.forEach((String key, Object value) { | 272 result: decodedResult); |
| 269 response.setResult(key, value); | |
| 270 }); | |
| 271 } | |
| 272 return response; | |
| 273 } catch (exception) { | 273 } catch (exception) { |
| 274 return null; | 274 return null; |
| 275 } | 275 } |
| 276 } | 276 } |
| 277 | 277 |
| 278 /** | 278 /** |
| 279 * Set the value of the result field with the given [name] to the given [value
]. | |
| 280 */ | |
| 281 void setResult(String name, Object value) { | |
| 282 if (result == null) { | |
| 283 result = new HashMap<String, Object>(); | |
| 284 } | |
| 285 result[name] = _toJson(value); | |
| 286 } | |
| 287 | |
| 288 /** | |
| 289 * Set the result to be an empty map. | |
| 290 */ | |
| 291 void setEmptyResult() { | |
| 292 result = new HashMap<String, Object>(); | |
| 293 } | |
| 294 | |
| 295 /** | |
| 296 * Return a table representing the structure of the Json object that will be | 279 * Return a table representing the structure of the Json object that will be |
| 297 * sent to the client to represent this response. | 280 * sent to the client to represent this response. |
| 298 */ | 281 */ |
| 299 Map<String, Object> toJson() { | 282 Map<String, Object> toJson() { |
| 300 Map<String, Object> jsonObject = new HashMap<String, Object>(); | 283 Map<String, Object> jsonObject = new HashMap<String, Object>(); |
| 301 jsonObject[ID] = id; | 284 jsonObject[ID] = id; |
| 302 if (error != null) { | 285 if (error != null) { |
| 303 jsonObject[ERROR] = error.toJson(); | 286 jsonObject[ERROR] = error.toJson(); |
| 304 } | 287 } |
| 305 if (result != null) { | 288 if (result != null) { |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 * The name of the JSON attribute containing the result values. | 481 * The name of the JSON attribute containing the result values. |
| 499 */ | 482 */ |
| 500 static const String PARAMS = 'params'; | 483 static const String PARAMS = 'params'; |
| 501 | 484 |
| 502 /** | 485 /** |
| 503 * The name of the event that triggered the notification. | 486 * The name of the event that triggered the notification. |
| 504 */ | 487 */ |
| 505 final String event; | 488 final String event; |
| 506 | 489 |
| 507 /** | 490 /** |
| 508 * A table mapping the names of notification parameters to their values. | 491 * A table mapping the names of notification parameters to their values, or |
| 492 * null if there are no notification parameters. |
| 509 */ | 493 */ |
| 510 final Map<String, Object> params = new HashMap<String, Object>(); | 494 Map<String, Object> params; |
| 511 | 495 |
| 512 /** | 496 /** |
| 513 * Initialize a newly created [Notification] to have the given [event] name. | 497 * Initialize a newly created [Notification] to have the given [event] name. |
| 498 * If [params] is provided, it will be used as the params; otherwise no |
| 499 * params will be used. |
| 514 */ | 500 */ |
| 515 Notification(this.event); | 501 Notification(this.event, [this.params]); |
| 516 | 502 |
| 517 /** | 503 /** |
| 518 * Initialize a newly created instance based upon the given JSON data | 504 * Initialize a newly created instance based upon the given JSON data |
| 519 */ | 505 */ |
| 520 factory Notification.fromJson(Map<String, Object> json) { | 506 factory Notification.fromJson(Map<String, Object> json) { |
| 521 try { | 507 try { |
| 522 String event = json[Notification.EVENT]; | 508 String event = json[Notification.EVENT]; |
| 523 Object params = json[Notification.PARAMS]; | 509 Object params = json[Notification.PARAMS]; |
| 524 Notification notification = new Notification(event); | 510 Notification notification = new Notification(event); |
| 525 if (params is Map) { | 511 if (params is Map) { |
| 526 params.forEach((String key, Object value) { | 512 params.forEach((String key, Object value) { |
| 527 notification.setParameter(key, value); | 513 notification.setParameter(key, value); |
| 528 }); | 514 }); |
| 529 } | 515 } |
| 530 return notification; | 516 return notification; |
| 531 } catch (exception) { | 517 } catch (exception) { |
| 532 return null; | 518 return null; |
| 533 } | 519 } |
| 534 } | 520 } |
| 535 | 521 |
| 536 /** | 522 /** |
| 537 * Return the value of the parameter with the given [name], or `null` if there | |
| 538 * is no such parameter associated with this notification. | |
| 539 */ | |
| 540 Object getParameter(String name) => params[name]; | |
| 541 | |
| 542 /** | |
| 543 * Set the value of the parameter with the given [name] to the given [value]. | 523 * Set the value of the parameter with the given [name] to the given [value]. |
| 544 */ | 524 */ |
| 545 void setParameter(String name, Object value) { | 525 void setParameter(String name, Object value) { |
| 526 if (params == null) { |
| 527 params = new HashMap<String, Object>(); |
| 528 } |
| 546 params[name] = _toJson(value); | 529 params[name] = _toJson(value); |
| 547 } | 530 } |
| 548 | 531 |
| 549 /** | 532 /** |
| 550 * Return a table representing the structure of the Json object that will be | 533 * Return a table representing the structure of the Json object that will be |
| 551 * sent to the client to represent this response. | 534 * sent to the client to represent this response. |
| 552 */ | 535 */ |
| 553 Map<String, Object> toJson() { | 536 Map<String, Object> toJson() { |
| 554 Map<String, Object> jsonObject = {}; | 537 Map<String, Object> jsonObject = {}; |
| 555 jsonObject[EVENT] = event; | 538 jsonObject[EVENT] = event; |
| 556 if (!params.isEmpty) { | 539 if (params != null) { |
| 557 jsonObject[PARAMS] = params; | 540 jsonObject[PARAMS] = params; |
| 558 } | 541 } |
| 559 return jsonObject; | 542 return jsonObject; |
| 560 } | 543 } |
| 561 } | 544 } |
| 562 | 545 |
| 563 /** | 546 /** |
| 564 * Instances of the class [RequestHandler] implement a handler that can handle | 547 * Instances of the class [RequestHandler] implement a handler that can handle |
| 565 * requests and produce responses for them. | 548 * requests and produce responses for them. |
| 566 */ | 549 */ |
| (...skipping 29 matching lines...) Expand all Loading... |
| 596 */ | 579 */ |
| 597 _toJson(Object value) { | 580 _toJson(Object value) { |
| 598 if (value is HasToJson) { | 581 if (value is HasToJson) { |
| 599 return value.toJson(); | 582 return value.toJson(); |
| 600 } | 583 } |
| 601 if (value is Iterable) { | 584 if (value is Iterable) { |
| 602 return value.map((item) => _toJson(item)).toList(); | 585 return value.map((item) => _toJson(item)).toList(); |
| 603 } | 586 } |
| 604 return value; | 587 return value; |
| 605 } | 588 } |
| OLD | NEW |