Skip to content
Merged
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
5 changes: 5 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.1.11

* Added flag to generate null safety annotated Dart code `--dart-null-safety`.
* Made it so Dart API setup methods can take null.

## 0.1.10+1

* Updated the examples page.
Expand Down
105 changes: 59 additions & 46 deletions packages/pigeon/lib/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
import 'ast.dart';
import 'generator_tools.dart';

void _writeHostApi(Indent indent, Api api) {
/// Options that control how Dart code will be generated.
class DartOptions {
/// Determines if the generated code has null safety annotations (Dart >2.10 required).
bool isNullSafe = false;
}

void _writeHostApi(DartOptions opt, Indent indent, Api api) {
assert(api.location == ApiLocation.host);
final String nullTag = opt.isNullSafe ? '?' : '';
indent.write('class ${api.name} ');
indent.scoped('{', '}', () {
for (Method func in api.methods) {
Expand Down Expand Up @@ -38,7 +45,7 @@ void _writeHostApi(Indent indent, Api api) {
? '// noop'
: 'return ${func.returnType}._fromMap(replyMap[\'${Keys.result}\']);';
indent.format(
'''final Map<dynamic, dynamic> replyMap = await channel.send($sendArgument);
'''final Map<dynamic, dynamic>$nullTag replyMap = await channel.send($sendArgument);
if (replyMap == null) {
\tthrow PlatformException(
\t\tcode: 'channel-error',
Expand All @@ -60,9 +67,10 @@ if (replyMap == null) {
indent.writeln('');
}

void _writeFlutterApi(Indent indent, Api api,
void _writeFlutterApi(DartOptions opt, Indent indent, Api api,
{String Function(Method) channelNameFunc, bool isMockHandler = false}) {
assert(api.location == ApiLocation.flutter);
final String nullTag = opt.isNullSafe ? '?' : '';
indent.write('abstract class ${api.name} ');
indent.scoped('{', '}', () {
for (Method func in api.methods) {
Expand All @@ -73,7 +81,7 @@ void _writeFlutterApi(Indent indent, Api api,
func.argType == 'void' ? '' : '${func.argType} arg';
indent.writeln('$returnType ${func.name}($argSignature);');
}
indent.write('static void setup(${api.name} api) ');
indent.write('static void setup(${api.name}$nullTag api) ');
indent.scoped('{', '}', () {
for (Method func in api.methods) {
indent.write('');
Expand All @@ -90,39 +98,45 @@ void _writeFlutterApi(Indent indent, Api api,
indent.dec();
final String messageHandlerSetter =
isMockHandler ? 'setMockMessageHandler' : 'setMessageHandler';
indent
.write('channel.$messageHandlerSetter((dynamic message) async ');
indent.scoped('{', '});', () {
final String argType = func.argType;
final String returnType = func.returnType;
final bool isAsync = func.isAsynchronous;
String call;
if (argType == 'void') {
call = 'api.${func.name}()';
} else {
indent.writeln(
'final Map<dynamic, dynamic> mapMessage = message as Map<dynamic, dynamic>;');
indent.writeln(
'final $argType input = $argType._fromMap(mapMessage);');
call = 'api.${func.name}(input)';
}
if (returnType == 'void') {
indent.writeln('$call;');
if (isMockHandler) {
indent.writeln('return <dynamic, dynamic>{};');
indent.write('if (api == null) ');
indent.scoped('{', '} else {', () {
indent.writeln('channel.$messageHandlerSetter(null);');
});
indent.scoped('', '}', () {
indent.write(
'channel.$messageHandlerSetter((dynamic message) async ');
indent.scoped('{', '});', () {
final String argType = func.argType;
final String returnType = func.returnType;
final bool isAsync = func.isAsynchronous;
String call;
if (argType == 'void') {
call = 'api.${func.name}()';
} else {
indent.writeln(
'final Map<dynamic, dynamic> mapMessage = message as Map<dynamic, dynamic>;');
indent.writeln(
'final $argType input = $argType._fromMap(mapMessage);');
call = 'api.${func.name}(input)';
}
} else {
if (isAsync) {
indent.writeln('final $returnType output = await $call;');
if (returnType == 'void') {
indent.writeln('$call;');
if (isMockHandler) {
indent.writeln('return <dynamic, dynamic>{};');
}
} else {
indent.writeln('final $returnType output = $call;');
if (isAsync) {
indent.writeln('final $returnType output = await $call;');
} else {
indent.writeln('final $returnType output = $call;');
}
const String returnExpresion = 'output._toMap()';
final String returnStatement = isMockHandler
? 'return <dynamic, dynamic>{\'${Keys.result}\': $returnExpresion};'
: 'return $returnExpresion;';
indent.writeln(returnStatement);
}
const String returnExpresion = 'output._toMap()';
final String returnStatement = isMockHandler
? 'return <dynamic, dynamic>{\'${Keys.result}\': $returnExpresion};'
: 'return $returnExpresion;';
indent.writeln(returnStatement);
}
});
});
});
}
Expand All @@ -133,26 +147,29 @@ void _writeFlutterApi(Indent indent, Api api,

/// Generates Dart source code for the given AST represented by [root],
/// outputting the code to [sink].
void generateDart(Root root, StringSink sink) {
void generateDart(DartOptions opt, Root root, StringSink sink) {
final List<String> customClassNames =
root.classes.map((Class x) => x.name).toList();
final Indent indent = Indent(sink);
indent.writeln('// $generatedCodeWarning');
indent.writeln('// $seeAlsoWarning');
indent.writeln(
'// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import');
indent.writeln('// @dart = 2.8');
indent.writeln('// @dart = ${opt.isNullSafe ? '2.10' : '2.8'}');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This is control via the SDK in pubspec.yaml, so there is no need to add // @dart = 2.10

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need it for the dartanalyzer, maybe there is a command line flag that can replace it?

indent.writeln('import \'dart:async\';');
indent.writeln('import \'package:flutter/services.dart\';');
indent.writeln(
'import \'dart:typed_data\' show Uint8List, Int32List, Int64List, Float64List;');
indent.writeln('');

final String nullBang = opt.isNullSafe ? '!' : '';
for (Class klass in root.classes) {
sink.write('class ${klass.name} ');
indent.scoped('{', '}', () {
for (Field field in klass.fields) {
indent.writeln('${field.dataType} ${field.name};');
final String datatype =
opt.isNullSafe ? '${field.dataType}?' : field.dataType;
indent.writeln('$datatype ${field.name};');
}
indent.writeln('// ignore: unused_element');
indent.write('Map<dynamic, dynamic> _toMap() ');
Expand All @@ -163,7 +180,7 @@ void generateDart(Root root, StringSink sink) {
indent.write('pigeonMap[\'${field.name}\'] = ');
if (customClassNames.contains(field.dataType)) {
indent.addln(
'${field.name} == null ? null : ${field.name}._toMap();');
'${field.name} == null ? null : ${field.name}$nullBang._toMap();');
} else {
indent.addln('${field.name};');
}
Expand All @@ -174,16 +191,12 @@ void generateDart(Root root, StringSink sink) {
indent.write(
'static ${klass.name} _fromMap(Map<dynamic, dynamic> pigeonMap) ');
indent.scoped('{', '}', () {
indent.write('if (pigeonMap == null)');
indent.scoped('{', '}', () {
indent.writeln('return null;');
});
indent.writeln('final ${klass.name} result = ${klass.name}();');
for (Field field in klass.fields) {
indent.write('result.${field.name} = ');
if (customClassNames.contains(field.dataType)) {
indent.addln(
'${field.dataType}._fromMap(pigeonMap[\'${field.name}\']);');
'pigeonMap[\'${field.name}\'] != null ? ${field.dataType}._fromMap(pigeonMap[\'${field.name}\']) : null;');
} else {
indent.addln('pigeonMap[\'${field.name}\'];');
}
Expand All @@ -195,18 +208,18 @@ void generateDart(Root root, StringSink sink) {
}
for (Api api in root.apis) {
if (api.location == ApiLocation.host) {
_writeHostApi(indent, api);
_writeHostApi(opt, indent, api);
if (api.dartHostTestHandler != null) {
final Api mockApi = Api(
name: api.dartHostTestHandler,
methods: api.methods,
location: ApiLocation.flutter);
_writeFlutterApi(indent, mockApi,
_writeFlutterApi(opt, indent, mockApi,
channelNameFunc: (Method func) => makeChannelName(api, func),
isMockHandler: true);
}
} else if (api.location == ApiLocation.flutter) {
_writeFlutterApi(indent, api);
_writeFlutterApi(opt, indent, api);
}
}
}
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.
const String pigeonVersion = '0.1.10+1';
const String pigeonVersion = '0.1.11';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
12 changes: 10 additions & 2 deletions packages/pigeon/lib/pigeon_lib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ class PigeonOptions {

/// Options that control how Java will be generated.
JavaOptions javaOptions = JavaOptions();

/// Options that control how Dart will be generated.
DartOptions dartOptions = DartOptions();
}

/// A collection of an AST represented as a [Root] and [Error]'s.
Expand Down Expand Up @@ -282,6 +285,8 @@ options:
..addOption('java_out', help: 'Path to generated Java file (.java).')
..addOption('java_package',
help: 'The package that generated Java code will be in.')
..addFlag('dart_null_safety',
help: 'Makes generated Dart code have null safety annotations')
..addOption('objc_header_out',
help: 'Path to generated Objective-C header file (.h).')
..addOption('objc_prefix',
Expand All @@ -299,6 +304,7 @@ options:
opts.objcOptions.prefix = results['objc_prefix'];
opts.javaOut = results['java_out'];
opts.javaOptions.package = results['java_package'];
opts.dartOptions.isNullSafe = results['dart_null_safety'];
return opts;
}

Expand Down Expand Up @@ -402,8 +408,10 @@ options:
errors.add(Error(message: err.message, filename: options.input));
}
if (options.dartOut != null) {
await _runGenerator(options.dartOut,
(StringSink sink) => generateDart(parseResults.root, sink));
await _runGenerator(
options.dartOut,
(StringSink sink) =>
generateDart(options.dartOptions, parseResults.root, sink));
}
if (options.objcHeaderOut != null) {
await _runGenerator(
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: pigeon
version: 0.1.10+1
version: 0.1.11
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
homepage: https://github.com/flutter/packages/tree/master/packages/pigeon
dependencies:
Expand Down
19 changes: 18 additions & 1 deletion packages/pigeon/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ test_pigeon_android() {
rm -rf $temp_dir
}

# test_null_safe_dart(<path to pigeon file>)
#
# Compiles the pigeon file to a temp directory and attempts to run the dart
# analyzer on it with null safety turned on.
test_null_safe_dart() {
temp_dir=$(mktemp -d -t pigeon)

pub run pigeon \
--input $1 \
--dart_null_safety \
--dart_out $temp_dir/pigeon.dart

dartanalyzer $temp_dir/pigeon.dart --fatal-infos --fatal-warnings --packages ./e2e_tests/test_objc/.packages --enable-experiment=non-nullable
Comment thread
blasten marked this conversation as resolved.
rm -rf $temp_dir
}

###############################################################################
# Dart unit tests
###############################################################################
Expand All @@ -76,7 +92,7 @@ pub run test test/
###############################################################################
# Execute without arguments test
###############################################################################
pub run pigeon
pub run pigeon 1> /dev/null

###############################################################################
# Compilation tests (Code is generated and compiled)
Expand All @@ -88,6 +104,7 @@ pushd $PWD
cd e2e_tests/test_objc/
flutter pub get
popd
test_null_safe_dart ./pigeons/message.dart

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it make sense to test the other scenarios?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it when I was developing but then I realized I don't think any of the other cases are exercising the code differently. The changes were in pieces of code that would have shown up in message.dart.

test_pigeon_android ./pigeons/voidflutter.dart
test_pigeon_android ./pigeons/voidhost.dart
test_pigeon_android ./pigeons/host2flutter.dart
Expand Down
Loading