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 May 2, 2023
1 parent c294b53 commit 0483c61
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
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())
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 0483c61

Please sign in to comment.