From 8af697da3898e1f421912c87fe9b2900800f24f6 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Tue, 3 Sep 2024 16:58:06 +0200 Subject: [PATCH] Fix Float default values --- knee-compiler-plugin/build.gradle.kts | 2 +- .../src/main/kotlin/utils/PoetUtils.kt | 4 ++-- .../deepmedia/tools/knee/tests/DefaultValuesTests.kt | 10 ++++++++++ .../src/backendMain/kotlin/DefaultValuesDefinitions.kt | 8 ++++++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/knee-compiler-plugin/build.gradle.kts b/knee-compiler-plugin/build.gradle.kts index f318684..859b857 100644 --- a/knee-compiler-plugin/build.gradle.kts +++ b/knee-compiler-plugin/build.gradle.kts @@ -7,7 +7,7 @@ plugins { dependencies { compileOnly("org.jetbrains.kotlin:kotlin-compiler-embeddable") - implementation("com.squareup:kotlinpoet:1.12.0") + implementation("com.squareup:kotlinpoet:1.18.1") implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3") } diff --git a/knee-compiler-plugin/src/main/kotlin/utils/PoetUtils.kt b/knee-compiler-plugin/src/main/kotlin/utils/PoetUtils.kt index 90be022..7bec3f8 100644 --- a/knee-compiler-plugin/src/main/kotlin/utils/PoetUtils.kt +++ b/knee-compiler-plugin/src/main/kotlin/utils/PoetUtils.kt @@ -208,12 +208,12 @@ fun IrValueParameter.defaultValueForCodegen(functionExpects: List CodeBlock.of("null") is IrConstKind.String -> CodeBlock.of("%S", kind.valueOf(expression)) + is IrConstKind.Float -> CodeBlock.of("%LF", kind.valueOf(expression)) + is IrConstKind.Long -> CodeBlock.of("%LL", kind.valueOf(expression)) else -> CodeBlock.of("%L", kind.valueOf(expression)) // is IrConstKind.Boolean -> CodeBlock.of(kind.valueOf(expression).toString()) // is IrConstKind.Int -> CodeBlock.of(kind.valueOf(expression).toString()) // is IrConstKind.Double -> CodeBlock.of(kind.valueOf(expression).toString()) - // is IrConstKind.Float -> CodeBlock.of(kind.valueOf(expression).toString() + "F") - // is IrConstKind.Long -> CodeBlock.of(kind.valueOf(expression).toString() + "L") // else -> return null } } else if (expression is IrGetEnumValue && type is IrSimpleType) { diff --git a/tests/test-misc/src/androidInstrumentedTest/kotlin/io/deepmedia/tools/knee/tests/DefaultValuesTests.kt b/tests/test-misc/src/androidInstrumentedTest/kotlin/io/deepmedia/tools/knee/tests/DefaultValuesTests.kt index 24d1c45..077cdfa 100644 --- a/tests/test-misc/src/androidInstrumentedTest/kotlin/io/deepmedia/tools/knee/tests/DefaultValuesTests.kt +++ b/tests/test-misc/src/androidInstrumentedTest/kotlin/io/deepmedia/tools/knee/tests/DefaultValuesTests.kt @@ -31,4 +31,14 @@ class DefaultValuesTests { enumDefaultValue() } + @Test + fun testDefaultValue_float() { + floatDefaultValue() + } + + @Test + fun testDefaultValue_long() { + floatDefaultValue() + } + } diff --git a/tests/test-misc/src/backendMain/kotlin/DefaultValuesDefinitions.kt b/tests/test-misc/src/backendMain/kotlin/DefaultValuesDefinitions.kt index 3d2e322..8a54608 100644 --- a/tests/test-misc/src/backendMain/kotlin/DefaultValuesDefinitions.kt +++ b/tests/test-misc/src/backendMain/kotlin/DefaultValuesDefinitions.kt @@ -24,6 +24,14 @@ interface BaseInterfaceWithDefaultValues { fun emptyStringDefaultValue(foo: String = "") { } +@Knee +fun floatDefaultValue(foo: Float = 1F) { +} + +@Knee +fun longDefaultValue(foo: Long = 1000L) { +} + @Knee fun enumDefaultValue(foo: DefaultValuesEnum = DefaultValuesEnum.First) { }