Skip to content

Commit 591fa0c

Browse files
authored
Merge pull request #37 from tobrun/kdz-fix-dokka
Fixes to make Dokka happy
2 parents c100e35 + 31f5b38 commit 591fa0c

File tree

5 files changed

+262
-149
lines changed

5 files changed

+262
-149
lines changed

README.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ And you will have to include the required dependencies:
2828

2929
```groovy
3030
dependencies {
31-
implementation 'com.github.tobrun.kotlin-data-compat:annotation:0.5.1'
32-
ksp 'com.github.tobrun.kotlin-data-compat:processor:0.5.1'
31+
implementation 'com.github.tobrun.kotlin-data-compat:annotation:0.5.2'
32+
ksp 'com.github.tobrun.kotlin-data-compat:processor:0.5.2'
3333
}
3434
```
3535

@@ -62,6 +62,9 @@ annotation class SampleAnnotation
6262
private data class PersonData(
6363
@Default("\"John\" + Date(1580897313933L).toString()")
6464
val name: String,
65+
/**
66+
* Additional comment.
67+
*/
6568
val nickname: String?,
6669
@Default("42")
6770
val age: Int
@@ -90,20 +93,39 @@ import kotlin.jvm.JvmSynthetic
9093
*/
9194
@SampleAnnotation
9295
public class Person private constructor(
96+
/**
97+
* The full name.
98+
*/
9399
public val name: String,
100+
/**
101+
* The nickname.
102+
* Additional comment.
103+
*/
94104
public val nickname: String?,
105+
/**
106+
* The age.
107+
*/
95108
public val age: Int
96109
) : SampleInterface {
110+
/**
111+
* Overloaded toString function.
112+
*/
97113
public override fun toString() = """Person(name=$name, nickname=$nickname,
98114
age=$age)""".trimIndent()
99115

116+
/**
117+
* Overloaded equals function.
118+
*/
100119
public override fun equals(other: Any?): Boolean {
101120
if (this === other) return true
102121
if (javaClass != other?.javaClass) return false
103122
other as Person
104123
return name == other.name && nickname == other.nickname && age == other.age
105124
}
106125

126+
/**
127+
* Overloaded hashCode function based on all class properties.
128+
*/
107129
public override fun hashCode(): Int = Objects.hash(name, nickname, age)
108130

109131
/**
@@ -121,12 +143,22 @@ public class Person private constructor(
121143
* @property age The age.
122144
*/
123145
public class Builder {
146+
/**
147+
* The full name.
148+
*/
124149
@set:JvmSynthetic
125150
public var name: String? = "John" + Date(1580897313933L).toString()
126151

152+
/**
153+
* The nickname.
154+
* Additional comment.
155+
*/
127156
@set:JvmSynthetic
128157
public var nickname: String? = null
129158

159+
/**
160+
* The age.
161+
*/
130162
@set:JvmSynthetic
131163
public var age: Int? = 42
132164

@@ -143,6 +175,7 @@ public class Person private constructor(
143175

144176
/**
145177
* Set the nickname.
178+
* Additional comment.
146179
*
147180
* @param nickname the nickname.
148181
* @return Builder

example/src/main/kotlin/com/tobrun/data/compat/example/PersonData.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ annotation class SampleAnnotation
1717
private data class PersonData(
1818
@Default("\"John\" + Date(1580897313933L).toString()")
1919
val name: String,
20+
/**
21+
* Additional comment.
22+
*/
2023
val nickname: String?,
2124
@Default("42")
2225
val age: Int

processor/src/main/kotlin/com/tobrun/datacompat/DataCompatProcessor.kt

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ class DataCompatProcessor(
182182
for (entry in propertyMap) {
183183
addProperty(
184184
PropertySpec.builder(entry.key.toString(), entry.value)
185+
.addKdoc(
186+
"""
187+
|${getKDocProperty(kdocPropertyList, entry.key.toString()).replaceFirstChar {
188+
if (it.isLowerCase()) it.titlecase(
189+
Locale.getDefault()
190+
) else it.toString()
191+
}}
192+
|${entry.key.docString?.trimStart(' ', '\n') ?: ""}
193+
""".trimMargin()
194+
)
185195
.initializer(entry.key.toString())
186196
.build()
187197
)
@@ -191,6 +201,11 @@ class DataCompatProcessor(
191201
addFunction(
192202
FunSpec.builder("toString")
193203
.addModifiers(KModifier.OVERRIDE)
204+
.addKdoc(
205+
"""
206+
Overloaded toString function.
207+
""".trimIndent()
208+
)
194209
// using triple quote for long strings
195210
.addStatement(
196211
propertyMap.keys.joinToString(
@@ -205,6 +220,11 @@ class DataCompatProcessor(
205220
// Function equals
206221
val equalsBuilder = FunSpec.builder("equals")
207222
.addModifiers(KModifier.OVERRIDE)
223+
.addKdoc(
224+
"""
225+
Overloaded equals function.
226+
""".trimIndent()
227+
)
208228
.addParameter("other", ANY.copy(nullable = true))
209229
.addStatement("if (this === other) return true")
210230
.addStatement("if (javaClass != other?.javaClass) return false")
@@ -223,6 +243,11 @@ class DataCompatProcessor(
223243
// Function hashCode
224244
addFunction(
225245
FunSpec.builder("hashCode")
246+
.addKdoc(
247+
"""
248+
Overloaded hashCode function based on all class properties.
249+
""".trimIndent()
250+
)
226251
.addModifiers(KModifier.OVERRIDE)
227252
.addStatement(
228253
propertyMap.keys.joinToString(
@@ -265,15 +290,25 @@ class DataCompatProcessor(
265290
val builderBuilder = TypeSpec.classBuilder("Builder")
266291
for (property in propertyMap) {
267292
val propertyName = property.key.toString()
268-
269293
val nullableType = property.value.copy(nullable = true)
294+
val kDocProperty = getKDocProperty(kdocPropertyList, propertyName)
270295
builderBuilder.addProperty(
271296
PropertySpec.builder(propertyName, nullableType)
272297
.initializer(
273298
CodeBlock.builder()
274299
.add(defaultValuesMap[classDeclaration]?.get(propertyName) ?: "null")
275300
.build()
276301
)
302+
.addKdoc(
303+
"""
304+
|${kDocProperty.replaceFirstChar {
305+
if (it.isLowerCase()) it.titlecase(
306+
Locale.getDefault()
307+
) else it.toString()
308+
}}
309+
|${property.key.docString?.trimStart(' ', '\n') ?: ""}
310+
""".trimMargin()
311+
)
277312
.addAnnotation(
278313
AnnotationSpec.builder(JvmSynthetic::class)
279314
.useSiteTarget(AnnotationSpec.UseSiteTarget.SET)
@@ -283,16 +318,6 @@ class DataCompatProcessor(
283318
.build()
284319
)
285320

286-
var kDocProperty = kdocPropertyList
287-
.filter { it.startsWith("$propertyName ") }
288-
.joinToString {
289-
it.substringAfter("$propertyName ").lowercase(Locale.getDefault())
290-
}
291-
292-
if (kDocProperty.isEmpty()) {
293-
kDocProperty = propertyName
294-
}
295-
296321
builderBuilder.addFunction(
297322
FunSpec
298323
.builder(
@@ -303,7 +328,7 @@ class DataCompatProcessor(
303328
.addKdoc(
304329
"""
305330
|Set $kDocProperty
306-
|
331+
|${property.key.docString?.trimStart(' ', '\n') ?: ""}
307332
|@param $propertyName $kDocProperty
308333
|@return Builder
309334
""".trimMargin()
@@ -356,7 +381,7 @@ class DataCompatProcessor(
356381
|
357382
|${
358383
kdocPropertyList.joinToString(
359-
prefix = "$KDOC_PROPERTY_ANNOTATION ",
384+
prefix = if (kdocPropertyList.isEmpty()) "" else "$KDOC_PROPERTY_ANNOTATION ",
360385
separator = "\n$KDOC_PROPERTY_ANNOTATION "
361386
)
362387
}
@@ -454,6 +479,19 @@ class DataCompatProcessor(
454479

455480
private fun KSClassDeclaration.isDataClass() = modifiers.contains(Modifier.DATA)
456481

482+
private fun getKDocProperty(kdocPropertyList: List<String>, propertyName: String): String {
483+
var kDocProperty = kdocPropertyList
484+
.filter { it.startsWith("$propertyName ") }
485+
.joinToString {
486+
it.substringAfter("$propertyName ").lowercase(Locale.getDefault())
487+
}
488+
489+
if (kDocProperty.isEmpty()) {
490+
kDocProperty = propertyName
491+
}
492+
return kDocProperty
493+
}
494+
457495
private companion object {
458496
private const val CLASS_NAME_DROP_LAST_CHARACTERS = 4
459497
private const val KDOC_PROPERTY_ANNOTATION = "@property"

processor/src/test/kotlin/com/tobrun/datacompat/DataCompatProcessorTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ private data class PersonData(
3434
val nickname: String?,
3535
@Default("23")
3636
val age: Int,
37+
/**
38+
* Actually it's a very short description.
39+
*/
3740
val veryLongAndVeryDetailedDescription: String?
3841
) : EmptyInterface, EmptyInterface2
3942
""".trimIndent()

0 commit comments

Comments
 (0)