Skip to content

Commit cc913d5

Browse files
authored
[pigeon] finish kotlin and swift safe cast removal (#3360)
[pigeon] finish kotlin and swift safe cast removal
1 parent 79fbb7a commit cc913d5

File tree

30 files changed

+293
-291
lines changed

30 files changed

+293
-291
lines changed

packages/pigeon/CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 9.0.6
2+
3+
* [kotlin] Removes safe casting from decode process.
4+
* [swift] Removes safe casting from decode process.
5+
16
## 9.0.5
27

38
* Removes the unnecessary Flutter constraint.
@@ -688,7 +693,7 @@ class Foo {
688693

689694
## 0.1.0-experimental.3
690695

691-
* Added support for for Android Java.
696+
* Added support for Android Java.
692697

693698
## 0.1.0-experimental.2
694699

packages/pigeon/lib/ast.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Method extends Node {
6161
/// Specifies how handlers are dispatched with respect to threading.
6262
TaskQueueType taskQueueType;
6363

64-
/// List of documentation comments, seperated by line.
64+
/// List of documentation comments, separated by line.
6565
///
6666
/// Lines should not include the comment marker itself, but should include any
6767
/// leading whitespace, so that any indentation in the original comment is preserved.
@@ -101,7 +101,7 @@ class Api extends Node {
101101
/// The name of the Dart test interface to generate to help with testing.
102102
String? dartHostTestHandler;
103103

104-
/// List of documentation comments, seperated by line.
104+
/// List of documentation comments, separated by line.
105105
///
106106
/// Lines should not include the comment marker itself, but should include any
107107
/// leading whitespace, so that any indentation in the original comment is preserved.
@@ -194,7 +194,7 @@ class NamedType extends Node {
194194
/// The offset in the source file where the [NamedType] appears.
195195
int? offset;
196196

197-
/// List of documentation comments, seperated by line.
197+
/// List of documentation comments, separated by line.
198198
///
199199
/// Lines should not include the comment marker itself, but should include any
200200
/// leading whitespace, so that any indentation in the original comment is preserved.
@@ -222,7 +222,7 @@ class Class extends Node {
222222
/// All the fields contained in the class.
223223
List<NamedType> fields;
224224

225-
/// List of documentation comments, seperated by line.
225+
/// List of documentation comments, separated by line.
226226
///
227227
/// Lines should not include the comment marker itself, but should include any
228228
/// leading whitespace, so that any indentation in the original comment is preserved.
@@ -250,7 +250,7 @@ class Enum extends Node {
250250
/// All of the members of the enum.
251251
List<EnumMember> members;
252252

253-
/// List of documentation comments, seperated by line.
253+
/// List of documentation comments, separated by line.
254254
///
255255
/// Lines should not include the comment marker itself, but should include any
256256
/// leading whitespace, so that any indentation in the original comment is preserved.
@@ -274,7 +274,7 @@ class EnumMember extends Node {
274274
/// The name of the enum member.
275275
final String name;
276276

277-
/// List of documentation comments, seperated by line.
277+
/// List of documentation comments, separated by line.
278278
///
279279
/// Lines should not include the comment marker itself, but should include any
280280
/// leading whitespace, so that any indentation in the original comment is preserved.

packages/pigeon/lib/cpp_generator.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ const flutter::StandardMessageCodec& ${api.name}::GetCodec() {
656656
final HostDatatype returnType = getHostDatatype(func.returnType,
657657
root.classes, root.enums, _shortBaseCppTypeForBuiltinDartType);
658658

659-
// Determine the input paramater list, saved in a structured form for later
659+
// Determine the input parameter list, saved in a structured form for later
660660
// use as platform channel call arguments.
661661
final Iterable<_HostNamedType> hostParameters =
662662
indexMap(func.arguments, (int i, NamedType arg) {
@@ -1265,7 +1265,7 @@ String _hostApiReturnType(HostDatatype type) {
12651265
return 'ErrorOr<$valueType>';
12661266
}
12671267

1268-
/// Returns the C++ type to use for the paramer to the asyncronous "return"
1268+
/// Returns the C++ type to use for the paramer to the asynchronous "return"
12691269
/// callback of a Flutter API method returning [type].
12701270
String _flutterApiReturnType(HostDatatype type) {
12711271
if (type.datatype == 'void') {

packages/pigeon/lib/dart_generator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ $resultAt != null
431431
///
432432
/// Messages will be sent and received in a list.
433433
///
434-
/// If the message recieved was succesful,
434+
/// If the message received was successful,
435435
/// the result will be contained at the 0'th index.
436436
///
437437
/// If the message was a failure, the list will contain 3 items:

packages/pigeon/lib/generator_tools.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'ast.dart';
1111
/// The current version of pigeon.
1212
///
1313
/// This must match the version in pubspec.yaml.
14-
const String pigeonVersion = '9.0.5';
14+
const String pigeonVersion = '9.0.6';
1515

1616
/// Read all the content from [stdin] to a String.
1717
String readStdin() {

packages/pigeon/lib/kotlin_generator.dart

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,10 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
242242
});
243243
} else if (isInt) {
244244
indent.write('val ${field.name} = $listValue');
245-
indent.addln(
246-
'.let { if (it is Int) it.toLong() else it as Long? }');
245+
indent.addln('.let { ${_cast(listValue, type: field.type)} }');
247246
} else {
248247
indent.writeln(
249-
'val ${field.name} = ${_cast(listValue, kotlinType: '$fieldType?')}');
248+
'val ${field.name} = ${_cast(listValue, type: field.type)}');
250249
}
251250
} else {
252251
if (!hostDatatype.isBuiltin &&
@@ -259,11 +258,10 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
259258
'val ${field.name} = $fieldType.ofRaw($listValue as Int)!!');
260259
} else if (isInt) {
261260
indent.write('val ${field.name} = $listValue');
262-
indent
263-
.addln('.let { if (it is Int) it.toLong() else it as Long }');
261+
indent.addln('.let { ${_cast(listValue, type: field.type)} }');
264262
} else {
265263
indent.writeln(
266-
'val ${field.name} = ${_cast(listValue, kotlinType: fieldType)}');
264+
'val ${field.name} = ${_cast(listValue, type: field.type)}');
267265
}
268266
}
269267
});
@@ -384,15 +382,8 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
384382
});
385383
} else {
386384
indent.addScoped('{', '}', () {
387-
if (func.returnType.baseName == 'int') {
388-
final String forceUnwrap =
389-
func.returnType.isNullable ? '?' : '';
390-
indent.writeln(
391-
'val result = if (it is Int) it.toLong() else it as$forceUnwrap Long');
392-
} else {
393-
indent.writeln(
394-
'val result = ${_cast('it', kotlinType: returnType, safeCast: func.returnType.isNullable)}');
395-
}
385+
indent.writeln(
386+
'val result = ${_cast('it', type: func.returnType)}');
396387
indent.writeln('callback(result)');
397388
});
398389
}
@@ -678,11 +669,9 @@ String _castForceUnwrap(String value, TypeDeclaration type, Root root) {
678669
// a Dart 'int'. To keep things simple we just use 64bit
679670
// longs in Pigeon with Kotlin.
680671
if (type.baseName == 'int') {
681-
final String castUnwrap = type.isNullable ? '?' : '';
682-
return '$value.let { if (it is Int) it.toLong() else it as$castUnwrap Long }';
672+
return '$value.let { ${_cast(value, type: type)} }';
683673
} else {
684-
return _cast(value,
685-
kotlinType: _kotlinTypeForDartType(type), safeCast: type.isNullable);
674+
return _cast(value, type: type);
686675
}
687676
}
688677
}
@@ -748,11 +737,19 @@ String _nullsafeKotlinTypeForDartType(TypeDeclaration type) {
748737
}
749738

750739
/// Returns an expression to cast [variable] to [kotlinType].
751-
String _cast(String variable,
752-
{required String kotlinType, bool safeCast = false}) {
740+
String _cast(String variable, {required TypeDeclaration type}) {
753741
// Special-case Any, since no-op casts cause warnings.
754-
if (kotlinType == 'Any?' || (safeCast && kotlinType == 'Any')) {
742+
final String typeString = _kotlinTypeForDartType(type);
743+
if (type.isNullable && typeString == 'Any') {
755744
return variable;
756745
}
757-
return '$variable as${safeCast ? '?' : ''} $kotlinType';
746+
if (typeString == 'Int' || typeString == 'Long') {
747+
return _castInt(type.isNullable);
748+
}
749+
return '$variable as ${_nullsafeKotlinTypeForDartType(type)}';
750+
}
751+
752+
String _castInt(bool isNullable) {
753+
final String nullability = isNullable ? '?' : '';
754+
return 'if (it is Int) it.toLong() else it as Long$nullability';
758755
}

packages/pigeon/lib/objc_generator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ String _className(String? prefix, String className) {
893893
}
894894
}
895895

896-
/// Calculates callback block signature for for async methods.
896+
/// Calculates callback block signature for async methods.
897897
String _callbackForType(TypeDeclaration type, _ObjcPtr objcType) {
898898
return type.isVoid
899899
? 'void (^)(FlutterError *_Nullable)'

packages/pigeon/lib/swift_generator.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ import FlutterMacOS
455455
indent.addScoped('{ $messageVarName, reply in', '}', () {
456456
final List<String> methodArgument = <String>[];
457457
if (components.arguments.isNotEmpty) {
458-
indent.writeln('let args = message as! [Any?]');
458+
indent.writeln('let args = message as! [Any]');
459459
enumerate(components.arguments,
460460
(int index, _SwiftFunctionArgument arg) {
461461
final String argName =
@@ -668,17 +668,17 @@ String _camelCase(String text) {
668668
}
669669

670670
String _castForceUnwrap(String value, TypeDeclaration type, Root root) {
671+
final String forceUnwrap = type.isNullable ? '' : '!';
671672
if (isEnum(root, type)) {
672-
final String forceUnwrap = type.isNullable ? '' : '!';
673673
final String nullableConditionPrefix =
674674
type.isNullable ? '$value == nil ? nil : ' : '';
675675
return '$nullableConditionPrefix${_swiftTypeForDartType(type)}(rawValue: $value as! Int)$forceUnwrap';
676676
} else if (type.baseName == 'Object') {
677677
// Special-cased to avoid warnings about using 'as' with Any.
678-
return type.isNullable ? value : '$value!';
678+
return value;
679679
} else {
680-
final String castUnwrap = type.isNullable ? '?' : '!';
681-
return '$value as$castUnwrap ${_swiftTypeForDartType(type)}';
680+
final String castUnwrap = type.isNullable ? '?' : '';
681+
return '$value as! ${_swiftTypeForDartType(type)}$castUnwrap';
682682
}
683683
}
684684

packages/pigeon/mock_handler_tester/test/message.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44
//
5-
// Autogenerated from Pigeon (v9.0.5), do not edit directly.
5+
// Autogenerated from Pigeon (v9.0.6), do not edit directly.
66
// See also: https://pub.dev/packages/pigeon
77
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import
88

packages/pigeon/mock_handler_tester/test/test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44
//
5-
// Autogenerated from Pigeon (v9.0.5), do not edit directly.
5+
// Autogenerated from Pigeon (v9.0.6), do not edit directly.
66
// See also: https://pub.dev/packages/pigeon
77
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, unnecessary_import
88
// ignore_for_file: avoid_relative_lib_imports

0 commit comments

Comments
 (0)