Skip to content

Commit

Permalink
Make it an error to use non data objects
Browse files Browse the repository at this point in the history
  • Loading branch information
ansman committed Mar 26, 2024
1 parent b3981ed commit 97a264d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import kotlinx.metadata.isData
import se.ansman.kotshi.Errors
import se.ansman.kotshi.kapt.MetadataAccessor
import se.ansman.kotshi.kapt.isObject
import se.ansman.kotshi.kapt.logKotshiWarning
import se.ansman.kotshi.kapt.logKotshiError
import se.ansman.kotshi.model.GeneratableJsonAdapter
import se.ansman.kotshi.model.GlobalConfig
import se.ansman.kotshi.model.ObjectJsonAdapter
Expand All @@ -26,7 +26,7 @@ class ObjectAdapterGenerator(
init {
require(metadata.isObject)
if (!metadata.isData && metadataAccessor.getLanguageVersion(element) >= KotlinVersion(1, 9)) {
messager.logKotshiWarning(Errors.nonDataObject, element)
messager.logKotshiError(Errors.nonDataObject, element)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.google.devtools.ksp.symbol.ClassKind
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.Modifier
import se.ansman.kotshi.Errors
import se.ansman.kotshi.ksp.logKotshiWarning
import se.ansman.kotshi.ksp.logKotshiError
import se.ansman.kotshi.model.GeneratableJsonAdapter
import se.ansman.kotshi.model.GlobalConfig
import se.ansman.kotshi.model.ObjectJsonAdapter
Expand All @@ -20,7 +20,7 @@ class ObjectAdapterGenerator(
init {
require(element.classKind == ClassKind.OBJECT)
if (Modifier.DATA !in element.modifiers && environment.apiVersion >= KotlinVersion(1, 9)) {
environment.logger.logKotshiWarning(Errors.nonDataObject, element)
environment.logger.logKotshiError(Errors.nonDataObject, element)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class KspGeneratorTest : BaseGeneratorTest() {
object2 = SourceFile.kotlin(
"object2.kt", """
@se.ansman.kotshi.JsonSerializable
object Object2
data object Object2
""".trimIndent()
)
with(compile(factory, object1, object2)) {
Expand Down Expand Up @@ -318,7 +318,4 @@ class KspGeneratorTest : BaseGeneratorTest() {
)
}
}

@Disabled("kotlin-compile-testing doesn't work well with overriding language version yet")
override fun `non data object logs warnings`() {}
}
5 changes: 2 additions & 3 deletions tests/src/test/kotlin/se/ansman/kotshi/BaseGeneratorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -574,13 +574,13 @@ abstract class BaseGeneratorTest {
}

@Test
open fun `non data object logs warnings`() {
fun `non data object fails the build`() {
val source = kotlin("source.kt", """
@se.ansman.kotshi.JsonSerializable
object TestObject
""")
val result = compile(source)
assertThat(result::exitCode).isEqualTo(KotlinCompilation.ExitCode.OK)
assertThat(result::exitCode).isEqualTo(KotlinCompilation.ExitCode.COMPILATION_ERROR)
assertThat(result::messages).contains(Errors.nonDataObject)
}

Expand Down Expand Up @@ -619,7 +619,6 @@ abstract class BaseGeneratorTest {
workingDir = temporaryFolder
this.sources = sources.asList()
inheritClassPath = true
// TODO: Remove once kotlin-compile-testing supports Kotlin 1.9
this.languageVersion = languageVersion
messageOutputStream = System.out // see diagnostics in real time
setUp(options)
Expand Down

0 comments on commit 97a264d

Please sign in to comment.