started handling null correctly for setup methods in java (#155)

This commit is contained in:
gaaclarke
2020-05-13 13:51:04 -07:00
committed by GitHub
parent ed004de397
commit c41258ea35
8 changed files with 51 additions and 36 deletions

View File

@ -1,3 +1,7 @@
## 0.1.0-experimental.11
* Fixed setting an api to null in Java.
## 0.1.0-experimental.10 ## 0.1.0-experimental.10
* Added support for void argument functions. * Added support for void argument functions.

View File

@ -1,4 +1,4 @@
// Autogenerated from Pigeon (v0.1.0-experimental.10), do not edit directly. // Autogenerated from Pigeon (v0.1.0-experimental.11), do not edit directly.
// See also: https://pub.dev/packages/pigeon // See also: https://pub.dev/packages/pigeon
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
@protocol FlutterBinaryMessenger; @protocol FlutterBinaryMessenger;

View File

@ -1,4 +1,4 @@
// Autogenerated from Pigeon (v0.1.0-experimental.10), do not edit directly. // Autogenerated from Pigeon (v0.1.0-experimental.11), do not edit directly.
// See also: https://pub.dev/packages/pigeon // See also: https://pub.dev/packages/pigeon
#import "dartle.h" #import "dartle.h"
#import <Flutter/Flutter.h> #import <Flutter/Flutter.h>

View File

@ -1,4 +1,4 @@
// Autogenerated from Pigeon (v0.1.0-experimental.10), do not edit directly. // Autogenerated from Pigeon (v0.1.0-experimental.11), do not edit directly.
// See also: https://pub.dev/packages/pigeon // See also: https://pub.dev/packages/pigeon
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import // ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import
import 'dart:async'; import 'dart:async';

View File

@ -8,7 +8,7 @@ import 'dart:mirrors';
import 'ast.dart'; import 'ast.dart';
/// The current version of pigeon. /// The current version of pigeon.
const String pigeonVersion = '0.1.0-experimental.10'; const String pigeonVersion = '0.1.0-experimental.11';
/// Read all the content from [stdin] to a String. /// Read all the content from [stdin] to a String.
String readStdin() { String readStdin() {
@ -64,11 +64,15 @@ class Indent {
/// Scoped increase of the ident level. For the execution of [func] the /// Scoped increase of the ident level. For the execution of [func] the
/// indentation will be incremented. /// indentation will be incremented.
void scoped(String begin, String end, Function func) { void scoped(String begin, String end, Function func) {
_sink.write(begin + newline); if (begin != null) {
_sink.write(begin + newline);
}
inc(); inc();
func(); func();
dec(); dec();
_sink.write(str() + end + newline); if (end != null) {
_sink.write(str() + end + newline);
}
} }
/// Add [str] with indentation and a newline. /// Add [str] with indentation and a newline.

View File

@ -53,44 +53,50 @@ void _writeHostApi(Indent indent, Api api) {
'new BasicMessageChannel<Object>(binaryMessenger, "$channelName", new StandardMessageCodec());'); 'new BasicMessageChannel<Object>(binaryMessenger, "$channelName", new StandardMessageCodec());');
indent.dec(); indent.dec();
indent.dec(); indent.dec();
indent.write( indent.write('if (api != null) ');
'channel.setMessageHandler(new BasicMessageChannel.MessageHandler<Object>() '); indent.scoped('{', '} else {', () {
indent.scoped('{', '});', () {
indent.write( indent.write(
'public void onMessage(Object message, BasicMessageChannel.Reply<Object> reply) '); 'channel.setMessageHandler(new BasicMessageChannel.MessageHandler<Object>() ');
indent.scoped('{', '}', () { indent.scoped('{', '});', () {
final String argType = method.argType; indent.write(
final String returnType = method.returnType; 'public void onMessage(Object message, BasicMessageChannel.Reply<Object> reply) ');
String methodArgument;
if (argType == 'void') {
methodArgument = '';
} else {
indent.writeln(
'$argType input = $argType.fromMap((HashMap)message);');
methodArgument = 'input';
}
indent.writeln(
'HashMap<String, HashMap> wrapped = new HashMap<String, HashMap>();');
indent.write('try ');
indent.scoped('{', '}', () { indent.scoped('{', '}', () {
final String call = 'api.${method.name}($methodArgument)'; final String argType = method.argType;
if (method.returnType == 'void') { final String returnType = method.returnType;
indent.writeln('$call;'); String methodArgument;
indent.writeln('wrapped.put("${Keys.result}", null);'); if (argType == 'void') {
methodArgument = '';
} else { } else {
indent.writeln('$returnType output = $call;');
indent.writeln( indent.writeln(
'wrapped.put("${Keys.result}", output.toMap());'); '$argType input = $argType.fromMap((HashMap)message);');
methodArgument = 'input';
} }
});
indent.write('catch (Exception exception) ');
indent.scoped('{', '}', () {
indent.writeln( indent.writeln(
'wrapped.put("${Keys.error}", wrapError(exception));'); 'HashMap<String, HashMap> wrapped = new HashMap<String, HashMap>();');
indent.write('try ');
indent.scoped('{', '}', () {
final String call = 'api.${method.name}($methodArgument)';
if (method.returnType == 'void') {
indent.writeln('$call;');
indent.writeln('wrapped.put("${Keys.result}", null);');
} else {
indent.writeln('$returnType output = $call;');
indent.writeln(
'wrapped.put("${Keys.result}", output.toMap());');
}
});
indent.write('catch (Exception exception) ');
indent.scoped('{', '}', () {
indent.writeln(
'wrapped.put("${Keys.error}", wrapError(exception));');
});
indent.writeln('reply.reply(wrapped);');
}); });
indent.writeln('reply.reply(wrapped);');
}); });
}); });
indent.scoped(null, '}', () {
indent.writeln('channel.setMessageHandler(null);');
});
}); });
} }
}); });

View File

@ -1,5 +1,5 @@
name: pigeon name: pigeon
version: 0.1.0-experimental.10 version: 0.1.0-experimental.11
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier. 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 homepage: https://github.com/flutter/packages/tree/master/packages/pigeon
dependencies: dependencies:

View File

@ -69,6 +69,7 @@ void main() {
final String code = sink.toString(); final String code = sink.toString();
expect(code, contains('public interface Api')); expect(code, contains('public interface Api'));
expect(code, matches('Output.*doSomething.*Input')); expect(code, matches('Output.*doSomething.*Input'));
expect(code, contains('channel.setMessageHandler(null)'));
}); });
test('all the simple datatypes header', () { test('all the simple datatypes header', () {