Add support for serializing kotlin.time.Duration in Parcelize plugin.

This commit adds support for the Parcelize plugin to generate default
serialization implementation for the kotlin.time.Duration. As Parcelize
already supports serializing some of the kotlin library types it makes
sense to support more common ones for the user convenience.

https://issuetracker.google.com/issues/264614661
This commit is contained in:
Rafał Galczak
2023-12-22 13:41:54 +01:00
committed by Space Team
parent b828365bb5
commit c7d4a7e1f1
6 changed files with 73 additions and 1 deletions
@@ -47,6 +47,7 @@ class AndroidSymbols(
private val kotlin: IrPackageFragment = createPackage("kotlin")
private val kotlinJvm: IrPackageFragment = createPackage("kotlin.jvm")
private val kotlinJvmInternalPackage: IrPackageFragment = createPackage("kotlin.jvm.internal")
private val kotlinTime: IrPackageFragment = createPackage("kotlin.time")
private val androidOs: IrPackageFragment = createPackage("android.os")
private val androidUtil: IrPackageFragment = createPackage("android.util")
@@ -185,6 +186,12 @@ class AndroidSymbols(
}
}.symbol
val kotlinTimeDuration: IrClassSymbol = createClass(
kotlinTime, "Duration", ClassKind.CLASS, Modality.FINAL, true
).apply {
owner.valueClassRepresentation = InlineClassRepresentation(Name.identifier("rawValue"), irBuiltIns.longType as IrSimpleType)
}
val kotlinKClassJava: IrPropertySymbol = irFactory.buildProperty {
name = Name.identifier("java")
}.apply {
@@ -153,6 +153,9 @@ class IrParcelSerializerFactory(private val symbols: AndroidSymbols) {
return uintArraySerializer
"kotlin.ULongArray" ->
return ulongArraySerializer
// Library types
"kotlin.time.Duration" ->
return wrapNullableSerializerIfNeeded(irType, durationSerializer)
}
// Generic container types
@@ -429,4 +432,11 @@ class IrParcelSerializerFactory(private val symbols: AndroidSymbols) {
private val sizeFSerializer = IrSimpleParcelSerializer(symbols.parcelReadSizeF, symbols.parcelWriteSizeF)
private val sparseBooleanArraySerializer =
IrSimpleParcelSerializer(symbols.parcelReadSparseBooleanArray, symbols.parcelWriteSparseBooleanArray)
// library types serializers
private val durationSerializer = IrUnsafeCoerceWrappedSerializer(
longSerializer,
symbols.kotlinTimeDuration.defaultType,
irBuiltIns.longType
)
}