Skip to content

Commit

Permalink
[pigeon] finish kotlin and swift safe cast removal (flutter#3360)
Browse files Browse the repository at this point in the history
[pigeon] finish kotlin and swift safe cast removal
  • Loading branch information
tarrinneal authored Mar 10, 2023
1 parent 79fbb7a commit cc913d5
Show file tree
Hide file tree
Showing 30 changed files with 293 additions and 291 deletions.
7 changes: 6 additions & 1 deletion packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 9.0.6

* [kotlin] Removes safe casting from decode process.
* [swift] Removes safe casting from decode process.

## 9.0.5

* Removes the unnecessary Flutter constraint.
Expand Down Expand Up @@ -688,7 +693,7 @@ class Foo {

## 0.1.0-experimental.3

* Added support for for Android Java.
* Added support for Android Java.

## 0.1.0-experimental.2

Expand Down
12 changes: 6 additions & 6 deletions packages/pigeon/lib/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Method extends Node {
/// Specifies how handlers are dispatched with respect to threading.
TaskQueueType taskQueueType;

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

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

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

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

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

/// List of documentation comments, seperated by line.
/// List of documentation comments, separated by line.
///
/// Lines should not include the comment marker itself, but should include any
/// leading whitespace, so that any indentation in the original comment is preserved.
Expand Down
4 changes: 2 additions & 2 deletions packages/pigeon/lib/cpp_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ const flutter::StandardMessageCodec& ${api.name}::GetCodec() {
final HostDatatype returnType = getHostDatatype(func.returnType,
root.classes, root.enums, _shortBaseCppTypeForBuiltinDartType);

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

/// Returns the C++ type to use for the paramer to the asyncronous "return"
/// Returns the C++ type to use for the paramer to the asynchronous "return"
/// callback of a Flutter API method returning [type].
String _flutterApiReturnType(HostDatatype type) {
if (type.datatype == 'void') {
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ $resultAt != null
///
/// Messages will be sent and received in a list.
///
/// If the message recieved was succesful,
/// If the message received was successful,
/// the result will be contained at the 0'th index.
///
/// If the message was a failure, the list will contain 3 items:
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'ast.dart';
/// The current version of pigeon.
///
/// This must match the version in pubspec.yaml.
const String pigeonVersion = '9.0.5';
const String pigeonVersion = '9.0.6';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
43 changes: 20 additions & 23 deletions packages/pigeon/lib/kotlin_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,10 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
});
} else if (isInt) {
indent.write('val ${field.name} = $listValue');
indent.addln(
'.let { if (it is Int) it.toLong() else it as Long? }');
indent.addln('.let { ${_cast(listValue, type: field.type)} }');
} else {
indent.writeln(
'val ${field.name} = ${_cast(listValue, kotlinType: '$fieldType?')}');
'val ${field.name} = ${_cast(listValue, type: field.type)}');
}
} else {
if (!hostDatatype.isBuiltin &&
Expand All @@ -259,11 +258,10 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
'val ${field.name} = $fieldType.ofRaw($listValue as Int)!!');
} else if (isInt) {
indent.write('val ${field.name} = $listValue');
indent
.addln('.let { if (it is Int) it.toLong() else it as Long }');
indent.addln('.let { ${_cast(listValue, type: field.type)} }');
} else {
indent.writeln(
'val ${field.name} = ${_cast(listValue, kotlinType: fieldType)}');
'val ${field.name} = ${_cast(listValue, type: field.type)}');
}
}
});
Expand Down Expand Up @@ -384,15 +382,8 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
});
} else {
indent.addScoped('{', '}', () {
if (func.returnType.baseName == 'int') {
final String forceUnwrap =
func.returnType.isNullable ? '?' : '';
indent.writeln(
'val result = if (it is Int) it.toLong() else it as$forceUnwrap Long');
} else {
indent.writeln(
'val result = ${_cast('it', kotlinType: returnType, safeCast: func.returnType.isNullable)}');
}
indent.writeln(
'val result = ${_cast('it', type: func.returnType)}');
indent.writeln('callback(result)');
});
}
Expand Down Expand Up @@ -678,11 +669,9 @@ String _castForceUnwrap(String value, TypeDeclaration type, Root root) {
// a Dart 'int'. To keep things simple we just use 64bit
// longs in Pigeon with Kotlin.
if (type.baseName == 'int') {
final String castUnwrap = type.isNullable ? '?' : '';
return '$value.let { if (it is Int) it.toLong() else it as$castUnwrap Long }';
return '$value.let { ${_cast(value, type: type)} }';
} else {
return _cast(value,
kotlinType: _kotlinTypeForDartType(type), safeCast: type.isNullable);
return _cast(value, type: type);
}
}
}
Expand Down Expand Up @@ -748,11 +737,19 @@ String _nullsafeKotlinTypeForDartType(TypeDeclaration type) {
}

