-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
167 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package at.kopyk | ||
|
||
import at.kopyk.poet.addReturn | ||
import at.kopyk.poet.append | ||
import at.kopyk.utils.ClassCompileScope | ||
import at.kopyk.utils.addGeneratedMarker | ||
import at.kopyk.utils.baseName | ||
import at.kopyk.utils.getPrimaryConstructorProperties | ||
import at.kopyk.utils.lang.mapRun | ||
import com.google.devtools.ksp.symbol.KSClassDeclaration | ||
import com.google.devtools.ksp.symbol.KSType | ||
import com.squareup.kotlinpoet.FileSpec | ||
import com.squareup.kotlinpoet.ksp.toTypeName | ||
|
||
internal val ClassCompileScope.copyFromParentKt: FileSpec | ||
get() = buildFile(fileName = target.append("FromParent").reflectionName()) { | ||
val parameterized = target.parameterized | ||
addGeneratedMarker() | ||
|
||
parentTypes | ||
.filter { it.containsAllPropertiesOf(classDeclaration) } | ||
.forEach { parent -> | ||
addInlinedFunction(name = target.simpleName, receives = null, returns = parameterized) { | ||
addParameter( | ||
name = "from", | ||
type = parent.toTypeName(typeParameterResolver) | ||
) | ||
properties | ||
.mapRun { "$baseName = from.$baseName" } | ||
.run { addReturn("${target.simpleName}(${joinToString()})") } | ||
} | ||
} | ||
} | ||
|
||
internal fun KSType.containsAllPropertiesOf(child: KSClassDeclaration): Boolean = | ||
child.getPrimaryConstructorProperties().all { childProperty -> | ||
(this.declaration as? KSClassDeclaration)?.getAllProperties().orEmpty().any { parentProperty -> | ||
childProperty.baseName == parentProperty.baseName && | ||
childProperty.type.resolve().isAssignableFrom(parentProperty.type.resolve()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
kopykat-ksp/src/test/kotlin/at/kopyk/CopyFromParentTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package at.kopyk | ||
|
||
import org.junit.jupiter.api.Test | ||
|
||
class CopyFromParentTest { | ||
|
||
@Test | ||
fun `simple test`() { | ||
""" | ||
|interface Person { | ||
| val name: String | ||
| val age: Int | ||
|} | ||
|data class Person1(override val name: String, override val age: Int): Person | ||
|data class Person2(override val name: String, override val age: Int): Person | ||
| | ||
|val p1 = Person1("Alex", 1) | ||
|val p2 = Person2(p1) | ||
|val r = p2.age | ||
""".evals("r" to 1) | ||
} | ||
|
||
@Test | ||
fun `simple test, non-data class`() { | ||
""" | ||
|interface Person { | ||
| val name: String | ||
| val age: Int | ||
|} | ||
|class Person1(override val name: String, override val age: Int): Person | ||
|data class Person2(override val name: String, override val age: Int): Person | ||
| | ||
|val p1 = Person1("Alex", 1) | ||
|val p2 = Person2(p1) | ||
|val r = p2.age | ||
""".evals("r" to 1) | ||
} | ||
|
||
@Test | ||
fun `missing field should not create`() { | ||
""" | ||
|interface Person { | ||
| val name: String | ||
|} | ||
|data class Person1(override val name: String, val age: Int): Person | ||
|data class Person2(override val name: String, val age: Int): Person | ||
| | ||
|val p1 = Person1("Alex", 1) | ||
|val p2 = Person2(p1) | ||
|val r = p2.age | ||
""".failsWith { it.contains("No value passed for parameter") } | ||
} | ||
} |