Skip to content

Commit

Permalink
Full compatibility with the "ExistentialAny" upcoming feature (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM authored Jul 12, 2023
1 parent f048771 commit 8c5258b
Show file tree
Hide file tree
Showing 22 changed files with 79 additions and 61 deletions.
23 changes: 18 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
//===----------------------------------------------------------------------===//
import PackageDescription

// General Swift-settings for all targets.
let swiftSettings: [SwiftSetting] = [
// https://github.com/apple/swift-evolution/blob/main/proposals/0335-existential-any.md
// Require `any` for existential types.
.enableUpcomingFeature("ExistentialAny")
]

let package = Package(
name: "swift-openapi-generator",
platforms: [
Expand Down Expand Up @@ -72,6 +79,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
],
targets: [

// Generator Core
.target(
name: "_OpenAPIGeneratorCore",
Expand All @@ -83,15 +91,17 @@ let package = Package(
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
.product(name: "SwiftFormat", package: "swift-format"),
.product(name: "SwiftFormatConfiguration", package: "swift-format"),
]
],
swiftSettings: swiftSettings
),

// Generator Core Tests
.testTarget(
name: "OpenAPIGeneratorCoreTests",
dependencies: [
"_OpenAPIGeneratorCore"
]
],
swiftSettings: swiftSettings
),

// GeneratorReferenceTests
Expand All @@ -104,7 +114,8 @@ let package = Package(
],
resources: [
.copy("Resources")
]
],
swiftSettings: swiftSettings
),

// PetstoreConsumerTests
Expand All @@ -114,7 +125,8 @@ let package = Package(
name: "PetstoreConsumerTests",
dependencies: [
.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime")
]
],
swiftSettings: swiftSettings
),

// Generator CLI
Expand All @@ -123,7 +135,8 @@ let package = Package(
dependencies: [
"_OpenAPIGeneratorCore",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
]
],
swiftSettings: swiftSettings
),

