Skip to content

Commit ad6b1eb

Browse files
committed
Support for 'double' fields in protocol.
[email protected] Change-Id: If8dacd4578e01380f4d9e4ce1f809b9485bffc7f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/108462 Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 5479175 commit ad6b1eb

File tree

6 files changed

+52
-2
lines changed

6 files changed

+52
-2
lines changed

pkg/analysis_server/test/integration/support/integration_tests.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import 'protocol_matchers.dart';
1818

1919
const Matcher isBool = const TypeMatcher<bool>();
2020

21+
const Matcher isDouble = const TypeMatcher<double>();
22+
2123
const Matcher isInt = const TypeMatcher<int>();
2224

2325
const Matcher isNotification = const MatchesJsonObject(

pkg/analysis_server/tool/spec/codegen_dart_protocol.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,8 @@ class CodegenProtocolVisitor extends DartCodegenVisitor with CodeGenerator {
10421042
return new FromJsonFunction('jsonDecoder.decodeString');
10431043
case 'bool':
10441044
return new FromJsonFunction('jsonDecoder.decodeBool');
1045+
case 'double':
1046+
return new FromJsonFunction('jsonDecoder.decodeDouble');
10451047
case 'int':
10461048
case 'long':
10471049
return new FromJsonFunction('jsonDecoder.decodeInt');

pkg/analysis_server/tool/spec/codegen_java.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class CodegenJavaVisitor extends HierarchicalApiVisitor with CodeGenerator {
5050
*/
5151
static const Map<String, String> _typeRenames = const {
5252
'bool': 'boolean',
53+
'double': 'double',
5354
'int': 'int',
5455
'AvailableSuggestionRelevanceTag': 'String',
5556
'ExecutionContextId': 'String',
@@ -126,7 +127,10 @@ class CodegenJavaVisitor extends HierarchicalApiVisitor with CodeGenerator {
126127
bool isPrimitive(TypeDecl type) {
127128
if (type is TypeReference) {
128129
String typeStr = javaType(type);
129-
return typeStr == 'boolean' || typeStr == 'int' || typeStr == 'long';
130+
return typeStr == 'boolean' ||
131+
typeStr == 'double' ||
132+
typeStr == 'int' ||
133+
typeStr == 'long';
130134
}
131135
return false;
132136
}
@@ -167,6 +171,8 @@ class CodegenJavaVisitor extends HierarchicalApiVisitor with CodeGenerator {
167171
if (optional) {
168172
if (typeName == 'boolean') {
169173
typeName = 'Boolean';
174+
} else if (typeName == 'double') {
175+
typeName = 'Double';
170176
} else if (typeName == 'int') {
171177
typeName = 'Integer';
172178
}

pkg/analysis_server/tool/spec/codegen_java_types.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ class CodegenJavaType extends CodegenJavaVisitor {
140140
return 'getAsString';
141141
} else if (name == 'boolean' || name == 'Boolean') {
142142
return 'getAsBoolean';
143+
} else if (name == 'double' || name == 'Double') {
144+
return 'getAsDouble';
143145
} else if (name == 'int' || name == 'Integer') {
144146
return 'getAsInt';
145147
} else if (name == 'long' || name == 'Long') {

pkg/analysis_server_client/lib/src/protocol/protocol_internal.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import 'dart:collection';
66
import 'dart:convert' hide JsonDecoder;
77

88
import 'package:analysis_server_client/src/protocol/protocol_base.dart';
9-
import 'package:analysis_server_client/src/protocol/protocol_generated.dart';
109
import 'package:analysis_server_client/src/protocol/protocol_common.dart';
10+
import 'package:analysis_server_client/src/protocol/protocol_generated.dart';
1111

1212
final Map<String, RefactoringKind> REQUEST_ID_REFACTORING_KINDS =
1313
new HashMap<String, RefactoringKind>();
@@ -276,6 +276,25 @@ abstract class JsonDecoder {
276276
throw mismatch(jsonPath, 'bool', json);
277277
}
278278

279+
/**
280+
* Decode a JSON object that is expected to be a double. A string
281+
* representation of a double is also accepted.
282+
*/
283+
double decodeDouble(String jsonPath, Object json) {
284+
if (json is double) {
285+
return json;
286+
} else if (json is int) {
287+
return json.toDouble();
288+
} else if (json is String) {
289+
double value = double.tryParse(json);
290+
if (value == null) {
291+
throw mismatch(jsonPath, 'double', json);
292+
}
293+
return value;
294+
}
295+
throw mismatch(jsonPath, 'double', json);
296+
}
297+
279298
/**
280299
* Decode a JSON object that is expected to be an integer. A string
281300
* representation of an integer is also accepted.

pkg/analyzer_plugin/lib/src/protocol/protocol_internal.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,25 @@ abstract class JsonDecoder {
269269
throw mismatch(jsonPath, 'bool', json);
270270
}
271271

272+
/**
273+
* Decode a JSON object that is expected to be a double. A string
274+
* representation of a double is also accepted.
275+
*/
276+
double decodeDouble(String jsonPath, Object json) {
277+
if (json is double) {
278+
return json;
279+
} else if (json is int) {
280+
return json.toDouble();
281+
} else if (json is String) {
282+
double value = double.tryParse(json);
283+
if (value == null) {
284+
throw mismatch(jsonPath, 'double', json);
285+
}
286+
return value;
287+
}
288+
throw mismatch(jsonPath, 'double', json);
289+
}
290+
272291
/**
273292
* Decode a JSON object that is expected to be an integer. A string
274293
* representation of an integer is also accepted.

0 commit comments

Comments
 (0)