From a9eaa849e58bd476ce80c30e4e4ed7788c97202e Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Fri, 22 Oct 2021 12:18:59 -0400 Subject: [PATCH] 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 --- .../squareup/moshi/kotlin/codegen/api/AdapterGenerator.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/AdapterGenerator.kt b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/AdapterGenerator.kt index 6c63e62..d9a3f48 100644 --- a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/AdapterGenerator.kt +++ b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/AdapterGenerator.kt @@ -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",