From b1b41ec9585779ffa04807fb9a55ed7c85b2c0e0 Mon Sep 17 00:00:00 2001 From: Dimitris Koutsogiorgas Date: Mon, 25 Sep 2023 10:58:50 -0700 Subject: [PATCH] Update wire-swift-generator/src/main/java/com/squareup/wire/swift/SwiftGenerator.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: BenoƮt Quenaudon --- .../com/squareup/wire/swift/SwiftGenerator.kt | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/wire-swift-generator/src/main/java/com/squareup/wire/swift/SwiftGenerator.kt b/wire-swift-generator/src/main/java/com/squareup/wire/swift/SwiftGenerator.kt index ff37514358..66c991ce61 100644 --- a/wire-swift-generator/src/main/java/com/squareup/wire/swift/SwiftGenerator.kt +++ b/wire-swift-generator/src/main/java/com/squareup/wire/swift/SwiftGenerator.kt @@ -159,12 +159,18 @@ class SwiftGenerator private constructor( } private val Field.defaultedValue: CodeBlock? - get() = default?.let { - return defaultFieldInitializer(type!!, it) - } ?: if (isMessage && !isRequiredParameter && !isCollection) { - val subType = schema.getType(type!!) as MessageType - if (subType!!.fields.all { !it.isRequiredParameter }) CodeBlock.of("%T()", subType.typeName) else null - } else null + get() { + when (val default = default) { + null -> { + if (!isMessage || isRequiredParameter || isCollection) return null + val subType = schema.getType(type!!) as MessageType + if (subType.fields.any { it.isRequiredParameter }) return null + return CodeBlock.of("%T()", subType.typeName) + } + + else -> return defaultFieldInitializer(type!!, default) + } + } // see https://protobuf.dev/programming-guides/proto3/#default private val Field.proto3InitialValue: String