| 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 /** | 10 /** |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 final String id; | 71 final String id; |
| 72 | 72 |
| 73 /** | 73 /** |
| 74 * The method being requested. | 74 * The method being requested. |
| 75 */ | 75 */ |
| 76 final String method; | 76 final String method; |
| 77 | 77 |
| 78 /** | 78 /** |
| 79 * A table mapping the names of request parameters to their values. | 79 * A table mapping the names of request parameters to their values. |
| 80 */ | 80 */ |
| 81 final Map<String, Object> params = new HashMap<String, Object>(); | 81 final HashMap<String, Object> params = new HashMap<String, Object>(); |
| 82 | 82 |
| 83 /** | 83 /** |
| 84 * A decoder that can be used to decode strings into JSON objects. | 84 * A decoder that can be used to decode strings into JSON objects. |
| 85 */ | 85 */ |
| 86 static const JsonDecoder DECODER = const JsonDecoder(null); | 86 static const JsonDecoder DECODER = const JsonDecoder(null); |
| 87 | 87 |
| 88 /** | 88 /** |
| 89 * Initialize a newly created [Request] to have the given [id] and [method] | 89 * Initialize a newly created [Request] to have the given [id] and [method] |
| 90 * name. | 90 * name. |
| 91 */ | 91 */ |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 * Set the value of the parameter with the given [name] to the given [value]. | 160 * Set the value of the parameter with the given [name] to the given [value]. |
| 161 */ | 161 */ |
| 162 void setParameter(String name, Object value) { | 162 void setParameter(String name, Object value) { |
| 163 params[name] = value; | 163 params[name] = value; |
| 164 } | 164 } |
| 165 | 165 |
| 166 /** | 166 /** |
| 167 * Return a table representing the structure of the Json object that will be | 167 * Return a table representing the structure of the Json object that will be |
| 168 * sent to the client to represent this response. | 168 * sent to the client to represent this response. |
| 169 */ | 169 */ |
| 170 Map<String, Object> toJson() { | 170 HashMap<String, Object> toJson() { |
| 171 Map<String, Object> jsonObject = new Map<String, Object>(); | 171 HashMap<String, Object> jsonObject = new HashMap<String, Object>(); |
| 172 jsonObject[ID] = id; | 172 jsonObject[ID] = id; |
| 173 jsonObject[METHOD] = method; | 173 jsonObject[METHOD] = method; |
| 174 if (params.isNotEmpty) { | 174 if (params.isNotEmpty) { |
| 175 jsonObject[PARAMS] = params; | 175 jsonObject[PARAMS] = params; |
| 176 } | 176 } |
| 177 return jsonObject; | 177 return jsonObject; |
| 178 } | 178 } |
| 179 } | 179 } |
| 180 | 180 |
| 181 /** | 181 /** |
| (...skipping 22 matching lines...) Expand all Loading... |
| 204 * Create a RequestDatum for decoding and validating [datum], which refers to | 204 * Create a RequestDatum for decoding and validating [datum], which refers to |
| 205 * [request] in any errors it reports. | 205 * [request] in any errors it reports. |
| 206 */ | 206 */ |
| 207 RequestDatum(this.request, this.path, this.datum); | 207 RequestDatum(this.request, this.path, this.datum); |
| 208 | 208 |
| 209 /** | 209 /** |
| 210 * Validate that the datum is a Map containing the given [key], and return | 210 * Validate that the datum is a Map containing the given [key], and return |
| 211 * a [RequestDatum] containing the corresponding value. | 211 * a [RequestDatum] containing the corresponding value. |
| 212 */ | 212 */ |
| 213 RequestDatum operator [](String key) { | 213 RequestDatum operator [](String key) { |
| 214 Map<String, Object> map = _asMap(); | 214 HashMap<String, Object> map = _asMap(); |
| 215 if (!map.containsKey(key)) { | 215 if (!map.containsKey(key)) { |
| 216 throw new RequestFailure(new Response.invalidParameter(request, path, | 216 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 217 "contain key '$key'")); | 217 "contain key '$key'")); |
| 218 } | 218 } |
| 219 return new RequestDatum(request, "$path.$key", map[key]); | 219 return new RequestDatum(request, "$path.$key", map[key]); |
| 220 } | 220 } |
| 221 | 221 |
| 222 /** | 222 /** |
| 223 * Return `true` if the datum is a Map containing the given [key]. | 223 * Return `true` if the datum is a Map containing the given [key]. |
| 224 */ | 224 */ |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 * Determine if the datum is a map. Note: null is considered a synonym for | 367 * Determine if the datum is a map. Note: null is considered a synonym for |
| 368 * the empty map. | 368 * the empty map. |
| 369 */ | 369 */ |
| 370 bool get isMap { | 370 bool get isMap { |
| 371 return datum == null || datum is Map; | 371 return datum == null || datum is Map; |
| 372 } | 372 } |
| 373 | 373 |
| 374 /** | 374 /** |
| 375 * Validate that the datum is a map, and return it in raw form. | 375 * Validate that the datum is a map, and return it in raw form. |
| 376 */ | 376 */ |
| 377 Map<String, Object> _asMap() { | 377 HashMap<String, Object> _asMap() { |
| 378 if (!isMap) { | 378 if (!isMap) { |
| 379 throw new RequestFailure(new Response.invalidParameter(request, path, | 379 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 380 "be a map")); | 380 "be a map")); |
| 381 } | 381 } |
| 382 if (datum == null) { | 382 if (datum == null) { |
| 383 return {}; | 383 return {}; |
| 384 } else { | 384 } else { |
| 385 return datum; | 385 return datum; |
| 386 } | 386 } |
| 387 } | 387 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 401 if (value is! String) { | 401 if (value is! String) { |
| 402 return false; | 402 return false; |
| 403 } | 403 } |
| 404 } | 404 } |
| 405 return true; | 405 return true; |
| 406 } | 406 } |
| 407 | 407 |
| 408 /** | 408 /** |
| 409 * Validate that the datum is a map from strings to strings, and return it. | 409 * Validate that the datum is a map from strings to strings, and return it. |
| 410 */ | 410 */ |
| 411 Map<String, String> asStringMap() { | 411 HashMap<String, String> asStringMap() { |
| 412 if (!isStringMap) { | 412 if (!isStringMap) { |
| 413 throw new RequestFailure(new Response.invalidParameter(request, path, | 413 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 414 "be a string map")); | 414 "be a string map")); |
| 415 } | 415 } |
| 416 return _asMap(); | 416 return _asMap(); |
| 417 } | 417 } |
| 418 | 418 |
| 419 /** | 419 /** |
| 420 * Determine if the datum is a map whose values are all string lists. Note: | 420 * Determine if the datum is a map whose values are all string lists. Note: |
| 421 * null is considered a synonym for the empty map. | 421 * null is considered a synonym for the empty map. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 437 } | 437 } |
| 438 } | 438 } |
| 439 } | 439 } |
| 440 return true; | 440 return true; |
| 441 } | 441 } |
| 442 | 442 |
| 443 /** | 443 /** |
| 444 * Validate that the datum is a map from strings to string lists, and return | 444 * Validate that the datum is a map from strings to string lists, and return |
| 445 * it. | 445 * it. |
| 446 */ | 446 */ |
| 447 Map<String, List<String>> asStringListMap() { | 447 HashMap<String, List<String>> asStringListMap() { |
| 448 if (!isStringListMap) { | 448 if (!isStringListMap) { |
| 449 throw new RequestFailure(new Response.invalidParameter(request, path, | 449 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 450 "be a string list map")); | 450 "be a string list map")); |
| 451 } | 451 } |
| 452 return _asMap(); | 452 return _asMap(); |
| 453 } | 453 } |
| 454 | 454 |
| 455 bool get isNull => datum == null; | 455 bool get isNull => datum == null; |
| 456 } | 456 } |
| 457 | 457 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 484 /** | 484 /** |
| 485 * The error that was caused by attempting to handle the request, or `null` if | 485 * The error that was caused by attempting to handle the request, or `null` if |
| 486 * there was no error. | 486 * there was no error. |
| 487 */ | 487 */ |
| 488 final RequestError error; | 488 final RequestError error; |
| 489 | 489 |
| 490 /** | 490 /** |
| 491 * A table mapping the names of result fields to their values. The table | 491 * A table mapping the names of result fields to their values. The table |
| 492 * should be empty if there was an error. | 492 * should be empty if there was an error. |
| 493 */ | 493 */ |
| 494 final Map<String, Object> result = new HashMap<String, Object>(); | 494 final HashMap<String, Object> result = new HashMap<String, Object>(); |
| 495 | 495 |
| 496 /** | 496 /** |
| 497 * Initialize a newly created instance to represent a response to a request | 497 * Initialize a newly created instance to represent a response to a request |
| 498 * with the given [id]. If an [error] is provided then the response will | 498 * with the given [id]. If an [error] is provided then the response will |
| 499 * represent an error condition. | 499 * represent an error condition. |
| 500 */ | 500 */ |
| 501 Response(this.id, [this.error]); | 501 Response(this.id, [this.error]); |
| 502 | 502 |
| 503 /** | 503 /** |
| 504 * Initialize a newly created instance to represent an error condition caused | 504 * Initialize a newly created instance to represent an error condition caused |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 573 * Initialize a newly created instance to represent an error condition caused | 573 * Initialize a newly created instance to represent an error condition caused |
| 574 * by a `analysis.updateOptions` [request] that includes an unknown analysis | 574 * by a `analysis.updateOptions` [request] that includes an unknown analysis |
| 575 * option. | 575 * option. |
| 576 */ | 576 */ |
| 577 Response.unknownOptionName(Request request, String optionName) | 577 Response.unknownOptionName(Request request, String optionName) |
| 578 : this(request.id, new RequestError(-12, 'Unknown analysis option: "$optionN
ame"')); | 578 : this(request.id, new RequestError(-12, 'Unknown analysis option: "$optionN
ame"')); |
| 579 | 579 |
| 580 /** | 580 /** |
| 581 * Initialize a newly created instance based upon the given JSON data | 581 * Initialize a newly created instance based upon the given JSON data |
| 582 */ | 582 */ |
| 583 factory Response.fromJson(Map<String, Object> json) { | 583 factory Response.fromJson(HashMap<String, Object> json) { |
| 584 try { | 584 try { |
| 585 Object id = json[Response.ID]; | 585 Object id = json[Response.ID]; |
| 586 if (id is! String) { | 586 if (id is! String) { |
| 587 return null; | 587 return null; |
| 588 } | 588 } |
| 589 Object error = json[Response.ERROR]; | 589 Object error = json[Response.ERROR]; |
| 590 Object result = json[Response.RESULT]; | 590 Object result = json[Response.RESULT]; |
| 591 Response response; | 591 Response response; |
| 592 if (error is Map) { | 592 if (error is Map) { |
| 593 response = new Response(id, new RequestError.fromJson(error)); | 593 response = new Response(id, new RequestError.fromJson(error)); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 616 * Set the value of the result field with the given [name] to the given [value
]. | 616 * Set the value of the result field with the given [name] to the given [value
]. |
| 617 */ | 617 */ |
| 618 void setResult(String name, Object value) { | 618 void setResult(String name, Object value) { |
| 619 result[name] = value; | 619 result[name] = value; |
| 620 } | 620 } |
| 621 | 621 |
| 622 /** | 622 /** |
| 623 * Return a table representing the structure of the Json object that will be | 623 * Return a table representing the structure of the Json object that will be |
| 624 * sent to the client to represent this response. | 624 * sent to the client to represent this response. |
| 625 */ | 625 */ |
| 626 Map<String, Object> toJson() { | 626 HashMap<String, Object> toJson() { |
| 627 Map<String, Object> jsonObject = new Map<String, Object>(); | 627 HashMap<String, Object> jsonObject = new HashMap<String, Object>(); |
| 628 jsonObject[ID] = id; | 628 jsonObject[ID] = id; |
| 629 if (error != null) { | 629 if (error != null) { |
| 630 jsonObject[ERROR] = error.toJson(); | 630 jsonObject[ERROR] = error.toJson(); |
| 631 } | 631 } |
| 632 if (!result.isEmpty) { | 632 if (!result.isEmpty) { |
| 633 jsonObject[RESULT] = result; | 633 jsonObject[RESULT] = result; |
| 634 } | 634 } |
| 635 return jsonObject; | 635 return jsonObject; |
| 636 } | 636 } |
| 637 } | 637 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 708 final int code; | 708 final int code; |
| 709 | 709 |
| 710 /** | 710 /** |
| 711 * A short description of the error. | 711 * A short description of the error. |
| 712 */ | 712 */ |
| 713 final String message; | 713 final String message; |
| 714 | 714 |
| 715 /** | 715 /** |
| 716 * A table mapping the names of notification parameters to their values. | 716 * A table mapping the names of notification parameters to their values. |
| 717 */ | 717 */ |
| 718 final Map<String, Object> data = new HashMap<String, Object>(); | 718 final HashMap<String, Object> data = new HashMap<String, Object>(); |
| 719 | 719 |
| 720 /** | 720 /** |
| 721 * Initialize a newly created [Error] to have the given [code] and [message]. | 721 * Initialize a newly created [Error] to have the given [code] and [message]. |
| 722 */ | 722 */ |
| 723 RequestError(this.code, this.message); | 723 RequestError(this.code, this.message); |
| 724 | 724 |
| 725 /** | 725 /** |
| 726 * Initialize a newly created [Error] to indicate a parse error. Invalid JSON | 726 * Initialize a newly created [Error] to indicate a parse error. Invalid JSON |
| 727 * was received by the server. An error occurred on the server while parsing | 727 * was received by the server. An error occurred on the server while parsing |
| 728 * the JSON text. | 728 * the JSON text. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 755 RequestError.invalidParameters() : this(CODE_INVALID_PARAMS, "Invalid paramete
rs"); | 755 RequestError.invalidParameters() : this(CODE_INVALID_PARAMS, "Invalid paramete
rs"); |
| 756 | 756 |
| 757 /** | 757 /** |
| 758 * Initialize a newly created [Error] to indicate an internal error. | 758 * Initialize a newly created [Error] to indicate an internal error. |
| 759 */ | 759 */ |
| 760 RequestError.internalError() : this(CODE_INTERNAL_ERROR, "Internal error"); | 760 RequestError.internalError() : this(CODE_INTERNAL_ERROR, "Internal error"); |
| 761 | 761 |
| 762 /** | 762 /** |
| 763 * Initialize a newly created [Error] from the given JSON. | 763 * Initialize a newly created [Error] from the given JSON. |
| 764 */ | 764 */ |
| 765 factory RequestError.fromJson(Map<String, Object> json) { | 765 factory RequestError.fromJson(HashMap<String, Object> json) { |
| 766 try { | 766 try { |
| 767 int code = json[RequestError.CODE]; | 767 int code = json[RequestError.CODE]; |
| 768 String message = json[RequestError.MESSAGE]; | 768 String message = json[RequestError.MESSAGE]; |
| 769 Map<String, Object> data = json[RequestError.DATA]; | 769 HashMap<String, Object> data = json[RequestError.DATA]; |
| 770 RequestError requestError = new RequestError(code, message); | 770 RequestError requestError = new RequestError(code, message); |
| 771 if (data != null) { | 771 if (data != null) { |
| 772 data.forEach((String key, Object value) { | 772 data.forEach((String key, Object value) { |
| 773 requestError.setData(key, value); | 773 requestError.setData(key, value); |
| 774 }); | 774 }); |
| 775 } | 775 } |
| 776 return requestError; | 776 return requestError; |
| 777 } catch (exception) { | 777 } catch (exception) { |
| 778 return null; | 778 return null; |
| 779 } | 779 } |
| 780 } | 780 } |
| 781 | 781 |
| 782 /** | 782 /** |
| 783 * Return the value of the data with the given [name], or `null` if there is | 783 * Return the value of the data with the given [name], or `null` if there is |
| 784 * no such data associated with this error. | 784 * no such data associated with this error. |
| 785 */ | 785 */ |
| 786 Object getData(String name) => data[name]; | 786 Object getData(String name) => data[name]; |
| 787 | 787 |
| 788 /** | 788 /** |
| 789 * Set the value of the data with the given [name] to the given [value]. | 789 * Set the value of the data with the given [name] to the given [value]. |
| 790 */ | 790 */ |
| 791 void setData(String name, Object value) { | 791 void setData(String name, Object value) { |
| 792 data[name] = value; | 792 data[name] = value; |
| 793 } | 793 } |
| 794 | 794 |
| 795 /** | 795 /** |
| 796 * Return a table representing the structure of the Json object that will be | 796 * Return a table representing the structure of the Json object that will be |
| 797 * sent to the client to represent this response. | 797 * sent to the client to represent this response. |
| 798 */ | 798 */ |
| 799 Map<String, Object> toJson() { | 799 HashMap<String, Object> toJson() { |
| 800 Map<String, Object> jsonObject = new Map<String, Object>(); | 800 HashMap<String, Object> jsonObject = new HashMap<String, Object>(); |
| 801 jsonObject[CODE] = code; | 801 jsonObject[CODE] = code; |
| 802 jsonObject[MESSAGE] = message; | 802 jsonObject[MESSAGE] = message; |
| 803 if (!data.isEmpty) { | 803 if (!data.isEmpty) { |
| 804 jsonObject[DATA] = data; | 804 jsonObject[DATA] = data; |
| 805 } | 805 } |
| 806 return jsonObject; | 806 return jsonObject; |
| 807 } | 807 } |
| 808 | 808 |
| 809 @override | 809 @override |
| 810 String toString() => toJson().toString(); | 810 String toString() => toJson().toString(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 827 static const String PARAMS = 'params'; | 827 static const String PARAMS = 'params'; |
| 828 | 828 |
| 829 /** | 829 /** |
| 830 * The name of the event that triggered the notification. | 830 * The name of the event that triggered the notification. |
| 831 */ | 831 */ |
| 832 final String event; | 832 final String event; |
| 833 | 833 |
| 834 /** | 834 /** |
| 835 * A table mapping the names of notification parameters to their values. | 835 * A table mapping the names of notification parameters to their values. |
| 836 */ | 836 */ |
| 837 final Map<String, Object> params = new HashMap<String, Object>(); | 837 final HashMap<String, Object> params = new HashMap<String, Object>(); |
| 838 | 838 |
| 839 /** | 839 /** |
| 840 * Initialize a newly created [Notification] to have the given [event] name. | 840 * Initialize a newly created [Notification] to have the given [event] name. |
| 841 */ | 841 */ |
| 842 Notification(this.event); | 842 Notification(this.event); |
| 843 | 843 |
| 844 /** | 844 /** |
| 845 * Initialize a newly created instance based upon the given JSON data | 845 * Initialize a newly created instance based upon the given JSON data |
| 846 */ | 846 */ |
| 847 factory Notification.fromJson(Map<String, Object> json) { | 847 factory Notification.fromJson(HashMap<String, Object> json) { |
| 848 try { | 848 try { |
| 849 String event = json[Notification.EVENT]; | 849 String event = json[Notification.EVENT]; |
| 850 Object params = json[Notification.PARAMS]; | 850 Object params = json[Notification.PARAMS]; |
| 851 Notification notification = new Notification(event); | 851 Notification notification = new Notification(event); |
| 852 if (params is Map) { | 852 if (params is Map) { |
| 853 params.forEach((String key, Object value) { | 853 params.forEach((String key, Object value) { |
| 854 notification.setParameter(key, value); | 854 notification.setParameter(key, value); |
| 855 }); | 855 }); |
| 856 } | 856 } |
| 857 return notification; | 857 return notification; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 870 * Set the value of the parameter with the given [name] to the given [value]. | 870 * Set the value of the parameter with the given [name] to the given [value]. |
| 871 */ | 871 */ |
| 872 void setParameter(String name, Object value) { | 872 void setParameter(String name, Object value) { |
| 873 params[name] = value; | 873 params[name] = value; |
| 874 } | 874 } |
| 875 | 875 |
| 876 /** | 876 /** |
| 877 * Return a table representing the structure of the Json object that will be | 877 * Return a table representing the structure of the Json object that will be |
| 878 * sent to the client to represent this response. | 878 * sent to the client to represent this response. |
| 879 */ | 879 */ |
| 880 Map<String, Object> toJson() { | 880 HashMap<String, Object> toJson() { |
| 881 Map<String, Object> jsonObject = new Map<String, Object>(); | 881 HashMap<String, Object> jsonObject = new HashMap<String, Object>(); |
| 882 jsonObject[EVENT] = event; | 882 jsonObject[EVENT] = event; |
| 883 if (!params.isEmpty) { | 883 if (!params.isEmpty) { |
| 884 jsonObject[PARAMS] = params; | 884 jsonObject[PARAMS] = params; |
| 885 } | 885 } |
| 886 return jsonObject; | 886 return jsonObject; |
| 887 } | 887 } |
| 888 } | 888 } |
| 889 | 889 |
| 890 /** | 890 /** |
| 891 * Instances of the class [RequestHandler] implement a handler that can handle | 891 * Instances of the class [RequestHandler] implement a handler that can handle |
| (...skipping 18 matching lines...) Expand all Loading... |
| 910 /** | 910 /** |
| 911 * The response to be returned as a result of the failure. | 911 * The response to be returned as a result of the failure. |
| 912 */ | 912 */ |
| 913 final Response response; | 913 final Response response; |
| 914 | 914 |
| 915 /** | 915 /** |
| 916 * Initialize a newly created exception to return the given reponse. | 916 * Initialize a newly created exception to return the given reponse. |
| 917 */ | 917 */ |
| 918 RequestFailure(this.response); | 918 RequestFailure(this.response); |
| 919 } | 919 } |
| OLD | NEW |