Skip to content

Commit da3ff4f

Browse files
committed
Initial commit
1 parent c2ce16e commit da3ff4f

20 files changed

+1204
-0
lines changed

.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.resolved

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// swift-tools-version: 5.9
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
import CompilerPluginSupport
6+
7+
let package = Package(
8+
name: "ExtractCaseValue",
9+
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .macCatalyst(.v13)],
10+
products: [
11+
// Products define the executables and libraries a package produces, making them visible to other packages.
12+
.library(
13+
name: "ExtractCaseValue",
14+
targets: ["ExtractCaseValue"]
15+
),
16+
.executable(
17+
name: "ExtractCaseValueClient",
18+
targets: ["ExtractCaseValueClient"]
19+
),
20+
],
21+
dependencies: [
22+
// Depend on the latest Swift 5.9 prerelease of SwiftSyntax
23+
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0-swift-5.9-DEVELOPMENT-SNAPSHOT-2023-04-25-b"),
24+
],
25+
targets: [
26+
// Targets are the basic building blocks of a package, defining a module or a test suite.
27+
// Targets can depend on other targets in this package and products from dependencies.
28+
// Macro implementation that performs the source transformation of a macro.
29+
.macro(
30+
name: "ExtractCaseValueMacros",
31+
dependencies: [
32+
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
33+
.product(name: "SwiftCompilerPlugin", package: "swift-syntax")
34+
]
35+
),
36+
37+
// Library that exposes a macro as part of its API, which is used in client programs.
38+
.target(name: "ExtractCaseValue", dependencies: ["ExtractCaseValueMacros"]),
39+
40+
// A client of the library, which is able to use the macro in its own code.
41+
.executableTarget(name: "ExtractCaseValueClient", dependencies: ["ExtractCaseValue"]),
42+
43+
// A test target used to develop the macro implementation.
44+
.testTarget(
45+
name: "ExtractCaseValueTests",
46+
dependencies: [
47+
"ExtractCaseValueMacros",
48+
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
49+
]
50+
),
51+
]
52+
)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# ExtractCaseValue
2+
3+
The `ExtractCaseValue` package provides a macro to expose assiocated values from enum cases as a computed property.
4+
5+
@Metadata {
6+
@PageColor(blue)
7+
}
8+
9+
## Overview
10+
11+
@Row {
12+
@Column {
13+
To extract a simple value annotate an enum with the `ExtractCaseValue` macro and provide the expected type as a generic along with a name for the comuted property. This will use the `firstMatchingType` as a default to use the first associated value in a case that matches the expected type (in this case `String`).
14+
}
15+
@Column {
16+
![Screenshot of Xcode showing the marco expansion on a Path enum with a String as return type](sample-one)
17+
}
18+
}
19+
20+
@Row {
21+
@Column {
22+
If the return type is optional the macro will infer `nil` as the default value.
23+
}
24+
@Column {
25+
![Screenshot of Xcode showing the marco expansion on a JSON enum with an optional String as return type](sample-two)
26+
}
27+
}
28+
29+
@Row {
30+
@Column {
31+
![Screenshot of Xcode showing the fix-it](fix-it)
32+
}
33+
@Column {
34+
Otherwise, you will get a fix-it that recommends to use a default value.
35+
}
36+
}
37+
38+
@Row {
39+
@Column {
40+
You can also add mutliple `ExtractCaseValue` macros.
41+
![Screenshot of Xcode showing the marco expansion on a Coordinate enum which uses multiple macros](sample-three)
42+
}
43+
}
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)