| 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 final String id; | 73 final String id; |
| 74 | 74 |
| 75 /** | 75 /** |
| 76 * The method being requested. | 76 * The method being requested. |
| 77 */ | 77 */ |
| 78 final String method; | 78 final String method; |
| 79 | 79 |
| 80 /** | 80 /** |
| 81 * A table mapping the names of request parameters to their values. | 81 * A table mapping the names of request parameters to their values. |
| 82 */ | 82 */ |
| 83 final Map<String, Object> params = new HashMap<String, Object>(); | 83 final Map<String, Object> params; |
| 84 | 84 |
| 85 /** | 85 /** |
| 86 * A decoder that can be used to decode strings into JSON objects. | 86 * A decoder that can be used to decode strings into JSON objects. |
| 87 */ | 87 */ |
| 88 static const JsonDecoder DECODER = const JsonDecoder(null); | 88 static const JsonDecoder DECODER = const JsonDecoder(null); |
| 89 | 89 |
| 90 /** | 90 /** |
| 91 * Initialize a newly created [Request] to have the given [id] and [method] | 91 * Initialize a newly created [Request] to have the given [id] and [method] |
| 92 * name. | 92 * name. If [params] is supplied, it is used as the "params" map for the |
| 93 * request. Otherwise an empty "params" map is allocated. |
| 93 */ | 94 */ |
| 94 Request(this.id, this.method); | 95 Request(this.id, this.method, [Map<String, Object> params]) |
| 96 : params = params != null ? params : new HashMap<String, Object>(); |
| 95 | 97 |
| 96 /** | 98 /** |
| 97 * Return a request parsed from the given [data], or `null` if the [data] is | 99 * Return a request parsed from the given [data], or `null` if the [data] is |
| 98 * not a valid json representation of a request. The [data] is expected to | 100 * not a valid json representation of a request. The [data] is expected to |
| 99 * have the following format: | 101 * have the following format: |
| 100 * | 102 * |
| 101 * { | 103 * { |
| 102 * 'id': String, | 104 * 'id': String, |
| 103 * 'method': methodName, | 105 * 'method': methodName, |
| 104 * 'params': { | 106 * 'params': { |
| 105 * paramter_name: value | 107 * paramter_name: value |
| 106 * } | 108 * } |
| 107 * } | 109 * } |
| 108 * | 110 * |
| 109 * where the parameters are optional and can contain any number of name/value | 111 * where the parameters are optional and can contain any number of name/value |
| 110 * pairs. | 112 * pairs. |
| 111 */ | 113 */ |
| 112 factory Request.fromString(String data) { | 114 factory Request.fromString(String data) { |
| 113 try { | 115 try { |
| 114 var result = DECODER.convert(data); | 116 var result = DECODER.convert(data); |
| 115 if (result is! Map) { | 117 if (result is! Map) { |
| 116 return null; | 118 return null; |
| 117 } | 119 } |
| 118 var id = result[Request.ID]; | 120 var id = result[Request.ID]; |
| 119 var method = result[Request.METHOD]; | 121 var method = result[Request.METHOD]; |
| 120 if (id is! String || method is! String) { | 122 if (id is! String || method is! String) { |
| 121 return null; | 123 return null; |
| 122 } | 124 } |
| 123 var params = result[Request.PARAMS]; | 125 var params = result[Request.PARAMS]; |
| 124 Request request = new Request(id, method); | 126 if (params is Map || params == null) { |
| 125 if (params is Map) { | 127 return new Request(id, method, params); |
| 126 params.forEach((String key, Object value) { | 128 } else { |
| 127 request.setParameter(key, value); | |
| 128 }); | |
| 129 } else if (params != null) { | |
| 130 return null; | 129 return null; |
| 131 } | 130 } |
| 132 return request; | |
| 133 } catch (exception) { | 131 } catch (exception) { |
| 134 return null; | 132 return null; |
| 135 } | 133 } |
| 136 } | 134 } |
| 137 | 135 |
| 138 /** | 136 /** |
| 139 * Return the value of the parameter with the given [name], or [defaultValue] | 137 * Return the value of the parameter with the given [name], or [defaultValue] |
| 140 * if there is no such parameter associated with this request. | 138 * if there is no such parameter associated with this request. |
| 141 */ | 139 */ |
| 142 RequestDatum getParameter(String name, defaultValue) { | 140 RequestDatum getParameter(String name, defaultValue) { |
| (...skipping 812 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 955 */ | 953 */ |
| 956 _toJson(Object value) { | 954 _toJson(Object value) { |
| 957 if (value is HasToJson) { | 955 if (value is HasToJson) { |
| 958 return value.toJson(); | 956 return value.toJson(); |
| 959 } | 957 } |
| 960 if (value is Iterable) { | 958 if (value is Iterable) { |
| 961 return value.map((item) => _toJson(item)).toList(); | 959 return value.map((item) => _toJson(item)).toList(); |
| 962 } | 960 } |
| 963 return value; | 961 return value; |
| 964 } | 962 } |
| OLD | NEW |