| 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:convert' show JsonDecoder; | 7 import 'dart:convert' show JsonDecoder; |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * Instances of the class [Request] represent a request that was received. | 10 * Instances of the class [Request] represent a request that was received. |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 * Create a RequestDatum for decoding and validating [datum], which refers to | 166 * Create a RequestDatum for decoding and validating [datum], which refers to |
| 167 * [request] in any errors it reports. | 167 * [request] in any errors it reports. |
| 168 */ | 168 */ |
| 169 RequestDatum(this.request, this.path, this.datum); | 169 RequestDatum(this.request, this.path, this.datum); |
| 170 | 170 |
| 171 /** | 171 /** |
| 172 * Validate that the datum is a Map containing the given [key], and return | 172 * Validate that the datum is a Map containing the given [key], and return |
| 173 * a [RequestDatum] containing the corresponding value. | 173 * a [RequestDatum] containing the corresponding value. |
| 174 */ | 174 */ |
| 175 RequestDatum operator [](String key) { | 175 RequestDatum operator [](String key) { |
| 176 if (datum is! Map<String, dynamic>) { | 176 if (datum is! Map) { |
| 177 throw new RequestFailure(new Response.invalidParameter(request, path, | 177 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 178 "be a map")); | 178 "be a map")); |
| 179 } | 179 } |
| 180 if (!datum.containsKey(key)) { | 180 if (!datum.containsKey(key)) { |
| 181 throw new RequestFailure(new Response.invalidParameter(request, path, | 181 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 182 "contain key '$key'")); | 182 "contain key '$key'")); |
| 183 } | 183 } |
| 184 return new RequestDatum(request, "$path.$key", datum[key]); | 184 return new RequestDatum(request, "$path.$key", datum[key]); |
| 185 } | 185 } |
| 186 | 186 |
| 187 /** | 187 /** |
| 188 * Return `true` if the datum is a Map containing the given [key]. | 188 * Return `true` if the datum is a Map containing the given [key]. |
| 189 */ | 189 */ |
| 190 bool hasKey(String key) { | 190 bool hasKey(String key) { |
| 191 if (datum is! Map<String, dynamic>) { | 191 if (datum is! Map) { |
| 192 throw new RequestFailure(new Response.invalidParameter(request, path, | 192 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 193 "be a map")); | 193 "be a map")); |
| 194 } | 194 } |
| 195 return datum.containsKey(key); | 195 return datum.containsKey(key); |
| 196 } | 196 } |
| 197 | 197 |
| 198 /** | 198 /** |
| 199 * Validate that the datum is a Map whose keys are strings, and call [f] on | 199 * Validate that the datum is a Map whose keys are strings, and call [f] on |
| 200 * each key/value pair in the map. | 200 * each key/value pair in the map. |
| 201 */ | 201 */ |
| 202 void forEachMap(void f(String key, RequestDatum value)) { | 202 void forEachMap(void f(String key, RequestDatum value)) { |
| 203 if (datum is! Map<String, dynamic>) { | 203 if (datum is! Map) { |
| 204 throw new RequestFailure(new Response.invalidParameter(request, path, | 204 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 205 "be a map")); | 205 "be a map")); |
| 206 } | 206 } |
| 207 datum.forEach((String key, dynamic value) { | 207 datum.forEach((String key, dynamic value) { |
| 208 f(key, new RequestDatum(request, "$path.$key", value)); | 208 f(key, new RequestDatum(request, "$path.$key", value)); |
| 209 }); | 209 }); |
| 210 } | 210 } |
| 211 | 211 |
| 212 /** | 212 /** |
| 213 * Validate that the datum is an integer (or a string that can be parsed | 213 * Validate that the datum is an integer (or a string that can be parsed |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 */ | 250 */ |
| 251 String asString() { | 251 String asString() { |
| 252 if (datum is! String) { | 252 if (datum is! String) { |
| 253 throw new RequestFailure(new Response.invalidParameter(request, path, | 253 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 254 "be a string")); | 254 "be a string")); |
| 255 } | 255 } |
| 256 return datum; | 256 return datum; |
| 257 } | 257 } |
| 258 | 258 |
| 259 /** | 259 /** |
| 260 * Determine if the datum is a list of strings. |
| 261 */ |
| 262 bool isStringList() { |
| 263 if (datum is! List) { |
| 264 return false; |
| 265 } |
| 266 for (var element in datum) { |
| 267 if (element is! String) { |
| 268 return false; |
| 269 } |
| 270 } |
| 271 return true; |
| 272 } |
| 273 |
| 274 /** |
| 260 * Validate that the datum is a list of strings, and return it. | 275 * Validate that the datum is a list of strings, and return it. |
| 261 */ | 276 */ |
| 262 List<String> asStringList() { | 277 List<String> asStringList() { |
| 263 if (datum is! List<String>) { | 278 if (!isStringList()) { |
| 264 throw new RequestFailure(new Response.invalidParameter(request, path, | 279 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 265 "be a list of strings")); | 280 "be a list of strings")); |
| 266 } | 281 } |
| 267 return datum; | 282 return datum; |
| 268 } | 283 } |
| 269 | 284 |
| 270 /** | 285 /** |
| 286 * Determine if the datum is a map whose values are all strings. |
| 287 * |
| 288 * Note: we can safely assume that the keys are all strings, since JSON maps |
| 289 * cannot have any other key type. |
| 290 */ |
| 291 bool isStringMap() { |
| 292 if (datum is! Map) { |
| 293 return false; |
| 294 } |
| 295 for (var value in datum.values) { |
| 296 if (value is! String) { |
| 297 return false; |
| 298 } |
| 299 } |
| 300 return true; |
| 301 } |
| 302 |
| 303 /** |
| 271 * Validate that the datum is a map from strings to strings, and return it. | 304 * Validate that the datum is a map from strings to strings, and return it. |
| 272 */ | 305 */ |
| 273 Map<String, String> asStringMap() { | 306 Map<String, String> asStringMap() { |
| 274 if (datum is! Map<String, String>) { | 307 if (!isStringMap()) { |
| 275 throw new RequestFailure(new Response.invalidParameter(request, path, | 308 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 276 "be a string map")); | 309 "be a string map")); |
| 277 } | 310 } |
| 278 return datum; | 311 return datum; |
| 279 } | 312 } |
| 280 } | 313 } |
| 281 | 314 |
| 282 /** | 315 /** |
| 283 * Instances of the class [Response] represent a response to a request. | 316 * Instances of the class [Response] represent a response to a request. |
| 284 */ | 317 */ |
| (...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 704 /** | 737 /** |
| 705 * The response to be returned as a result of the failure. | 738 * The response to be returned as a result of the failure. |
| 706 */ | 739 */ |
| 707 final Response response; | 740 final Response response; |
| 708 | 741 |
| 709 /** | 742 /** |
| 710 * Initialize a newly created exception to return the given reponse. | 743 * Initialize a newly created exception to return the given reponse. |
| 711 */ | 744 */ |
| 712 RequestFailure(this.response); | 745 RequestFailure(this.response); |
| 713 } | 746 } |
| OLD | NEW |