-
Notifications
You must be signed in to change notification settings - Fork 27.8k
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
[pigeon] Empty generated Dart classes aren't serializable #160501
Comments
This comment was marked as off-topic.
This comment was marked as off-topic.
Relates to #153995 |
Please do not ping individual people when filling issues; we have a triage process. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This is actually a result of the class having no fields. Just for my understanding and to guide future work, what is the use case of having the empty class? |
Well, first of all, Kotlin and Swift do not have such problem: sealed class A
/** Generated class from Pigeon that represents data sent in messages. */
data class B (
val number: Long? = null,
val message: String? = null
) : A()
{
companion object {
fun fromList(pigeonVar_list: List<Any?>): B {
val number = pigeonVar_list[0] as Long?
val message = pigeonVar_list[1] as String?
return B(number, message)
}
}
fun toList(): List<Any?> {
return listOf(
number,
message,
)
}
}
/** Generated class from Pigeon that represents data sent in messages. */
data class C (
) : A()
{
companion object {
fun fromList(pigeonVar_list: List<Any?>): C {
return C()
}
}
fun toList(): List<Any?> {
return listOf(
)
}
}
private open class MessagesPigeonCodec : StandardMessageCodec() {
override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? {
return when (type) {
129.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
B.fromList(it)
}
}
130.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
C.fromList(it)
}
}
else -> super.readValueOfType(type, buffer)
}
}
override fun writeValue(stream: ByteArrayOutputStream, value: Any?) {
when (value) {
is B -> {
stream.write(129)
writeValue(stream, value.toList())
}
is C -> {
stream.write(130)
writeValue(stream, value.toList())
}
else -> super.writeValue(stream, value)
}
}
} protocol A {
}
/// Generated class from Pigeon that represents data sent in messages.
struct B: A {
var number: Int64? = nil
var message: String? = nil
// swift-format-ignore: AlwaysUseLowerCamelCase
static func fromList(_ pigeonVar_list: [Any?]) -> B? {
let number: Int64? = nilOrValue(pigeonVar_list[0])
let message: String? = nilOrValue(pigeonVar_list[1])
return B(
number: number,
message: message
)
}
func toList() -> [Any?] {
return [
number,
message,
]
}
}
/// Generated class from Pigeon that represents data sent in messages.
struct C: A {
// swift-format-ignore: AlwaysUseLowerCamelCase
static func fromList(_ pigeonVar_list: [Any?]) -> C? {
return C(
)
}
func toList() -> [Any?] {
return [
]
}
} Secondly, the typical use case for sealed classes are events/states so let's image we are implementing custom video player. I would definitely have (especially with sealed class VideoPlayerEvent {}
class VideoPlayerStarted extends VideoPlayerEvent {}
class VideoPlayerPaused extends VideoPlayerEvent {}
class VideoPlayerStopped extends VideoPlayerEvent {}
class VideoPlayerResumed extends VideoPlayerEvent {}
class VideoPlayerSeek extends VideoPlayerEvent {
final int positionUs;
VideoPlayerSeek({required this.positionUs});
} As you can see - I don't need properties/fields for all of these classes. In my opinion, described behaviour in this issue is unexpected and should not limit user for specific implementations. Even if I want all my classes to have no fields - it should work as it works for Kotlin/Swift. If it is expected behaviour - Pigeon should throw exception with verbose description of a reason why user shouldn't generate empty classes (can't imagine such reason) |
Steps to reproduce
create
pigeon/messages.dart
:Define sealed class
A
Define
B
which extendsA
with some fieldsDefine
C
which extendsA
without any fieldsrun
flutter pub run pigeon --input ./pigeon/messages.dart
get generated file
src/messages.g.dart
Expected results
C.decode
/C.encode
is generated and we got no errorsActual results
C.decode
/C.encode
was not generated, howeverB.decode
/B.encode
was.Code sample
Code sample
Screenshots or Video
Screenshots / Video demonstration
[Upload media here]
Logs
Logs
Flutter Doctor output
Doctor output
The text was updated successfully, but these errors were encountered: