-
Notifications
You must be signed in to change notification settings - Fork 0
Add ci action workflow #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+291
−43
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,28 @@ | ||
| name: CI | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main] | ||
| pull_request: | ||
| branches: [main] | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: actions/setup-java@v4 | ||
| with: | ||
| java-version: "21" | ||
| distribution: temurin | ||
| - uses: gradle/actions/setup-gradle@v4 | ||
| with: | ||
| cache-cleanup: on-success | ||
| - run: ./gradlew assemble | ||
| - run: ./gradlew check | ||
| - uses: dorny/test-reporter@v1 | ||
| if: always() | ||
| with: | ||
| name: Test Results | ||
| path: "**/build/test-results/test/*.xml" | ||
| reporter: java-junit |
29 changes: 29 additions & 0 deletions
29
ksp-test-fixtures/src/test/kotlin/com/faire/ksp/test/ReflectionEquivalenceTest.kt
This file contains hidden or 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,29 @@ | ||
| package com.faire.ksp.test | ||
|
|
||
| import com.faire.ksp.adapters.ksClassDeclaration | ||
| import com.faire.ksp.snapshot.snapshots.toSnapshot | ||
| import com.faire.ksp.test.fixtures.SimpleDataClass | ||
| import com.faire.ksp.test.fixtures.SimpleDataClassKspSnapshot | ||
| import com.faire.ksp.test.fixtures.SimpleInterface | ||
| import com.faire.ksp.test.fixtures.SimpleInterfaceKspSnapshot | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.junit.jupiter.api.Test | ||
|
|
||
| class ReflectionEquivalenceTest { | ||
|
|
||
| @Test | ||
| fun `SimpleDataClass reflection adapter is equivalent to KSP`() { | ||
| val kspSnapshot = SimpleDataClassKspSnapshot.snapshot | ||
| val reflectionSnapshot = SimpleDataClass::class.ksClassDeclaration.toSnapshot() | ||
|
|
||
| assertThat(reflectionSnapshot).isEqualTo(kspSnapshot) | ||
| } | ||
|
|
||
| @Test | ||
| fun `SimpleInterface reflection adapter is equivalent to KSP`() { | ||
| val kspSnapshot = SimpleInterfaceKspSnapshot.snapshot | ||
| val reflectionSnapshot = SimpleInterface::class.ksClassDeclaration.toSnapshot() | ||
|
|
||
| assertThat(reflectionSnapshot).isEqualTo(kspSnapshot) | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
ksp-test-fixtures/src/test/kotlin/com/faire/ksp/test/fixtures/SimpleDataClass.kt
This file contains hidden or 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,10 @@ | ||
| package com.faire.ksp.test.fixtures | ||
|
|
||
| import com.faire.ksp.snapshot.GenerateSnapshot | ||
|
|
||
| @GenerateSnapshot | ||
| data class SimpleDataClass( | ||
| val name: String, | ||
| val age: Int, | ||
| var mutableField: Boolean, | ||
| ) |
9 changes: 9 additions & 0 deletions
9
ksp-test-fixtures/src/test/kotlin/com/faire/ksp/test/fixtures/SimpleInterface.kt
This file contains hidden or 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,9 @@ | ||
| package com.faire.ksp.test.fixtures | ||
|
|
||
| import com.faire.ksp.snapshot.GenerateSnapshot | ||
|
|
||
| @GenerateSnapshot | ||
| interface SimpleInterface { | ||
| val id: String | ||
| fun process(input: Int): Boolean | ||
| } |
75 changes: 75 additions & 0 deletions
75
test-processor/src/main/kotlin/com/faire/ksp/snapshot/SnapshotLiteralWriter.kt
This file contains hidden or 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,75 @@ | ||
| package com.faire.ksp.snapshot | ||
|
|
||
| import com.google.devtools.ksp.processing.Resolver | ||
| import com.google.devtools.ksp.symbol.KSType | ||
| import com.squareup.kotlinpoet.CodeBlock | ||
| import com.squareup.kotlinpoet.asClassName | ||
| import com.squareup.kotlinpoet.buildCodeBlock | ||
|
|
||
| internal class SnapshotLiteralWriter( | ||
| private val resolver: Resolver | ||
| ) { | ||
| fun toLiteralCode(value: Any): CodeBlock { | ||
| val qualifiedName = value::class.qualifiedName | ||
| ?: error("No qualified name for ${value::class}") | ||
|
|
||
| val ksClass = resolver.getClassDeclarationByName(resolver.getKSNameFromString(qualifiedName)) | ||
| ?: error("Cannot resolve $qualifiedName via KSP") | ||
|
|
||
| val constructor = ksClass.primaryConstructor | ||
| ?: error("No primary constructor for $qualifiedName") | ||
|
|
||
| val className = value::class.asClassName() | ||
|
|
||
| return buildCodeBlock { | ||
| add("%T(\n", className) | ||
| indent() | ||
| for (param in constructor.parameters) { | ||
| val paramName = param.name!!.asString() | ||
| val paramType = param.type.resolve() | ||
| val propValue = readProperty(value, paramName) | ||
| add("$paramName = ") | ||
| add(formatValue(propValue, paramType)) | ||
| add(",\n") | ||
| } | ||
| unindent() | ||
| add(")") | ||
| } | ||
| } | ||
|
|
||
| private fun readProperty(instance: Any, name: String): Any? { | ||
| val field = instance::class.java.getDeclaredField(name) | ||
| field.isAccessible = true | ||
| return field.get(instance) | ||
| } | ||
|
|
||
| private fun formatValue(value: Any?, type: KSType): CodeBlock { | ||
| if (value == null) return CodeBlock.of("null") | ||
|
|
||
| return when (type.declaration.qualifiedName?.asString()) { | ||
| "kotlin.String" -> CodeBlock.of("%S", value) | ||
| "kotlin.Boolean", "kotlin.Int" -> CodeBlock.of("%L", value) | ||
| "kotlin.collections.List" -> { | ||
| val list = value as List<*> | ||
| if (list.isEmpty()) return CodeBlock.of("listOf()") | ||
| val elementType = type.arguments.first().type!!.resolve() | ||
| formatList(list, elementType) | ||
| } | ||
|
|
||
| else -> toLiteralCode(value) | ||
| } | ||
| } | ||
|
|
||
| private fun formatList(list: List<*>, elementType: KSType): CodeBlock { | ||
| return buildCodeBlock { | ||
| add("listOf(\n") | ||
| indent() | ||
| for (item in list) { | ||
| add(formatValue(item, elementType)) | ||
| add(",\n") | ||
| } | ||
| unindent() | ||
| add(")") | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
37 changes: 0 additions & 37 deletions
37
test-processor/src/main/kotlin/com/faire/ksp/snapshot/Snapshots.kt
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
test-processor/src/main/kotlin/com/faire/ksp/snapshot/handlers/ClassSnapshotHandler.kt
This file contains hidden or 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,21 @@ | ||
| package com.faire.ksp.snapshot.handlers | ||
|
|
||
| import com.faire.ksp.snapshot.snapshots.SnapshotResult | ||
| import com.faire.ksp.snapshot.snapshots.toSnapshot | ||
| import com.google.devtools.ksp.symbol.KSAnnotated | ||
| import com.google.devtools.ksp.symbol.KSClassDeclaration | ||
|
|
||
| internal class ClassSnapshotHandler : SnapshotHandler { | ||
| override fun process(symbol: KSAnnotated): SnapshotResult? { | ||
| if (symbol !is KSClassDeclaration) return null | ||
|
|
||
| val qn = symbol.qualifiedName?.asString() ?: return null | ||
|
|
||
| return SnapshotResult( | ||
| qualifiedName = qn, | ||
| simpleName = symbol.simpleName.asString(), | ||
| packageName = symbol.packageName.asString(), | ||
| snapshot = symbol.toSnapshot(), | ||
| ) | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
test-processor/src/main/kotlin/com/faire/ksp/snapshot/handlers/FunctionSnapshotHandler.kt
This file contains hidden or 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,21 @@ | ||
| package com.faire.ksp.snapshot.handlers | ||
|
|
||
| import com.faire.ksp.snapshot.snapshots.SnapshotResult | ||
| import com.faire.ksp.snapshot.snapshots.toSnapshot | ||
| import com.google.devtools.ksp.symbol.KSAnnotated | ||
| import com.google.devtools.ksp.symbol.KSFunctionDeclaration | ||
|
|
||
| internal class FunctionSnapshotHandler : SnapshotHandler { | ||
| override fun process(symbol: KSAnnotated): SnapshotResult? { | ||
| if (symbol !is KSFunctionDeclaration) return null | ||
|
|
||
| val qn = symbol.qualifiedName?.asString() ?: return null | ||
|
|
||
| return SnapshotResult( | ||
| qualifiedName = qn, | ||
| simpleName = symbol.simpleName.asString(), | ||
| packageName = symbol.packageName.asString(), | ||
| snapshot = symbol.toSnapshot(), | ||
| ) | ||
| } | ||
| } | ||
21 changes: 21 additions & 0 deletions
21
test-processor/src/main/kotlin/com/faire/ksp/snapshot/handlers/PropertySnapshotHandler.kt
This file contains hidden or 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,21 @@ | ||
| package com.faire.ksp.snapshot.handlers | ||
|
|
||
| import com.faire.ksp.snapshot.snapshots.SnapshotResult | ||
| import com.faire.ksp.snapshot.snapshots.toSnapshot | ||
| import com.google.devtools.ksp.symbol.KSAnnotated | ||
| import com.google.devtools.ksp.symbol.KSPropertyDeclaration | ||
|
|
||
| internal class PropertySnapshotHandler : SnapshotHandler { | ||
| override fun process(symbol: KSAnnotated): SnapshotResult? { | ||
| if (symbol !is KSPropertyDeclaration) return null | ||
|
|
||
| val qn = symbol.qualifiedName?.asString() ?: return null | ||
|
|
||
| return SnapshotResult( | ||
| qualifiedName = qn, | ||
| simpleName = symbol.simpleName.asString(), | ||
| packageName = symbol.packageName.asString(), | ||
| snapshot = symbol.toSnapshot(), | ||
| ) | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
test-processor/src/main/kotlin/com/faire/ksp/snapshot/handlers/SnapshotHandler.kt
This file contains hidden or 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,9 @@ | ||
| package com.faire.ksp.snapshot.handlers | ||
|
|
||
| import com.faire.ksp.snapshot.snapshots.SnapshotResult | ||
| import com.google.devtools.ksp.symbol.KSAnnotated | ||
|
|
||
| internal interface SnapshotHandler { | ||
| fun process(symbol: KSAnnotated): SnapshotResult? | ||
| } | ||
|
|
15 changes: 15 additions & 0 deletions
15
test-processor/src/main/kotlin/com/faire/ksp/snapshot/snapshots/ClassDeclarationSnapshot.kt
This file contains hidden or 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,15 @@ | ||
| package com.faire.ksp.snapshot.snapshots | ||
|
|
||
| data class ClassDeclarationSnapshot( | ||
| val simpleName: String, | ||
| val qualifiedName: String?, | ||
| val classKind: String, | ||
| val modifiers: List<String>, | ||
| val isCompanionObject: Boolean, | ||
| val packageName: String, | ||
| val origin: String, | ||
| val superTypes: List<String>, | ||
| val properties: List<PropertyDeclarationSnapshot>, | ||
| val functions: List<FunctionDeclarationSnapshot>, | ||
| ) : DeclarationSnapshot | ||
|
|
8 changes: 8 additions & 0 deletions
8
...processor/src/main/kotlin/com/faire/ksp/snapshot/snapshots/FunctionDeclarationSnapshot.kt
This file contains hidden or 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,8 @@ | ||
| package com.faire.ksp.snapshot.snapshots | ||
|
|
||
| data class FunctionDeclarationSnapshot( | ||
| val simpleName: String, | ||
| val returnTypeQualifiedName: String?, | ||
| val parameters: List<ValueParameterSnapshot>, | ||
| val isAbstract: Boolean, | ||
| ) : DeclarationSnapshot |
2 changes: 1 addition & 1 deletion
2
...aire/ksp/snapshot/KSSnapshotExtensions.kt → ...napshot/snapshots/KSSnapshotExtensions.kt
This file contains hidden or 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
8 changes: 8 additions & 0 deletions
8
...processor/src/main/kotlin/com/faire/ksp/snapshot/snapshots/PropertyDeclarationSnapshot.kt
This file contains hidden or 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,8 @@ | ||
| package com.faire.ksp.snapshot.snapshots | ||
|
|
||
| data class PropertyDeclarationSnapshot( | ||
| val simpleName: String, | ||
| val typeQualifiedName: String?, | ||
| val modifiers: List<String>, | ||
| val isMutable: Boolean, | ||
| ) : DeclarationSnapshot |
8 changes: 8 additions & 0 deletions
8
test-processor/src/main/kotlin/com/faire/ksp/snapshot/snapshots/SnapshotResult.kt
This file contains hidden or 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,8 @@ | ||
| package com.faire.ksp.snapshot.snapshots | ||
|
|
||
| internal data class SnapshotResult( | ||
| val qualifiedName: String, | ||
| val simpleName: String, | ||
| val packageName: String, | ||
| val snapshot: DeclarationSnapshot, | ||
| ) |
4 changes: 4 additions & 0 deletions
4
test-processor/src/main/kotlin/com/faire/ksp/snapshot/snapshots/Snapshots.kt
This file contains hidden or 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,4 @@ | ||
| package com.faire.ksp.snapshot.snapshots | ||
|
|
||
| sealed interface DeclarationSnapshot | ||
|
|
8 changes: 8 additions & 0 deletions
8
test-processor/src/main/kotlin/com/faire/ksp/snapshot/snapshots/ValueParameterSnapshot.kt
This file contains hidden or 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,8 @@ | ||
| package com.faire.ksp.snapshot.snapshots | ||
|
|
||
| data class ValueParameterSnapshot( | ||
| val name: String?, | ||
| val typeQualifiedName: String?, | ||
| val isVararg: Boolean, | ||
| val hasDefault: Boolean, | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.