From c477caa30b801410243aaf28f803dc736ce08b2a Mon Sep 17 00:00:00 2001 From: J!nl!n <461260911@qq.com> Date: Mon, 31 Oct 2022 22:14:17 +0800 Subject: [PATCH] [pigeon] Fix Kotlin generated sync host api error (#2693) --- packages/pigeon/CHANGELOG.md | 4 ++++ packages/pigeon/lib/generator_tools.dart | 2 +- packages/pigeon/lib/kotlin_generator.dart | 4 ++-- packages/pigeon/pubspec.yaml | 2 +- packages/pigeon/test/kotlin_generator_test.dart | 17 +++++++++++++++++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/pigeon/CHANGELOG.md b/packages/pigeon/CHANGELOG.md index d0beccfc95..ddf96c88e1 100644 --- a/packages/pigeon/CHANGELOG.md +++ b/packages/pigeon/CHANGELOG.md @@ -1,3 +1,7 @@ +## 4.2.4 + +* [kotlin] Fixes Kotlin generated sync host api error. + ## 4.2.3 * [java] Adds assert `args != null`. diff --git a/packages/pigeon/lib/generator_tools.dart b/packages/pigeon/lib/generator_tools.dart index 1f9a641150..bc58a70436 100644 --- a/packages/pigeon/lib/generator_tools.dart +++ b/packages/pigeon/lib/generator_tools.dart @@ -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.3'; +const String pigeonVersion = '4.2.4'; /// Read all the content from [stdin] to a String. String readStdin() { diff --git a/packages/pigeon/lib/kotlin_generator.dart b/packages/pigeon/lib/kotlin_generator.dart index d049f860b4..700c1003ba 100644 --- a/packages/pigeon/lib/kotlin_generator.dart +++ b/packages/pigeon/lib/kotlin_generator.dart @@ -250,7 +250,7 @@ void _writeHostApi(Indent indent, Api api, Root root) { } }, addTrailingNewline: false); indent.add(' catch (exception: Error) '); - indent.scoped('{', '', () { + indent.scoped('{', '}', () { indent.writeln( 'wrapped["${Keys.error}"] = wrapError(exception)'); if (method.isAsynchronous) { @@ -262,7 +262,7 @@ void _writeHostApi(Indent indent, Api api, Root root) { } }); }, addTrailingNewline: false); - indent.scoped('} else {', '}', () { + indent.scoped(' else {', '}', () { indent.writeln('channel.setMessageHandler(null)'); }); }); diff --git a/packages/pigeon/pubspec.yaml b/packages/pigeon/pubspec.yaml index c87de52515..42674d6159 100644 --- a/packages/pigeon/pubspec.yaml +++ b/packages/pigeon/pubspec.yaml @@ -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.3 # This must match the version in lib/generator_tools.dart +version: 4.2.4 # This must match the version in lib/generator_tools.dart environment: sdk: ">=2.12.0 <3.0.0" diff --git a/packages/pigeon/test/kotlin_generator_test.dart b/packages/pigeon/test/kotlin_generator_test.dart index c9c8c6fa52..5bc0fedc3f 100644 --- a/packages/pigeon/test/kotlin_generator_test.dart +++ b/packages/pigeon/test/kotlin_generator_test.dart @@ -126,6 +126,23 @@ void main() { expect(code, contains('interface Api')); expect(code, contains('fun doSomething(input: Input): Output')); expect(code, contains('channel.setMessageHandler')); + expect(code, contains(''' + if (api != null) { + channel.setMessageHandler { message, reply -> + val wrapped = hashMapOf() + try { + val args = message as List + val inputArg = args[0] as Input + wrapped["result"] = api.doSomething(inputArg) + } catch (exception: Error) { + wrapped["error"] = wrapError(exception) + } + reply.reply(wrapped) + } + } else { + channel.setMessageHandler(null) + } + ''')); }); test('all the simple datatypes header', () {