diff --git a/wire-swift-generator/api/wire-swift-generator.api b/wire-swift-generator/api/wire-swift-generator.api index e0a1d8cf6d..f4fe5adeff 100644 --- a/wire-swift-generator/api/wire-swift-generator.api +++ b/wire-swift-generator/api/wire-swift-generator.api @@ -4,6 +4,7 @@ public final class com/squareup/wire/swift/SwiftGenerator { public final fun generateTypeTo (Lcom/squareup/wire/schema/Type;Lio/outfoxx/swiftpoet/FileSpec$Builder;)V public final fun generatedTypeName (Lcom/squareup/wire/schema/Type;)Lio/outfoxx/swiftpoet/DeclaredTypeName; public static final fun get (Lcom/squareup/wire/schema/Schema;Ljava/util/Map;)Lcom/squareup/wire/swift/SwiftGenerator; + public final fun getSafeDeclaredTypeName (Lcom/squareup/wire/schema/Type;)Lio/outfoxx/swiftpoet/DeclaredTypeName; public final fun getSchema ()Lcom/squareup/wire/schema/Schema; } @@ -11,6 +12,11 @@ public final class com/squareup/wire/swift/SwiftGenerator$Companion { public final fun builtInType (Lcom/squareup/wire/schema/ProtoType;)Z public final fun get (Lcom/squareup/wire/schema/Schema;Ljava/util/Map;)Lcom/squareup/wire/swift/SwiftGenerator; public static synthetic fun get$default (Lcom/squareup/wire/swift/SwiftGenerator$Companion;Lcom/squareup/wire/schema/Schema;Ljava/util/Map;ILjava/lang/Object;)Lcom/squareup/wire/swift/SwiftGenerator; + public final fun getDeclaredFieldsAndOneOfFields (Lcom/squareup/wire/schema/MessageType;)Ljava/util/List; + public final fun getSafeName (Lcom/squareup/wire/schema/Field;)Ljava/lang/String; + public final fun getSafeName (Lcom/squareup/wire/schema/ProtoType;)Ljava/lang/String; + public final fun isExtensible (Lcom/squareup/wire/schema/MessageType;)Z + public final fun isHeapAllocated (Lcom/squareup/wire/schema/MessageType;)Z } public final class com/squareup/wire/swift/SwiftSchemaHandler : com/squareup/wire/schema/SchemaHandler { 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 b0172e8f1f..665a68d88e 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,13 +159,6 @@ class SwiftGenerator private constructor( else -> isMap || isRepeated } - // TODO use a NameAllocator - private val Field.safeName: String - get() = when (name) { - "description" -> "description_" - else -> name - } - private val Field.codableDefaultValue: String? get() = default?.let { return it @@ -252,6 +245,11 @@ class SwiftGenerator private constructor( } } + val Type.safeDeclaredTypeName: DeclaredTypeName + get() = generatedTypeName(this).let { result -> + if (result.simpleName == "Error") result.peerType("Error_") else result + } + fun generateTypeTo(type: Type, builder: FileSpec.Builder) { val fileMembers = mutableListOf() @@ -294,8 +292,6 @@ class SwiftGenerator private constructor( return field.type in referenceCycleIndirections.getOrDefault(type.type, emptySet()) } - private val MessageType.isHeapAllocated get() = declaredFields.size + oneOfs.size >= 16 - /** * Checks that every enum in a proto3 message contains a value with tag 0. * @@ -1771,6 +1767,31 @@ class SwiftGenerator private constructor( fun builtInType(protoType: ProtoType): Boolean = protoType in BUILT_IN_TYPES.keys + // TODO use a NameAllocator + val ProtoType.safeName: String + get() { + return when (this.simpleName) { + "Type" -> "Type_" + "Error" -> "Error_" + else -> this.simpleName + } + } + + // TODO use a NameAllocator + val Field.safeName: String + get() = when (name) { + "description" -> "description_" + else -> name + } + + val MessageType.isExtensible: Boolean + get() = extensionsList.isNotEmpty() + + val MessageType.declaredFieldsAndOneOfFields: List + get() = declaredFields + oneOfs.flatMap { it.fields } + + val MessageType.isHeapAllocated get() = declaredFields.size + oneOfs.size >= 16 + private val SWIFT_COMMON_TYPES = setOf( "Any", "AnyClass", @@ -1845,32 +1866,17 @@ class SwiftGenerator private constructor( fun putAll(enclosingClassName: DeclaredTypeName?, types: List) { for (type in types) { val protoType = type.type - val simpleName = protoType.simpleName val className = if (enclosingClassName != null) { - // TODO use a NameAllocator - val safeName = if (simpleName == "Type") { - // Foo.Type is effectively reserved for the comppiler - "Type_" - } else { - simpleName - } - - enclosingClassName.nestedType(safeName, alwaysQualify = true) + enclosingClassName.nestedType(protoType.safeName, alwaysQualify = true) } else { - // TODO use a NameAllocator - val safeName = if (simpleName == "Error") { - // Error is _way_ too common to pollute the developer's namespace - "Error_" - } else { - simpleName - } + val safeName = protoType.safeName val moduleName = existingTypeModuleName[protoType] ?: "" // In some cases a proto declares a message that collides with built-in Foundation and Swift stdlib // types. For those we always qualify the type name to disambiguate. - if (simpleName in SWIFT_COMMON_TYPES) { + if (protoType.simpleName in SWIFT_COMMON_TYPES) { DeclaredTypeName.qualifiedTypeName("$moduleName.$safeName") } else { DeclaredTypeName(moduleName, safeName) @@ -1947,11 +1953,5 @@ class SwiftGenerator private constructor( return indirections } - - private val MessageType.isExtensible: Boolean - get() = extensionsList.isNotEmpty() - - private val MessageType.declaredFieldsAndOneOfFields: List - get() = declaredFields + oneOfs.flatMap { it.fields } } } diff --git a/wire-tests-swift/no-manifest/src/main/swift/SwiftEdgeCases.swift b/wire-tests-swift/no-manifest/src/main/swift/SwiftEdgeCases.swift index 71170c8901..48e4db9c4a 100644 --- a/wire-tests-swift/no-manifest/src/main/swift/SwiftEdgeCases.swift +++ b/wire-tests-swift/no-manifest/src/main/swift/SwiftEdgeCases.swift @@ -6,7 +6,7 @@ public struct SwiftEdgeCases { @ProtoDefaulted public var `return`: String? - public var error: SwiftEdgeCases.Error? + public var error: SwiftEdgeCases.Error_? public var type: SwiftEdgeCases.Type_? public var unknownFields: UnknownFields = .init() @@ -48,14 +48,14 @@ extension SwiftEdgeCases : Proto2Codable { public init(from protoReader: ProtoReader) throws { var `return`: String? = nil - var error: SwiftEdgeCases.Error? = nil + var error: SwiftEdgeCases.Error_? = nil var type: SwiftEdgeCases.Type_? = nil let token = try protoReader.beginMessage() while let tag = try protoReader.nextTag(token: token) { switch tag { case 1: `return` = try protoReader.decode(String.self) - case 2: error = try protoReader.decode(SwiftEdgeCases.Error.self) + case 2: error = try protoReader.decode(SwiftEdgeCases.Error_.self) case 3: type = try protoReader.decode(SwiftEdgeCases.Type_.self) default: try protoReader.readUnknownField(tag: tag) } @@ -82,7 +82,7 @@ extension SwiftEdgeCases : Codable { public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: StringLiteralCodingKeys.self) self._return.wrappedValue = try container.decodeIfPresent(String.self, forKey: "return") - self.error = try container.decodeIfPresent(SwiftEdgeCases.Error.self, forKey: "error") + self.error = try container.decodeIfPresent(SwiftEdgeCases.Error_.self, forKey: "error") self.type = try container.decodeIfPresent(SwiftEdgeCases.Type_.self, forKey: "type") } @@ -102,7 +102,7 @@ extension SwiftEdgeCases : Codable { */ extension SwiftEdgeCases { - public enum Error : Int32, CaseIterable, Proto2Enum { + public enum Error_ : Int32, CaseIterable, Proto2Enum { case UNKNOWN = 0 case INNER_BAD_VALUE = 1 @@ -132,7 +132,7 @@ extension SwiftEdgeCases { } -extension SwiftEdgeCases.Error : Sendable { +extension SwiftEdgeCases.Error_ : Sendable { } extension SwiftEdgeCases.Type_ : Sendable {