Skip to content

Commit

Permalink
macro doc and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dankinsoid committed Mar 28, 2024
1 parent 1aaf05a commit 0e900a0
Show file tree
Hide file tree
Showing 9 changed files with 915 additions and 91 deletions.
12 changes: 6 additions & 6 deletions Example/Sources/PetStoreWithMacros/PetStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public extension PetStore {
@Path("{id}")
public struct PetByID {

#GET(PetModel)
#DELETE
@GET("/") public func get() -> PetModel {}
@DELETE("/") public func delete() {}
@POST("/") public func update(@Query name: String?, @Query status: PetStatus?) -> PetModel {}
@POST public func uploadImage(_ body: Data, @Query additionalMetadata: String? = nil) {}
}
Expand All @@ -61,8 +61,8 @@ public extension PetStore {
@Path("order", "{id}")
public struct Order {

#GET(OrderModel)
#DELETE(OrderModel)
@GET("/") public func get() -> OrderModel {}
@DELETE("/") public func delete() {}
}
}
}
Expand All @@ -89,8 +89,8 @@ extension PetStore {
@Path("{username}")
public struct UserByUsername {

#GET(UserModel)
#DELETE(UserModel)
@GET("/") public func get() -> UserModel {}
@DELETE("/") public func delete() {}
@PUT("/") public func update(_ body: UserModel) -> UserModel {}
}
}
Expand Down
16 changes: 3 additions & 13 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// swift-tools-version:5.9
// swift-tools-version:5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import CompilerPluginSupport
import PackageDescription

var package = Package(
Expand All @@ -17,14 +16,12 @@ var package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.3"),
.package(url: "https://github.com/apple/swift-http-types.git", from: "1.0.3"),
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.2"),
.package(url: "https://github.com/apple/swift-http-types.git", from: "1.0.3")
],
targets: [
.target(
name: "SwiftAPIClient",
dependencies: [
.target(name: "SwiftAPIClientMacros"),
.product(name: "Logging", package: "swift-log"),
.product(name: "HTTPTypes", package: "swift-http-types"),
.product(name: "HTTPTypesFoundation", package: "swift-http-types"),
Expand All @@ -33,13 +30,6 @@ var package = Package(
.testTarget(
name: "SwiftAPIClientTests",
dependencies: [.target(name: "SwiftAPIClient")]
),
.macro(
name: "SwiftAPIClientMacros",
dependencies: [
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
]
),
)
]
)
52 changes: 52 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// swift-tools-version:5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import CompilerPluginSupport
import PackageDescription

var package = Package(
name: "swift-api-client",
platforms: [
.macOS(.v10_15),
.iOS(.v13),
.watchOS(.v5),
.tvOS(.v13),
],
products: [
.library(name: "SwiftAPIClient", targets: ["SwiftAPIClient"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.3"),
.package(url: "https://github.com/apple/swift-http-types.git", from: "1.0.3"),
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.2"),
],
targets: [
.target(
name: "SwiftAPIClient",
dependencies: [
.target(name: "SwiftAPIClientMacros"),
.product(name: "Logging", package: "swift-log"),
.product(name: "HTTPTypes", package: "swift-http-types"),
.product(name: "HTTPTypesFoundation", package: "swift-http-types"),
]
),
.testTarget(
name: "SwiftAPIClientTests",
dependencies: [.target(name: "SwiftAPIClient")]
),
.macro(
name: "SwiftAPIClientMacros",
dependencies: [
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
]
),
.testTarget(
name: "SwiftAPIClientMacrosTests",
dependencies: [
.target(name: "SwiftAPIClientMacros"),
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax")
]
),
]
)
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,17 @@ Also, you can use macros for API declaration:
@Path
struct Pet {

/// PUT /pet
@PUT("/") public func update(_ body: PetModel) -> PetModel {}

/// POST /pet
@POST("/") public func add(_ body: PetModel) -> PetModel {}

/// GET /pet/findByStatus
@GET func findByStatus(@Query _ status: PetStatus) -> [PetModel] {}
@GET public func findByStatus(@Query _ status: PetStatus) -> [PetModel] {}

/// GET /pet/findByTags
@GET public func findByTags(@Query _ tags: [String]) -> [PetModel] {}
}
```

Expand Down Expand Up @@ -255,6 +264,9 @@ print(configs.intValue) // 3

## Macros
`swift-api-client` provides a set of macros for easier API declarations.
- `API` macro that generates an API client struct.
- `Path` macro that generates an API client scope for the path.
- `Cal(_ method:)`, `GET`, `POST`, `PUT`, etc macros for declaring API methods.
Example:
```swift
/// /pet
Expand Down Expand Up @@ -291,7 +303,7 @@ struct Pet {
}
}
```
Macros are not necessary for using `swift-api-client`; they are just syntax sugar. In general, it's not recommended to use a lot of macros for large projects when compilation time becomes a problem; core library syntax is minimalistic enough.
Macros are not necessary for using `swift-api-client`; they are just syntax sugar.

## Introducing `swift-api-client-addons`

Expand Down
Loading

0 comments on commit 0e900a0

Please sign in to comment.