Skip to content

[pigeon] Adds Kotlin lint tests to example code and fix lints #9034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Apr 17, 2025
Merged
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 @@
## 25.3.1

* [kotlin] Fixes Kotlin InstanceManager not properly removing callbacks from handler.
* [kotlin] Fixes `SyntheticAccessor` lint caused by private utility methods.

## 25.3.0

* [swift] Adds equality methods to generated data classes.
Original file line number Diff line number Diff line change
@@ -12,33 +12,32 @@ import io.flutter.plugin.common.StandardMethodCodec
import java.io.ByteArrayOutputStream
import java.nio.ByteBuffer

private fun deepEqualsEventChannelMessages(a: Any?, b: Any?): Boolean {
if (a is ByteArray && b is ByteArray) {
return a.contentEquals(b)
}
if (a is IntArray && b is IntArray) {
return a.contentEquals(b)
}
if (a is LongArray && b is LongArray) {
return a.contentEquals(b)
}
if (a is DoubleArray && b is DoubleArray) {
return a.contentEquals(b)
}
if (a is Array<*> && b is Array<*>) {
return a.size == b.size && a.indices.all { deepEqualsEventChannelMessages(a[it], b[it]) }
}
if (a is List<*> && b is List<*>) {
return a.size == b.size && a.indices.all { deepEqualsEventChannelMessages(a[it], b[it]) }
}
if (a is Map<*, *> && b is Map<*, *>) {
return a.size == b.size &&
a.all {
(b as Map<Any?, Any?>).containsKey(it.key) &&
deepEqualsEventChannelMessages(it.value, b[it.key])
}
private object EventChannelMessagesPigeonUtils {
fun deepEquals(a: Any?, b: Any?): Boolean {
if (a is ByteArray && b is ByteArray) {
return a.contentEquals(b)
}
if (a is IntArray && b is IntArray) {
return a.contentEquals(b)
}
if (a is LongArray && b is LongArray) {
return a.contentEquals(b)
}
if (a is DoubleArray && b is DoubleArray) {
return a.contentEquals(b)
}
if (a is Array<*> && b is Array<*>) {
return a.size == b.size && a.indices.all { deepEquals(a[it], b[it]) }
}
if (a is List<*> && b is List<*>) {
return a.size == b.size && a.indices.all { deepEquals(a[it], b[it]) }
}
if (a is Map<*, *> && b is Map<*, *>) {
return a.size == b.size &&
a.all { (b as Map<Any?, Any?>).containsKey(it.key) && deepEquals(it.value, b[it.key]) }
}
return a == b
}
return a == b
}

/**
@@ -68,7 +67,7 @@ data class IntEvent(val data: Long) : PlatformEvent() {
if (this === other) {
return true
}
return deepEqualsEventChannelMessages(toList(), other.toList())
return EventChannelMessagesPigeonUtils.deepEquals(toList(), other.toList())
}

override fun hashCode(): Int = toList().hashCode()
@@ -96,7 +95,7 @@ data class StringEvent(val data: String) : PlatformEvent() {
if (this === other) {
return true
}
return deepEqualsEventChannelMessages(toList(), other.toList())
return EventChannelMessagesPigeonUtils.deepEquals(toList(), other.toList())
}

override fun hashCode(): Int = toList().hashCode()
Original file line number Diff line number Diff line change
@@ -13,24 +13,53 @@ import io.flutter.plugin.common.StandardMessageCodec
import java.io.ByteArrayOutputStream
import java.nio.ByteBuffer

private fun wrapResult(result: Any?): List<Any?> {
return listOf(result)
}
private object MessagesPigeonUtils {

private fun wrapError(exception: Throwable): List<Any?> {
return if (exception is FlutterError) {
listOf(exception.code, exception.message, exception.details)
} else {
listOf(
exception.javaClass.simpleName,
exception.toString(),
"Cause: " + exception.cause + ", Stacktrace: " + Log.getStackTraceString(exception))
fun createConnectionError(channelName: String): FlutterError {
return FlutterError(
"channel-error", "Unable to establish connection on channel: '$channelName'.", "")
}

fun wrapResult(result: Any?): List<Any?> {
return listOf(result)
}

fun wrapError(exception: Throwable): List<Any?> {
return if (exception is FlutterError) {
listOf(exception.code, exception.message, exception.details)
} else {
listOf(
exception.javaClass.simpleName,
exception.toString(),
"Cause: " + exception.cause + ", Stacktrace: " + Log.getStackTraceString(exception))
}
}
}

private fun createConnectionError(channelName: String): FlutterError {
return FlutterError(
"channel-error", "Unable to establish connection on channel: '$channelName'.", "")
fun deepEquals(a: Any?, b: Any?): Boolean {
if (a is ByteArray && b is ByteArray) {
return a.contentEquals(b)
}
if (a is IntArray && b is IntArray) {
return a.contentEquals(b)
}
if (a is LongArray && b is LongArray) {
return a.contentEquals(b)
}
if (a is DoubleArray && b is DoubleArray) {
return a.contentEquals(b)
}
if (a is Array<*> && b is Array<*>) {
return a.size == b.size && a.indices.all { deepEquals(a[it], b[it]) }
}
if (a is List<*> && b is List<*>) {
return a.size == b.size && a.indices.all { deepEquals(a[it], b[it]) }
}
if (a is Map<*, *> && b is Map<*, *>) {
return a.size == b.size &&
a.all { (b as Map<Any?, Any?>).containsKey(it.key) && deepEquals(it.value, b[it.key]) }
}
return a == b
}
}

/**
@@ -46,34 +75,6 @@ class FlutterError(
val details: Any? = null
) : Throwable()

private fun deepEqualsMessages(a: Any?, b: Any?): Boolean {
if (a is ByteArray && b is ByteArray) {
return a.contentEquals(b)
}
if (a is IntArray && b is IntArray) {
return a.contentEquals(b)
}
if (a is LongArray && b is LongArray) {
return a.contentEquals(b)
}
if (a is DoubleArray && b is DoubleArray) {
return a.contentEquals(b)
}
if (a is Array<*> && b is Array<*>) {
return a.size == b.size && a.indices.all { deepEqualsMessages(a[it], b[it]) }
}
if (a is List<*> && b is List<*>) {
return a.size == b.size && a.indices.all { deepEqualsMessages(a[it], b[it]) }
}
if (a is Map<*, *> && b is Map<*, *>) {
return a.size == b.size &&
a.all {
(b as Map<Any?, Any?>).containsKey(it.key) && deepEqualsMessages(it.value, b[it.key])
}
}
return a == b
}

enum class Code(val raw: Int) {
ONE(0),
TWO(1);
@@ -118,7 +119,7 @@ data class MessageData(
if (this === other) {
return true
}
return deepEqualsMessages(toList(), other.toList())
return MessagesPigeonUtils.deepEquals(toList(), other.toList())
}

override fun hashCode(): Int = toList().hashCode()
@@ -184,7 +185,7 @@ interface ExampleHostApi {
try {
listOf(api.getHostLanguage())
} catch (exception: Throwable) {
wrapError(exception)
MessagesPigeonUtils.wrapError(exception)
}
reply.reply(wrapped)
}
@@ -207,7 +208,7 @@ interface ExampleHostApi {
try {
listOf(api.add(aArg, bArg))
} catch (exception: Throwable) {
wrapError(exception)
MessagesPigeonUtils.wrapError(exception)
}
reply.reply(wrapped)
}
@@ -228,10 +229,10 @@ interface ExampleHostApi {
api.sendMessage(messageArg) { result: Result<Boolean> ->
val error = result.exceptionOrNull()
if (error != null) {
reply.reply(wrapError(error))
reply.reply(MessagesPigeonUtils.wrapError(error))
} else {
val data = result.getOrNull()
reply.reply(wrapResult(data))
reply.reply(MessagesPigeonUtils.wrapResult(data))
}
}
}
@@ -274,7 +275,7 @@ class MessageFlutterApi(
callback(Result.success(output))
}
} else {
callback(Result.failure(createConnectionError(channelName)))
callback(Result.failure(MessagesPigeonUtils.createConnectionError(channelName)))
}
}
}
2 changes: 1 addition & 1 deletion packages/pigeon/lib/src/generator_tools.dart
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ import 'generator.dart';
/// The current version of pigeon.
///
/// This must match the version in pubspec.yaml.
const String pigeonVersion = '25.3.0';
const String pigeonVersion = '25.3.1';

/// Read all the content from [stdin] to a String.
String readStdin() {
Loading