Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(154)

Side by Side Diff: pkg/json_rpc_2/lib/src/parameters.dart

Issue 205713007: Add helper getters for URIs and date/times to json_rpc_2. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: bump the version Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/json_rpc_2/pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 json_rpc_2.parameters; 5 library json_rpc_2.parameters;
6 6
7 import 'dart:convert'; 7 import 'dart:convert';
8 8
9 import 'exception.dart'; 9 import 'exception.dart';
10 10
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 /// request if [value] doesn't exist. 217 /// request if [value] doesn't exist.
218 List get asList => _getTyped('an Array', (value) => value is List); 218 List get asList => _getTyped('an Array', (value) => value is List);
219 219
220 /// Asserts that [value] is a [List] and returns it. 220 /// Asserts that [value] is a [List] and returns it.
221 /// 221 ///
222 /// If [value] doesn't exist, this returns [defaultValue]. 222 /// If [value] doesn't exist, this returns [defaultValue].
223 List asListOr(List defaultValue) => asList; 223 List asListOr(List defaultValue) => asList;
224 224
225 /// Asserts that [value] exists and is a [Map] and returns it. 225 /// Asserts that [value] exists and is a [Map] and returns it.
226 /// 226 ///
227 /// [asListOr] may be used to provide a default value instead of rejecting the 227 /// [asMapOr] may be used to provide a default value instead of rejecting the
228 /// request if [value] doesn't exist. 228 /// request if [value] doesn't exist.
229 Map get asMap => _getTyped('an Object', (value) => value is Map); 229 Map get asMap => _getTyped('an Object', (value) => value is Map);
230 230
231 /// Asserts that [value] is a [Map] and returns it. 231 /// Asserts that [value] is a [Map] and returns it.
232 /// 232 ///
233 /// If [value] doesn't exist, this returns [defaultValue]. 233 /// If [value] doesn't exist, this returns [defaultValue].
234 Map asMapOr(Map defaultValue) => asMap; 234 Map asMapOr(Map defaultValue) => asMap;
235 235
236 /// Asserts that [value] exists, is a string, and can be parsed as a
237 /// [DateTime] and returns it.
238 ///
239 /// [asDateTimeOr] may be used to provide a default value instead of rejecting
240 /// the request if [value] doesn't exist.
241 DateTime get asDateTime => _getParsed('date/time', DateTime.parse);
242
243 /// Asserts that [value] exists, is a string, and can be parsed as a
244 /// [DateTime] and returns it.
245 ///
246 /// If [value] doesn't exist, this returns [defaultValue].
247 DateTime asDateTimeOr(DateTime defaultValue) => asDateTime;
248
249 /// Asserts that [value] exists, is a string, and can be parsed as a
250 /// [Uri] and returns it.
251 ///
252 /// [asUriOr] may be used to provide a default value instead of rejecting the
253 /// request if [value] doesn't exist.
254 Uri get asUri => _getParsed('URI', Uri.parse);
255
256 /// Asserts that [value] exists, is a string, and can be parsed as a
257 /// [Uri] and returns it.
258 ///
259 /// If [value] doesn't exist, this returns [defaultValue].
260 Uri asUriOr(Uri defaultValue) => asUri;
261
236 /// Get a parameter named [named] that matches [test], or the value of calling 262 /// Get a parameter named [named] that matches [test], or the value of calling
237 /// [orElse]. 263 /// [orElse].
238 /// 264 ///
239 /// [type] is used for the error message. It should begin with an indefinite 265 /// [type] is used for the error message. It should begin with an indefinite
240 /// article. 266 /// article.
241 _getTyped(String type, bool test(value)) { 267 _getTyped(String type, bool test(value)) {
242 if (test(value)) return value; 268 if (test(value)) return value;
243 throw new RpcException.invalidParams('Parameter $_path for method ' 269 throw new RpcException.invalidParams('Parameter $_path for method '
244 '"$method" must be $type, but was ${JSON.encode(value)}.'); 270 '"$method" must be $type, but was ${JSON.encode(value)}.');
245 } 271 }
246 272
273 _getParsed(String description, parse(String value)) {
274 var string = asString;
275 try {
276 return parse(string);
277 } on FormatException catch (error) {
278 // DateTime.parse doesn't actually include any useful information in the
279 // FormatException, just the string that was being parsed. There's no use
280 // in including that in the RPC exception. See issue 17753.
281 var message = error.message;
282 if (message == string) {
283 message = '';
284 } else {
285 message = '\n$message';
286 }
287
288 throw new RpcException.invalidParams('Parameter $_path for method '
289 '"$method" must be a valid $description, but was '
290 '${JSON.encode(string)}.$message');
291 }
292 }
293
247 void _assertPositional() { 294 void _assertPositional() {
248 // Throw the standard exception for a mis-typed list. 295 // Throw the standard exception for a mis-typed list.
249 asList; 296 asList;
250 } 297 }
251 298
252 void _assertNamed() { 299 void _assertNamed() {
253 // Throw the standard exception for a mis-typed map. 300 // Throw the standard exception for a mis-typed map.
254 asMap; 301 asMap;
255 } 302 }
256 } 303 }
(...skipping 16 matching lines...) Expand all
273 320
274 int asIntOr(int defaultValue) => defaultValue; 321 int asIntOr(int defaultValue) => defaultValue;
275 322
276 bool asBoolOr(bool defaultValue) => defaultValue; 323 bool asBoolOr(bool defaultValue) => defaultValue;
277 324
278 String asStringOr(String defaultValue) => defaultValue; 325 String asStringOr(String defaultValue) => defaultValue;
279 326
280 List asListOr(List defaultValue) => defaultValue; 327 List asListOr(List defaultValue) => defaultValue;
281 328
282 Map asMapOr(Map defaultValue) => defaultValue; 329 Map asMapOr(Map defaultValue) => defaultValue;
330
331 DateTime asDateTimeOr(DateTime defaultValue) => defaultValue;
332
333 Uri asUriOr(Uri defaultValue) => defaultValue;
283 } 334 }
OLDNEW
« no previous file with comments | « no previous file | pkg/json_rpc_2/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698