Skip to content

Commit 0aa6774

Browse files
authored
Fixes #114 - Add scoped imports (#184)
### Motivation Fixes #114 ### Modifications Add scoped imports ### Result Specific imports ### Test Plan Run CI and Tests
1 parent 5dbd05d commit 0aa6774

File tree

9 files changed

+62
-7
lines changed

9 files changed

+62
-7
lines changed

Sources/_OpenAPIGeneratorCore/Layers/StructuredSwiftRepresentation.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ struct ImportDescription: Equatable, Codable {
2222
/// For example, the `Foo` in `import Foo`.
2323
var moduleName: String
2424

25+
/// An array of module types imported from the module, if applicable.
26+
///
27+
/// For example, if there are type imports like `import Foo.Bar`, they would be listed here.
28+
var moduleTypes: [String]?
29+
2530
/// The name of the private interface for an `@_spi` import.
2631
///
2732
/// For example, if `spi` was "Secret" and the module name was "Foo" then the import

Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,16 @@ struct TextBasedRenderer: RendererProtocol {
7373

7474
/// Renders a single import statement.
7575
func renderImport(_ description: ImportDescription) -> String {
76-
func render(moduleName: String, spi: String?, preconcurrency: Bool) -> String {
76+
func render(moduleName: String, moduleTypes: [String]? = nil, spi: String?, preconcurrency: Bool) -> String {
7777
let spiPrefix = spi.map { "@_spi(\($0)) " } ?? ""
7878
let preconcurrencyPrefix = preconcurrency ? "@preconcurrency " : ""
79+
var types = [String]()
80+
if let moduleTypes, preconcurrency {
81+
types = moduleTypes.map {
82+
"\(preconcurrencyPrefix)\(spiPrefix)import \($0)"
83+
}
84+
return types.joinedLines()
85+
}
7986
return "\(preconcurrencyPrefix)\(spiPrefix)import \(moduleName)"
8087
}
8188

@@ -87,7 +94,14 @@ struct TextBasedRenderer: RendererProtocol {
8794
case .onOS(let operatingSystems):
8895
var lines = [String]()
8996
lines.append("#if \(operatingSystems.map { "os(\($0))" }.joined(separator: " || "))")
90-
lines.append(render(moduleName: description.moduleName, spi: description.spi, preconcurrency: true))
97+
lines.append(
98+
render(
99+
moduleName: description.moduleName,
100+
moduleTypes: description.moduleTypes,
101+
spi: description.spi,
102+
preconcurrency: true
103+
)
104+
)
91105
lines.append("#else")
92106
lines.append(render(moduleName: description.moduleName, spi: description.spi, preconcurrency: false))
93107
lines.append("#endif")

Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct ClientFileTranslator: FileTranslator {
3838

3939
let imports =
4040
Constants.File.imports
41+
+ Constants.Client.scopedImports
4142
+ config.additionalImports
4243
.map { ImportDescription(moduleName: $0) }
4344

Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/Constants.swift

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ enum Constants {
2525

2626
/// The descriptions of modules imported by every generated file.
2727
static let imports: [ImportDescription] = [
28-
ImportDescription(moduleName: "OpenAPIRuntime", spi: "Generated"),
29-
ImportDescription(moduleName: "Foundation", preconcurrency: .onOS(["Linux"])),
28+
ImportDescription(moduleName: "OpenAPIRuntime", spi: "Generated")
3029
]
3130
}
3231

@@ -62,6 +61,15 @@ enum Constants {
6261
/// Constants related to the generated client type.
6362
enum Client {
6463

64+
/// An array of scoped imports specific to the `Client` namespace.
65+
static let scopedImports: [ImportDescription] = [
66+
ImportDescription(
67+
moduleName: "Foundation",
68+
moduleTypes: ["struct Foundation.URL", "struct Foundation.Data"],
69+
preconcurrency: .onOS(["Linux"])
70+
)
71+
]
72+
6573
/// The name of the client type.
6674
static let typeName: String = "Client"
6775

@@ -91,9 +99,30 @@ enum Constants {
9199
}
92100
}
93101

102+
enum Types {
103+
104+
/// An array of scoped imports specific to the `Types` namespace.
105+
static let scopedImports: [ImportDescription] = [
106+
ImportDescription(
107+
moduleName: "Foundation",
108+
moduleTypes: ["struct Foundation.URL", "struct Foundation.Data", "struct Foundation.Date"],
109+
preconcurrency: .onOS(["Linux"])
110+
)
111+
]
112+
}
113+
94114
/// Constants related to the generated server types.
95115
enum Server {
96116

117+
/// An array of scoped imports specific to the `Server` namespace.
118+
static let scopedImports: [ImportDescription] = [
119+
ImportDescription(
120+
moduleName: "Foundation",
121+
moduleTypes: ["struct Foundation.URL", "struct Foundation.Data"],
122+
preconcurrency: .onOS(["Linux"])
123+
)
124+
]
125+
97126
/// Constants related to the universal server.
98127
enum Universal {
99128

Sources/_OpenAPIGeneratorCore/Translator/ServerTranslator/ServerTranslator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct ServerFileTranslator: FileTranslator {
3636

3737
let imports =
3838
Constants.File.imports
39+
+ Constants.Server.scopedImports
3940
+ config.additionalImports
4041
.map { ImportDescription(moduleName: $0) }
4142

Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/TypesFileTranslator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct TypesFileTranslator: FileTranslator {
3838

3939
let imports =
4040
Constants.File.imports
41+
+ Constants.Types.scopedImports
4142
+ config.additionalImports
4243
.map { ImportDescription(moduleName: $0) }
4344

Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Generated by swift-openapi-generator, do not modify.
22
@_spi(Generated) import OpenAPIRuntime
33
#if os(Linux)
4-
@preconcurrency import Foundation
4+
@preconcurrency import struct Foundation.URL
5+
@preconcurrency import struct Foundation.Data
56
#else
67
import Foundation
78
#endif

Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Server.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Generated by swift-openapi-generator, do not modify.
22
@_spi(Generated) import OpenAPIRuntime
33
#if os(Linux)
4-
@preconcurrency import Foundation
4+
@preconcurrency import struct Foundation.URL
5+
@preconcurrency import struct Foundation.Data
56
#else
67
import Foundation
78
#endif

Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Generated by swift-openapi-generator, do not modify.
22
@_spi(Generated) import OpenAPIRuntime
33
#if os(Linux)
4-
@preconcurrency import Foundation
4+
@preconcurrency import struct Foundation.URL
5+
@preconcurrency import struct Foundation.Data
6+
@preconcurrency import struct Foundation.Date
57
#else
68
import Foundation
79
#endif

0 commit comments

Comments
 (0)