-
Notifications
You must be signed in to change notification settings - Fork 440
Add an accessor macro example #2565
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b7252d2
Add an accessor macro example
rockname 441be20
Add a document comment
rockname a283c96
Remove unnecessary raw interpolations
rockname 74a3d68
Remove unnecessary EnvironmentKey
rockname aaefc0e
Fix typo
rockname b795cd8
Add '#if canImport(SwiftUI)'
rockname File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
Examples/Sources/MacroExamples/Implementation/Accessor/EnvironmentValueMacro.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SwiftSyntax | ||
import SwiftSyntaxMacros | ||
|
||
public struct EnvironmentValueMacro: AccessorMacro { | ||
public static func expansion( | ||
of node: AttributeSyntax, | ||
providingAccessorsOf declaration: some DeclSyntaxProtocol, | ||
in context: some MacroExpansionContext | ||
) throws -> [AccessorDeclSyntax] { | ||
guard | ||
case let .argumentList(arguments) = node.arguments, | ||
let argument = arguments.first | ||
else { return [] } | ||
|
||
return [ | ||
""" | ||
get { self[\(argument.expression)] } | ||
""", | ||
""" | ||
set { self[\(argument.expression)] = newValue } | ||
""", | ||
] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
Examples/Sources/MacroExamples/Interface/AccessorMacros.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#if canImport(SwiftUI) | ||
|
||
import SwiftUI | ||
|
||
// MARK: - EnvironmentValue Accessor | ||
|
||
/// Adds getter / setter to an attached environment value with specified EnvironmentKey | ||
@attached(accessor) | ||
public macro EnvironmentValue(for key: any EnvironmentKey.Type) = | ||
#externalMacro(module: "MacroExamplesImplementation", type: "EnvironmentValueMacro") | ||
|
||
#endif |
37 changes: 37 additions & 0 deletions
37
Examples/Sources/MacroExamples/Playground/AccessorMacrosPlayground.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// MARK: - EnvironmentValue Accessor | ||
|
||
import MacroExamplesInterface | ||
|
||
#if canImport(SwiftUI) | ||
|
||
import SwiftUI | ||
|
||
private struct MyEnvironmentKey: EnvironmentKey { | ||
static let defaultValue: String = "Default value" | ||
} | ||
|
||
extension EnvironmentValues { | ||
@EnvironmentValue(for: MyEnvironmentKey.self) | ||
var myCustomValue: String | ||
} | ||
|
||
func runEnvironmentValueAccessorMacroPlayground() { | ||
var environmentValues = EnvironmentValues() | ||
print("Default myCustomValue: \(environmentValues.myCustomValue)") | ||
environmentValues.myCustomValue = "New value" | ||
print("New myCustomValue: \(environmentValues.myCustomValue)") | ||
} | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
Examples/Tests/MacroExamples/Implementation/Accessor/EnvironmentValueMacroTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import MacroExamplesImplementation | ||
import SwiftSyntaxMacros | ||
import SwiftSyntaxMacrosTestSupport | ||
import XCTest | ||
|
||
final class EnvironmentValueMacroMacroTests: XCTestCase { | ||
private let macros = ["EnvironmentValue": EnvironmentValueMacro.self] | ||
|
||
func testEnvironmentValue() { | ||
assertMacroExpansion( | ||
""" | ||
extension EnvironmentValues { | ||
@EnvironmentValue(for: MyEnvironmentKey.self) | ||
var myCustomValue: String | ||
} | ||
""", | ||
expandedSource: """ | ||
extension EnvironmentValues { | ||
var myCustomValue: String { | ||
get { | ||
self[MyEnvironmentKey.self] | ||
} | ||
set { | ||
self[MyEnvironmentKey.self] = newValue | ||
} | ||
} | ||
} | ||
""", | ||
macros: macros | ||
) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example won’t compile on Linux and Windows if it depends on
SwiftUI
.We’ve got two options here: Either wrap the macro in
#if canImport(SwiftUI)
or change it so it doesn’t depend onSwiftUI
. Which one would you prefer?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for proposing two options!
I'll choose the
#if canImport(SwiftUI)
option this time 🙏b795cd8