-
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
Changes from 2 commits
b7252d2
441be20
a283c96
74a3d68
aaefc0e
b795cd8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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[\(raw: argument.expression)] } | ||
""", | ||
""" | ||
set { self[\(raw: argument.expression)] = newValue } | ||
""", | ||
] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 SwiftUI | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 We’ve got two options here: Either wrap the macro in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for proposing two options! |
||
|
||
// 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") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
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)") | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -47,3 +47,7 @@ runMemberMacrosPlayground() | |||||
// MARK: - Peer Macros | ||||||
|
||||||
runPeerMacrosPlayground() | ||||||
|
||||||
// MARKL - Accessor Macros | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed 🙈 : aaefc0e |
||||||
|
||||||
runEnvironmentValueAccessorMacroPlayground() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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( | ||
""" | ||
private struct MyEnvironmentKey: EnvironmentKey { | ||
static let defaultValue: String = "Default value" | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the expansion of the macro is syntactic and doesn’t actually try to resolve There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed: 74a3d68 |
||
extension EnvironmentValues { | ||
@EnvironmentValue(for: MyEnvironmentKey.self) | ||
var myCustomValue: String | ||
} | ||
""", | ||
expandedSource: """ | ||
private struct MyEnvironmentKey: EnvironmentKey { | ||
static let defaultValue: String = "Default value" | ||
} | ||
|
||
extension EnvironmentValues { | ||
var myCustomValue: String { | ||
get { | ||
self[MyEnvironmentKey.self] | ||
} | ||
set { | ||
self[MyEnvironmentKey.self] = newValue | ||
} | ||
} | ||
} | ||
""", | ||
macros: macros | ||
) | ||
} | ||
} |
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.
I don’t think you need to use
raw:
here or in the setter.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.
Fixed: a283c96