// Build Plugin
Expand Down
10 changes: 5 additions & 5 deletions Sources/_OpenAPIGeneratorCore/GeneratorPipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ struct GeneratorPipeline {
public func runGenerator(
input: InMemoryInputFile,
config: Config,
diagnostics: DiagnosticCollector
diagnostics: any DiagnosticCollector
) throws -> InMemoryOutputFile {
try makeGeneratorPipeline(config: config, diagnostics: diagnostics).run(input)
}
Expand All @@ -105,12 +105,12 @@ public func runGenerator(
/// - Returns: A configured generator pipeline that can be executed with
/// ``GeneratorPipeline/run(_:)``.
func makeGeneratorPipeline(
parser: ParserProtocol = YamsParser(),
translator: TranslatorProtocol = MultiplexTranslator(),
renderer: RendererProtocol = TextBasedRenderer(),
parser: any ParserProtocol = YamsParser(),
translator: any TranslatorProtocol = MultiplexTranslator(),
renderer: any RendererProtocol = TextBasedRenderer(),
formatter: @escaping (InMemoryOutputFile) throws -> InMemoryOutputFile = { try $0.swiftFormatted },
config: Config,
diagnostics: DiagnosticCollector
diagnostics: any DiagnosticCollector
) -> GeneratorPipeline {
return .init(
parseOpenAPIFileStage: .init(
Expand Down
2 changes: 1 addition & 1 deletion Sources/_OpenAPIGeneratorCore/Parser/ParserProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ protocol ParserProtocol {
func parseOpenAPI(
_ input: InMemoryInputFile,
config: Config,
diagnostics: DiagnosticCollector
diagnostics: any DiagnosticCollector
) throws -> ParsedOpenAPIRepresentation
}
4 changes: 2 additions & 2 deletions Sources/_OpenAPIGeneratorCore/Parser/YamsParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct YamsParser: ParserProtocol {
func parseOpenAPI(
_ input: InMemoryInputFile,
config: Config,
diagnostics: DiagnosticCollector
diagnostics: any DiagnosticCollector
) throws -> ParsedOpenAPIRepresentation {
let decoder = YAMLDecoder()
let openapiData = input.contents
Expand Down Expand Up @@ -86,7 +86,7 @@ struct YamsParser: ParserProtocol {
location: .init(filePath: input.absolutePath.path, lineNumber: yamlMark.line - 1)
)
}
} else if let openAPIError = context.underlyingError as? OpenAPIError {
} else if let openAPIError = context.underlyingError as? (any OpenAPIError) {
throw Diagnostic.error(
message: openAPIError.localizedDescription,
location: .init(filePath: input.absolutePath.path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ protocol RendererProtocol {
func render(
structured code: StructuredSwiftRepresentation,
config: Config,
diagnostics: DiagnosticCollector
diagnostics: any DiagnosticCollector
) throws -> InMemoryOutputFile
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct TextBasedRenderer: RendererProtocol {
func render(
structured: StructuredSwiftRepresentation,
config: Config,
diagnostics: DiagnosticCollector
diagnostics: any DiagnosticCollector
) throws -> InMemoryOutputFile {
let namedFile = structured.file
return InMemoryOutputFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import OpenAPIKit30
struct ClientFileTranslator: FileTranslator {

var config: Config
var diagnostics: DiagnosticCollector
var diagnostics: any DiagnosticCollector
var components: OpenAPI.Components

func translateFile(
Expand Down Expand Up @@ -79,7 +79,10 @@ struct ClientFileTranslator: FileTranslator {
type: Constants.Configuration.typeName,
defaultValue: .dot("init").call([])
),
.init(label: "transport", type: Constants.Client.Transport.typeName),
.init(
label: "transport",
type: Constants.Client.Transport.typeName
),
.init(
label: "middlewares",
type: "[\(Constants.Client.Middleware.typeName)]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ fileprivate extension FileTranslator {
accessModifier: config.access,
kind: .function(name: "encode"),
parameters: [
.init(label: "to", name: "encoder", type: "Encoder")
.init(label: "to", name: "encoder", type: "any Encoder")
],
keywords: [
.throws
Expand All @@ -610,7 +610,7 @@ fileprivate extension FileTranslator {
accessModifier: config.access,
kind: .initializer,
parameters: [
.init(label: "from", name: "decoder", type: "Decoder")
.init(label: "from", name: "decoder", type: "any Decoder")
],
keywords: [
.throws
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ enum Constants {
enum Transport {

/// The name of the client transport type.
static let typeName: String = "ClientTransport"
static let typeName: String = "any ClientTransport"
}

/// Constants related to the client middleware type.
enum Middleware {

/// The name of the client middleware type.
static let typeName: String = "ClientMiddleware"
static let typeName: String = "any ClientMiddleware"
}
}

Expand All @@ -110,14 +110,14 @@ enum Constants {
enum Transport {

/// The name of the server transport type.
static let typeName: String = "ServerTransport"
static let typeName: String = "any ServerTransport"
}

/// Constants related to the server middleware type.
enum Middleware {

/// The name of the server middleware type.
static let typeName: String = "ServerMiddleware"
static let typeName: String = "any ServerMiddleware"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protocol FileTranslator {

/// The collector receives diagnostics from the translator, which should
/// be surfaced to the user in some way.
var diagnostics: DiagnosticCollector { get }
var diagnostics: any DiagnosticCollector { get }

/// The components section of the OpenAPI document is required by the
/// translator logic to follow JSON references to schemas, parameters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ struct MultiplexTranslator: TranslatorProtocol {
func translate(
parsedOpenAPI: ParsedOpenAPIRepresentation,
config: Config,
diagnostics: DiagnosticCollector
diagnostics: any DiagnosticCollector
) throws -> StructuredSwiftRepresentation {
let translator: FileTranslator
let translator: any FileTranslator
switch config.mode {
case .types:
translator = TypesFileTranslator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import OpenAPIKit30
struct ServerFileTranslator: FileTranslator {

var config: Config
var diagnostics: DiagnosticCollector
var diagnostics: any DiagnosticCollector
var components: OpenAPI.Components

func translateFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ protocol TranslatorProtocol {
func translate(
parsedOpenAPI: ParsedOpenAPIRepresentation,
config: Config,
diagnostics: DiagnosticCollector
diagnostics: any DiagnosticCollector
) throws -> StructuredSwiftRepresentation
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import OpenAPIKit30
struct TypesFileTranslator: FileTranslator {

var config: Config
var diagnostics: DiagnosticCollector
var diagnostics: any DiagnosticCollector
var components: OpenAPI.Components

func translateFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extension _GenerateOptions {
additionalImports: resolvedAdditionalImports
)
}
let diagnostics: DiagnosticCollector
let diagnostics: any DiagnosticCollector
let finalizeDiagnostics: () throws -> Void
if let diagnosticsOutputPath {
let _diagnostics = _YamlFileDiagnosticsCollector(url: diagnosticsOutputPath)
Expand Down
4 changes: 2 additions & 2 deletions Sources/swift-openapi-generator/runGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension _Tool {
configs: [Config],
isPluginInvocation: Bool,
outputDirectory: URL,
diagnostics: DiagnosticCollector
diagnostics: any DiagnosticCollector
) throws {
let docData: Data
do {
Expand Down Expand Up @@ -73,7 +73,7 @@ extension _Tool {
docData: Data,
config: Config,
outputFilePath: URL,
diagnostics: DiagnosticCollector
diagnostics: any DiagnosticCollector
) throws {
let didChange = try replaceFileContents(at: outputFilePath) {
let output = try _OpenAPIGeneratorCore.runGenerator(
Expand Down
6 changes: 3 additions & 3 deletions Tests/OpenAPIGeneratorCoreTests/TestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class Test_Core: XCTestCase {

func makeTranslator(
components: OpenAPI.Components = .noComponents,
diagnostics: DiagnosticCollector = PrintingDiagnosticCollector()
) -> FileTranslator {
diagnostics: any DiagnosticCollector = PrintingDiagnosticCollector()
) -> any FileTranslator {
makeTypesTranslator(
components: components,
diagnostics: diagnostics
Expand All @@ -35,7 +35,7 @@ class Test_Core: XCTestCase {

func makeTypesTranslator(
components: OpenAPI.Components = .noComponents,
diagnostics: DiagnosticCollector = PrintingDiagnosticCollector()
diagnostics: any DiagnosticCollector = PrintingDiagnosticCollector()
) -> TypesFileTranslator {
TypesFileTranslator(
config: .init(mode: .types),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import OpenAPIKit30

class Test_isSchemaSupported: XCTestCase {

var translator: FileTranslator {
var translator: any FileTranslator {
TypesFileTranslator(
config: .init(mode: .types),
diagnostics: PrintingDiagnosticCollector(),
Expand Down
6 changes: 3 additions & 3 deletions Tests/OpenAPIGeneratorReferenceTests/ReferenceTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class ReferenceTests: XCTestCase {
)

// Run the requested generator invocation
let diagnostics: DiagnosticCollector = strictDiagnosticsCollector
let diagnostics: any DiagnosticCollector = strictDiagnosticsCollector
let generatorPipeline = self.makeGeneratorPipeline(
config: referenceTest.asConfig,
diagnostics: diagnostics
Expand Down Expand Up @@ -160,13 +160,13 @@ struct StrictDiagnosticsCollector: DiagnosticCollector {

extension ReferenceTests {

var strictDiagnosticsCollector: DiagnosticCollector {
var strictDiagnosticsCollector: any DiagnosticCollector {
StrictDiagnosticsCollector(test: self)
}

private func makeGeneratorPipeline(
config: Config,
diagnostics: DiagnosticCollector
diagnostics: any DiagnosticCollector
) -> GeneratorPipeline {

let parser = YamsParser()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public struct Client: APIProtocol {
public init(
serverURL: URL,
configuration: Configuration = .init(),
transport: ClientTransport,
middlewares: [ClientMiddleware] = []
transport: any ClientTransport,
middlewares: [any ClientMiddleware] = []
) {
self.client = .init(
serverURL: serverURL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ extension APIProtocol {
/// - configuration: A set of configuration values for the server.
/// - middlewares: A list of middlewares to call before the handler.
public func registerHandlers(
on transport: ServerTransport,
on transport: any ServerTransport,
serverURL: URL = .defaultOpenAPIServerURL,
configuration: Configuration = .init(),
middlewares: [ServerMiddleware] = []
middlewares: [any ServerMiddleware] = []
) throws {
let server = UniversalServer(
serverURL: serverURL,
Expand Down
Loading

0 comments on commit 8c5258b

Please sign in to comment.