Skip to content

Commit fdf3baf

Browse files
committed
Support for enum expression in default value parameters
1 parent de57aa4 commit fdf3baf

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

knee-compiler-plugin/src/main/kotlin/utils/PoetUtils.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ package io.deepmedia.tools.knee.plugin.compiler.utils
22

33
import com.squareup.kotlinpoet.*
44
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
5+
import io.deepmedia.tools.knee.plugin.compiler.codegen.CodegenType
56
import org.jetbrains.kotlin.descriptors.*
67
import org.jetbrains.kotlin.ir.declarations.*
78
import org.jetbrains.kotlin.ir.expressions.IrConst
89
import org.jetbrains.kotlin.ir.expressions.IrConstKind
910
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
11+
import org.jetbrains.kotlin.ir.expressions.IrGetEnumValue
1012
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
13+
import org.jetbrains.kotlin.ir.symbols.IrEnumEntrySymbol
1114
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
1215
import org.jetbrains.kotlin.ir.types.*
16+
import org.jetbrains.kotlin.ir.util.dump
1317
import org.jetbrains.kotlin.types.Variance
1418

1519

@@ -212,8 +216,12 @@ fun IrValueParameter.defaultValueForCodegen(functionExpects: List<IrDeclarationW
212216
// is IrConstKind.Long -> CodeBlock.of(kind.valueOf(expression).toString() + "L")
213217
// else -> return null
214218
}
219+
} else if (expression is IrGetEnumValue && type is IrSimpleType) {
220+
// No need to check whether the type is serializable, that would throw an error somewhere else
221+
val type: TypeName = (type as IrSimpleType).asTypeName()
222+
val entry: String = expression.symbol.owner.name.asString()
223+
return CodeBlock.of("%T.%N", type, entry)
215224
}
216-
// risky option: take expression.dumpKotlinLike() as string.
217225
return null
218226
}
219227

tests/test-misc/src/androidInstrumentedTest/kotlin/io/deepmedia/tools/knee/tests/DefaultValuesTests.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,9 @@ class DefaultValuesTests {
2626
emptyStringDefaultValue()
2727
}
2828

29+
@Test
30+
fun testDefaultValue_enum() {
31+
enumDefaultValue()
32+
}
33+
2934
}

tests/test-misc/src/backendMain/kotlin/DefaultValuesDefinitions.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,13 @@ interface BaseInterfaceWithDefaultValues {
2222

2323
@Knee
2424
fun emptyStringDefaultValue(foo: String = "") {
25+
}
26+
27+
@Knee
28+
fun enumDefaultValue(foo: DefaultValuesEnum = DefaultValuesEnum.First) {
29+
}
30+
31+
@KneeEnum
32+
enum class DefaultValuesEnum {
33+
First, Second
2534
}

0 commit comments

Comments
 (0)