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 <brianwilkerson@google.com>
diff --git a/pkg/analysis_server/lib/protocol/protocol.dart b/pkg/analysis_server/lib/protocol/protocol.dart
index 65969e4..83c691e 100644
--- a/pkg/analysis_server/lib/protocol/protocol.dart
+++ b/pkg/analysis_server/lib/protocol/protocol.dart
@@ -21,10 +21,10 @@
 class Notification {
   /// The name of the JSON attribute containing the name of the event that
   /// triggered the notification.
-  static const String EVENT = 'event';
+  static const String eventAttributeName = 'event';
 
   /// The name of the JSON attribute containing the result values.
-  static const String PARAMS = 'params';
+  static const String paramsAttributeName = 'params';
 
   /// The name of the event that triggered the notification.
   final String event;
@@ -41,22 +41,17 @@
   /// Initialize a newly created instance based on the given JSON data.
   factory Notification.fromJson(Map<Object?, Object?> json) {
     return Notification(
-      json[Notification.EVENT] as String,
-      json[Notification.PARAMS] as Map<String, Object?>?,
+      json[Notification.eventAttributeName] as String,
+      json[Notification.paramsAttributeName] as Map<String, Object?>?,
     );
   }
 
-  /// Return a table representing the structure of the Json object that will be
+  /// Returns a table representing the structure of the JSON object that will be
   /// sent to the client to represent this response.
-  Map<String, Object> toJson() {
-    var jsonObject = <String, Object>{};
-    jsonObject[EVENT] = event;
-    var params = this.params;
-    if (params != null) {
-      jsonObject[PARAMS] = params;
-    }
-    return jsonObject;
-  }
+  Map<String, Object> toJson() => {
+    eventAttributeName: event,
+    paramsAttributeName: ?params,
+  };
 }
 
 /// A request that was received from the client.
@@ -64,17 +59,17 @@
 /// Clients may not extend, implement or mix-in this class.
 class Request extends RequestOrResponse {
   /// The name of the JSON attribute containing the id of the request.
-  static const String ID = 'id';
+  static const String idAttributeName = 'id';
 
   /// The name of the JSON attribute containing the name of the request.
-  static const String METHOD = 'method';
+  static const String methodAttributeName = 'method';
 
   /// The name of the JSON attribute containing the request parameters.
-  static const String PARAMS = 'params';
+  static const String paramsAttributeName = 'params';
 
   /// The name of the optional JSON attribute indicating the time (milliseconds
   /// since epoch) at which the client made the request.
-  static const String CLIENT_REQUEST_TIME = 'clientRequestTime';
+  static const String clientRequestTimeAttributeName = 'clientRequestTime';
 
   /// The unique identifier used to identify this request.
   @override
@@ -123,21 +118,14 @@
         _equalMaps(params, other.params);
   }
 
-  /// Return a table representing the structure of the Json object that will be
+  /// Returns a table representing the structure of the JSON object that will be
   /// sent to the client to represent this response.
-  Map<String, Object> toJson() {
-    var jsonObject = <String, Object>{};
-    jsonObject[ID] = id;
-    jsonObject[METHOD] = method;
-    if (params.isNotEmpty) {
-      jsonObject[PARAMS] = params;
-    }
-    var clientRequestTime = this.clientRequestTime;
-    if (clientRequestTime != null) {
-      jsonObject[CLIENT_REQUEST_TIME] = clientRequestTime;
-    }
-    return jsonObject;
-  }
+  Map<String, Object> toJson() => {
+    idAttributeName: id,
+    methodAttributeName: method,
+    if (params.isNotEmpty) paramsAttributeName: params,
+    clientRequestTimeAttributeName: ?clientRequestTime,
+  };
 
   bool _equalLists(List<Object?> first, List<Object?> second) {
     var length = first.length;
@@ -189,35 +177,35 @@
     return first == second;
   }
 
-  /// Return a request parsed from the given json, or `null` if the [data] is
-  /// not a valid json representation of a request. The [data] is expected to
-  /// have the following format:
+  /// Returns a request parsed from the given JSON [result], or `null` if the
+  /// data is not a valid JSON representation of a request. The data is expected
+  /// to have the following format:
   ///
-  ///   {
-  ///     'clientRequestTime': millisecondsSinceEpoch
-  ///     'id': String,
-  ///     'method': methodName,
-  ///     'params': {
-  ///       parameter_name: value
+  ///     {
+  ///       'clientRequestTime': millisecondsSinceEpoch
+  ///       'id': String,
+  ///       'method': methodName,
+  ///       'params': {
+  ///         parameter_name: value
+  ///       }
   ///     }
-  ///   }
   ///
