Skip to content

Commit a2e9153

Browse files
committed
DAS: Tidy map literals and constant names in protocol.dart
We rename constant names to use lowerCamelCase, and append "AttributeName" in most cases, as most constants refer to a (as it is documented) "JSON attribute." Additionally the `toJson` methods can be simplified by returning Map literals, instead of setting key/value pairs in individual statements. Change-Id: I5f0adabfa3c4d274407ae8a5b5684dce22f9b5fa Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/455880 Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 445955e commit a2e9153

File tree

5 files changed

+79
-96
lines changed

5 files changed

+79
-96
lines changed

pkg/analysis_server/lib/protocol/protocol.dart

Lines changed: 61 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ export 'package:analyzer_plugin/protocol/protocol.dart' show Enum;
2121
class Notification {
2222
/// The name of the JSON attribute containing the name of the event that
2323
/// triggered the notification.
24-
static const String EVENT = 'event';
24+
static const String eventAttributeName = 'event';
2525

2626
/// The name of the JSON attribute containing the result values.
27-
static const String PARAMS = 'params';
27+
static const String paramsAttributeName = 'params';
2828

2929
/// The name of the event that triggered the notification.
3030
final String event;
@@ -41,40 +41,35 @@ class Notification {
4141
/// Initialize a newly created instance based on the given JSON data.
4242
factory Notification.fromJson(Map<Object?, Object?> json) {
4343
return Notification(
44-
json[Notification.EVENT] as String,
45-
json[Notification.PARAMS] as Map<String, Object?>?,
44+
json[Notification.eventAttributeName] as String,
45+
json[Notification.paramsAttributeName] as Map<String, Object?>?,
4646
);
4747
}
4848

49-
/// Return a table representing the structure of the Json object that will be
49+
/// Returns a table representing the structure of the JSON object that will be
5050
/// sent to the client to represent this response.
51-
Map<String, Object> toJson() {
52-
var jsonObject = <String, Object>{};
53-
jsonObject[EVENT] = event;
54-
var params = this.params;
55-
if (params != null) {
56-
jsonObject[PARAMS] = params;
57-
}
58-
return jsonObject;
59-
}
51+
Map<String, Object> toJson() => {
52+
eventAttributeName: event,
53+
paramsAttributeName: ?params,
54+
};
6055
}
6156

6257
/// A request that was received from the client.
6358
///
6459
/// Clients may not extend, implement or mix-in this class.
6560
class Request extends RequestOrResponse {
6661
/// The name of the JSON attribute containing the id of the request.
67-
static const String ID = 'id';
62+
static const String idAttributeName = 'id';
6863

6964
/// The name of the JSON attribute containing the name of the request.
70-
static const String METHOD = 'method';
65+
static const String methodAttributeName = 'method';
7166

7267
/// The name of the JSON attribute containing the request parameters.
73-
static const String PARAMS = 'params';
68+
static const String paramsAttributeName = 'params';
7469

7570
/// The name of the optional JSON attribute indicating the time (milliseconds
7671
/// since epoch) at which the client made the request.
77-
static const String CLIENT_REQUEST_TIME = 'clientRequestTime';
72+
static const String clientRequestTimeAttributeName = 'clientRequestTime';
7873

7974
/// The unique identifier used to identify this request.
8075
@override
@@ -123,21 +118,14 @@ class Request extends RequestOrResponse {
123118
_equalMaps(params, other.params);
124119
}
125120

126-
/// Return a table representing the structure of the Json object that will be
121+
/// Returns a table representing the structure of the JSON object that will be
127122
/// sent to the client to represent this response.
128-
Map<String, Object> toJson() {
129-
var jsonObject = <String, Object>{};
130-
jsonObject[ID] = id;
131-
jsonObject[METHOD] = method;
132-
if (params.isNotEmpty) {
133-
jsonObject[PARAMS] = params;
134-
}
135-
var clientRequestTime = this.clientRequestTime;
136-
if (clientRequestTime != null) {
137-
jsonObject[CLIENT_REQUEST_TIME] = clientRequestTime;
138-
}
139-
return jsonObject;
140-
}
123+
Map<String, Object> toJson() => {
124+
idAttributeName: id,
125+
methodAttributeName: method,
126+
if (params.isNotEmpty) paramsAttributeName: params,
127+
clientRequestTimeAttributeName: ?clientRequestTime,
128+
};
141129

142130
bool _equalLists(List<Object?> first, List<Object?> second) {
143131
var length = first.length;
@@ -189,60 +177,60 @@ class Request extends RequestOrResponse {
189177
return first == second;
190178
}
191179

192-
/// Return a request parsed from the given json, or `null` if the [data] is
193-
/// not a valid json representation of a request. The [data] is expected to
194-
/// have the following format:
180+
/// Returns a request parsed from the given JSON [result], or `null` if the
181+
/// data is not a valid JSON representation of a request. The data is expected
182+
/// to have the following format:
195183
///
196-
/// {
197-
/// 'clientRequestTime': millisecondsSinceEpoch
198-
/// 'id': String,
199-
/// 'method': methodName,
200-
/// 'params': {
201-
/// parameter_name: value
184+
/// {
185+
/// 'clientRequestTime': millisecondsSinceEpoch
186+
/// 'id': String,
187+
/// 'method': methodName,
188+
/// 'params': {
189+
/// parameter_name: value
190+
/// }
202191
/// }
203-
/// }
204192
///
205-
/// where both the parameters and clientRequestTime are optional.
193+
/// where both the parameters and `clientRequestTime` are optional.
206194
///
207195
/// The parameters can contain any number of name/value pairs. The
208-
/// clientRequestTime must be an int representing the time at which the client
209-
/// issued the request (milliseconds since epoch).
196+
/// `clientRequestTime` must be an int representing the time at which the
197+
/// client issued the request (milliseconds since epoch).
210198
static Request? fromJson(Map<String, Object?> result) {
211-
var id = result[Request.ID];
212-
var method = result[Request.METHOD];
199+
var id = result[Request.idAttributeName];
200+
var method = result[Request.methodAttributeName];
213201
if (id is! String || method is! String) {
214202
return null;
215203
}
216-
var time = result[Request.CLIENT_REQUEST_TIME];
204+
var time = result[Request.clientRequestTimeAttributeName];
217205
if (time is! int?) {
218206
return null;
219207
}
220-
var params = result[Request.PARAMS];
208+
var params = result[Request.paramsAttributeName];
221209
if (params is Map<String, Object?>?) {
222210
return Request(id, method, params, time);
223211
} else {
224212
return null;
225213
}
226214
}
227215

228-
/// Return a request parsed from the given [data], or `null` if the [data] is
229-
/// not a valid json representation of a request. The [data] is expected to
216+
/// Returns a request parsed from the given [data], or `null` if the [data] is
217+
/// not a valid JSON representation of a request. The [data] is expected to
230218
/// have the following format:
231219
///
232-
/// {
233-
/// 'clientRequestTime': millisecondsSinceEpoch
234-
/// 'id': String,
235-
/// 'method': methodName,
236-
/// 'params': {
237-
/// parameter_name: value
220+
/// {
221+
/// 'clientRequestTime': millisecondsSinceEpoch
222+
/// 'id': String,
223+
/// 'method': methodName,
224+
/// 'params': {
225+
/// parameter_name: value
226+
/// }
238227
/// }
239-
/// }
240228
///
241-
/// where both the parameters and clientRequestTime are optional.
229+
/// where both the parameters and `clientRequestTime` are optional.
242230
///
243231
/// The parameters can contain any number of name/value pairs. The
244-
/// clientRequestTime must be an int representing the time at which the client
245-
/// issued the request (milliseconds since epoch).
232+
/// `clientRequestTime` must be an int representing the time at which the
233+
/// client issued the request (milliseconds since epoch).
246234
static Request? fromString(String data) {
247235
try {
248236
var result = json.decode(data);
@@ -293,13 +281,13 @@ abstract class RequestOrResponse {
293281
class Response extends RequestOrResponse {
294282
/// The name of the JSON attribute containing the id of the request for which
295283
/// this is a response.
296-
static const String ID = 'id';
284+
static const String idAttributeName = 'id';
297285

298286
/// The name of the JSON attribute containing the error message.
299-
static const String ERROR = 'error';
287+
static const String errorAttributeName = 'error';
300288

301289
/// The name of the JSON attribute containing the result values.
302-
static const String RESULT = 'result';
290+
static const String resultAttributeName = 'result';
303291

304292
/// The unique identifier used to identify the request that this response is
305293
/// associated with.
@@ -315,7 +303,7 @@ class Response extends RequestOrResponse {
315303
Map<String, Object?>? result;
316304

317305
/// Initialize a newly created instance to represent a response to a request
318-
/// with the given [id]. If [_result] is provided, it will be used as the
306+
/// with the given [id]. If [result] is provided, it will be used as the
319307
/// result; otherwise an empty result will be used. If an [error] is provided
320308
/// then the response will represent an error condition.
321309
Response(this.id, {this.result, this.error});
@@ -615,30 +603,22 @@ class Response extends RequestOrResponse {
615603

616604
/// Return a table representing the structure of the Json object that will be
617605
/// sent to the client to represent this response.
618-
Map<String, Object> toJson() {
619-
var jsonObject = <String, Object>{};
620-
jsonObject[ID] = id;
621-
var error = this.error;
622-
if (error != null) {
623-
jsonObject[ERROR] = error.toJson(clientUriConverter: null);
624-
}
625-
var result = this.result;
626-
if (result != null) {
627-
jsonObject[RESULT] = result;
628-
}
629-
return jsonObject;
630-
}
606+
Map<String, Object> toJson() => {
607+
idAttributeName: id,
608+
errorAttributeName: ?error?.toJson(clientUriConverter: null),
609+
resultAttributeName: ?result,
610+
};
631611

632612
/// Initialize a newly created instance based on the given JSON data.
633613
static Response? fromJson(Map<String, Object?> json) {
634614
try {
635-
var id = json[Response.ID];
615+
var id = json[Response.idAttributeName];
636616
if (id is! String) {
637617
return null;
638618
}
639619

640620
RequestError? decodedError;
641-
var error = json[Response.ERROR];
621+
var error = json[Response.errorAttributeName];
642622
if (error is Map) {
643623
decodedError = RequestError.fromJson(
644624
ResponseDecoder(null),
@@ -649,7 +629,7 @@ class Response extends RequestOrResponse {
649629
}
650630

651631
Map<String, Object?>? decodedResult;
652-
var result = json[Response.RESULT];
632+
var result = json[Response.resultAttributeName];
653633
if (result is Map<String, Object?>) {
654634
decodedResult = result;
655635
}

pkg/analysis_server/lib/src/channel/byte_stream_channel.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ class ByteStreamClientChannel implements ClientCommunicationChannel {
3333
.cast<Map<String, Object?>>()
3434
.asBroadcastStream();
3535
var responseStream = jsonStream
36-
.where((json) => json[Notification.EVENT] == null)
36+
.where((json) => json[Notification.eventAttributeName] == null)
3737
.transform(ResponseConverter())
3838
.where((response) => response != null)
3939
.cast<Response>()
4040
.asBroadcastStream();
4141
var notificationStream = jsonStream
42-
.where((json) => json[Notification.EVENT] != null)
42+
.where((json) => json[Notification.eventAttributeName] != null)
4343
.transform(NotificationConverter())
4444
.asBroadcastStream();
4545
return ByteStreamClientChannel._(

pkg/analysis_server/test/channel/byte_stream_channel_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ class ByteStreamClientChannelTest {
8787
var assertCount = 0;
8888
var request = Request('72', 'foo.bar');
8989
outputLineStream.first.then((line) => json.decode(line)).then((json) {
90-
expect(json[Request.ID], equals('72'));
91-
expect(json[Request.METHOD], equals('foo.bar'));
90+
expect(json[Request.idAttributeName], equals('72'));
91+
expect(json[Request.methodAttributeName], equals('foo.bar'));
9292
inputSink.writeln('{"id":"73"}');
9393
inputSink.writeln('{"id":"72"}');
9494
assertCount++;

pkg/analysis_server/test/domain_server_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ class ServerDomainTest extends PubPackageAnalysisServerTest {
7676
expect(
7777
response.toJson(),
7878
equals({
79-
Response.ID: '0',
80-
Response.RESULT: {VERSION: PROTOCOL_VERSION},
79+
Response.idAttributeName: '0',
80+
Response.resultAttributeName: {VERSION: PROTOCOL_VERSION},
8181
}),
8282
);
8383
}

pkg/analysis_server/test/protocol_test.dart

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class RequestTest {
136136
var original = Request('one', 'aMethod', null, 347);
137137
var map = original.toJson();
138138
// Insert bad value - should be int but client sent string instead
139-
map[Request.CLIENT_REQUEST_TIME] = '347';
139+
map[Request.clientRequestTimeAttributeName] = '347';
140140
var jsonData = json.encode(map);
141141
var request = Request.fromString(jsonData);
142142
expect(request, isNull);
@@ -164,7 +164,10 @@ class RequestTest {
164164
var request = Request('one', 'aMethod');
165165
expect(
166166
request.toJson(),
167-
equals({Request.ID: 'one', Request.METHOD: 'aMethod'}),
167+
equals({
168+
Request.idAttributeName: 'one',
169+
Request.methodAttributeName: 'aMethod',
170+
}),
168171
);
169172
}
170173

@@ -173,9 +176,9 @@ class RequestTest {
173176
expect(
174177
request.toJson(),
175178
equals({
176-
Request.ID: 'one',
177-
Request.METHOD: 'aMethod',
178-
Request.PARAMS: {'foo': 'bar'},
179+
Request.idAttributeName: 'one',
180+
Request.methodAttributeName: 'aMethod',
181+
Request.paramsAttributeName: {'foo': 'bar'},
179182
}),
180183
);
181184
}
@@ -190,8 +193,8 @@ class ResponseTest {
190193
expect(
191194
response.toJson(),
192195
equals({
193-
Response.ID: '',
194-
Response.ERROR: {
196+
Response.idAttributeName: '',
197+
Response.errorAttributeName: {
195198
'code': 'INVALID_REQUEST',
196199
'message': 'Invalid request',
197200
},
@@ -206,8 +209,8 @@ class ResponseTest {
206209
expect(
207210
response.toJson(),
208211
equals({
209-
Response.ID: '0',
210-
Response.ERROR: {
212+
Response.idAttributeName: '0',
213+
Response.errorAttributeName: {
211214
'code': 'UNKNOWN_REQUEST',
212215
'message': 'Unknown request',
213216
},

0 commit comments

Comments
 (0)