Index: pkg/json_rpc_2/lib/src/parameters.dart |
diff --git a/pkg/json_rpc_2/lib/src/parameters.dart b/pkg/json_rpc_2/lib/src/parameters.dart |
index afc4a402c66e783875ec4fa8d39e84014849913d..1e2522013eb8f12e28df2e018757be181d50d92d 100644 |
--- a/pkg/json_rpc_2/lib/src/parameters.dart |
+++ b/pkg/json_rpc_2/lib/src/parameters.dart |
@@ -224,7 +224,7 @@ class Parameter extends Parameters { |
/// Asserts that [value] exists and is a [Map] and returns it. |
/// |
- /// [asListOr] may be used to provide a default value instead of rejecting the |
+ /// [asMapOr] may be used to provide a default value instead of rejecting the |
/// request if [value] doesn't exist. |
Map get asMap => _getTyped('an Object', (value) => value is Map); |
@@ -233,6 +233,32 @@ class Parameter extends Parameters { |
/// If [value] doesn't exist, this returns [defaultValue]. |
Map asMapOr(Map defaultValue) => asMap; |
+ /// Asserts that [value] exists, is a string, and can be parsed as a |
+ /// [DateTime] and returns it. |
+ /// |
+ /// [asDateTimeOr] may be used to provide a default value instead of rejecting |
+ /// the request if [value] doesn't exist. |
+ DateTime get asDateTime => _getParsed('date/time', DateTime.parse); |
+ |
+ /// Asserts that [value] exists, is a string, and can be parsed as a |
+ /// [DateTime] and returns it. |
+ /// |
+ /// If [value] doesn't exist, this returns [defaultValue]. |
+ DateTime asDateTimeOr(DateTime defaultValue) => asDateTime; |
+ |
+ /// Asserts that [value] exists, is a string, and can be parsed as a |
+ /// [Uri] and returns it. |
+ /// |
+ /// [asUriOr] may be used to provide a default value instead of rejecting the |
+ /// request if [value] doesn't exist. |
+ Uri get asUri => _getParsed('URI', Uri.parse); |
+ |
+ /// Asserts that [value] exists, is a string, and can be parsed as a |
+ /// [Uri] and returns it. |
+ /// |
+ /// If [value] doesn't exist, this returns [defaultValue]. |
+ Uri asUriOr(Uri defaultValue) => asUri; |
+ |
/// Get a parameter named [named] that matches [test], or the value of calling |
/// [orElse]. |
/// |
@@ -244,6 +270,27 @@ class Parameter extends Parameters { |
'"$method" must be $type, but was ${JSON.encode(value)}.'); |
} |
+ _getParsed(String description, parse(String value)) { |
+ var string = asString; |
+ try { |
+ return parse(string); |
+ } on FormatException catch (error) { |
+ // DateTime.parse doesn't actually include any useful information in the |
+ // FormatException, just the string that was being parsed. There's no use |
+ // in including that in the RPC exception. See issue 17753. |
+ var message = error.message; |
+ if (message == string) { |
+ message = ''; |
+ } else { |
+ message = '\n$message'; |
+ } |
+ |
+ throw new RpcException.invalidParams('Parameter $_path for method ' |
+ '"$method" must be a valid $description, but was ' |
+ '${JSON.encode(string)}.$message'); |
+ } |
+ } |
+ |
void _assertPositional() { |
// Throw the standard exception for a mis-typed list. |
asList; |
@@ -280,4 +327,8 @@ class _MissingParameter extends Parameter { |
List asListOr(List defaultValue) => defaultValue; |
Map asMapOr(Map defaultValue) => defaultValue; |
+ |
+ DateTime asDateTimeOr(DateTime defaultValue) => defaultValue; |
+ |
+ Uri asUriOr(Uri defaultValue) => defaultValue; |
} |