Skip to content

Commit

Permalink
Support for enum expression in default value parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
natario1 committed Aug 24, 2024
1 parent de57aa4 commit fdf3baf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
10 changes: 9 additions & 1 deletion knee-compiler-plugin/src/main/kotlin/utils/PoetUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package io.deepmedia.tools.knee.plugin.compiler.utils

import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import io.deepmedia.tools.knee.plugin.compiler.codegen.CodegenType
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.IrConst
import org.jetbrains.kotlin.ir.expressions.IrConstKind
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
import org.jetbrains.kotlin.ir.expressions.IrGetEnumValue
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrEnumEntrySymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.dump
import org.jetbrains.kotlin.types.Variance


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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ class DefaultValuesTests {
emptyStringDefaultValue()
}

@Test
fun testDefaultValue_enum() {
enumDefaultValue()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@ interface BaseInterfaceWithDefaultValues {

@Knee
fun emptyStringDefaultValue(foo: String = "") {
}

@Knee
fun enumDefaultValue(foo: DefaultValuesEnum = DefaultValuesEnum.First) {
}

@KneeEnum
enum class DefaultValuesEnum {
First, Second
}

0 comments on commit fdf3baf

Please sign in to comment.