Skip to content

Commit

Permalink
[pigeon] Fix Kotlin generator to use provided errorClassName (flutter…
Browse files Browse the repository at this point in the history
…#5480)

Fix bug with Pigeon Kotlin code generator not using the provided `errorClassName`.

fixes: flutter/flutter#139031

*If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].*
  • Loading branch information
NathanKolbas authored and HugoOlthof committed Dec 13, 2023
1 parent 700824c commit 460021b
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 58 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 @@
## 15.0.0

* **Breaking Change** [kotlin] Updates Flutter API to use new errorClassName.

## 14.0.1

* Updates minimum supported SDK version to Flutter 3.10/Dart 3.0.
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 @@ -13,7 +13,7 @@ import 'ast.dart';
/// The current version of pigeon.
///
/// This must match the version in pubspec.yaml.
const String pigeonVersion = '14.0.1';
const String pigeonVersion = '15.0.0';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
24 changes: 15 additions & 9 deletions packages/pigeon/lib/kotlin_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
});
});

final String errorClassName = _getErrorClassName(generatorOptions);
for (final Method func in api.methods) {
final String returnType = func.returnType.isVoid
? 'Unit'
Expand Down Expand Up @@ -396,12 +397,12 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
indent.writeScoped('if (it is List<*>) {', '} ', () {
indent.writeScoped('if (it.size > 1) {', '} ', () {
indent.writeln(
'callback(Result.failure(FlutterError(it[0] as String, it[1] as String, it[2] as String?)))');
'callback(Result.failure($errorClassName(it[0] as String, it[1] as String, it[2] as String?)))');
}, addTrailingNewline: false);
if (!func.returnType.isNullable && !func.returnType.isVoid) {
indent.addScoped('else if (it[0] == null) {', '} ', () {
indent.writeln(
'callback(Result.failure(FlutterError("null-error", "Flutter api returned null value for non-null return value.", "")))');
'callback(Result.failure($errorClassName("null-error", "Flutter api returned null value for non-null return value.", "")))');
}, addTrailingNewline: false);
}
indent.addScoped('else {', '}', () {
Expand Down Expand Up @@ -681,8 +682,8 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
indent.newln();
indent.write('private fun wrapError(exception: Throwable): List<Any?> ');
indent.addScoped('{', '}', () {
indent.write(
'if (exception is ${generatorOptions.errorClassName ?? "FlutterError"}) ');
indent
.write('if (exception is ${_getErrorClassName(generatorOptions)}) ');
indent.addScoped('{', '}', () {
indent.write('return ');
indent.addScoped('listOf(', ')', () {
Expand Down Expand Up @@ -713,7 +714,7 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
indent.writeln(
' * @property details The error details. Must be a datatype supported by the api codec.');
indent.writeln(' */');
indent.write('class ${generatorOptions.errorClassName ?? "FlutterError"} ');
indent.write('class ${_getErrorClassName(generatorOptions)} ');
indent.addScoped('(', ')', () {
indent.writeln('val code: String,');
indent.writeln('override val message: String? = null,');
Expand All @@ -722,13 +723,15 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
indent.addln(' : Throwable()');
}

void _writeCreateConnectionError(Indent indent) {
void _writeCreateConnectionError(
KotlinOptions generatorOptions, Indent indent) {
final String errorClassName = _getErrorClassName(generatorOptions);
indent.newln();
indent.write(
'private fun createConnectionError(channelName: String): FlutterError ');
'private fun createConnectionError(channelName: String): $errorClassName ');
indent.addScoped('{', '}', () {
indent.write(
'return FlutterError("channel-error", "Unable to establish connection on channel: \'\$channelName\'.", "")');
'return $errorClassName("channel-error", "Unable to establish connection on channel: \'\$channelName\'.", "")');
});
}

Expand All @@ -749,7 +752,7 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
_writeWrapError(generatorOptions, indent);
}
if (hasFlutterApi) {
_writeCreateConnectionError(indent);
_writeCreateConnectionError(generatorOptions, indent);
}
_writeErrorClass(generatorOptions, indent);
}
Expand All @@ -763,6 +766,9 @@ HostDatatype _getHostDatatype(Root root, NamedType field) {
/// Calculates the name of the codec that will be generated for [api].
String _getCodecName(Api api) => '${api.name}Codec';

String _getErrorClassName(KotlinOptions generatorOptions) =>
generatorOptions.errorClassName ?? 'FlutterError';

String _getArgumentName(int count, NamedType argument) =>
argument.name.isEmpty ? 'arg$count' : argument.name;

Expand Down
Loading

0 comments on commit 460021b

Please sign in to comment.