mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Unwrap nested typealiases in code gen (#996)
* Add further nested typealias * Recursively unwrap classnames The unwrapped alias could be to something other than a classname!
This commit is contained in:
@@ -322,7 +322,7 @@ private fun String.escapeDollarSigns(): String {
|
|||||||
private fun TypeName.unwrapTypeAlias(): TypeName {
|
private fun TypeName.unwrapTypeAlias(): TypeName {
|
||||||
return when (this) {
|
return when (this) {
|
||||||
is ClassName -> {
|
is ClassName -> {
|
||||||
tag<TypeNameAliasTag>()?.type ?: this
|
tag<TypeNameAliasTag>()?.type?.unwrapTypeAlias() ?: this
|
||||||
}
|
}
|
||||||
is ParameterizedTypeName -> {
|
is ParameterizedTypeName -> {
|
||||||
return (rawType.unwrapTypeAlias() as ClassName).parameterizedBy(typeArguments.map { it.unwrapTypeAlias() })
|
return (rawType.unwrapTypeAlias() as ClassName).parameterizedBy(typeArguments.map { it.unwrapTypeAlias() })
|
||||||
|
@@ -416,10 +416,10 @@ class DualKotlinTest(useReflection: Boolean) {
|
|||||||
|
|
||||||
val testValue = TypeAliasUnwrapping(
|
val testValue = TypeAliasUnwrapping(
|
||||||
simpleClass = 6,
|
simpleClass = 6,
|
||||||
parameterized = TypeAliasGeneric(6),
|
parameterized = GenericClass(6),
|
||||||
wildcardIn = TypeAliasGeneric(6),
|
wildcardIn = GenericClass(6),
|
||||||
wildcardOut = TypeAliasGeneric(6),
|
wildcardOut = GenericClass(6),
|
||||||
complex = TypeAliasGeneric(listOf(TypeAliasGeneric(6)))
|
complex = GenericClass(listOf(GenericClass(6)))
|
||||||
)
|
)
|
||||||
assertThat(adapter.toJson(testValue)).isEqualTo(testJson)
|
assertThat(adapter.toJson(testValue)).isEqualTo(testJson)
|
||||||
|
|
||||||
@@ -430,18 +430,19 @@ class DualKotlinTest(useReflection: Boolean) {
|
|||||||
@JsonClass(generateAdapter = true)
|
@JsonClass(generateAdapter = true)
|
||||||
data class TypeAliasUnwrapping(
|
data class TypeAliasUnwrapping(
|
||||||
val simpleClass: TypeAlias,
|
val simpleClass: TypeAlias,
|
||||||
val parameterized: TypeAliasGeneric<TypeAlias>,
|
val parameterized: GenericClass<TypeAlias>,
|
||||||
val wildcardIn: TypeAliasGeneric<in TypeAlias>,
|
val wildcardIn: GenericClass<in TypeAlias>,
|
||||||
val wildcardOut: TypeAliasGeneric<out TypeAlias>,
|
val wildcardOut: GenericClass<out TypeAlias>,
|
||||||
@Suppress("REDUNDANT_PROJECTION")
|
val complex: GenericClass<GenericTypeAlias>
|
||||||
val complex: TypeAliasGeneric<List<out TypeAliasGeneric<in TypeAlias>>>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
typealias TypeAlias = Int
|
typealias TypeAlias = Int
|
||||||
|
@Suppress("REDUNDANT_PROJECTION")
|
||||||
|
typealias GenericTypeAlias = List<out GenericClass<in TypeAlias>>
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
@JsonClass(generateAdapter = true)
|
||||||
data class TypeAliasGeneric<T>(val value: T)
|
data class GenericClass<T>(val value: T)
|
||||||
|
|
||||||
// Has to be outside since inline classes are only allowed on top level
|
// Has to be outside since inline classes are only allowed on top level
|
||||||
@JsonClass(generateAdapter = true)
|
@JsonClass(generateAdapter = true)
|
||||||
|
Reference in New Issue
Block a user