Skip to content

Commit

Permalink
Reenable JsonClassSymbolProcessorTest (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacSweers committed Aug 21, 2021
1 parent e4c42ac commit 103678f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Dependencies.kt
Expand Up @@ -77,7 +77,7 @@ object Dependencies {

object Testing {
const val compileTesting = "com.github.tschuchortdev:kotlin-compile-testing:1.4.2"
const val kspCompileTesting = "com.github.tschuchortdev:kotlin-compile-testing-ksp:1.4.2"
const val kspCompileTesting = "com.github.tschuchortdev:kotlin-compile-testing-ksp:1.4.3"
const val junit = "junit:junit:4.13.2"
const val truth = "com.google.truth:truth:1.1.2"
}
Expand Down
Expand Up @@ -35,6 +35,7 @@ import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.TypeVariableName
import com.squareup.kotlinpoet.WildcardTypeName
import dev.zacsweers.moshix.ksp.shade.api.rawType
import java.io.OutputStreamWriter
import java.nio.charset.StandardCharsets.UTF_8
import com.squareup.kotlinpoet.STAR as KpStar
Expand All @@ -49,7 +50,20 @@ internal fun KSType.toTypeName(typeParamResolver: TypeParameterResolver): TypeNa
val type = when (val decl = declaration) {
is KSClassDeclaration -> decl.toTypeName(arguments.map { it.toTypeName(typeParamResolver) })
is KSTypeParameter -> typeParamResolver[decl.name.getShortName()]
is KSTypeAlias -> decl.type.resolve().toTypeName(typeParamResolver)
is KSTypeAlias -> {
val extraResolver = if (decl.typeParameters.isEmpty()) {
typeParamResolver
} else {
decl.typeParameters.toTypeParameterResolver(typeParamResolver)
}
val args = arguments.map { it.toTypeName(typeParamResolver) }
val firstPass = decl.type.resolve().toTypeName(extraResolver).copy(nullable = isMarkedNullable)
return if (args.isNotEmpty()) {
firstPass.rawType().parameterizedBy(args).copy(nullable = firstPass.isNullable)
} else {
firstPass
}
}
else -> error("Unsupported type: $declaration")
}

Expand Down Expand Up @@ -78,7 +92,10 @@ internal fun List<KSTypeParameter>.toTypeParameterResolver(
val typeParamResolver = { id: String ->
parametersMap[id]
?: fallback?.get(id)
?: throw IllegalStateException("No type argument found for $id! Anaylzing $sourceType")
?: throw IllegalStateException(
"No type argument found for $id! Analyzing $sourceType with known parameters " +
"${parametersMap.keys}"
)
}

val resolver = object : TypeParameterResolver {
Expand Down
@@ -0,0 +1,11 @@
package dev.zacsweers.moshix.ksp

import com.squareup.moshi.JsonClass

typealias AnimalMap<T> = Map<Int, T>

// Regression test for https://github.com/ZacSweers/MoshiX/issues/127
@JsonClass(generateAdapter = true)
data class ApiResponse(
val animalNames: AnimalMap<String>,
)

0 comments on commit 103678f

Please sign in to comment.