Skip to content

Commit

Permalink
[pigeon] Fix missed casting of not nullable Dart int to Kotlin long
Browse files Browse the repository at this point in the history
  • Loading branch information
ycherniavskyi committed Dec 25, 2022
1 parent 70205c5 commit c9ba455
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.2.16

* [kotlin] Fixes a missed casting of not nullable Dart 'int' to Kotlin 64bit long.

## 4.2.15

* Relocates generator classes. (Reverted)
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 @@ -9,7 +9,7 @@ import 'dart:mirrors';
import 'ast.dart';

/// The current version of pigeon. This must match the version in pubspec.yaml.
const String pigeonVersion = '4.2.15';
const String pigeonVersion = '4.2.16';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
4 changes: 4 additions & 0 deletions packages/pigeon/lib/kotlin_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,10 @@ void generateKotlin(KotlinOptions options, Root root, StringSink sink) {
rootEnumNameSet.contains(field.type.baseName)) {
indent.write(
'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 }');
} else {
indent.writeln('val ${field.name} = $listValue as $fieldType');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: pigeon
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3Apigeon
version: 4.2.15 # This must match the version in lib/generator_tools.dart
version: 4.2.16 # This must match the version in lib/generator_tools.dart

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
87 changes: 86 additions & 1 deletion packages/pigeon/test/kotlin_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void main() {
'''));
});

test('all the simple datatypes header', () {
test('all the simple nullable datatypes header', () {
final Root root = Root(apis: <Api>[], classes: <Class>[
Class(name: 'Foobar', fields: <NamedType>[
NamedType(
Expand Down Expand Up @@ -223,6 +223,91 @@ void main() {
expect(code, contains('val aInt32List: IntArray? = null'));
expect(code, contains('val aInt64List: LongArray? = null'));
expect(code, contains('val aFloat64List: DoubleArray? = null'));
expect(
code,
contains(
'val aInt = list[1].let { if (it is Int) it.toLong() else it as? Long }'));
});

test('all the simple not nullable datatypes header', () {
final Root root = Root(apis: <Api>[], classes: <Class>[
Class(name: 'Foobar', fields: <NamedType>[
NamedType(
type: const TypeDeclaration(
baseName: 'bool',
isNullable: false,
),
name: 'aBool',
),
NamedType(
type: const TypeDeclaration(
baseName: 'int',
isNullable: false,
),
name: 'aInt',
),
NamedType(
type: const TypeDeclaration(
baseName: 'double',
isNullable: false,
),
name: 'aDouble',
),
NamedType(
type: const TypeDeclaration(
baseName: 'String',
isNullable: false,
),
name: 'aString',
),
NamedType(
type: const TypeDeclaration(
baseName: 'Uint8List',
isNullable: true,
),
name: 'aUint8List',
),
NamedType(
type: const TypeDeclaration(
baseName: 'Int32List',
isNullable: false,
),
name: 'aInt32List',
),
NamedType(
type: const TypeDeclaration(
baseName: 'Int64List',
isNullable: false,
),
name: 'aInt64List',
),
NamedType(
type: const TypeDeclaration(
baseName: 'Float64List',
isNullable: false,
),
name: 'aFloat64List',
),
]),
], enums: <Enum>[]);

final StringBuffer sink = StringBuffer();

const KotlinOptions kotlinOptions = KotlinOptions();
generateKotlin(kotlinOptions, root, sink);
final String code = sink.toString();
expect(code, contains('val aBool: Boolean'));
expect(code, contains('val aInt: Long'));
expect(code, contains('val aDouble: Double'));
expect(code, contains('val aString: String'));
expect(code, contains('val aUint8List: ByteArray'));
expect(code, contains('val aInt32List: IntArray'));
expect(code, contains('val aInt64List: LongArray'));
expect(code, contains('val aFloat64List: DoubleArray'));
expect(
code,
contains(
'val aInt = list[1].let { if (it is Int) it.toLong() else it as Long }'));
});

test('gen one flutter api', () {
Expand Down

0 comments on commit c9ba455

Please sign in to comment.