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

Disable "respectsExistingLineBreaks" in .swift-format for more consistent styling #346

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .swift-format
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"lineLength" : 120,
"maximumBlankLines" : 1,
"prioritizeKeepingFunctionOutputTogether" : false,
"respectsExistingLineBreaks" : true,
"respectsExistingLineBreaks" : false,
"rules" : {
"AllPublicDeclarationsHaveDocumentation" : true,
"AlwaysUseLowerCamelCase" : false,
Expand Down
13 changes: 3 additions & 10 deletions Examples/GreetingService/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ import PackageDescription

let package = Package(
name: "GreetingService",
platforms: [
.macOS(.v13)
],
platforms: [.macOS(.v13)],
dependencies: [
.package(url: "https://github.com/apple/swift-openapi-generator", .upToNextMinor(from: "0.3.0")),
.package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "0.3.0")),
Expand All @@ -33,16 +31,11 @@ let package = Package(
.product(name: "OpenAPIVapor", package: "swift-openapi-vapor"),
.product(name: "Vapor", package: "vapor"),
],
plugins: [
.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")
]
plugins: [.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")]
),
.testTarget(
name: "GreetingServiceMockTests",
dependencies: [
"GreetingService",
.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"),
]
dependencies: ["GreetingService", .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime")]
),
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ import Vapor
struct Handler: APIProtocol {

// This function is a protocol requirement, derived from the operation in the OpenAPI document.
func getGreeting(
_ input: Operations.getGreeting.Input
) async throws -> Operations.getGreeting.Output {
func getGreeting(_ input: Operations.getGreeting.Input) async throws -> Operations.getGreeting.Output {
// Extract the query parameter from the input, using generated type-safe property.
let name = input.query.name ?? "Stranger"

Expand All @@ -30,8 +28,7 @@ struct Handler: APIProtocol {
}
}

@main
struct Main {
@main struct Main {
/// The entry point of the program.
///
/// This is where the execution of the program begins. Any code you want to run
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import GreetingService

// Mock operates on value types, and requires no concrete client or server transport.
struct MockGreetingService: APIProtocol {
func getGreeting(
_ input: Operations.getGreeting.Input
) async throws -> Operations.getGreeting.Output {
func getGreeting(_ input: Operations.getGreeting.Input) async throws -> Operations.getGreeting.Output {
let name = input.query.name ?? "<unknown>"
return .ok(.init(body: .json(.init(message: "(mock) Hello, \(name)"))))
}
Expand Down
8 changes: 2 additions & 6 deletions Examples/GreetingServiceClient/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ import PackageDescription

let package = Package(
name: "GreetingServiceClient",
platforms: [
.macOS(.v13)
],
platforms: [.macOS(.v13)],
dependencies: [
.package(url: "https://github.com/apple/swift-openapi-generator", .upToNextMinor(from: "0.3.0")),
.package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "0.3.0")),
Expand All @@ -31,9 +29,7 @@ let package = Package(
.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"),
.product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"),
],
plugins: [
.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")
]
plugins: [.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")]
)
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
import OpenAPIRuntime
import OpenAPIURLSession

@main
struct GreetingServiceClient {
@main struct GreetingServiceClient {

static func main() async throws {
// Create an instance of the generated client type.
Expand Down Expand Up @@ -45,6 +44,7 @@ struct GreetingServiceClient {

// Use shorthand APIs to get an expected response or otherwise throw a runtime error.
print(try await client.getGreeting().ok.body.json.message)

// ^ ^ ^
// | | `- Throws if body did not parse as documented JSON.
// | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import GreetingService

// Mock operates on value types, and requires no concrete client or server transport.
struct MockGreetingService: APIProtocol {
func getGreeting(
_ input: Operations.getGreeting.Input
) async throws -> Operations.getGreeting.Output {
func getGreeting(_ input: Operations.getGreeting.Input) async throws -> Operations.getGreeting.Output {
let name = input.query.name ?? "<unknown>"
return .ok(.init(body: .json(.init(message: "(mock) Hello, \(name)"))))
}
Expand Down
72 changes: 10 additions & 62 deletions IntegrationTest/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,11 @@ import PackageDescription

let package = Package(
name: "swift-openapi-integration-test",
platforms: [
.macOS(.v13)
],
platforms: [.macOS(.v13)],
products: [
.library(
name: "IntegrationTestLibrary",
targets: [
"Types",
"Client",
"Server",
"MockTransportClient",
"MockTransportServer",
]
targets: ["Types", "Client", "Server", "MockTransportClient", "MockTransportServer"]
)
],
dependencies: [
Expand All @@ -38,70 +30,26 @@ let package = Package(
targets: [
.target(
name: "Types",
dependencies: [
.product(
name: "OpenAPIRuntime",
package: "swift-openapi-runtime"
)
],
plugins: [
.plugin(
name: "OpenAPIGenerator",
package: "swift-openapi-generator"
)
]
dependencies: [.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime")],
plugins: [.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")]
),
.target(
name: "Client",
dependencies: [
"Types",
.product(
name: "OpenAPIRuntime",
package: "swift-openapi-runtime"
),
],
plugins: [
.plugin(
name: "OpenAPIGenerator",
package: "swift-openapi-generator"
)
]
dependencies: ["Types", .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime")],
plugins: [.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")]
),
.target(
name: "Server",
dependencies: [
"Types",
.product(
name: "OpenAPIRuntime",
package: "swift-openapi-runtime"
),
],
plugins: [
.plugin(
name: "OpenAPIGenerator",
package: "swift-openapi-generator"
)
]
dependencies: ["Types", .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime")],
plugins: [.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")]
),
.target(
name: "MockTransportClient",
dependencies: [
"Client",
.product(
name: "OpenAPIRuntime",
package: "swift-openapi-runtime"
),
]
dependencies: ["Client", .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime")]
),
.target(
name: "MockTransportServer",
dependencies: [
"Server",
.product(
name: "OpenAPIRuntime",
package: "swift-openapi-runtime"
),
]
dependencies: ["Server", .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime")]
),
]
)
16 changes: 4 additions & 12 deletions IntegrationTest/Sources/MockTransportClient/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,12 @@ import Foundation
import HTTPTypes

struct MockClientTransport: ClientTransport {
func send(
_ request: HTTPTypes.HTTPRequest,
body: OpenAPIRuntime.HTTPBody?,
baseURL: URL,
operationID: String
) async throws -> (HTTPTypes.HTTPResponse, OpenAPIRuntime.HTTPBody?) {
(HTTPTypes.HTTPResponse(status: 200), nil)
}
func send(_ request: HTTPTypes.HTTPRequest, body: OpenAPIRuntime.HTTPBody?, baseURL: URL, operationID: String)
async throws -> (HTTPTypes.HTTPResponse, OpenAPIRuntime.HTTPBody?)
{ (HTTPTypes.HTTPResponse(status: 200), nil) }
}

func run() async throws {
let client = Client(
serverURL: try Servers.server1(),
transport: MockClientTransport()
)
let client = Client(serverURL: try Servers.server1(), transport: MockClientTransport())
_ = try await client.getGreeting(.init())
}
15 changes: 3 additions & 12 deletions IntegrationTest/Sources/MockTransportServer/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ import OpenAPIRuntime
import HTTPTypes

actor SimpleAPIImpl: APIProtocol {
func getGreeting(
_ input: Operations.getGreeting.Input
) async throws -> Operations.getGreeting.Output {
func getGreeting(_ input: Operations.getGreeting.Input) async throws -> Operations.getGreeting.Output {
let message = "Hello, \(input.query.name ?? "Stranger")!"
return .ok(.init(body: .json(.init(message: message))))
}
Expand All @@ -30,20 +28,13 @@ class MockServerTransport: ServerTransport {
HTTPTypes.HTTPRequest, OpenAPIRuntime.HTTPBody?, OpenAPIRuntime.ServerRequestMetadata
) async throws -> (HTTPTypes.HTTPResponse, OpenAPIRuntime.HTTPBody?)

func register(
_ handler: @escaping Handler,
method: HTTPTypes.HTTPRequest.Method,
path: String
) throws {
func register(_ handler: @escaping Handler, method: HTTPTypes.HTTPRequest.Method, path: String) throws {
// noop.
}
}

func initializeServer() throws {
let handler = SimpleAPIImpl()
let transport = MockServerTransport()
try handler.registerHandlers(
on: transport,
serverURL: Servers.server1()
)
try handler.registerHandlers(on: transport, serverURL: Servers.server1())
}