Skip to content

Commit 999add0

Browse files
authored
feat: SwiftWriter imports types that are written to code (#745)
1 parent 71a0045 commit 999add0

File tree

7 files changed

+58
-47
lines changed

7 files changed

+58
-47
lines changed

Sources/WeatherSDK/EndpointResolver.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Code generated by smithy-swift-codegen. DO NOT EDIT!
22

33
import ClientRuntime
4+
import protocol ClientRuntime.EndpointsAuthSchemeResolver
5+
import struct ClientRuntime.DefaultEndpointsAuthSchemeResolver
46

57
public struct EndpointParams {
68
/// docs

Sources/WeatherSDK/WeatherClient.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ import SmithyJSON
77
import SmithyReadWrite
88
import SmithyRetries
99
import SmithyRetriesAPI
10+
import enum ClientRuntime.ClientLogMode
11+
import enum ClientRuntime.DefaultRetryErrorInfoProvider
12+
import enum ClientRuntime.SDKLogLevel
13+
import protocol ClientRuntime.IdempotencyTokenGenerator
14+
import protocol ClientRuntime.LogAgent
15+
import protocol ClientRuntime.SDKLogHandlerFactory
16+
import protocol ClientRuntime.TelemetryProvider
1017

1118
public class WeatherClient: Client {
1219
public static let clientName = "WeatherClient"

Sources/WeatherSDK/models/ListCitiesInput+QueryItemProvider.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Code generated by smithy-swift-codegen. DO NOT EDIT!
22

33
import ClientRuntime
4+
import struct ClientRuntime.SDKURLQueryItem
45

56
extension ListCitiesInput {
67

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/ClientRuntimeTypes.kt

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,35 +77,36 @@ object ClientRuntimeTypes {
7777
}
7878

7979
object Core {
80-
val Endpoint = runtimeSymbol("Endpoint")
81-
val Date = runtimeSymbol("Date")
82-
val Data = runtimeSymbol("Data")
83-
val SDKURLQueryItem = runtimeSymbol("SDKURLQueryItem")
84-
val URL = runtimeSymbol("URL")
85-
val ModeledError = runtimeSymbol("ModeledError")
80+
val Endpoint = runtimeSymbol("Endpoint", SwiftDeclaration.STRUCT)
81+
val Date = runtimeSymbol("Date", SwiftDeclaration.STRUCT)
82+
val Data = runtimeSymbol("Data", SwiftDeclaration.STRUCT)
83+
val SDKURLQueryItem = runtimeSymbol("SDKURLQueryItem", SwiftDeclaration.STRUCT)
84+
val URL = runtimeSymbol("URL", SwiftDeclaration.STRUCT)
85+
val ModeledError = runtimeSymbol("ModeledError", SwiftDeclaration.PROTOCOL)
8686
val UnknownClientError = runtimeSymbol("ClientError.unknownError")
87-
val ServiceError = runtimeSymbol("ServiceError")
88-
val Logger = runtimeSymbol("LogAgent")
89-
val TelemetryProvider = runtimeSymbol("TelemetryProvider")
90-
val SDKLogHandlerFactory = runtimeSymbol("SDKLogHandlerFactory")
91-
val SDKLogLevel = runtimeSymbol("SDKLogLevel")
92-
val ClientLogMode = runtimeSymbol("ClientLogMode")
93-
val IdempotencyTokenGenerator = runtimeSymbol("IdempotencyTokenGenerator")
94-
val DefaultRetryErrorInfoProvider = runtimeSymbol("DefaultRetryErrorInfoProvider")
95-
val PaginateToken = runtimeSymbol("PaginateToken")
96-
val PaginatorSequence = runtimeSymbol("PaginatorSequence")
97-
val EndpointsRuleEngine = runtimeSymbol("EndpointsRuleEngine")
98-
val EndpointsRequestContext = runtimeSymbol("EndpointsRequestContext")
99-
val PartitionDefinition = runtimeSymbol("partitionJSON")
100-
val EndpointsAuthSchemeResolver = runtimeSymbol("EndpointsAuthSchemeResolver")
101-
val DefaultEndpointsAuthSchemeResolver = runtimeSymbol("DefaultEndpointsAuthSchemeResolver")
102-
val EndpointsAuthScheme = runtimeSymbol("EndpointsAuthScheme")
87+
val ServiceError = runtimeSymbol("ServiceError", SwiftDeclaration.PROTOCOL)
88+
val Logger = runtimeSymbol("LogAgent", SwiftDeclaration.PROTOCOL)
89+
val TelemetryProvider = runtimeSymbol("TelemetryProvider", SwiftDeclaration.PROTOCOL)
90+
val SDKLogHandlerFactory = runtimeSymbol("SDKLogHandlerFactory", SwiftDeclaration.PROTOCOL)
91+
val SDKLogLevel = runtimeSymbol("SDKLogLevel", SwiftDeclaration.ENUM)
92+
val ClientLogMode = runtimeSymbol("ClientLogMode", SwiftDeclaration.ENUM)
93+
val IdempotencyTokenGenerator = runtimeSymbol("IdempotencyTokenGenerator", SwiftDeclaration.PROTOCOL)
94+
val DefaultRetryErrorInfoProvider = runtimeSymbol("DefaultRetryErrorInfoProvider", SwiftDeclaration.ENUM)
95+
val PaginateToken = runtimeSymbol("PaginateToken", SwiftDeclaration.PROTOCOL)
96+
val PaginatorSequence = runtimeSymbol("PaginatorSequence", SwiftDeclaration.STRUCT)
97+
val EndpointsRuleEngine = runtimeSymbol("EndpointsRuleEngine", SwiftDeclaration.CLASS)
98+
val EndpointsRequestContext = runtimeSymbol("EndpointsRequestContext", SwiftDeclaration.CLASS)
99+
val PartitionDefinition = runtimeSymbol("partitionJSON", SwiftDeclaration.LET)
100+
val EndpointsAuthSchemeResolver = runtimeSymbol("EndpointsAuthSchemeResolver", SwiftDeclaration.PROTOCOL)
101+
val DefaultEndpointsAuthSchemeResolver = runtimeSymbol("DefaultEndpointsAuthSchemeResolver", SwiftDeclaration.STRUCT)
102+
val EndpointsAuthScheme = runtimeSymbol("EndpointsAuthScheme", SwiftDeclaration.ENUM)
103103
}
104104
}
105105

106-
private fun runtimeSymbol(name: String): Symbol = buildSymbol {
106+
private fun runtimeSymbol(name: String, declaration: SwiftDeclaration? = null): Symbol = buildSymbol {
107107
this.name = name
108108
this.namespace = SwiftDependency.CLIENT_RUNTIME.target
109+
declaration?.let { this.setProperty("decl", it.keyword) }
109110
dependency(SwiftDependency.CLIENT_RUNTIME)
110111
}
111112

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/FoundationTypes.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ package software.amazon.smithy.swift.codegen
88
import software.amazon.smithy.swift.codegen.model.buildSymbol
99

1010
object FoundationTypes {
11-
val TimeInterval = builtInSymbol("TimeInterval")
11+
val TimeInterval = builtInSymbol("TimeInterval", SwiftDeclaration.TYPEALIAS)
1212
}
1313

14-
private fun builtInSymbol(symbol: String) = buildSymbol {
14+
private fun builtInSymbol(symbol: String, declaration: SwiftDeclaration? = null) = buildSymbol {
1515
name = symbol
16+
declaration?.let { this.setProperty("decl", it.keyword) }
1617
namespace = "Foundation"
1718
}

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SwiftWriter.kt

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ import software.amazon.smithy.swift.codegen.model.defaultValue
2525
import software.amazon.smithy.swift.codegen.model.isBoxed
2626
import software.amazon.smithy.swift.codegen.model.isBuiltIn
2727
import software.amazon.smithy.swift.codegen.model.isGeneric
28-
import software.amazon.smithy.swift.codegen.model.isInternalSPI
2928
import software.amazon.smithy.swift.codegen.model.isOptional
3029
import software.amazon.smithy.swift.codegen.model.isServiceNestedNamespace
3130
import java.util.function.BiFunction
31+
import kotlin.jvm.optionals.getOrNull
3232

3333
/**
3434
* Handles indenting follow on params to a function that takes several params or a builder object
@@ -98,14 +98,11 @@ class SwiftWriter(private val fullPackageName: String, swiftImportContainer: Swi
9898

9999
fun addImport(symbol: Symbol) {
100100
if (symbol.isBuiltIn || symbol.isServiceNestedNamespace || symbol.namespace.isEmpty()) return
101-
if (symbol.isInternalSPI()) {
102-
val kind = symbol.getProperty("kind").orElseThrow()
103-
val spiName = symbol.getProperty("spiName").orElseThrow()
104-
addImport(
105-
"$kind ${symbol.namespace}.${symbol.name}",
106-
internalSPIName = "$spiName"
107-
)
108-
} else {
101+
val spiName = symbol.getProperty("spiName").getOrNull()?.toString()
102+
val decl = symbol.getProperty("decl").getOrNull()?.toString()
103+
decl?.let {
104+
addImport("$it ${symbol.namespace}.${symbol.name}", internalSPIName = spiName)
105+
} ?: run {
109106
addImport(symbol.namespace)
110107
}
111108
}
@@ -121,8 +118,8 @@ class SwiftWriter(private val fullPackageName: String, swiftImportContainer: Swi
121118

122119
// Adds an import statement that imports the individual type from the specified module
123120
// Example: addIndividualTypeImport("struct", "Foundation", "Date") -> "import struct Foundation.Date"
124-
fun addIndividualTypeImport(kind: SwiftDeclaration, module: String, type: String) {
125-
importContainer.addImport("${kind.kind} $module.$type", false)
121+
fun addIndividualTypeImport(decl: SwiftDeclaration, module: String, type: String) {
122+
importContainer.addImport("${decl.keyword} $module.$type", false)
126123
}
127124

128125
fun addImportReferences(symbol: Symbol, vararg options: SymbolReference.ContextOption) {
@@ -142,10 +139,16 @@ class SwiftWriter(private val fullPackageName: String, swiftImportContainer: Swi
142139
return GENERATED_FILE_HEADER + imports + contents
143140
}
144141

145-
private class SwiftSymbolFormatter(val shouldSetDefault: Boolean, val shouldRenderOptional: Boolean) : BiFunction<Any, String, String> {
142+
private class SwiftSymbolFormatter(
143+
val writer: SwiftWriter,
144+
val shouldSetDefault: Boolean,
145+
val shouldRenderOptional: Boolean,
146+
) : BiFunction<Any, String, String> {
147+
146148
override fun apply(type: Any, indent: String): String {
147149
when (type) {
148150
is Symbol -> {
151+
writer.addImport(type)
149152
var formatted = type.fullName
150153
if (type.isBoxed() && shouldRenderOptional) {
151154
formatted += "?"
@@ -283,13 +286,13 @@ class SwiftWriter(private val fullPackageName: String, swiftImportContainer: Swi
283286
// Default values and optionality can be configured for a symbol (See SymbolBuilder)
284287
// The following custom formatters will render symbols according to its configuration
285288
// See https://smithy.io/2.0/guides/building-codegen/generating-code.html#formatters
286-
putFormatter('D', SwiftSymbolFormatter(shouldSetDefault = true, shouldRenderOptional = true))
287-
putFormatter('T', SwiftSymbolFormatter(shouldSetDefault = false, shouldRenderOptional = true))
288-
putFormatter('N', SwiftSymbolFormatter(shouldSetDefault = false, shouldRenderOptional = false))
289+
putFormatter('D', SwiftSymbolFormatter(this, shouldSetDefault = true, shouldRenderOptional = true))
290+
putFormatter('T', SwiftSymbolFormatter(this, shouldSetDefault = false, shouldRenderOptional = true))
291+
putFormatter('N', SwiftSymbolFormatter(this, shouldSetDefault = false, shouldRenderOptional = false))
289292
}
290293
}
291294

292-
enum class SwiftDeclaration(val kind: String) {
295+
enum class SwiftDeclaration(val keyword: String) {
293296
STRUCT("struct"),
294297
CLASS("class"),
295298
ENUM("enum"),

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/model/SymbolBuilder.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,11 @@ fun Symbol.isOptional(): Boolean {
109109
return this.getProperty("isOptional").orElse(false) as Boolean
110110
}
111111

112-
fun Symbol.isInternalSPI(): Boolean {
113-
return this.getProperty("isInternalSPI").orElse(false) as Boolean
114-
}
115-
116-
fun Symbol.toInternalSPI(kind: SwiftDeclaration, spiName: String): Symbol {
112+
fun Symbol.toInternalSPI(decl: SwiftDeclaration, spiName: String): Symbol {
117113
return this.toBuilder()
118114
.putProperty("isInternalSPI", true)
119115
.putProperty("spiName", spiName)
120-
.putProperty("kind", kind.kind)
116+
.putProperty("decl", decl.keyword)
121117
.build()
122118
}
123119

0 commit comments

Comments
 (0)