Skip to content

[Macros] Implement a type wrapper transformation as a macro. #1225

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 1 commit into from
Jan 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions Tests/SwiftSyntaxMacrosTest/MacroSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,74 @@ public struct WrapStoredProperties: MemberAttributeMacro {
}
}

struct CustomTypeWrapperMacro {}

extension CustomTypeWrapperMacro: MemberDeclarationMacro {
static func expansion(
of node: AttributeSyntax,
attachedTo declaration: DeclSyntax,
in context: inout MacroExpansionContext
) throws -> [DeclSyntax] {
return [
"""

var _storage: Wrapper<Self>
"""
]
}
}

extension CustomTypeWrapperMacro: MemberAttributeMacro {
static func expansion(
of node: AttributeSyntax,
attachedTo declaration: DeclSyntax,
annotating member: DeclSyntax,
in context: inout MacroExpansionContext
) throws -> [AttributeSyntax] {
return [
AttributeSyntax(
attributeName: SimpleTypeIdentifierSyntax(
name: .identifier("customTypeWrapper")
)
)
.withLeadingTrivia([.newlines(1), .spaces(2)])
]
}
}

extension CustomTypeWrapperMacro: AccessorDeclarationMacro {
static func expansion(
of node: AttributeSyntax,
attachedTo declaration: DeclSyntax,
in context: inout MacroExpansionContext
) throws -> [AccessorDeclSyntax] {
guard let property = declaration.as(VariableDeclSyntax.self),
let binding = property.bindings.first,
let identifier = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier,
binding.accessor == nil
else {
return []
}

if identifier.text == "_storage" { return [] }

return [
"""

get {
_storage[wrappedKeyPath: \\.\(identifier)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI that type wrapper is a bit more then just wrappedKeyPath:, it also detects whether it’s attached to a reference type and if so, adds wrappedSelf: argument and always storageKeyPath: as the last argument which points to \$Storage.<prop-name>.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, this is just a proof of concept. The macro can generate whatever it wants in the accessors. For example, if the macro is attached to an actor or class, it could apply a different macro attribute to each of the stored properties to direct the recursive expansion to generate special accessors in that case. It could also just choose to put whatever code is in the subscript implementation directly into the accessors

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

}
""",
"""

set {
_storage[wrappedKeyPath: \\.\(identifier)] = newValue
}
""",
]
}
}

// MARK: Assertion helper functions

/// Assert that expanding the given macros in the original source produces
Expand Down Expand Up @@ -534,6 +602,7 @@ public let testMacros: [String: Macro.Type] = [
"addBackingStorage": AddBackingStorage.self,
"wrapAllProperties": WrapAllProperties.self,
"wrapStoredProperties": WrapStoredProperties.self,
"customTypeWrapper": CustomTypeWrapperMacro.self,
]

final class MacroSystemTests: XCTestCase {
Expand Down Expand Up @@ -788,4 +857,41 @@ final class MacroSystemTests: XCTestCase {
"""
)
}

func testTypeWrapperTransform() {
AssertMacroExpansion(
macros: testMacros,
"""
@customTypeWrapper
struct Point {
var x: Int
var y: Int
}
""",
// FIXME: Accessor brace indentation is off
"""

struct Point {
var x: Int {
get {
_storage[wrappedKeyPath: \\.x]
}
set {
_storage[wrappedKeyPath: \\.x] = newValue
}
}
var y: Int {
get {
_storage[wrappedKeyPath: \\.y]
}
set {
_storage[wrappedKeyPath: \\.y] = newValue
}
}
var _storage: Wrapper<Self>
}
"""
)

}
}