Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[swift] Expose a few helper methods from SwiftGenerator #3115

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions wire-swift-generator/api/wire-swift-generator.api
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ 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;
}

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

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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<Field>
get() = declaredFields + oneOfs.flatMap { it.fields }

val MessageType.isHeapAllocated get() = declaredFields.size + oneOfs.size >= 16

private val SWIFT_COMMON_TYPES = setOf(
"Any",
"AnyClass",
Expand Down Expand Up @@ -1845,32 +1866,17 @@ class SwiftGenerator private constructor(
fun putAll(enclosingClassName: DeclaredTypeName?, types: List<Type>) {
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)
Expand Down Expand Up @@ -1947,11 +1953,5 @@ class SwiftGenerator private constructor(

return indirections
}

private val MessageType.isExtensible: Boolean
get() = extensionsList.isNotEmpty()

private val MessageType.declaredFieldsAndOneOfFields: List<Field>
get() = declaredFields + oneOfs.flatMap { it.fields }
}
}
12 changes: 6 additions & 6 deletions wire-tests-swift/no-manifest/src/main/swift/SwiftEdgeCases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public struct SwiftEdgeCases {

@ProtoDefaulted
public var `return`: String?
public var error: SwiftEdgeCases.Error?
public var error: SwiftEdgeCases.Error_?
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lickel the fact that I consolidated this under a single helper method it now affected enums just like it did messages, does it look good to you as a change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it also looks like enums called Type are also emitted Type_ so this looks more aligned now

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

public var type: SwiftEdgeCases.Type_?
public var unknownFields: UnknownFields = .init()

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

Expand All @@ -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
Expand Down Expand Up @@ -132,7 +132,7 @@ extension SwiftEdgeCases {

}

extension SwiftEdgeCases.Error : Sendable {
extension SwiftEdgeCases.Error_ : Sendable {
}

extension SwiftEdgeCases.Type_ : Sendable {
Expand Down