Skip to content

Commit

Permalink
Fix Float default values (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
natario1 authored Sep 3, 2024
1 parent f20d9e0 commit 244de51
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion knee-compiler-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
4 changes: 2 additions & 2 deletions knee-compiler-plugin/src/main/kotlin/utils/PoetUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,12 @@ fun IrValueParameter.defaultValueForCodegen(functionExpects: List<IrDeclarationW
return when (val kind = expression.kind) {
is IrConstKind.Null -> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@ class DefaultValuesTests {
enumDefaultValue()
}

@Test
fun testDefaultValue_float() {
floatDefaultValue()
}

@Test
fun testDefaultValue_long() {
floatDefaultValue()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
Expand Down

0 comments on commit 244de51

Please sign in to comment.