Skip to content

Commit

Permalink
Add support of nullable serializers to UseSerializers annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
shanshin committed Nov 6, 2020
1 parent e434a1c commit 7c84138
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ abstract class AbstractSerialGenerator(val bindingContext: BindingContext, val c
).toSet()
}

val additionalSerializersInScopeOfCurrentFile: Map<ClassDescriptor, ClassDescriptor> by lazy {
val additionalSerializersInScopeOfCurrentFile: Map<KotlinType, ClassDescriptor> by lazy {
getKClassListFromFileAnnotation(SerializationAnnotations.additionalSerializersFqName, currentDeclaration)
.associateBy(
{
it.supertypes().find(::isKSerializer)?.arguments?.firstOrNull()?.type.toClassDescriptor
it.supertypes().find(::isKSerializer)?.arguments?.firstOrNull()?.type
?: throw AssertionError("Argument for ${SerializationAnnotations.additionalSerializersFqName} does not implement KSerializer or does not provide serializer for concrete type")
},
{ it.toClassDescriptor!! }
Expand All @@ -59,4 +59,4 @@ abstract class AbstractSerialGenerator(val bindingContext: BindingContext, val c

protected fun ClassDescriptor.getFuncDesc(funcName: String): Sequence<FunctionDescriptor> =
unsubstitutedMemberScope.getDescriptorsFiltered { it == Name.identifier(funcName) }.asSequence().filterIsInstance<FunctionDescriptor>()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ open class SerialTypeInfo(
)

fun AbstractSerialGenerator.findAddOnSerializer(propertyType: KotlinType, module: ModuleDescriptor): ClassDescriptor? {
additionalSerializersInScopeOfCurrentFile[propertyType.toClassDescriptor]?.let { return it }
additionalSerializersInScopeOfCurrentFile[propertyType]?.let { return it }
if (propertyType in contextualKClassListInCurrentFile)
return module.getClassFromSerializationPackage(SpecialBuiltins.contextSerializer)
if (propertyType.toClassDescriptor?.annotations?.hasAnnotation(SerializationAnnotations.polymorphicFqName) == true)
Expand Down Expand Up @@ -123,9 +123,12 @@ fun AbstractSerialGenerator.findTypeSerializerOrContextUnchecked(
): ClassDescriptor? {
val annotations = kType.annotations
if (kType.isTypeParameter()) return null
if (kType.isMarkedNullable) return findTypeSerializerOrContextUnchecked(module, kType.makeNotNullable())
annotations.serializableWith(module)?.let { return it.toClassDescriptor }
additionalSerializersInScopeOfCurrentFile[kType.toClassDescriptor]?.let { return it }
additionalSerializersInScopeOfCurrentFile[kType]?.let { return it }
if (!kType.isMarkedNullable) {
additionalSerializersInScopeOfCurrentFile[kType.makeNullable()]?.let { return it }
}

if (kType in contextualKClassListInCurrentFile) return module.getClassFromSerializationPackage(SpecialBuiltins.contextSerializer)
return analyzeSpecialSerializers(module, annotations) ?: findTypeSerializer(module, kType)
}
Expand Down

0 comments on commit 7c84138

Please sign in to comment.