Skip to content

Commit 295068d

Browse files
committed
Add an accessor macro example
1 parent 7233193 commit 295068d

File tree

6 files changed

+144
-0
lines changed

6 files changed

+144
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
import SwiftSyntaxMacros
15+
16+
public struct EnvironmentValueMacro: AccessorMacro {
17+
public static func expansion(
18+
of node: AttributeSyntax,
19+
providingAccessorsOf declaration: some DeclSyntaxProtocol,
20+
in context: some MacroExpansionContext
21+
) throws -> [AccessorDeclSyntax] {
22+
guard
23+
case let .argumentList(arguments) = node.arguments,
24+
let argument = arguments.first
25+
else { return [] }
26+
27+
return [
28+
"""
29+
get { self[\(raw: argument.expression)] }
30+
""",
31+
"""
32+
set { self[\(raw: argument.expression)] = newValue }
33+
""",
34+
]
35+
}
36+
}

Examples/Sources/MacroExamples/Implementation/Plugin.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct MyPlugin: CompilerPlugin {
2626
DefaultFatalErrorImplementationMacro.self,
2727
DictionaryStorageMacro.self,
2828
DictionaryStoragePropertyMacro.self,
29+
EnvironmentValueMacro.self,
2930
EquatableExtensionMacro.self,
3031
FontLiteralMacro.self,
3132
FuncUniqueMacro.self,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
// MARK: - EnvironmentValue Accessor
14+
15+
import SwiftUI
16+
17+
@attached(accessor)
18+
public macro EnvironmentValue(for key: any EnvironmentKey.Type) =
19+
#externalMacro(module: "MacroExamplesImplementation", type: "EnvironmentValueMacro")
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
// MARK: - EnvironmentValue Accessor
14+
15+
import MacroExamplesInterface
16+
import SwiftUI
17+
18+
private struct MyEnvironmentKey: EnvironmentKey {
19+
static let defaultValue: String = "Default value"
20+
}
21+
22+
extension EnvironmentValues {
23+
@EnvironmentValue(for: MyEnvironmentKey.self)
24+
var myCustomValue: String
25+
}
26+
27+
func runEnvironmentValueAccessorMacroPlayground() {
28+
var environmentValues = EnvironmentValues()
29+
print("Default myCustomValue: \(environmentValues.myCustomValue)")
30+
environmentValues.myCustomValue = "New value"
31+
print("New myCustomValue: \(environmentValues.myCustomValue)")
32+
}

Examples/Sources/MacroExamples/Playground/main.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,7 @@ runMemberMacrosPlayground()
4747
// MARK: - Peer Macros
4848

4949
runPeerMacrosPlayground()
50+
51+
// MARKL - Accessor Macros
52+
53+
runEnvironmentValueAccessorMacroPlayground()
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import MacroExamplesImplementation
14+
import SwiftSyntaxMacros
15+
import SwiftSyntaxMacrosTestSupport
16+
import XCTest
17+
18+
final class EnvironmentValueMacroMacroTests: XCTestCase {
19+
private let macros = ["EnvironmentValue": EnvironmentValueMacro.self]
20+
21+
func testEnvironmentValue() {
22+
assertMacroExpansion(
23+
"""
24+
private struct MyEnvironmentKey: EnvironmentKey {
25+
static let defaultValue: String = "Default value"
26+
}
27+
28+
extension EnvironmentValues {
29+
@EnvironmentValue(for: MyEnvironmentKey.self)
30+
var myCustomValue: String
31+
}
32+
""",
33+
expandedSource: """
34+
private struct MyEnvironmentKey: EnvironmentKey {
35+
static let defaultValue: String = "Default value"
36+
}
37+
38+
extension EnvironmentValues {
39+
var myCustomValue: String {
40+
get {
41+
self[MyEnvironmentKey.self]
42+
}
43+
set {
44+
self[MyEnvironmentKey.self] = newValue
45+
}
46+
}
47+
}
48+
""",
49+
macros: macros
50+
)
51+
}
52+
}

0 commit comments

Comments
 (0)