Only assign setters if present (#1405)

* Only assign setters if present

Rather than generating

```kotlin
val result: Content
result = Content()
result.content = if (contentSet) content else result.content
result.text = if (textSet) text else result.text
```

This will now conditionally set

```kotlin
val result: Content
result = Content()
if (contentSet) {
  result.content = content
}
if (textSet) {
  result.text = text
}
```

* Spotless
This commit is contained in:
Zac Sweers
2021-10-22 12:18:59 -04:00
committed by GitHub
parent 3a88349f34
commit a9eaa849e5

View File

@@ -643,13 +643,14 @@ internal class AdapterGenerator(
continue // Property already handled.
}
if (property.hasLocalIsPresentName) {
result.beginControlFlow("if (%N)", property.localIsPresentName)
result.addStatement(
"%1N.%2N = if (%3N) %4N else %1N.%2N",
"%N.%N = %N",
resultName,
property.name,
property.localIsPresentName,
property.localName
)
result.endControlFlow()
} else {
result.addStatement(
"%1N.%2N = %3N ?: %1N.%2N",