/// Returns an expression to cast [variable] to [kotlinType].
String _cast(String variable,
{required String kotlinType, bool safeCast = false}) {
String _cast(String variable, {required TypeDeclaration type}) {
// Special-case Any, since no-op casts cause warnings.
if (kotlinType == 'Any?' || (safeCast && kotlinType == 'Any')) {
final String typeString = _kotlinTypeForDartType(type);
if (type.isNullable && typeString == 'Any') {
return variable;
}
return '$variable as${safeCast ? '?' : ''} $kotlinType';
if (typeString == 'Int' || typeString == 'Long') {
return _castInt(type.isNullable);
}
return '$variable as ${_nullsafeKotlinTypeForDartType(type)}';
}

String _castInt(bool isNullable) {
final String nullability = isNullable ? '?' : '';
return 'if (it is Int) it.toLong() else it as Long$nullability';
}
2 changes: 1 addition & 1 deletion packages/pigeon/lib/objc_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ String _className(String? prefix, String className) {
}
}

/// Calculates callback block signature for for async methods.
/// Calculates callback block signature for async methods.
String _callbackForType(TypeDeclaration type, _ObjcPtr objcType) {
return type.isVoid
? 'void (^)(FlutterError *_Nullable)'
Expand Down
10 changes: 5 additions & 5 deletions packages/pigeon/lib/swift_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ import FlutterMacOS
indent.addScoped('{ $messageVarName, reply in', '}', () {
final List<String> methodArgument = <String>[];
if (components.arguments.isNotEmpty) {
indent.writeln('let args = message as! [Any?]');
indent.writeln('let args = message as! [Any]');
enumerate(components.arguments,
(int index, _SwiftFunctionArgument arg) {
final String argName =
Expand Down Expand Up @@ -668,17 +668,17 @@ String _camelCase(String text) {
}

String _castForceUnwrap(String value, TypeDeclaration type, Root root) {
final String forceUnwrap = type.isNullable ? '' : '!';
if (isEnum(root, type)) {
final String forceUnwrap = type.isNullable ? '' : '!';
final String nullableConditionPrefix =
type.isNullable ? '$value == nil ? nil : ' : '';
return '$nullableConditionPrefix${_swiftTypeForDartType(type)}(rawValue: $value as! Int)$forceUnwrap';
} else if (type.baseName == 'Object') {
// Special-cased to avoid warnings about using 'as' with Any.
return type.isNullable ? value : '$value!';
return value;
} else {
final String castUnwrap = type.isNullable ? '?' : '!';
return '$value as$castUnwrap ${_swiftTypeForDartType(type)}';
final String castUnwrap = type.isNullable ? '?' : '';
return '$value as! ${_swiftTypeForDartType(type)}$castUnwrap';
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/mock_handler_tester/test/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v9.0.5), do not edit directly.
// Autogenerated from Pigeon (v9.0.6), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// 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

Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/mock_handler_tester/test/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v9.0.5), do not edit directly.
// Autogenerated from Pigeon (v9.0.6), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, unnecessary_import
// ignore_for_file: avoid_relative_lib_imports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v9.0.5), do not edit directly.
// Autogenerated from Pigeon (v9.0.6), do not edit directly.
// See also: https://pub.dev/packages/pigeon

package com.example.alternate_language_test_plugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v9.0.5), do not edit directly.
// Autogenerated from Pigeon (v9.0.6), do not edit directly.
// See also: https://pub.dev/packages/pigeon

#import <Foundation/Foundation.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v9.0.5), do not edit directly.
// Autogenerated from Pigeon (v9.0.6), do not edit directly.
// See also: https://pub.dev/packages/pigeon

#import "CoreTests.gen.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v9.0.5), do not edit directly.
// Autogenerated from Pigeon (v9.0.6), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v9.0.5), do not edit directly.
// Autogenerated from Pigeon (v9.0.6), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v9.0.5), do not edit directly.
// Autogenerated from Pigeon (v9.0.6), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v9.0.5), do not edit directly.
// Autogenerated from Pigeon (v9.0.6), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v9.0.5), do not edit directly.
// Autogenerated from Pigeon (v9.0.6), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v9.0.5), do not edit directly.
// Autogenerated from Pigeon (v9.0.6), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v9.0.5), do not edit directly.
// Autogenerated from Pigeon (v9.0.6), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ void runPigeonIntegrationTests(TargetGenerator targetGenerator) {
(WidgetTester _) async {
final HostIntegrationCoreApi api = HostIntegrationCoreApi();

const String sentObject = 'Hello, asyncronously!';
const String sentObject = 'Hello, asynchronously!';

final String echoObject = await api.echoAsyncString(sentObject);
expect(echoObject, sentObject);
Expand Down Expand Up @@ -840,7 +840,7 @@ void runPigeonIntegrationTests(TargetGenerator targetGenerator) {
(WidgetTester _) async {
final HostIntegrationCoreApi api = HostIntegrationCoreApi();

const String sentObject = 'Hello, asyncronously!';
const String sentObject = 'Hello, asynchronously!';

final String? echoObject = await api.echoAsyncNullableString(sentObject);
expect(echoObject, sentObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v9.0.5), do not edit directly.
// Autogenerated from Pigeon (v9.0.6), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// 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

Expand Down
Loading

0 comments on commit cc913d5

Please sign in to comment.