Skip to content
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

Replace equality checks on sealed objects with is checks (fixes #129) #130

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
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
80 changes: 44 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,21 @@ sealed class Alpha {
will generate the following object:
```kotlin
object AlphaSealedEnum : SealedEnum<Alpha> {
override val values: List<Alpha> = listOf(
Alpha.Beta,
Alpha.Gamma
)
override val values: List<Alpha> by lazy(mode = LazyThreadSafetyMode.PUBLICATION) {
listOf(
Alpha.Beta,
Alpha.Gamma
)
}

override fun ordinalOf(obj: Alpha): Int = when (obj) {
Alpha.Beta -> 0
Alpha.Gamma -> 1
is Alpha.Beta -> 0
is Alpha.Gamma -> 1
}

override fun nameOf(obj: AlphaSealedEnum): String = when (obj) {
Alpha.Beta -> "Alpha_Beta"
Alpha.Gamma -> "Alpha_Gamma"
is Alpha.Beta -> "Alpha_Beta"
is Alpha.Gamma -> "Alpha_Gamma"
}

override fun valueOf(name: String): AlphaSealedEnum = when (name) {
Expand Down Expand Up @@ -150,22 +152,24 @@ sealed class Alpha {
will generate two objects:
```kotlin
object AlphaLevelOrderSealedEnum : SealedEnum<Alpha> {
override val values: List<Alpha> = listOf(
Alpha.Delta,
Alpha.Beta.Gamma,
Alpha.Epsilon.Zeta
)
override val values: List<Alpha> by lazy(mode = LazyThreadSafetyMode.PUBLICATION) {
listOf(
Alpha.Delta,
Alpha.Beta.Gamma,
Alpha.Epsilon.Zeta
)
}

override fun ordinalOf(obj: Alpha): Int = when (obj) {
Alpha.Delta -> 0
Alpha.Beta.Gamma -> 1
Alpha.Epsilon.Zeta -> 2
is Alpha.Delta -> 0
is Alpha.Beta.Gamma -> 1
is Alpha.Epsilon.Zeta -> 2
}

override fun nameOf(obj: AlphaLevelOrderSealedEnum): String = when (obj) {
Alpha.Delta -> "Alpha_Delta"
Alpha.Beta.Gamma -> "Alpha_Beta_Gamma"
Alpha.Epsilon.Zeta -> "Alpha_Epsilon_Zeta"
is Alpha.Delta -> "Alpha_Delta"
is Alpha.Beta.Gamma -> "Alpha_Beta_Gamma"
is Alpha.Epsilon.Zeta -> "Alpha_Epsilon_Zeta"
}

override fun valueOf(name: String): AlphaLevelOrderSealedEnum = when (name) {
Expand All @@ -177,22 +181,24 @@ object AlphaLevelOrderSealedEnum : SealedEnum<Alpha> {
}

object AlphaInOrderSealedEnum : SealedEnum<Alpha> {
override val values: List<Alpha> = listOf(
Alpha.Beta.Gamma,
Alpha.Delta,
Alpha.Epsilon.Zeta
)
override val values: List<Alpha> by lazy(mode = LazyThreadSafetyMode.PUBLICATION) {
listOf(
Alpha.Beta.Gamma,
Alpha.Delta,
Alpha.Epsilon.Zeta
)
}

override fun ordinalOf(obj: Alpha): Int = when (obj) {
Alpha.Beta.Gamma -> 0
Alpha.Delta -> 1
Alpha.Epsilon.Zeta -> 2
is Alpha.Beta.Gamma -> 0
is Alpha.Delta -> 1
is Alpha.Epsilon.Zeta -> 2
}

override fun nameOf(obj: AlphaInOrderSealedEnum): String = when (obj) {
Alpha.Beta.Gamma -> "Alpha_Beta_Gamma"
Alpha.Delta -> "Alpha_Delta"
Alpha.Epsilon.Zeta -> "Alpha_Epsilon_Zeta"
is Alpha.Beta.Gamma -> "Alpha_Beta_Gamma"
is Alpha.Delta -> "Alpha_Delta"
is Alpha.Epsilon.Zeta -> "Alpha_Epsilon_Zeta"
}

override fun valueOf(name: String): AlphaInOrderSealedEnum = when (name) {
Expand Down Expand Up @@ -287,14 +293,16 @@ object AlphaSealedEnum : SealedEnum<Alpha>, SealedEnumWithEnumProvider<Alpha, Al
EnumForSealedEnumProvider<Alpha, AlphaEnum> {
...

override val values: List<Alpha> = listOf(
Alpha.Beta,
Alpha.Gamma
)
override val values: List<Alpha> by lazy(mode = LazyThreadSafetyMode.PUBLICATION) {
listOf(
Alpha.Beta,
Alpha.Gamma
)
}

override fun sealedObjectToEnum(obj: Alpha): AlphaEnum = when (obj) {
Alpha.Beta -> AlphaEnum.Alpha_Beta
Alpha.Gamma -> AlphaEnum.Alpha_Gamma
is Alpha.Beta -> AlphaEnum.Alpha_Beta
is Alpha.Gamma -> AlphaEnum.Alpha_Gamma
}

override fun enumToSealedObject(enum: AlphaEnum): Alpha = when (enum) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.asTypeName
import com.squareup.kotlinpoet.buildCodeBlock
import javax.lang.model.element.TypeElement
import kotlin.reflect.KClass
Expand Down Expand Up @@ -77,8 +78,9 @@ internal data class SealedEnumTypeSpec(

private fun createObjectsProperty(): PropertySpec = PropertySpec.builder("values", listOfSealedClass)
.addModifiers(KModifier.OVERRIDE)
.initializer(
.delegate(
buildCodeBlock {
beginControlFlow("lazy(mode = %T.PUBLICATION)", LazyThreadSafetyMode::class.asTypeName())
if (sealedObjects.isEmpty()) {
addStatement("emptyList()")
} else {
Expand All @@ -91,6 +93,7 @@ internal data class SealedEnumTypeSpec(
unindent()
addStatement(")")
}
endControlFlow()
}
)
.build()
Expand All @@ -108,7 +111,7 @@ internal data class SealedEnumTypeSpec(
} else {
beginControlFlow("return when (obj)")
sealedObjects.forEachIndexed { index, obj ->
addStatement("%T -> $index", obj)
addStatement("is %T -> $index", obj)
}
endControlFlow()
}
Expand All @@ -131,7 +134,7 @@ internal data class SealedEnumTypeSpec(
} else {
beginControlFlow("return when (obj)")
sealedObjects.forEach { obj ->
addStatement("%T -> %S", obj, sealedObjectToName(obj))
addStatement("is %T -> %S", obj, sealedObjectToName(obj))
}
endControlFlow()
}
Expand Down Expand Up @@ -177,7 +180,7 @@ internal data class SealedEnumTypeSpec(
beginControlFlow("return when (obj)")
sealedObjects.forEach { obj ->
addStatement(
"%T -> %T",
"is %T -> %T",
obj,
enumForSealedEnum.nestedClass(obj.simpleNames.joinToString("_"))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.livefront.sealedenum.EnumForSealedEnumProvider
import com.livefront.sealedenum.SealedEnum
import com.livefront.sealedenum.SealedEnumWithEnumProvider
import kotlin.Int
import kotlin.LazyThreadSafetyMode
import kotlin.String
import kotlin.collections.List
import kotlin.reflect.KClass
Expand Down Expand Up @@ -43,7 +44,10 @@ public val EmptySealedClassEnum.sealedObject: EmptySealedClass
public object EmptySealedClassSealedEnum : SealedEnum<EmptySealedClass>,
SealedEnumWithEnumProvider<EmptySealedClass, EmptySealedClassEnum>,
EnumForSealedEnumProvider<EmptySealedClass, EmptySealedClassEnum> {
public override val values: List<EmptySealedClass> = emptyList()
public override val values: List<EmptySealedClass> by lazy(mode =
LazyThreadSafetyMode.PUBLICATION) {
emptyList()
}


public override val enumClass: KClass<EmptySealedClassEnum>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.livefront.sealedenum.EnumForSealedEnumProvider
import com.livefront.sealedenum.SealedEnum
import com.livefront.sealedenum.SealedEnumWithEnumProvider
import kotlin.Int
import kotlin.LazyThreadSafetyMode
import kotlin.String
import kotlin.collections.List
import kotlin.reflect.KClass
Expand Down Expand Up @@ -43,7 +44,10 @@ public val EmptySealedInterfaceEnum.sealedObject: EmptySealedInterface
public object EmptySealedInterfaceSealedEnum : SealedEnum<EmptySealedInterface>,
SealedEnumWithEnumProvider<EmptySealedInterface, EmptySealedInterfaceEnum>,
EnumForSealedEnumProvider<EmptySealedInterface, EmptySealedInterfaceEnum> {
public override val values: List<EmptySealedInterface> = emptyList()
public override val values: List<EmptySealedInterface> by lazy(mode =
LazyThreadSafetyMode.PUBLICATION) {
emptyList()
}


public override val enumClass: KClass<EmptySealedInterfaceEnum>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.livefront.sealedenum.EnumForSealedEnumProvider
import com.livefront.sealedenum.SealedEnum
import com.livefront.sealedenum.SealedEnumWithEnumProvider
import kotlin.Int
import kotlin.LazyThreadSafetyMode
import kotlin.String
import kotlin.collections.List
import kotlin.reflect.KClass
Expand Down Expand Up @@ -47,20 +48,23 @@ public val OneObjectSealedClassEnum.sealedObject: OneObjectSealedClass
public object OneObjectSealedClassSealedEnum : SealedEnum<OneObjectSealedClass>,
SealedEnumWithEnumProvider<OneObjectSealedClass, OneObjectSealedClassEnum>,
EnumForSealedEnumProvider<OneObjectSealedClass, OneObjectSealedClassEnum> {
public override val values: List<OneObjectSealedClass> = listOf(
OneObjectSealedClass.FirstObject
)
public override val values: List<OneObjectSealedClass> by lazy(mode =
LazyThreadSafetyMode.PUBLICATION) {
listOf(
OneObjectSealedClass.FirstObject
)
}


public override val enumClass: KClass<OneObjectSealedClassEnum>
get() = OneObjectSealedClassEnum::class

public override fun ordinalOf(obj: OneObjectSealedClass): Int = when (obj) {
OneObjectSealedClass.FirstObject -> 0
is OneObjectSealedClass.FirstObject -> 0
}

public override fun nameOf(obj: OneObjectSealedClass): String = when (obj) {
OneObjectSealedClass.FirstObject -> "OneObjectSealedClass_FirstObject"
is OneObjectSealedClass.FirstObject -> "OneObjectSealedClass_FirstObject"
}

public override fun valueOf(name: String): OneObjectSealedClass = when (name) {
Expand All @@ -70,7 +74,7 @@ public object OneObjectSealedClassSealedEnum : SealedEnum<OneObjectSealedClass>,

public override fun sealedObjectToEnum(obj: OneObjectSealedClass): OneObjectSealedClassEnum =
when (obj) {
OneObjectSealedClass.FirstObject ->
is OneObjectSealedClass.FirstObject ->
OneObjectSealedClassEnum.OneObjectSealedClass_FirstObject
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.livefront.sealedenum.EnumForSealedEnumProvider
import com.livefront.sealedenum.SealedEnum
import com.livefront.sealedenum.SealedEnumWithEnumProvider
import kotlin.Int
import kotlin.LazyThreadSafetyMode
import kotlin.String
import kotlin.collections.List
import kotlin.reflect.KClass
Expand Down Expand Up @@ -47,20 +48,23 @@ public val OneObjectSealedInterfaceEnum.sealedObject: OneObjectSealedInterface
public object OneObjectSealedInterfaceSealedEnum : SealedEnum<OneObjectSealedInterface>,
SealedEnumWithEnumProvider<OneObjectSealedInterface, OneObjectSealedInterfaceEnum>,
EnumForSealedEnumProvider<OneObjectSealedInterface, OneObjectSealedInterfaceEnum> {
public override val values: List<OneObjectSealedInterface> = listOf(
OneObjectSealedInterface.FirstObject
)
public override val values: List<OneObjectSealedInterface> by lazy(mode =
LazyThreadSafetyMode.PUBLICATION) {
listOf(
OneObjectSealedInterface.FirstObject
)
}


public override val enumClass: KClass<OneObjectSealedInterfaceEnum>
get() = OneObjectSealedInterfaceEnum::class

public override fun ordinalOf(obj: OneObjectSealedInterface): Int = when (obj) {
OneObjectSealedInterface.FirstObject -> 0
is OneObjectSealedInterface.FirstObject -> 0
}

public override fun nameOf(obj: OneObjectSealedInterface): String = when (obj) {
OneObjectSealedInterface.FirstObject -> "OneObjectSealedInterface_FirstObject"
is OneObjectSealedInterface.FirstObject -> "OneObjectSealedInterface_FirstObject"
}

public override fun valueOf(name: String): OneObjectSealedInterface = when (name) {
Expand All @@ -70,7 +74,7 @@ public object OneObjectSealedInterfaceSealedEnum : SealedEnum<OneObjectSealedInt

public override fun sealedObjectToEnum(obj: OneObjectSealedInterface):
OneObjectSealedInterfaceEnum = when (obj) {
OneObjectSealedInterface.FirstObject ->
is OneObjectSealedInterface.FirstObject ->
OneObjectSealedInterfaceEnum.OneObjectSealedInterface_FirstObject
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.livefront.sealedenum.EnumForSealedEnumProvider
import com.livefront.sealedenum.SealedEnum
import com.livefront.sealedenum.SealedEnumWithEnumProvider
import kotlin.Int
import kotlin.LazyThreadSafetyMode
import kotlin.String
import kotlin.collections.List
import kotlin.reflect.KClass
Expand Down Expand Up @@ -50,23 +51,26 @@ public val TwoObjectSealedClassEnum.sealedObject: TwoObjectSealedClass
public object TwoObjectSealedClassSealedEnum : SealedEnum<TwoObjectSealedClass>,
SealedEnumWithEnumProvider<TwoObjectSealedClass, TwoObjectSealedClassEnum>,
EnumForSealedEnumProvider<TwoObjectSealedClass, TwoObjectSealedClassEnum> {
public override val values: List<TwoObjectSealedClass> = listOf(
TwoObjectSealedClass.FirstObject,
TwoObjectSealedClass.SecondObject
)
public override val values: List<TwoObjectSealedClass> by lazy(mode =
LazyThreadSafetyMode.PUBLICATION) {
listOf(
TwoObjectSealedClass.FirstObject,
TwoObjectSealedClass.SecondObject
)
}


public override val enumClass: KClass<TwoObjectSealedClassEnum>
get() = TwoObjectSealedClassEnum::class

public override fun ordinalOf(obj: TwoObjectSealedClass): Int = when (obj) {
TwoObjectSealedClass.FirstObject -> 0
TwoObjectSealedClass.SecondObject -> 1
is TwoObjectSealedClass.FirstObject -> 0
is TwoObjectSealedClass.SecondObject -> 1
}

public override fun nameOf(obj: TwoObjectSealedClass): String = when (obj) {
TwoObjectSealedClass.FirstObject -> "TwoObjectSealedClass_FirstObject"
TwoObjectSealedClass.SecondObject -> "TwoObjectSealedClass_SecondObject"
is TwoObjectSealedClass.FirstObject -> "TwoObjectSealedClass_FirstObject"
is TwoObjectSealedClass.SecondObject -> "TwoObjectSealedClass_SecondObject"
}

public override fun valueOf(name: String): TwoObjectSealedClass = when (name) {
Expand All @@ -77,9 +81,9 @@ public object TwoObjectSealedClassSealedEnum : SealedEnum<TwoObjectSealedClass>,

public override fun sealedObjectToEnum(obj: TwoObjectSealedClass): TwoObjectSealedClassEnum =
when (obj) {
TwoObjectSealedClass.FirstObject ->
is TwoObjectSealedClass.FirstObject ->
TwoObjectSealedClassEnum.TwoObjectSealedClass_FirstObject
TwoObjectSealedClass.SecondObject ->
is TwoObjectSealedClass.SecondObject ->
TwoObjectSealedClassEnum.TwoObjectSealedClass_SecondObject
}

Expand Down
Loading