Skip to content

Commit

Permalink
More API tweaking (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacSweers committed Oct 25, 2021
1 parent 3e8a9e9 commit c635505
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public class AutoValueKotlinExtension : AutoValueExtension() {
isRedacted = isClassRedacted,
isParcelable = isParcelable,
superClass = superclass,
interfaces = avClass.interfaces.map { it.asSafeTypeName() },
interfaces = avClass.interfaces.associate { it.asSafeTypeName() to null },
typeParams = avClass.typeParameters.map { it.asTypeVariableName() },
properties = properties,
avkBuilder = avkBuilder,
Expand Down
37 changes: 32 additions & 5 deletions src/main/kotlin/com/slack/auto/value/kotlin/KotlinClass.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public data class KotlinClass(
val isRedacted: Boolean,
val isParcelable: Boolean,
val superClass: TypeName?,
val interfaces: List<TypeName>,
val interfaces: Map<TypeName, CodeBlock?>,
val typeParams: List<TypeVariableName>,
val properties: Map<String, PropertyContext>,
val avkBuilder: AvkBuilder?,
Expand Down Expand Up @@ -117,8 +117,7 @@ public data class KotlinClass(

val defaultCodeBlock = when {
avkBuilder != null -> null // Builders handle the defaults
prop.type.isNullable -> CodeBlock.of("null")
else -> prop.type.defaultPrimitiveValue().takeIf { it.toString() != "null" }
else -> prop.defaultValue
}
constructorBuilder.addParameter(
ParameterSpec.builder(prop.name, prop.type)
Expand Down Expand Up @@ -169,7 +168,13 @@ public data class KotlinClass(
typeBuilder.primaryConstructor(constructorBuilder.build())

superClass?.let { typeBuilder.superclass(it) }
typeBuilder.addSuperinterfaces(interfaces)
interfaces.forEach { (supertype, delegate) ->
if (delegate == null) {
typeBuilder.addSuperinterface(supertype)
} else {
typeBuilder.addSuperinterface(supertype, delegate)
}
}

val companionObjectBuilder = TypeSpec.companionObjectBuilder()
.addProperties(staticConstants)
Expand Down Expand Up @@ -218,22 +223,44 @@ public data class KotlinClass(
val text = outputPath.readText()
// Post-process to remove any kotlin intrinsic types
// Is this wildly inefficient? yes. Does it really matter in our cases? nah
var prevWasBlank = false
outputPath.writeText(
text
.lineSequence()
.filterNot { it in INTRINSIC_IMPORTS }
.map {
.mapNotNull {
if (it.trimStart().startsWith("public ")) {
prevWasBlank = false
val indent = it.substringBefore("public ")
it.removePrefix(indent).removePrefix("public ").prependIndent(indent)
} else if (it.isKotlinPackageImport) {
// Ignore kotlin implicit imports
null
} else if (it.isBlank()) {
if (prevWasBlank) {
null
} else {
prevWasBlank = true
it
}
} else {
prevWasBlank = false
it
}
}
.joinToString("\n")
)
}

/** Best-effort checks if the string is an import from `kotlin.*` */
@Suppress("MagicNumber")
private val String.isKotlinPackageImport: Boolean get() = startsWith("import kotlin.") &&
// Looks like a class
// 14 is the length of `import kotlin.`
get(14).isUpperCase() &&
// Exclude if it's importing a nested element
'.' !in removePrefix("import kotlin.")

private fun FileSpec.writeToLocal(directory: Path): Path {
require(Files.notExists(directory) || Files.isDirectory(directory)) {
"path $directory exists but is not a directory."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.slack.auto.value.kotlin

import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.TypeName

Expand All @@ -30,7 +31,11 @@ public data class PropertyContext(
val forcePropertyOverride: Boolean = false,
val isRedacted: Boolean,
val visibility: KModifier,
val doc: String?
val doc: String?,
val defaultValue: CodeBlock? = when {
type.isNullable -> CodeBlock.of("null")
else -> type.defaultPrimitiveValue().takeIf { it.toString() != "null" }
}
) {
public val usesGet: Boolean = name.startsWith("get") || funName.startsWith("get")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,6 @@ class AutoValueKotlinExtensionTest {
import com.slack.auto.`value`.kotlin.Redacted
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import kotlin.Boolean
import kotlin.Byte
import kotlin.Char
import kotlin.Deprecated
import kotlin.Double
import kotlin.Float
import kotlin.Int
import kotlin.Long
import kotlin.Nothing
import kotlin.ReplaceWith
import kotlin.Short
import kotlin.String
import kotlin.Suppress
import kotlin.jvm.JvmName
import kotlin.jvm.JvmStatic
import kotlin.jvm.JvmSynthetic
Expand Down Expand Up @@ -444,9 +431,6 @@ class AutoValueKotlinExtensionTest {
"""
package test
import kotlin.Deprecated
import kotlin.ReplaceWith
import kotlin.String
import kotlin.jvm.JvmName
import kotlin.jvm.JvmStatic
import kotlin.jvm.JvmSynthetic
Expand Down Expand Up @@ -517,9 +501,6 @@ class AutoValueKotlinExtensionTest {
"""
package test
import kotlin.Deprecated
import kotlin.ReplaceWith
import kotlin.String
import kotlin.jvm.JvmName
import kotlin.jvm.JvmSynthetic
Expand Down Expand Up @@ -623,17 +604,6 @@ class AutoValueKotlinExtensionTest {
"""
package test
import kotlin.Boolean
import kotlin.Byte
import kotlin.Char
import kotlin.Deprecated
import kotlin.Double
import kotlin.Float
import kotlin.Int
import kotlin.Long
import kotlin.ReplaceWith
import kotlin.Short
import kotlin.String
import kotlin.jvm.JvmName
import kotlin.jvm.JvmSynthetic
Expand Down Expand Up @@ -1130,8 +1100,6 @@ class AutoValueKotlinExtensionTest {
"""
package test
import kotlin.String
data class Example(
val `value`: String
)
Expand Down Expand Up @@ -1170,9 +1138,6 @@ class AutoValueKotlinExtensionTest {
"""
package test
import kotlin.Deprecated
import kotlin.ReplaceWith
import kotlin.String
import kotlin.jvm.JvmName
import kotlin.jvm.JvmSynthetic
Expand Down

0 comments on commit c635505

Please sign in to comment.