Skip to content

Commit fd25603

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

File tree

6 files changed

+144
-0
lines changed

6 files changed

+144
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 SwiftSyntaxBuilder
15+
import SwiftSyntaxMacros
16+
17+
public struct EnvironmentValueMacro: AccessorMacro {
18+
public static func expansion(
19+
of node: AttributeSyntax,
20+
providingAccessorsOf declaration: some DeclSyntaxProtocol,
21+
in context: some MacroExpansionContext
22+
) throws -> [AccessorDeclSyntax] {
23+
guard
24+
case let .argumentList(arguments) = node.arguments,
25+
let argument = arguments.first
26+
else { return [] }
27+
28+
return [
29+
"""
30+
get { self[\(raw: argument.expression)] }
31+
""",
32+
"""
33+
set { self[\(raw: argument.expression)] = newValue }
34+
""",
35+
]
36+
}
37+
}

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(key: any EnvironmentKey.Type) =
19+
#externalMacro(module: "MacroExamplesImplementation", type: "EnvironmentValueMacro")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
private struct MyEnvironmentKey: EnvironmentKey {
18+
static let defaultValue: String = "Default value"
19+
}
20+
21+
extension EnvironmentValues {
22+
@EnvironmentValue(key: MyEnvironmentKey.self)
23+
var : String
24+
}
25+
26+
func runEnvironmentValueAccessorMacroPlayground() {
27+
var environmentValues = EnvironmentValues()
28+
print("Default myCustomValue: \(environmentValues.myCustomValue)")
29+
environmentValues.myCustomValue = "New value"
30+
print("New myCustomValue: \(environmentValues.myCustomValue)")
31+
}

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(key: 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)