From 251411cfed52c60474878cfad74e893bf4012600 Mon Sep 17 00:00:00 2001 From: Tad Fisher Date: Thu, 7 Mar 2024 17:11:29 -0800 Subject: [PATCH] Kotlin: Generate sealed interfaces for tagged objects --- .golden/kotlinEnumSumOfProductDocSpec/golden | 6 +++--- .golden/kotlinEnumSumOfProductSpec/golden | 2 +- .../golden | 2 +- .../golden | 2 +- .../golden | 6 +++--- .../golden | 6 +++--- .../golden | 8 ++++---- .golden/kotlinSumOfProductWithTypeParameterSpec/golden | 8 ++++---- src/Moat/Pretty/Kotlin.hs | 6 ++---- 9 files changed, 22 insertions(+), 24 deletions(-) diff --git a/.golden/kotlinEnumSumOfProductDocSpec/golden b/.golden/kotlinEnumSumOfProductDocSpec/golden index 2ad1b67..fa42b0f 100644 --- a/.golden/kotlinEnumSumOfProductDocSpec/golden +++ b/.golden/kotlinEnumSumOfProductDocSpec/golden @@ -1,16 +1,16 @@ /** Top-level documentation describing [Enum]. */ @JsonClassDiscriminator("tag") @Serializable -sealed class Enum : Parcelable { +sealed interface Enum : Parcelable { /** A constructor. */ @Parcelize @Serializable @SerialName("dataCons0") - data class DataCons0(val contents: Record0) : Enum() + data class DataCons0(val contents: Record0) : Enum /** Another constructor. */ @Parcelize @Serializable @SerialName("dataCons1") - data class DataCons1(val contents: Record1) : Enum() + data class DataCons1(val contents: Record1) : Enum } \ No newline at end of file diff --git a/.golden/kotlinEnumSumOfProductSpec/golden b/.golden/kotlinEnumSumOfProductSpec/golden index 4e70440..612e86b 100644 --- a/.golden/kotlinEnumSumOfProductSpec/golden +++ b/.golden/kotlinEnumSumOfProductSpec/golden @@ -1,2 +1,2 @@ @Serializable -sealed class Enum : Parcelable \ No newline at end of file +sealed interface Enum : Parcelable \ No newline at end of file diff --git a/.golden/kotlinEnumSumOfProductWithLinkEnumInterfaceSpec/golden b/.golden/kotlinEnumSumOfProductWithLinkEnumInterfaceSpec/golden index c9da784..fa0b2b4 100644 --- a/.golden/kotlinEnumSumOfProductWithLinkEnumInterfaceSpec/golden +++ b/.golden/kotlinEnumSumOfProductWithLinkEnumInterfaceSpec/golden @@ -1,2 +1,2 @@ @Serializable(with = Enum1Serializer::class) -sealed class Enum : Parcelable \ No newline at end of file +sealed interface Enum : Parcelable \ No newline at end of file diff --git a/.golden/kotlinEnumSumOfProductWithTaggedFlatObjectStyleSpec/golden b/.golden/kotlinEnumSumOfProductWithTaggedFlatObjectStyleSpec/golden index 4e70440..612e86b 100644 --- a/.golden/kotlinEnumSumOfProductWithTaggedFlatObjectStyleSpec/golden +++ b/.golden/kotlinEnumSumOfProductWithTaggedFlatObjectStyleSpec/golden @@ -1,2 +1,2 @@ @Serializable -sealed class Enum : Parcelable \ No newline at end of file +sealed interface Enum : Parcelable \ No newline at end of file diff --git a/.golden/kotlinEnumSumOfProductWithTaggedObjectAndNonConcreteCasesSpec/golden b/.golden/kotlinEnumSumOfProductWithTaggedObjectAndNonConcreteCasesSpec/golden index eb7bff6..9228bc7 100644 --- a/.golden/kotlinEnumSumOfProductWithTaggedObjectAndNonConcreteCasesSpec/golden +++ b/.golden/kotlinEnumSumOfProductWithTaggedObjectAndNonConcreteCasesSpec/golden @@ -1,13 +1,13 @@ @JsonClassDiscriminator("tag") @Serializable -sealed class Enum : Parcelable { +sealed interface Enum : Parcelable { @Parcelize @Serializable @SerialName("dataCons0") - data class DataCons0(val contents: List) : Enum() + data class DataCons0(val contents: List) : Enum @Parcelize @Serializable @SerialName("dataCons1") - data class DataCons1(val contents: List) : Enum() + data class DataCons1(val contents: List) : Enum } \ No newline at end of file diff --git a/.golden/kotlinEnumSumOfProductWithTaggedObjectAndSingleNullarySpec/golden b/.golden/kotlinEnumSumOfProductWithTaggedObjectAndSingleNullarySpec/golden index b5847cb..f30535f 100644 --- a/.golden/kotlinEnumSumOfProductWithTaggedObjectAndSingleNullarySpec/golden +++ b/.golden/kotlinEnumSumOfProductWithTaggedObjectAndSingleNullarySpec/golden @@ -1,13 +1,13 @@ @JsonClassDiscriminator("tag") @Serializable -sealed class Enum : Parcelable { +sealed interface Enum : Parcelable { @Parcelize @Serializable @SerialName("dataCons0") - data class DataCons0(val contents: Record0) : Enum() + data class DataCons0(val contents: Record0) : Enum @Parcelize @Serializable @SerialName("dataCons1") - data object DataCons1 : Enum() + data object DataCons1 : Enum } \ No newline at end of file diff --git a/.golden/kotlinEnumSumOfProductWithTaggedObjectStyleSpec/golden b/.golden/kotlinEnumSumOfProductWithTaggedObjectStyleSpec/golden index 1878b3a..a1169ae 100644 --- a/.golden/kotlinEnumSumOfProductWithTaggedObjectStyleSpec/golden +++ b/.golden/kotlinEnumSumOfProductWithTaggedObjectStyleSpec/golden @@ -1,18 +1,18 @@ @JsonClassDiscriminator("tag") @Serializable -sealed class Enum : Parcelable { +sealed interface Enum : Parcelable { @Parcelize @Serializable @SerialName("dataCons0") - data class DataCons0(val contents: Record0) : Enum() + data class DataCons0(val contents: Record0) : Enum @Parcelize @Serializable @SerialName("dataCons1") - data class DataCons1(val contents: Record1) : Enum() + data class DataCons1(val contents: Record1) : Enum @Parcelize @Serializable @SerialName("dataCons2") - data object DataCons2 : Enum() + data object DataCons2 : Enum } \ No newline at end of file diff --git a/.golden/kotlinSumOfProductWithTypeParameterSpec/golden b/.golden/kotlinSumOfProductWithTypeParameterSpec/golden index a12eb99..b682635 100644 --- a/.golden/kotlinSumOfProductWithTypeParameterSpec/golden +++ b/.golden/kotlinSumOfProductWithTypeParameterSpec/golden @@ -1,15 +1,15 @@ @JsonClassDiscriminator("direction") @Serializable -sealed class CursorInput { +sealed interface CursorInput { @Serializable @SerialName("nextPage") - data class NextPage(val key: A?) : CursorInput() + data class NextPage(val key: A?) : CursorInput @Serializable @SerialName("previousPage") - data class PreviousPage(val key: A) : CursorInput() + data class PreviousPage(val key: A) : CursorInput @Serializable @SerialName("unknown") - data object Unknown : CursorInput() + data object Unknown : CursorInput } \ No newline at end of file diff --git a/src/Moat/Pretty/Kotlin.hs b/src/Moat/Pretty/Kotlin.hs index 050cfd5..9d678fc 100644 --- a/src/Moat/Pretty/Kotlin.hs +++ b/src/Moat/Pretty/Kotlin.hs @@ -247,7 +247,6 @@ prettyTaggedObject parentName tyVars anns ifaces cases indents SumOfProductEncod ++ prettyMoatType caseTy ++ ") : " ++ parentTypeHeader - ++ "()" EnumCase caseNm caseDoc [] -> prettyTypeDoc indents caseDoc [] ++ prettyAnnotations (Just caseNm) indents anns @@ -256,7 +255,6 @@ prettyTaggedObject parentName tyVars anns ifaces cases indents SumOfProductEncod ++ objectCaseTypeHeader caseNm ++ " : " ++ objectParentTypeHeader - ++ "()" EnumCase caseNm _ _ -> error $ "prettyTaggedObject: The data constructor " @@ -326,7 +324,7 @@ prettyEnum doc anns ifaces name tyVars cases sop@SumOfProductEncodingOptions {.. TaggedFlatObjectStyle -> prettyTypeDoc noIndent doc [] ++ prettyAnnotations Nothing noIndent (dontAddParcelizeToSealedClasses anns) - ++ "sealed class " + ++ "sealed interface " ++ classTyp ++ prettyInterfaces ifaces TaggedObjectStyle -> @@ -335,7 +333,7 @@ prettyEnum doc anns ifaces name tyVars cases sop@SumOfProductEncodingOptions {.. Nothing noIndent (dontAddParcelizeToSealedClasses (sumAnnotations ++ anns)) - ++ "sealed class " + ++ "sealed interface " ++ classTyp ++ prettyInterfaces ifaces ++ " {\n"