Skip to content

Commit

Permalink
Merge pull request #32 from tobrun/kdz-retain-imports
Browse files Browse the repository at this point in the history
Add import support for default parameters
  • Loading branch information
kiryldz authored Mar 13, 2023
2 parents 169045b + a66f2ee commit 37349dd
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 11 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ And you will have to include the required dependencies:

```groovy
dependencies {
implementation 'com.github.tobrun.kotlin-data-compat:annotation:0.3.0'
ksp 'com.github.tobrun.kotlin-data-compat:processor:0.3.0'
implementation 'com.github.tobrun.kotlin-data-compat:annotation:0.4.0'
ksp 'com.github.tobrun.kotlin-data-compat:processor:0.4.0'
}
```

Expand All @@ -41,6 +41,7 @@ Given an exisiting data class:
- mark class private
- append `Data` to the class name
- support default parameters by using `@Default` annotation
- support imports for default parameters
- retain existing class annotations (but not parameters for them)
- retain existing interfaces

Expand All @@ -59,7 +60,7 @@ annotation class SampleAnnotation
@DataCompat
@SampleAnnotation
private data class PersonData(
@Default("\"John\"")
@Default("\"John\" + Date(1580897313933L).toString()", imports = ["java.util.Date"])
val name: String,
val nickname: String?,
@Default("42")
Expand All @@ -72,6 +73,7 @@ After compilation, the following class will be generated:
```kotlin
package com.tobrun.`data`.compat.example

import java.util.Date
import java.util.Objects
import kotlin.Any
import kotlin.Boolean
Expand Down Expand Up @@ -120,7 +122,7 @@ public class Person private constructor(
*/
public class Builder {
@set:JvmSynthetic
public var name: String? = "John"
public var name: String? = "John" + Date(1580897313933L).toString()

@set:JvmSynthetic
public var nickname: String? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ package com.tobrun.datacompat.annotation
* @param valueAsString exact representation of the default value. E.g. if default [String] is used,
* it should be passed here as "\"STRING_VALUE\""; if default [Int] is used, it should be passed
* as "INT_VALUE".
*
* @param imports if default parameter requires additional imports, they should be passed here.
* E.g. `listOf("android.graphics.Color")`
*/
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.VALUE_PARAMETER)
annotation class Default(val valueAsString: String)
annotation class Default(
val valueAsString: String,
val imports: Array<String> = []
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ annotation class SampleAnnotation
@DataCompat
@SampleAnnotation
private data class PersonData(
@Default("\"John\"")
@Default("\"John\" + Date(1580897313933L).toString()", imports = ["java.util.Date"])
val name: String,
val nickname: String?,
@Default("42")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class DataCompatProcessor(
val implementedInterfaces = classDeclaration
.superTypes
.filter { (it.resolve().declaration as? KSClassDeclaration)?.classKind == ClassKind.INTERFACE }
val imports = ArrayList<String>()

// Map KSP properties with KoltinPoet TypeNames
val propertyMap = mutableMapOf<KSPropertyDeclaration, TypeName>()
Expand Down Expand Up @@ -219,9 +220,13 @@ class DataCompatProcessor(
val builderBuilder = TypeSpec.classBuilder("Builder")
for (property in propertyMap) {
val propertyName = property.key.toString()
val defaultValue = property.key.annotations
val defaultAnnotationsParams = property.key.annotations
.firstOrNull { it.annotationType.resolve().toString() == Default::class.simpleName }
?.arguments?.first()
?.arguments
val defaultValue = defaultAnnotationsParams?.first()
defaultAnnotationsParams?.getOrNull(1)?.value?.let {
imports.addAll(it as ArrayList<String>)
}
val nullableType = property.value.copy(nullable = true)
builderBuilder.addProperty(
PropertySpec.builder(propertyName, nullableType)
Expand Down Expand Up @@ -350,6 +355,14 @@ class DataCompatProcessor(
.addType(classBuilder.build())
.addFunction(initializerFunctionBuilder.build())

imports.forEach {
fileBuilder
.addImport(
it.split(".").dropLast(1).joinToString("."),
it.split(".").last()
)
}

fileBuilder.build().writeTo(codeGenerator = codeGenerator, aggregating = false)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface EmptyInterface2
@Deprecated
@DataCompat
private data class PersonData(
@Default("\"John\"")
@Default("\"John\"", ["java.util.Date"])
val name: String,
@Default("null")
val nickname: String?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tobrun.datacompat

internal val expectedSimpleTestContent = """
import java.util.Date
import java.util.Objects
import kotlin.Any
import kotlin.Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package com.tobrun.datacompat.annotation
annotation class DataCompat
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.CLASS)
annotation class Default(val valueAsString: String)
@Target(AnnotationTarget.VALUE_PARAMETER)
annotation class Default(val valueAsString: String, val importList: Array<String> = [])
""".trimIndent()
)

0 comments on commit 37349dd

Please sign in to comment.