Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.2

* Adds non-null object fields.

## 2.0.1

* Adds support for TaskQueues for serial background execution.
Expand Down
42 changes: 31 additions & 11 deletions packages/pigeon/lib/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -471,16 +471,26 @@ void generateDart(DartOptions opt, Root root, StringSink sink) {
);
for (final NamedType field in klass.fields) {
indent.write('pigeonMap[\'${field.name}\'] = ');
if (customClassNames.contains(field.type.baseName)) {
indent.addln(
'${field.name} == null ? null : ${field.name}$unwrapOperator.encode();',
);
} else if (customEnumNames.contains(field.type.baseName)) {
indent.addln(
'${field.name} == null ? null : ${field.name}$unwrapOperator.index;',
);
if (opt.isNullSafe) {
final String nullAwareOperator = field.type.isNullable ? '?' : '';
if (customClassNames.contains(field.type.baseName)) {
indent.addln('${field.name}$nullAwareOperator.encode();');
} else if (customEnumNames.contains(field.type.baseName)) {
indent.addln('${field.name}$nullAwareOperator.index;');
} else {
indent.addln('${field.name};');
}
} else {
indent.addln('${field.name};');
if (customClassNames.contains(field.type.baseName)) {
indent.addln(
'${field.name} == null ? null : ${field.name}.encode();');
} else if (customEnumNames.contains(field.type.baseName)) {
indent.addln(
'${field.name} == null ? null : ${field.name}.index;',
);
} else {
indent.addln('${field.name};');
}
}
}
indent.writeln('return pigeonMap;');
Expand All @@ -490,15 +500,25 @@ void generateDart(DartOptions opt, Root root, StringSink sink) {
void writeDecode() {
void writeValueDecode(NamedType field) {
if (customClassNames.contains(field.type.baseName)) {
indent.format('''
if (field.type.isNullable) {
indent.format('''
pigeonMap['${field.name}'] != null
\t\t? ${field.type.baseName}.decode(pigeonMap['${field.name}']$unwrapOperator)
\t\t: null''', leadingSpace: false, trailingNewline: false);
} else {
indent.add(
'${field.type.baseName}.decode(pigeonMap[\'${field.name}\']$unwrapOperator)');
}
} else if (customEnumNames.contains(field.type.baseName)) {
indent.format('''
if (field.type.isNullable) {
indent.format('''
pigeonMap['${field.name}'] != null
\t\t? ${field.type.baseName}.values[pigeonMap['${field.name}']$unwrapOperator as int]
\t\t: null''', leadingSpace: false, trailingNewline: false);
} else {
indent.add(
'${field.type.baseName}.values[pigeonMap[\'${field.name}\']$unwrapOperator as int]');
}
} else if (field.type.typeArguments.isNotEmpty) {
final String genericType =
_makeGenericTypeArguments(field.type, nullTag);
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 @@ -8,7 +8,7 @@ import 'dart:mirrors';
import 'ast.dart';

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

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
15 changes: 14 additions & 1 deletion packages/pigeon/pigeons/non_null_fields.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,24 @@ class SearchRequest {
String query;
}

enum SearchReplyType {
success,
error,
}

class SearchReply {
SearchReply(this.result, this.error, this.indices);
SearchReply(
this.result,
this.error,
this.indices,
this.request,
this.type,
);
String result;
String error;
List<int?> indices;
SearchRequest request;
SearchReplyType type;
}

@HostApi()
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: 2.0.1 # This must match the version in lib/generator_tools.dart
version: 2.0.2 # This must match the version in lib/generator_tools.dart

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
167 changes: 163 additions & 4 deletions packages/pigeon/test/dart_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,168 @@ void main() {
);
});

test('nested null class nullsafe', () {
final Root root = Root(apis: <Api>[], classes: <Class>[
Class(
name: 'Input',
fields: <NamedType>[
NamedType(
type: const TypeDeclaration(
baseName: 'String',
isNullable: true,
),
name: 'input',
offset: null)
],
),
Class(
name: 'Nested',
fields: <NamedType>[
NamedType(
type: const TypeDeclaration(
baseName: 'Input',
isNullable: true,
),
name: 'nested',
offset: null)
],
)
], enums: <Enum>[]);
final StringBuffer sink = StringBuffer();
generateDart(const DartOptions(isNullSafe: true), root, sink);
final String code = sink.toString();
expect(
code,
contains(
'pigeonMap[\'nested\'] = nested?.encode()',
),
);
expect(
code.replaceAll('\n', ' ').replaceAll(' ', ''),
contains(
'nested: pigeonMap[\'nested\'] != null ? Input.decode(pigeonMap[\'nested\']!) : null',
),
);
});

test('nested non-null class nullsafe', () {
final Root root = Root(apis: <Api>[], classes: <Class>[
Class(
name: 'Input',
fields: <NamedType>[
NamedType(
type: const TypeDeclaration(
baseName: 'String',
isNullable: true,
),
name: 'input',
offset: null)
],
),
Class(
name: 'Nested',
fields: <NamedType>[
NamedType(
type: const TypeDeclaration(
baseName: 'Input',
isNullable: false,
),
name: 'nested',
offset: null)
],
)
], enums: <Enum>[]);
final StringBuffer sink = StringBuffer();
generateDart(const DartOptions(isNullSafe: true), root, sink);
final String code = sink.toString();
expect(
code,
contains(
'pigeonMap[\'nested\'] = nested.encode()',
),
);
expect(
code.replaceAll('\n', ' ').replaceAll(' ', ''),
contains(
'nested: Input.decode(pigeonMap[\'nested\']!)',
),
);
});

test('nested null enum nullsafe', () {
final Root root = Root(apis: <Api>[], classes: <Class>[
Class(
name: 'Nested',
fields: <NamedType>[
NamedType(
type: const TypeDeclaration(
baseName: 'Input',
isNullable: true,
),
name: 'nested',
offset: null)
],
)
], enums: <Enum>[
Enum(
name: 'Input',
members: <String>['A', 'B'],
)
]);
final StringBuffer sink = StringBuffer();
generateDart(const DartOptions(isNullSafe: true), root, sink);
final String code = sink.toString();
expect(
code,
contains(
'pigeonMap[\'nested\'] = nested?.index',
),
);
expect(
code.replaceAll('\n', ' ').replaceAll(' ', ''),
contains(
'nested: pigeonMap[\'nested\'] != null ? Input.values[pigeonMap[\'nested\']! as int] : null',
),
);
});

test('nested non-null enum nullsafe', () {
final Root root = Root(apis: <Api>[], classes: <Class>[
Class(
name: 'Nested',
fields: <NamedType>[
NamedType(
type: const TypeDeclaration(
baseName: 'Input',
isNullable: false,
),
name: 'nested',
offset: null)
],
)
], enums: <Enum>[
Enum(
name: 'Input',
members: <String>['A', 'B'],
)
]);
final StringBuffer sink = StringBuffer();
generateDart(const DartOptions(isNullSafe: true), root, sink);
final String code = sink.toString();
expect(
code,
contains(
'pigeonMap[\'nested\'] = nested.index',
),
);
expect(
code.replaceAll('\n', ' ').replaceAll(' ', ''),
contains(
'nested: Input.values[pigeonMap[\'nested\']! as int]',
),
);
});

test('flutterapi', () {
final Root root = Root(apis: <Api>[
Api(name: 'Api', location: ApiLocation.flutter, methods: <Method>[
Expand Down Expand Up @@ -443,10 +605,7 @@ void main() {
final StringBuffer sink = StringBuffer();
generateDart(const DartOptions(isNullSafe: true), root, sink);
final String code = sink.toString();
expect(
code,
contains(
'pigeonMap[\'enum1\'] = enum1 == null ? null : enum1!.index;'));
expect(code, contains('pigeonMap[\'enum1\'] = enum1?.index;'));
expect(code, contains('? Enum.values[pigeonMap[\'enum1\']! as int]'));
expect(code, contains('EnumClass doSomething(EnumClass arg0);'));
});
Expand Down