@@ -25,10 +25,10 @@ import software.amazon.smithy.swift.codegen.model.defaultValue
2525import software.amazon.smithy.swift.codegen.model.isBoxed
2626import software.amazon.smithy.swift.codegen.model.isBuiltIn
2727import software.amazon.smithy.swift.codegen.model.isGeneric
28- import software.amazon.smithy.swift.codegen.model.isInternalSPI
2928import software.amazon.smithy.swift.codegen.model.isOptional
3029import software.amazon.smithy.swift.codegen.model.isServiceNestedNamespace
3130import 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" ),
0 commit comments