Update Kotlin to 2.0 (#900)

- update Kotlin from 1.7.10 to 2.0.21
  - Kotlin 1.6 dependencies in Gradle lock files are expected because kotlinc,
    which is also used by some tests, internally uses some 1.6 dependencies
    for backwards compatibility reasons.
- update kotlinx-html and kotlinx-serialization
- adapt Kotlin code where necessary
- use Kotlin stdlib Path APIs where possible
- fix IntelliJ Kotlin inspection warnings
- reformat code with `./gradlew spotlessApply`
  - ktfmt adds lots of trailing commas
- Add workaround to fix IntelliJ "unresolved reference" errors
This commit is contained in:
odenix
2025-01-23 14:41:59 -08:00
committed by GitHub
parent cdd6d52642
commit 258eda8630
87 changed files with 1042 additions and 1004 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -38,14 +38,15 @@ import org.pkl.config.kotlin.mapper.KotlinConverterFactories
*/
inline fun <reified T> Config.to(): T {
val javaType = object : JavaType<T>() {}
val result = `as`<T>(javaType.type)
// `as T?` may no longer be required after switching to JSpecify
val result = `as`<T>(javaType.type) as T?
if (result == null && null !is T) {
throw ConversionException(
"Expected a non-null value but got `null`. " +
"To allow null values, convert to a nullable Kotlin type, for example `String?`."
)
}
return result
return result as T
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -171,7 +171,7 @@ class ConfigExtensionsTest {
mapOf(
"l" to SimplePerson("lilly"),
"b" to SimplePerson("bob"),
"s" to SimplePerson("susan")
"s" to SimplePerson("susan"),
)
)
}
@@ -189,14 +189,14 @@ class ConfigExtensionsTest {
mapOf(
"l" to SimplePerson("lilly"),
"b" to SimplePerson("bob"),
"s" to SimplePerson("susan")
"s" to SimplePerson("susan"),
)
)
}
@Test
fun `convert enum with mangled names`() {
val values = MangledNameEnum.values().map { "\"$it\"" }
val values = MangledNameEnum.entries.map { "\"$it\"" }
val config =
evaluator.evaluate(
text(
@@ -208,7 +208,7 @@ class ConfigExtensionsTest {
)
)
val allEnumValues = config["allEnumValues"].to<Set<MangledNameEnum>>()
assertThat(allEnumValues).isEqualTo(MangledNameEnum.values().toSet())
assertThat(allEnumValues).isEqualTo(MangledNameEnum.entries.toSet())
}
data class SimplePerson(val name: String)
@@ -218,7 +218,7 @@ class ConfigExtensionsTest {
enum class Hobby {
SWIMMING,
@Suppress("unused") SURFING,
READING
READING,
}
data class Address<out T>(val street: T)
@@ -240,7 +240,7 @@ class ConfigExtensionsTest {
class PersonWithDefaults(
val name: String = "Pigeon",
val age: Int = 42,
val hobbies: List<String>
val hobbies: List<String>,
)
@Suppress("NonAsciiCharacters", "EnumEntryName")
@@ -252,6 +252,6 @@ class ConfigExtensionsTest {
_42_FROM_INVALID_START("42-from-invalid-start"),
__EMOJI__("❎Emoji✅✅"),
ÀŒÜ("àœü"),
日本_つくば("日本-つくば")
日本_つくば("日本-つくば"),
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -42,19 +42,19 @@ class KotlinConversionsTest {
@Test
fun pIntToULong() {
assertThat(KotlinConversions.pIntToULong.converter.convert(0, mapper)).isEqualTo(0UL)
assertThat(KotlinConversions.pIntToULong.converter.convert(0L, mapper)).isEqualTo(0UL)
assertThat(KotlinConversions.pIntToULong.converter.convert(Long.MAX_VALUE, mapper))
.isEqualTo(Long.MAX_VALUE.toULong())
assertThrows<ConversionException> {
KotlinConversions.pIntToULong.converter.convert(-1, mapper)
KotlinConversions.pIntToULong.converter.convert(-1L, mapper)
}
}
@Test
fun pIntToUInt() {
assertThat(KotlinConversions.pIntToUInt.converter.convert(0, mapper)).isEqualTo(0u)
assertThat(KotlinConversions.pIntToUInt.converter.convert(0L, mapper)).isEqualTo(0u)
assertThat(KotlinConversions.pIntToUInt.converter.convert(UInt.MAX_VALUE.toLong(), mapper))
.isEqualTo(UInt.MAX_VALUE)
@@ -63,12 +63,14 @@ class KotlinConversionsTest {
KotlinConversions.pIntToUInt.converter.convert(UInt.MAX_VALUE.toLong() + 1, mapper)
}
assertThrows<ConversionException> { KotlinConversions.pIntToUInt.converter.convert(-1, mapper) }
assertThrows<ConversionException> {
KotlinConversions.pIntToUInt.converter.convert(-1L, mapper)
}
}
@Test
fun pIntToUShort() {
assertThat(KotlinConversions.pIntToUShort.converter.convert(0, mapper)).isEqualTo(0.toUShort())
assertThat(KotlinConversions.pIntToUShort.converter.convert(0L, mapper)).isEqualTo(0.toUShort())
assertThat(KotlinConversions.pIntToUShort.converter.convert(UShort.MAX_VALUE.toLong(), mapper))
.isEqualTo(UShort.MAX_VALUE)
@@ -78,7 +80,7 @@ class KotlinConversionsTest {
}
assertThrows<ConversionException> {
KotlinConversions.pIntToUShort.converter.convert(-1, mapper)
KotlinConversions.pIntToUShort.converter.convert(-1L, mapper)
}
}
}