Skip to content

Commit

Permalink
Update wire-swift-generator/src/main/java/com/squareup/wire/swift/Swi…
Browse files Browse the repository at this point in the history
…ftGenerator.kt

Co-authored-by: Benoît Quenaudon <[email protected]>
  • Loading branch information
dnkoutso and oldergod committed Sep 26, 2023
1 parent a8fed3c commit 35a6331
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 35a6331

Please sign in to comment.