-  /// where both the parameters and clientRequestTime are optional.
+  /// where both the parameters and `clientRequestTime` are optional.
   ///
   /// The parameters can contain any number of name/value pairs. The
-  /// clientRequestTime must be an int representing the time at which the client
-  /// issued the request (milliseconds since epoch).
+  /// `clientRequestTime` must be an int representing the time at which the
+  /// client issued the request (milliseconds since epoch).
   static Request? fromJson(Map<String, Object?> result) {
-    var id = result[Request.ID];
-    var method = result[Request.METHOD];
+    var id = result[Request.idAttributeName];
+    var method = result[Request.methodAttributeName];
     if (id is! String || method is! String) {
       return null;
     }
-    var time = result[Request.CLIENT_REQUEST_TIME];
+    var time = result[Request.clientRequestTimeAttributeName];
     if (time is! int?) {
       return null;
     }
-    var params = result[Request.PARAMS];
+    var params = result[Request.paramsAttributeName];
     if (params is Map<String, Object?>?) {
       return Request(id, method, params, time);
     } else {
@@ -225,24 +213,24 @@
     }
   }
 
-  /// Return a request parsed from the given [data], or `null` if the [data] is
-  /// not a valid json representation of a request. The [data] is expected to
+  /// Returns a request parsed from the given [data], or `null` if the [data] is
+  /// not a valid JSON representation of a request. The [data] is expected to
   /// have the following format:
   ///
-  ///   {
-  ///     'clientRequestTime': millisecondsSinceEpoch
-  ///     'id': String,
-  ///     'method': methodName,
-  ///     'params': {
-  ///       parameter_name: value
+  ///     {
+  ///       'clientRequestTime': millisecondsSinceEpoch
+  ///       'id': String,
+  ///       'method': methodName,
+  ///       'params': {
+  ///         parameter_name: value
+  ///       }
   ///     }
-  ///   }
   ///
-  /// where both the parameters and clientRequestTime are optional.
+  /// where both the parameters and `clientRequestTime` are optional.
   ///
   /// The parameters can contain any number of name/value pairs. The
