Skip to content

Commit

Permalink
Kotlinpoet 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacSweers committed Nov 29, 2024
1 parent b2547fc commit 2834627
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 100 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jdk = "22"
jvmTarget = "11"
kotlin = "2.1.0"
kotlinCompileTesting = "0.7.0"
kotlinpoet = "1.18.1"
kotlinpoet = "2.0.0"
ksp = "2.1.0-1.0.29"
ktfmt = "0.53"
moshi = "1.15.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,39 +76,37 @@ class MoshiSealedSymbolProcessorProviderTest(private val useKSP2: Boolean) {
assertThat(generatedAdapter.readText().trim())
.isEqualTo(
"""
// Code generated by moshi-sealed. Do not edit.
package test
// Code generated by moshi-sealed. Do not edit.
package test
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import com.squareup.moshi.adapters.PolymorphicJsonAdapterFactory
import kotlin.Suppress
import kotlin.collections.emptySet
@Suppress("DEPRECATION", "unused", "ClassName", "REDUNDANT_PROJECTION", "RedundantExplicitType",
"LocalVariableName", "RedundantVisibilityModifier")
public class BaseTypeJsonAdapter(
moshi: Moshi,
) : JsonAdapter<BaseType>() {
@Suppress("UNCHECKED_CAST")
private val runtimeAdapter: JsonAdapter<BaseType> =
PolymorphicJsonAdapterFactory.of(BaseType::class.java, "type")
.withSubtype(BaseType.TypeA::class.java, "a")
.withSubtype(BaseType.TypeA::class.java, "aa")
.withSubtype(BaseType.TypeB::class.java, "b")
.withSubtype(BaseType.TypeC.TypeCImpl::class.java, "c")
.create(BaseType::class.java, emptySet(), moshi) as JsonAdapter<BaseType>
override fun fromJson(reader: JsonReader): BaseType? = runtimeAdapter.fromJson(reader)
override fun toJson(writer: JsonWriter, value_: BaseType?) {
runtimeAdapter.toJson(writer, value_)
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import com.squareup.moshi.adapters.PolymorphicJsonAdapterFactory
import kotlin.Suppress
import kotlin.collections.emptySet
@Suppress("DEPRECATION", "unused", "ClassName", "REDUNDANT_PROJECTION", "RedundantExplicitType", "LocalVariableName", "RedundantVisibilityModifier")
public class BaseTypeJsonAdapter(
moshi: Moshi,
) : JsonAdapter<BaseType>() {
@Suppress("UNCHECKED_CAST")
private val runtimeAdapter: JsonAdapter<BaseType> =
PolymorphicJsonAdapterFactory.of(BaseType::class.java, "type")
.withSubtype(BaseType.TypeA::class.java, "a")
.withSubtype(BaseType.TypeA::class.java, "aa")
.withSubtype(BaseType.TypeB::class.java, "b")
.withSubtype(BaseType.TypeC.TypeCImpl::class.java, "c")
.create(BaseType::class.java, emptySet(), moshi) as JsonAdapter<BaseType>
override fun fromJson(reader: JsonReader): BaseType? = runtimeAdapter.fromJson(reader)
override fun toJson(writer: JsonWriter, value_: BaseType?) {
runtimeAdapter.toJson(writer, value_)
}
}
}
"""
.trimIndent()
""".trimIndent()
)
}

Expand Down Expand Up @@ -398,48 +396,46 @@ class MoshiSealedSymbolProcessorProviderTest(private val useKSP2: Boolean) {
assertThat(generatedFile.readText().trim())
.isEqualTo(
"""
// Code generated by moshi-sealed. Do not edit.
package test
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import com.squareup.moshi.adapters.PolymorphicJsonAdapterFactory
import dev.zacsweers.moshix.`sealed`.runtime.`internal`.ObjectJsonAdapter
import kotlin.ExperimentalStdlibApi
import kotlin.OptIn
import kotlin.Suppress
import kotlin.collections.emptySet
@Suppress("DEPRECATION", "unused", "ClassName", "REDUNDANT_PROJECTION", "RedundantExplicitType",
"LocalVariableName", "RedundantVisibilityModifier")
public class BaseTypeJsonAdapter(
moshi: Moshi,
) : JsonAdapter<BaseType>() {
private val runtimeAdapter: JsonAdapter<BaseType>
init {
val moshi_ = moshi.newBuilder()
.add(BaseType.TypeA::class.java, ObjectJsonAdapter(BaseType.TypeA))
.add(BaseType.TypeB::class.java, ObjectJsonAdapter(BaseType.TypeB))
.build()
@Suppress("UNCHECKED_CAST")
@OptIn(ExperimentalStdlibApi::class)
runtimeAdapter = PolymorphicJsonAdapterFactory.of(BaseType::class.java, "type")
.withSubtype(BaseType.TypeA::class.java, "a")
.withSubtype(BaseType.TypeB::class.java, "b")
.create(BaseType::class.java, emptySet(), moshi_) as JsonAdapter<BaseType>
}
override fun fromJson(reader: JsonReader): BaseType? = runtimeAdapter.fromJson(reader)
// Code generated by moshi-sealed. Do not edit.
package test
override fun toJson(writer: JsonWriter, value_: BaseType?) {
runtimeAdapter.toJson(writer, value_)
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import com.squareup.moshi.adapters.PolymorphicJsonAdapterFactory
import dev.zacsweers.moshix.`sealed`.runtime.`internal`.ObjectJsonAdapter
import kotlin.ExperimentalStdlibApi
import kotlin.OptIn
import kotlin.Suppress
import kotlin.collections.emptySet
@Suppress("DEPRECATION", "unused", "ClassName", "REDUNDANT_PROJECTION", "RedundantExplicitType", "LocalVariableName", "RedundantVisibilityModifier")
public class BaseTypeJsonAdapter(
moshi: Moshi,
) : JsonAdapter<BaseType>() {
private val runtimeAdapter: JsonAdapter<BaseType>
init {
val moshi_ = moshi.newBuilder()
.add(BaseType.TypeA::class.java, ObjectJsonAdapter(BaseType.TypeA))
.add(BaseType.TypeB::class.java, ObjectJsonAdapter(BaseType.TypeB))
.build()
@Suppress("UNCHECKED_CAST")
@OptIn(ExperimentalStdlibApi::class)
runtimeAdapter = PolymorphicJsonAdapterFactory.of(BaseType::class.java, "type")
.withSubtype(BaseType.TypeA::class.java, "a")
.withSubtype(BaseType.TypeB::class.java, "b")
.create(BaseType::class.java, emptySet(), moshi_) as JsonAdapter<BaseType>
}
override fun fromJson(reader: JsonReader): BaseType? = runtimeAdapter.fromJson(reader)
override fun toJson(writer: JsonWriter, value_: BaseType?) {
runtimeAdapter.toJson(writer, value_)
}
}
}
"""
.trimIndent()
""".trimIndent()
)
}

