From 8e7fb83904cbac929efefe9a16cf17a9ce9ab344 Mon Sep 17 00:00:00 2001 From: Dimitris Koutsogiorgas Date: Wed, 11 Oct 2023 10:20:30 -0400 Subject: [PATCH] Update to use FOUNDATION_DATA for default data values --- .../com/squareup/wire/swift/SwiftGenerator.kt | 34 ++++++++++++++++--- .../no-manifest/src/main/swift/AllTypes.swift | 14 ++++---- .../no-manifest/src/main/swift/FooBar.swift | 14 ++++---- .../swift/MessageUsingMultipleEnums.swift | 14 ++++---- .../src/main/swift/OptionalEnumUser.swift | 7 ++-- .../src/main/swift/VersionOne.swift | 7 ++-- .../src/main/swift/VersionTwo.swift | 7 ++-- 7 files changed, 65 insertions(+), 32 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 9f3de9bbc2..905c55a89a 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 @@ -161,10 +161,33 @@ 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 + } ?: when { + !isOptional -> null + typeName == BOOL -> CodeBlock.of("%L", false) + typeName == INT -> CodeBlock.of("%L", 0) + typeName == INT32 -> CodeBlock.of("%L", 0) + typeName == INT64 -> CodeBlock.of("%L", 0) + typeName == UINT32 -> CodeBlock.of("%L", 0) + typeName == UINT64 -> CodeBlock.of("%L", 0) + typeName == FLOAT -> CodeBlock.of("%L", 0) + typeName == DOUBLE -> CodeBlock.of("%L", 0) + typeName == STRING -> CodeBlock.of("%S", "") + typeName == DATA -> CodeBlock.of( + "%T(base64Encoded: %S)!", + FOUNDATION_DATA, + "".encode(charset = Charsets.ISO_8859_1).base64(), + ) + isEnum -> { + val enumType = schema.getType(type!!) as EnumType + CodeBlock.of("%T.%L", typeName.makeNonOptional(), enumType.constants[0].name) + } + isMessage && !isRequiredParameter && !isCollection -> { + val messageType = schema.getType(type!!) as MessageType + if (messageType.fields.any { it.isRequiredParameter }) null else CodeBlock.of("%T()", messageType.typeName) + } + + else -> null + } // see https://protobuf.dev/programming-guides/proto3/#default private val Field.proto3InitialValue: String @@ -1285,7 +1308,8 @@ class SwiftGenerator private constructor( typeName == DOUBLE -> defaultValue.toDoubleFieldInitializer() typeName == STRING -> CodeBlock.of("%S", stringLiteralWithQuotes2(defaultValue.toString())) typeName == DATA -> CodeBlock.of( - "Foundation.Data(base64Encoded: %S)!", + "%T(base64Encoded: %S)!", + FOUNDATION_DATA, defaultValue.toString().encode(charset = Charsets.ISO_8859_1).base64(), ) protoType.isEnum -> CodeBlock.of("%T.%L", typeName, defaultValue) diff --git a/wire-tests-swift/no-manifest/src/main/swift/AllTypes.swift b/wire-tests-swift/no-manifest/src/main/swift/AllTypes.swift index 2fdcf3da41..a1ff917528 100644 --- a/wire-tests-swift/no-manifest/src/main/swift/AllTypes.swift +++ b/wire-tests-swift/no-manifest/src/main/swift/AllTypes.swift @@ -1859,7 +1859,7 @@ extension AllTypes.Storage { self.opt_double = opt_double self.opt_string = opt_string self.opt_bytes = opt_bytes - self.opt_nested_enum = opt_nested_enum + _opt_nested_enum.wrappedValue = opt_nested_enum _opt_nested_message.wrappedValue = opt_nested_message self.req_int32 = req_int32 self.req_uint32 = req_uint32 @@ -1956,7 +1956,7 @@ extension AllTypes.Storage { self.ext_opt_double = ext_opt_double self.ext_opt_string = ext_opt_string self.ext_opt_bytes = ext_opt_bytes - self.ext_opt_nested_enum = ext_opt_nested_enum + _ext_opt_nested_enum.wrappedValue = ext_opt_nested_enum _ext_opt_nested_message.wrappedValue = ext_opt_nested_message self.ext_rep_int32 = ext_rep_int32 self.ext_rep_uint32 = ext_rep_uint32 @@ -2016,6 +2016,7 @@ extension AllTypes { public var opt_double: Swift.Double? public var opt_string: Swift.String? public var opt_bytes: Foundation.Data? + @Wire.Defaulted(defaultValue: AllTypes.NestedEnum.UNKNOWN) public var opt_nested_enum: AllTypes.NestedEnum? @Wire.Defaulted(defaultValue: AllTypes.NestedMessage()) public var opt_nested_message: AllTypes.NestedMessage? @@ -2130,6 +2131,7 @@ extension AllTypes { public var ext_opt_double: Swift.Double? public var ext_opt_string: Swift.String? public var ext_opt_bytes: Foundation.Data? + @Wire.Defaulted(defaultValue: AllTypes.NestedEnum.UNKNOWN) public var ext_opt_nested_enum: AllTypes.NestedEnum? @Wire.Defaulted(defaultValue: AllTypes.NestedMessage()) public var ext_opt_nested_message: AllTypes.NestedMessage? @@ -2550,7 +2552,7 @@ extension AllTypes.Storage : Proto2Codable { self.opt_double = opt_double self.opt_string = opt_string self.opt_bytes = opt_bytes - self.opt_nested_enum = opt_nested_enum + _opt_nested_enum.wrappedValue = opt_nested_enum _opt_nested_message.wrappedValue = opt_nested_message self.req_int32 = try AllTypes.checkIfMissing(req_int32, "req_int32") self.req_uint32 = try AllTypes.checkIfMissing(req_uint32, "req_uint32") @@ -2647,7 +2649,7 @@ extension AllTypes.Storage : Proto2Codable { self.ext_opt_double = ext_opt_double self.ext_opt_string = ext_opt_string self.ext_opt_bytes = ext_opt_bytes - self.ext_opt_nested_enum = ext_opt_nested_enum + _ext_opt_nested_enum.wrappedValue = ext_opt_nested_enum _ext_opt_nested_message.wrappedValue = ext_opt_nested_message self.ext_rep_int32 = ext_rep_int32 self.ext_rep_uint32 = ext_rep_uint32 @@ -2853,7 +2855,7 @@ extension AllTypes.Storage : Codable { self.opt_double = try container.decodeIfPresent(Swift.Double.self, firstOfKeys: "optDouble", "opt_double") self.opt_string = try container.decodeIfPresent(Swift.String.self, firstOfKeys: "optString", "opt_string") self.opt_bytes = try container.decodeIfPresent(stringEncoded: Foundation.Data.self, firstOfKeys: "optBytes", "opt_bytes") - self.opt_nested_enum = try container.decodeIfPresent(AllTypes.NestedEnum.self, firstOfKeys: "optNestedEnum", "opt_nested_enum") + _opt_nested_enum.wrappedValue = try container.decodeIfPresent(AllTypes.NestedEnum.self, firstOfKeys: "optNestedEnum", "opt_nested_enum") _opt_nested_message.wrappedValue = try container.decodeIfPresent(AllTypes.NestedMessage.self, firstOfKeys: "optNestedMessage", "opt_nested_message") self.req_int32 = try container.decode(Swift.Int32.self, firstOfKeys: "reqInt32", "req_int32") self.req_uint32 = try container.decode(Swift.UInt32.self, firstOfKeys: "reqUint32", "req_uint32") @@ -2950,7 +2952,7 @@ extension AllTypes.Storage : Codable { self.ext_opt_double = try container.decodeIfPresent(Swift.Double.self, firstOfKeys: "extOptDouble", "ext_opt_double") self.ext_opt_string = try container.decodeIfPresent(Swift.String.self, firstOfKeys: "extOptString", "ext_opt_string") self.ext_opt_bytes = try container.decodeIfPresent(stringEncoded: Foundation.Data.self, firstOfKeys: "extOptBytes", "ext_opt_bytes") - self.ext_opt_nested_enum = try container.decodeIfPresent(AllTypes.NestedEnum.self, firstOfKeys: "extOptNestedEnum", "ext_opt_nested_enum") + _ext_opt_nested_enum.wrappedValue = try container.decodeIfPresent(AllTypes.NestedEnum.self, firstOfKeys: "extOptNestedEnum", "ext_opt_nested_enum") _ext_opt_nested_message.wrappedValue = try container.decodeIfPresent(AllTypes.NestedMessage.self, firstOfKeys: "extOptNestedMessage", "ext_opt_nested_message") self.ext_rep_int32 = try container.decodeProtoArray(Swift.Int32.self, firstOfKeys: "extRepInt32", "ext_rep_int32") self.ext_rep_uint32 = try container.decodeProtoArray(Swift.UInt32.self, firstOfKeys: "extRepUint32", "ext_rep_uint32") diff --git a/wire-tests-swift/no-manifest/src/main/swift/FooBar.swift b/wire-tests-swift/no-manifest/src/main/swift/FooBar.swift index 4cd1bc7757..676af04e06 100644 --- a/wire-tests-swift/no-manifest/src/main/swift/FooBar.swift +++ b/wire-tests-swift/no-manifest/src/main/swift/FooBar.swift @@ -13,6 +13,7 @@ public struct FooBar { public var fred: [Float] = [] public var daisy: Double? public var nested: [FooBar] = [] + @Defaulted(defaultValue: FooBar.FooBarBazEnum.FOO) public var ext: FooBar.FooBarBazEnum? public var rep: [FooBar.FooBarBazEnum] = [] public var more_string: String? @@ -48,7 +49,7 @@ extension FooBar { self.fred = fred self.daisy = daisy self.nested = nested - self.ext = ext + _ext.wrappedValue = ext self.rep = rep self.more_string = more_string } @@ -130,7 +131,7 @@ extension FooBar : Proto2Codable { self.fred = fred self.daisy = daisy self.nested = nested - self.ext = ext + _ext.wrappedValue = ext self.rep = rep self.more_string = more_string } @@ -163,7 +164,7 @@ extension FooBar : Codable { self.fred = try container.decodeProtoArray(Swift.Float.self, forKey: "fred") self.daisy = try container.decodeIfPresent(Swift.Double.self, forKey: "daisy") self.nested = try container.decodeProtoArray(FooBar.self, forKey: "nested") - self.ext = try container.decodeIfPresent(FooBar.FooBarBazEnum.self, forKey: "ext") + _ext.wrappedValue = try container.decodeIfPresent(FooBar.FooBarBazEnum.self, forKey: "ext") self.rep = try container.decodeProtoArray(FooBar.FooBarBazEnum.self, forKey: "rep") self.more_string = try container.decodeIfPresent(Swift.String.self, firstOfKeys: "moreString", "more_string") } @@ -201,6 +202,7 @@ extension FooBar { public struct Nested { + @Wire.Defaulted(defaultValue: FooBar.FooBarBazEnum.FOO) public var value: FooBar.FooBarBazEnum? public var unknownFields: Foundation.Data = .init() @@ -245,7 +247,7 @@ extension FooBar.Nested { @_disfavoredOverload @available(*, deprecated) public init(value: FooBar.FooBarBazEnum? = nil) { - self.value = value + _value.wrappedValue = value } } @@ -288,7 +290,7 @@ extension FooBar.Nested : Proto2Codable { } self.unknownFields = try protoReader.endMessage(token: token) - self.value = value + _value.wrappedValue = value } public func encode(to protoWriter: Wire.ProtoWriter) throws { @@ -303,7 +305,7 @@ extension FooBar.Nested : Codable { public init(from decoder: Swift.Decoder) throws { let container = try decoder.container(keyedBy: Wire.StringLiteralCodingKeys.self) - self.value = try container.decodeIfPresent(FooBar.FooBarBazEnum.self, forKey: "value") + _value.wrappedValue = try container.decodeIfPresent(FooBar.FooBarBazEnum.self, forKey: "value") } public func encode(to encoder: Swift.Encoder) throws { diff --git a/wire-tests-swift/no-manifest/src/main/swift/MessageUsingMultipleEnums.swift b/wire-tests-swift/no-manifest/src/main/swift/MessageUsingMultipleEnums.swift index b818f5f0b5..fc5b9e7b7c 100644 --- a/wire-tests-swift/no-manifest/src/main/swift/MessageUsingMultipleEnums.swift +++ b/wire-tests-swift/no-manifest/src/main/swift/MessageUsingMultipleEnums.swift @@ -8,7 +8,9 @@ import Wire */ public struct MessageUsingMultipleEnums { + @Defaulted(defaultValue: MessageWithStatus.Status.A) public var a: MessageWithStatus.Status? + @Defaulted(defaultValue: OtherMessageWithStatus.Status.A) public var b: OtherMessageWithStatus.Status? public var unknownFields: Foundation.Data = .init() @@ -24,8 +26,8 @@ extension MessageUsingMultipleEnums { @_disfavoredOverload @available(*, deprecated) public init(a: MessageWithStatus.Status? = nil, b: OtherMessageWithStatus.Status? = nil) { - self.a = a - self.b = b + _a.wrappedValue = a + _b.wrappedValue = b } } @@ -70,8 +72,8 @@ extension MessageUsingMultipleEnums : Proto2Codable { } self.unknownFields = try protoReader.endMessage(token: token) - self.a = a - self.b = b + _a.wrappedValue = a + _b.wrappedValue = b } public func encode(to protoWriter: Wire.ProtoWriter) throws { @@ -87,8 +89,8 @@ extension MessageUsingMultipleEnums : Codable { public init(from decoder: Swift.Decoder) throws { let container = try decoder.container(keyedBy: Wire.StringLiteralCodingKeys.self) - self.a = try container.decodeIfPresent(MessageWithStatus.Status.self, forKey: "a") - self.b = try container.decodeIfPresent(OtherMessageWithStatus.Status.self, forKey: "b") + _a.wrappedValue = try container.decodeIfPresent(MessageWithStatus.Status.self, forKey: "a") + _b.wrappedValue = try container.decodeIfPresent(OtherMessageWithStatus.Status.self, forKey: "b") } public func encode(to encoder: Swift.Encoder) throws { diff --git a/wire-tests-swift/no-manifest/src/main/swift/OptionalEnumUser.swift b/wire-tests-swift/no-manifest/src/main/swift/OptionalEnumUser.swift index cadf2f2e9a..6829068a7d 100644 --- a/wire-tests-swift/no-manifest/src/main/swift/OptionalEnumUser.swift +++ b/wire-tests-swift/no-manifest/src/main/swift/OptionalEnumUser.swift @@ -5,6 +5,7 @@ import Wire public struct OptionalEnumUser { + @Defaulted(defaultValue: OptionalEnumUser.OptionalEnum.FOO) public var optional_enum: OptionalEnumUser.OptionalEnum? public var unknownFields: Foundation.Data = .init() @@ -20,7 +21,7 @@ extension OptionalEnumUser { @_disfavoredOverload @available(*, deprecated) public init(optional_enum: OptionalEnumUser.OptionalEnum? = nil) { - self.optional_enum = optional_enum + _optional_enum.wrappedValue = optional_enum } } @@ -63,7 +64,7 @@ extension OptionalEnumUser : Proto2Codable { } self.unknownFields = try protoReader.endMessage(token: token) - self.optional_enum = optional_enum + _optional_enum.wrappedValue = optional_enum } public func encode(to protoWriter: Wire.ProtoWriter) throws { @@ -78,7 +79,7 @@ extension OptionalEnumUser : Codable { public init(from decoder: Swift.Decoder) throws { let container = try decoder.container(keyedBy: Wire.StringLiteralCodingKeys.self) - self.optional_enum = try container.decodeIfPresent(OptionalEnumUser.OptionalEnum.self, firstOfKeys: "optionalEnum", "optional_enum") + _optional_enum.wrappedValue = try container.decodeIfPresent(OptionalEnumUser.OptionalEnum.self, firstOfKeys: "optionalEnum", "optional_enum") } public func encode(to encoder: Swift.Encoder) throws { diff --git a/wire-tests-swift/no-manifest/src/main/swift/VersionOne.swift b/wire-tests-swift/no-manifest/src/main/swift/VersionOne.swift index dae0dbb476..add222899b 100644 --- a/wire-tests-swift/no-manifest/src/main/swift/VersionOne.swift +++ b/wire-tests-swift/no-manifest/src/main/swift/VersionOne.swift @@ -8,6 +8,7 @@ public struct VersionOne { public var i: Int32? @Defaulted(defaultValue: NestedVersionOne()) public var obj: NestedVersionOne? + @Defaulted(defaultValue: EnumVersionOne.SHREK_V1) public var en: EnumVersionOne? public var unknownFields: Foundation.Data = .init() @@ -29,7 +30,7 @@ extension VersionOne { ) { self.i = i _obj.wrappedValue = obj - self.en = en + _en.wrappedValue = en } } @@ -78,7 +79,7 @@ extension VersionOne : Proto2Codable { self.i = i _obj.wrappedValue = obj - self.en = en + _en.wrappedValue = en } public func encode(to protoWriter: Wire.ProtoWriter) throws { @@ -97,7 +98,7 @@ extension VersionOne : Codable { let container = try decoder.container(keyedBy: Wire.StringLiteralCodingKeys.self) self.i = try container.decodeIfPresent(Swift.Int32.self, forKey: "i") _obj.wrappedValue = try container.decodeIfPresent(NestedVersionOne.self, forKey: "obj") - self.en = try container.decodeIfPresent(EnumVersionOne.self, forKey: "en") + _en.wrappedValue = try container.decodeIfPresent(EnumVersionOne.self, forKey: "en") } public func encode(to encoder: Swift.Encoder) throws { diff --git a/wire-tests-swift/no-manifest/src/main/swift/VersionTwo.swift b/wire-tests-swift/no-manifest/src/main/swift/VersionTwo.swift index 9deac6ff8b..7bbf88bf47 100644 --- a/wire-tests-swift/no-manifest/src/main/swift/VersionTwo.swift +++ b/wire-tests-swift/no-manifest/src/main/swift/VersionTwo.swift @@ -13,6 +13,7 @@ public struct VersionTwo { public var v2_rs: [String] = [] @Defaulted(defaultValue: NestedVersionTwo()) public var obj: NestedVersionTwo? + @Defaulted(defaultValue: EnumVersionTwo.SHREK_V2) public var en: EnumVersionTwo? public var unknownFields: Foundation.Data = .init() @@ -44,7 +45,7 @@ extension VersionTwo { self.v2_f64 = v2_f64 self.v2_rs = v2_rs _obj.wrappedValue = obj - self.en = en + _en.wrappedValue = en } } @@ -108,7 +109,7 @@ extension VersionTwo : Proto2Codable { self.v2_f64 = v2_f64 self.v2_rs = v2_rs _obj.wrappedValue = obj - self.en = en + _en.wrappedValue = en } public func encode(to protoWriter: Wire.ProtoWriter) throws { @@ -137,7 +138,7 @@ extension VersionTwo : Codable { self.v2_f64 = try container.decodeIfPresent(stringEncoded: Swift.UInt64.self, firstOfKeys: "v2F64", "v2_f64") self.v2_rs = try container.decodeProtoArray(Swift.String.self, firstOfKeys: "v2Rs", "v2_rs") _obj.wrappedValue = try container.decodeIfPresent(NestedVersionTwo.self, forKey: "obj") - self.en = try container.decodeIfPresent(EnumVersionTwo.self, forKey: "en") + _en.wrappedValue = try container.decodeIfPresent(EnumVersionTwo.self, forKey: "en") } public func encode(to encoder: Swift.Encoder) throws {