Skip to content

Commit

Permalink
Update to use FOUNDATION_DATA for default data values
Browse files Browse the repository at this point in the history
  • Loading branch information
dnkoutso committed Oct 11, 2023
1 parent 20e9b50 commit 8e7fb83
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 8 additions & 6 deletions wire-tests-swift/no-manifest/src/main/swift/AllTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
14 changes: 8 additions & 6 deletions wire-tests-swift/no-manifest/src/main/swift/FooBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -245,7 +247,7 @@ extension FooBar.Nested {
@_disfavoredOverload
@available(*, deprecated)
public init(value: FooBar.FooBarBazEnum? = nil) {
self.value = value
_value.wrappedValue = value
}

}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
}

}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
}

}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions wire-tests-swift/no-manifest/src/main/swift/VersionOne.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -29,7 +30,7 @@ extension VersionOne {
) {
self.i = i
_obj.wrappedValue = obj
self.en = en
_en.wrappedValue = en
}

}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions wire-tests-swift/no-manifest/src/main/swift/VersionTwo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
}

}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8e7fb83

Please sign in to comment.