Expand Down Expand Up @@ -484,36 +480,34 @@ class MoshiSealedSymbolProcessorProviderTest(private val useKSP2: Boolean) {
assertThat(generatedAdapter.readText().trim())
.isEqualTo(
"""
// Code generated by moshi-sealed. Do not edit.
package test
// Code generated by moshi-sealed. Do not edit.
package test
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import com.squareup.moshi.adapters.PolymorphicJsonAdapterFactory
import kotlin.Suppress
import kotlin.collections.emptySet
@Suppress("DEPRECATION", "unused", "ClassName", "REDUNDANT_PROJECTION", "RedundantExplicitType",
"LocalVariableName", "RedundantVisibilityModifier")
public class BaseTypeJsonAdapter(
moshi: Moshi,
) : JsonAdapter<BaseType>() {
@Suppress("UNCHECKED_CAST")
private val runtimeAdapter: JsonAdapter<BaseType> =
PolymorphicJsonAdapterFactory.of(BaseType::class.java, "type")
.withSubtype(SubType::class.java, "a")
.create(BaseType::class.java, emptySet(), moshi) as JsonAdapter<BaseType>
override fun fromJson(reader: JsonReader): BaseType? = runtimeAdapter.fromJson(reader)
override fun toJson(writer: JsonWriter, value_: BaseType?) {
runtimeAdapter.toJson(writer, value_)
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import com.squareup.moshi.adapters.PolymorphicJsonAdapterFactory
import kotlin.Suppress
import kotlin.collections.emptySet
@Suppress("DEPRECATION", "unused", "ClassName", "REDUNDANT_PROJECTION", "RedundantExplicitType", "LocalVariableName", "RedundantVisibilityModifier")
public class BaseTypeJsonAdapter(
moshi: Moshi,
) : JsonAdapter<BaseType>() {
@Suppress("UNCHECKED_CAST")
private val runtimeAdapter: JsonAdapter<BaseType> =
PolymorphicJsonAdapterFactory.of(BaseType::class.java, "type")
.withSubtype(SubType::class.java, "a")
.create(BaseType::class.java, emptySet(), moshi) as JsonAdapter<BaseType>
override fun fromJson(reader: JsonReader): BaseType? = runtimeAdapter.fromJson(reader)
override fun toJson(writer: JsonWriter, value_: BaseType?) {
runtimeAdapter.toJson(writer, value_)
}
}
}
"""
.trimIndent()
""".trimIndent()
)
}

Expand Down

0 comments on commit 2834627

Please sign in to comment.