Skip to content

Commit

Permalink
Tidy up the code a bit (and use the cast method on the serializer to …
Browse files Browse the repository at this point in the history
…avoid casting warnings). Use a map to allow faster lookup of serializers to class (to allow for overlapping hierarchies).
  • Loading branch information
pdvrieze committed Oct 11, 2023
1 parent c3f15a5 commit 2e509cd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@ public class PolymorphicModuleBuilder<in Base : Any> @PublishedApi internal cons
private val baseClass: KClass<Base>,
private val baseSerializer: KSerializer<Base>? = null
) {
private val subclasses: MutableList<Pair<KClass<out Base>, KSerializer<out Base>>> = mutableListOf()
private val subclasses: MutableMap<KClass<out Base>, KSerializer<out Base>> = mutableMapOf()
private var defaultSerializerProvider: ((Base) -> SerializationStrategy<Base>?)? = null
private var defaultDeserializerProvider: ((String?) -> DeserializationStrategy<Base>?)? = null


/**
* Registers the child serializers for the sealed [subclass] [serializer] in the resulting module under the [base class][Base].
*/
public inline fun <reified T : Base> subclassesOf(): Unit =
subclassesOf(serializer<T>())


/**
* Registers the subclasses of the given class as subclasses of the outer class. Currently this requires `baseClass`
* Registers the subclasses of the given class as subclasses of the outer class. This currently requires `baseClass`
* to be sealed.
*/
public fun <T: Base> subclassesOf(baseClass: KClass<T>, serializer: KSerializer<T>) {
public fun <T: Base> subclassesOf(serializer: KSerializer<T>) {
require(serializer is SealedClassSerializer) {
"subClassesOf only supports automatic adding of subclasses of sealed types."
}
Expand All @@ -42,7 +50,7 @@ public class PolymorphicModuleBuilder<in Base : Any> @PublishedApi internal cons
* Registers a [subclass] [serializer] in the resulting module under the [base class][Base].
*/
public fun <T : Base> subclass(subclass: KClass<T>, serializer: KSerializer<T>) {
subclasses.add(subclass to serializer)
subclasses[subclass] = serializer
}

/**
Expand Down Expand Up @@ -132,14 +140,8 @@ public inline fun <Base : Any, reified T : Base> PolymorphicModuleBuilder<Base>.
public inline fun <Base : Any, reified T : Base> PolymorphicModuleBuilder<Base>.subclass(clazz: KClass<T>): Unit =
subclass(clazz, serializer())

/**
* Registers the child serializers for the sealed [subclass] [serializer] in the resulting module under the [base class][Base].
*/
public inline fun <Base : Any, reified T : Base> PolymorphicModuleBuilder<Base>.subclassesOf(serializer: KSerializer<T>): Unit =
subclassesOf(T::class, serializer)

/**
* Registers the child serializers for the sealed class [T] in the resulting module under the [base class][Base].
*/
public inline fun <Base : Any, reified T : Base> PolymorphicModuleBuilder<Base>.subclassesOf(clazz: KClass<T>): Unit =
subclassesOf(clazz, serializer())
subclassesOf(clazz.serializer())
2 changes: 1 addition & 1 deletion docs/polymorphism.md
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ fun main() {
> Note: On Kotlin/Native, you should use `format.encodeToString(PolymorphicSerializer(Project::class), data))` instead due to limited reflection capabilities.
### Registering sealed children as subclasses
A sealed parent interface or class can be used to directly register all its children using subclassesOf. This will
A sealed parent interface or class can be used to directly register all its children using `subclassesOf`. This will
expose all children that would be available when serializing the parent directly, but now as sealed. Please note that
this is will remain open serialization, and the sealed parent serializer will not be used in serialization.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class PolymorphicSealedChildTest {

val sealedModule = SerializersModule {
polymorphic(FooBase::class) {
subclassesOf(Foo.serializer())
subclassesOf<Foo>()
}
}

Expand Down

0 comments on commit 2e509cd

Please sign in to comment.