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:
committed by
Space Team
parent
b828365bb5
commit
c7d4a7e1f1
+7
@@ -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 {
|
||||
|
||||
+10
@@ -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
|
||||
)
|
||||
}
|
||||
|
||||
+2
-1
@@ -86,6 +86,7 @@ object BuiltinParcelableTypes {
|
||||
"kotlin.ULongArray",
|
||||
"kotlin.UShort",
|
||||
"kotlin.UShortArray",
|
||||
"kotlin.time.Duration",
|
||||
) + PARCELABLE_SUPERTYPE_FQNAMES
|
||||
|
||||
val PARCELABLE_CONTAINER_FQNAMES = setOf(
|
||||
@@ -120,4 +121,4 @@ object BuiltinParcelableTypes {
|
||||
"kotlin.collections.Set",
|
||||
*IMMUTABLE_COLLECTIONS_FQNAMES.toTypedArray(),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// WITH_STDLIB
|
||||
|
||||
@file:JvmName("TestKt")
|
||||
package test
|
||||
|
||||
import kotlinx.parcelize.*
|
||||
import android.os.Parcelable
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
import kotlin.time.Duration.Companion.nanoseconds
|
||||
|
||||
@Parcelize
|
||||
data class Test(
|
||||
val basic: Duration,
|
||||
val negative: Duration,
|
||||
val nullable: Duration?,
|
||||
val nullableWithValue: Duration?,
|
||||
val noNanoseconds: Duration,
|
||||
val noSeconds: Duration,
|
||||
val infinite: Duration,
|
||||
val negativeInfinite: Duration,
|
||||
) : Parcelable
|
||||
|
||||
|
||||
fun box() = parcelTest { parcel ->
|
||||
val test = Test(
|
||||
basic = 10.seconds + 2.nanoseconds,
|
||||
negative = -10.seconds - 2.nanoseconds,
|
||||
nullable = null,
|
||||
nullableWithValue = 10.seconds + 2.nanoseconds,
|
||||
noNanoseconds = 10.seconds,
|
||||
noSeconds = 2.nanoseconds,
|
||||
infinite = Duration.INFINITE,
|
||||
negativeInfinite = -Duration.INFINITE,
|
||||
)
|
||||
test.writeToParcel(parcel, 0)
|
||||
val bytes = parcel.marshall()
|
||||
parcel.unmarshall(bytes, 0, bytes.size)
|
||||
parcel.setDataPosition(0)
|
||||
val got = parcelableCreator<Test>().createFromParcel(parcel)
|
||||
assert(test == got)
|
||||
}
|
||||
+6
@@ -127,6 +127,12 @@ public class ParcelizeFirLightTreeBoxTestGenerated extends AbstractParcelizeFirL
|
||||
runTest("plugins/parcelize/parcelize-compiler/testData/box/customSimple.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("duration.kt")
|
||||
public void testDuration() throws Exception {
|
||||
runTest("plugins/parcelize/parcelize-compiler/testData/box/duration.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("enumObject.kt")
|
||||
public void testEnumObject() throws Exception {
|
||||
|
||||
+6
@@ -127,6 +127,12 @@ public class ParcelizeIrBoxTestGenerated extends AbstractParcelizeIrBoxTest {
|
||||
runTest("plugins/parcelize/parcelize-compiler/testData/box/customSimple.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("duration.kt")
|
||||
public void testDuration() throws Exception {
|
||||
runTest("plugins/parcelize/parcelize-compiler/testData/box/duration.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("enumObject.kt")
|
||||
public void testEnumObject() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user