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

Unified Diff: pkg/analysis_server/lib/src/protocol.dart

Issue 342383002: Allow "null" as a synonym for [] or {} in the analysis server API. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/analysis_server/test/protocol_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analysis_server/lib/src/protocol.dart
diff --git a/pkg/analysis_server/lib/src/protocol.dart b/pkg/analysis_server/lib/src/protocol.dart
index fae8c53990b9db38388d0a1996028ed1ac1839d3..1326f9a28493995df92efd14af2994ab9cb1d9ae 100644
--- a/pkg/analysis_server/lib/src/protocol.dart
+++ b/pkg/analysis_server/lib/src/protocol.dart
@@ -138,10 +138,10 @@ class Request {
* if there is no such parameter associated with this request.
*/
RequestDatum getParameter(String name, defaultValue) {
- Object value = params[name];
- if (value == null) {
+ if (!params.containsKey(name)) {
return new RequestDatum(this, "default for $name", defaultValue);
}
+ Object value = params[name];
Brian Wilkerson 2014/06/21 03:24:51 Remove this line, given that 'value' is never acce
Paul Berry 2014/06/23 16:51:45 Done.
return new RequestDatum(this, name, params[name]);
}
@@ -151,10 +151,10 @@ class Request {
* such parameter associated with this request.
*/
RequestDatum getRequiredParameter(String name) {
- Object value = params[name];
- if (value == null) {
+ if (!params.containsKey(name)) {
throw new RequestFailure(new Response.missingRequiredParameter(this, name));
}
+ Object value = params[name];
Brian Wilkerson 2014/06/21 03:24:51 Ditto.
Paul Berry 2014/06/23 16:51:45 Done.
return new RequestDatum(this, name, value);
}
@@ -213,26 +213,19 @@ class RequestDatum {
* a [RequestDatum] containing the corresponding value.
*/
RequestDatum operator [](String key) {
- if (datum is! Map) {
- throw new RequestFailure(new Response.invalidParameter(request, path,
- "be a map"));
- }
- if (!datum.containsKey(key)) {
+ Map<String, Object> map = _asMap();
+ if (!map.containsKey(key)) {
throw new RequestFailure(new Response.invalidParameter(request, path,
"contain key '$key'"));
}
- return new RequestDatum(request, "$path.$key", datum[key]);
+ return new RequestDatum(request, "$path.$key", map[key]);
}
/**
* Return `true` if the datum is a Map containing the given [key].
*/
bool hasKey(String key) {
- if (datum is! Map) {
- throw new RequestFailure(new Response.invalidParameter(request, path,
- "be a map"));
- }
- return datum.containsKey(key);
+ return _asMap().containsKey(key);
}
/**
@@ -240,11 +233,7 @@ class RequestDatum {
* each key/value pair in the map.
*/
void forEachMap(void f(String key, RequestDatum value)) {
- if (datum is! Map) {
- throw new RequestFailure(new Response.invalidParameter(request, path,
- "be a map"));
- }
- datum.forEach((String key, value) {
+ _asMap().forEach((String key, value) {
f(key, new RequestDatum(request, "$path.$key", value));
});
}
@@ -286,16 +275,35 @@ class RequestDatum {
}
/**
- * Validate that the datum is a list, and return a list where each element in
- * the datum has been converted using the provided function.
+ * Determine if the datum is a list. Note: null is considered a synonym for
+ * the empty list.
*/
- List asList(elementConverter(RequestDatum datum)) {
- if (datum is! List) {
+ bool get isList {
+ return datum == null || datum is List;
+ }
+
+ /**
+ * Validate that the datum is a list, and return it in raw form.
+ */
+ List _asList() {
+ if (!isList) {
throw new RequestFailure(new Response.invalidParameter(request, path,
"be a list"));
}
- List list = datum as List;
+ if (datum == null) {
+ return [];
+ } else {
+ return datum;
+ }
+ }
+
+ /**
+ * Validate that the datum is a list, and return a list where each element in
+ * the datum has been converted using the provided function.
+ */
+ List asList(elementConverter(RequestDatum datum)) {
List result = [];
+ List list = _asList();
for (int i = 0; i < list.length; i++) {
result.add(elementConverter(new RequestDatum(request, "$path.$i", list[i])));
}
@@ -314,13 +322,14 @@ class RequestDatum {
}
/**
- * Determine if the datum is a list of strings.
+ * Determine if the datum is a list of strings. Note: null is considered a
+ * synonym for the empty list.
*/
bool get isStringList {
- if (datum is! List) {
+ if (!isList) {
return false;
}
- for (var element in datum) {
+ for (var element in _asList()) {
if (element is! String) {
return false;
}
@@ -329,14 +338,15 @@ class RequestDatum {
}
/**
- * Validate that the datum is a list of strings, and return it.
+ * Validate that the datum is a list of strings, and return it. Note: null
+ * is considered a synonym for the empty list.
*/
List<String> asStringList() {
if (!isStringList) {
throw new RequestFailure(new Response.invalidParameter(request, path,
"be a list of strings"));
}
- return datum;
+ return _asList();
}
/**
@@ -356,16 +366,40 @@ class RequestDatum {
}
/**
- * Determine if the datum is a map whose values are all strings.
+ * Determine if the datum is a map. Note: null is considered a synonym for
+ * the empty map.
+ */
+ bool get isMap {
+ return datum == null || datum is Map;
+ }
+
+ /**
+ * Validate that the datum is a map, and return it in raw form.
+ */
+ Map<String, Object> _asMap() {
+ if (!isMap) {
+ throw new RequestFailure(new Response.invalidParameter(request, path,
+ "be a map"));
+ }
+ if (datum == null) {
+ return {};
+ } else {
+ return datum;
+ }
+ }
+
+ /**
+ * Determine if the datum is a map whose values are all strings. Note: null
+ * is considered a synonym for the empty map.
*
* Note: we can safely assume that the keys are all strings, since JSON maps
* cannot have any other key type.
*/
bool get isStringMap {
- if (datum is! Map) {
+ if (!isMap) {
return false;
}
- for (var value in datum.values) {
+ for (var value in _asMap().values) {
if (value is! String) {
return false;
}
@@ -381,20 +415,21 @@ class RequestDatum {
throw new RequestFailure(new Response.invalidParameter(request, path,
"be a string map"));
}
- return datum;
+ return _asMap();
}
/**
- * Determine if the datum is a map whose values are all string lists.
+ * Determine if the datum is a map whose values are all string lists. Note:
+ * null is considered a synonym for the empty map.
*
* Note: we can safely assume that the keys are all strings, since JSON maps
* cannot have any other key type.
*/
- bool isStringListMap() {
- if (datum is! Map) {
+ bool get isStringListMap {
+ if (!isMap) {
return false;
}
- for (var value in datum.values) {
+ for (var value in _asMap().values) {
if (value is! List) {
return false;
}
@@ -408,15 +443,15 @@ class RequestDatum {
}
/**
- * Validate that the datum is a map from strings to string listss, and return
+ * Validate that the datum is a map from strings to string lists, and return
* it.
*/
Map<String, List<String>> asStringListMap() {
- if (!isStringListMap()) {
+ if (!isStringListMap) {
throw new RequestFailure(new Response.invalidParameter(request, path,
"be a string list map"));
}
- return datum;
+ return _asMap();
}
bool get isNull => datum == null;
« no previous file with comments | « no previous file | pkg/analysis_server/test/protocol_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698