-  /// clientRequestTime must be an int representing the time at which the client
-  /// issued the request (milliseconds since epoch).
+  /// `clientRequestTime` must be an int representing the time at which the
+  /// client issued the request (milliseconds since epoch).
   static Request? fromString(String data) {
     try {
       var result = json.decode(data);
@@ -293,13 +281,13 @@
 class Response extends RequestOrResponse {
   /// The name of the JSON attribute containing the id of the request for which
   /// this is a response.
-  static const String ID = 'id';
+  static const String idAttributeName = 'id';
 
   /// The name of the JSON attribute containing the error message.
-  static const String ERROR = 'error';
+  static const String errorAttributeName = 'error';
 
   /// The name of the JSON attribute containing the result values.
-  static const String RESULT = 'result';
+  static const String resultAttributeName = 'result';
 
   /// The unique identifier used to identify the request that this response is
   /// associated with.
@@ -315,7 +303,7 @@
   Map<String, Object?>? result;
 
   /// Initialize a newly created instance to represent a response to a request
-  /// with the given [id].  If [_result] is provided, it will be used as the
+  /// with the given [id].  If [result] is provided, it will be used as the
   /// result; otherwise an empty result will be used.  If an [error] is provided
   /// then the response will represent an error condition.
   Response(this.id, {this.result, this.error});
@@ -615,30 +603,22 @@
 
   /// Return a table representing the structure of the Json object that will be
   /// sent to the client to represent this response.
-  Map<String, Object> toJson() {
-    var jsonObject = <String, Object>{};
-    jsonObject[ID] = id;
-    var error = this.error;
-    if (error != null) {
-      jsonObject[ERROR] = error.toJson(clientUriConverter: null);
-    }
-    var result = this.result;
-    if (result != null) {
-      jsonObject[RESULT] = result;
-    }
-    return jsonObject;
-  }
+  Map<String, Object> toJson() => {
+    idAttributeName: id,
+    errorAttributeName: ?error?.toJson(clientUriConverter: null),
+    resultAttributeName: ?result,
+  };
 
   /// Initialize a newly created instance based on the given JSON data.
   static Response? fromJson(Map<String, Object?> json) {
     try {
-      var id = json[Response.ID];
+      var id = json[Response.idAttributeName];
       if (id is! String) {
         return null;
       }
 
       RequestError? decodedError;
-      var error = json[Response.ERROR];
+      var error = json[Response.errorAttributeName];
       if (error is Map) {
         decodedError = RequestError.fromJson(
           ResponseDecoder(null),
@@ -649,7 +629,7 @@
       }
 
       Map<String, Object?>? decodedResult;
-      var result = json[Response.RESULT];
+      var result = json[Response.resultAttributeName];
       if (result is Map<String, Object?>) {
         decodedResult = result;
       }
diff --git a/pkg/analysis_server/lib/src/channel/byte_stream_channel.dart b/pkg/analysis_server/lib/src/channel/byte_stream_channel.dart
index 9c96d86..5225290 100644
--- a/pkg/analysis_server/lib/src/channel/byte_stream_channel.dart
+++ b/pkg/analysis_server/lib/src/channel/byte_stream_channel.dart
@@ -33,13 +33,13 @@
         .cast<Map<String, Object?>>()
         .asBroadcastStream();
     var responseStream = jsonStream
-        .where((json) => json[Notification.EVENT] == null)
+        .where((json) => json[Notification.eventAttributeName] == null)
         .transform(ResponseConverter())
         .where((response) => response != null)
         .cast<Response>()
         .asBroadcastStream();
     var notificationStream = jsonStream
-        .where((json) => json[Notification.EVENT] != null)
+        .where((json) => json[Notification.eventAttributeName] != null)
         .transform(NotificationConverter())
         .asBroadcastStream();
     return ByteStreamClientChannel._(
diff --git a/pkg/analysis_server/test/channel/byte_stream_channel_test.dart b/pkg/analysis_server/test/channel/byte_stream_channel_test.dart
index 3c378cb..478fb1a 100644
--- a/pkg/analysis_server/test/channel/byte_stream_channel_test.dart
+++ b/pkg/analysis_server/test/channel/byte_stream_channel_test.dart
@@ -87,8 +87,8 @@
     var assertCount = 0;
     var request = Request('72', 'foo.bar');
     outputLineStream.first.then((line) => json.decode(line)).then((json) {
-      expect(json[Request.ID], equals('72'));
-      expect(json[Request.METHOD], equals('foo.bar'));
+      expect(json[Request.idAttributeName], equals('72'));
+      expect(json[Request.methodAttributeName], equals('foo.bar'));
       inputSink.writeln('{"id":"73"}');
       inputSink.writeln('{"id":"72"}');
       assertCount++;
diff --git a/pkg/analysis_server/test/domain_server_test.dart b/pkg/analysis_server/test/domain_server_test.dart
index 7f2d470..510a350 100644
--- a/pkg/analysis_server/test/domain_server_test.dart
+++ b/pkg/analysis_server/test/domain_server_test.dart
@@ -76,8 +76,8 @@
     expect(
       response.toJson(),
       equals({
-        Response.ID: '0',
-        Response.RESULT: {VERSION: PROTOCOL_VERSION},
+        Response.idAttributeName: '0',
+        Response.resultAttributeName: {VERSION: PROTOCOL_VERSION},
       }),
     );
   }
diff --git a/pkg/analysis_server/test/protocol_test.dart b/pkg/analysis_server/test/protocol_test.dart
index 6102143..5eaa4ed 100644
--- a/pkg/analysis_server/test/protocol_test.dart
+++ b/pkg/analysis_server/test/protocol_test.dart
@@ -136,7 +136,7 @@
     var original = Request('one', 'aMethod', null, 347);
     var map = original.toJson();
     // Insert bad value - should be int but client sent string instead
-    map[Request.CLIENT_REQUEST_TIME] = '347';
+    map[Request.clientRequestTimeAttributeName] = '347';
     var jsonData = json.encode(map);
     var request = Request.fromString(jsonData);
     expect(request, isNull);
@@ -164,7 +164,10 @@
     var request = Request('one', 'aMethod');
     expect(
       request.toJson(),
-      equals({Request.ID: 'one', Request.METHOD: 'aMethod'}),
+      equals({
+        Request.idAttributeName: 'one',
+        Request.methodAttributeName: 'aMethod',
+      }),
     );
   }
 
@@ -173,9 +176,9 @@
     expect(
       request.toJson(),
       equals({
-        Request.ID: 'one',
-        Request.METHOD: 'aMethod',
-        Request.PARAMS: {'foo': 'bar'},
+        Request.idAttributeName: 'one',
+        Request.methodAttributeName: 'aMethod',
+        Request.paramsAttributeName: {'foo': 'bar'},
       }),
     );
   }
@@ -190,8 +193,8 @@
     expect(
       response.toJson(),
       equals({
-        Response.ID: '',
-        Response.ERROR: {
+        Response.idAttributeName: '',
+        Response.errorAttributeName: {
           'code': 'INVALID_REQUEST',
           'message': 'Invalid request',
         },
@@ -206,8 +209,8 @@
     expect(
       response.toJson(),
       equals({
-        Response.ID: '0',
-        Response.ERROR: {
+        Response.idAttributeName: '0',
+        Response.errorAttributeName: {
           'code': 'UNKNOWN_REQUEST',
           'message': 'Unknown request',
         },