-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 748450d
Showing
11 changed files
with
616 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,90 @@ | ||
# Xcode | ||
# | ||
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore | ||
|
||
## User settings | ||
xcuserdata/ | ||
|
||
## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) | ||
*.xcscmblueprint | ||
*.xccheckout | ||
|
||
## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) | ||
build/ | ||
DerivedData/ | ||
*.moved-aside | ||
*.pbxuser | ||
!default.pbxuser | ||
*.mode1v3 | ||
!default.mode1v3 | ||
*.mode2v3 | ||
!default.mode2v3 | ||
*.perspectivev3 | ||
!default.perspectivev3 | ||
|
||
## Obj-C/Swift specific | ||
*.hmap | ||
|
||
## App packaging | ||
*.ipa | ||
*.dSYM.zip | ||
*.dSYM | ||
|
||
## Playgrounds | ||
timeline.xctimeline | ||
playground.xcworkspace | ||
|
||
# Swift Package Manager | ||
# | ||
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. | ||
# Packages/ | ||
# Package.pins | ||
# Package.resolved | ||
# *.xcodeproj | ||
# | ||
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata | ||
# hence it is not needed unless you have added a package configuration file to your project | ||
.swiftpm | ||
|
||
.build/ | ||
|
||
# CocoaPods | ||
# | ||
# We recommend against adding the Pods directory to your .gitignore. However | ||
# you should judge for yourself, the pros and cons are mentioned at: | ||
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control | ||
# | ||
# Pods/ | ||
# | ||
# Add this line if you want to avoid checking in source code from the Xcode workspace | ||
# *.xcworkspace | ||
|
||
# Carthage | ||
# | ||
# Add this line if you want to avoid checking in source code from Carthage dependencies. | ||
# Carthage/Checkouts | ||
|
||
Carthage/Build/ | ||
|
||
# Accio dependency management | ||
Dependencies/ | ||
.accio/ | ||
|
||
# fastlane | ||
# | ||
# It is recommended to not store the screenshots in the git repo. | ||
# Instead, use fastlane to re-generate the screenshots whenever they are needed. | ||
# For more information about the recommended setup visit: | ||
# https://docs.fastlane.tools/best-practices/source-control/#source-control | ||
|
||
fastlane/report.xml | ||
fastlane/Preview.html | ||
fastlane/screenshots/**/*.png | ||
fastlane/test_output | ||
|
||
# Code Injection | ||
# | ||
# After new code Injection tools there's a generated folder /iOSInjectionProject | ||
# https://github.com/johnno1962/injectionforxcode | ||
|
||
iOSInjectionProject/ |
This file contains 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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Curiosity Software inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains 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,14 @@ | ||
{ | ||
"pins" : [ | ||
{ | ||
"identity" : "swift-syntax", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-syntax.git", | ||
"state" : { | ||
"revision" : "6ad4ea24b01559dde0773e3d091f1b9e36175036", | ||
"version" : "509.0.2" | ||
} | ||
} | ||
], | ||
"version" : 2 | ||
} |
This file contains 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,50 @@ | ||
// swift-tools-version: 5.9 | ||
|
||
import PackageDescription | ||
import CompilerPluginSupport | ||
|
||
let package = Package( | ||
name: "swift-property-name", | ||
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .macCatalyst(.v13)], | ||
products: [ | ||
.library( | ||
name: "PropertyName", | ||
targets: ["PropertyName"] | ||
), | ||
.executable( | ||
name: "PropertyNameMacroClient", | ||
targets: ["PropertyNameMacroClient"] | ||
), | ||
], | ||
dependencies: [ | ||
.package(url: "https://github.com/apple/swift-syntax.git", .upToNextMajor(from: "509.0.2")), | ||
], | ||
targets: [ | ||
.macro( | ||
name: "Implementation", | ||
dependencies: [ | ||
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"), | ||
.product(name: "SwiftCompilerPlugin", package: "swift-syntax") | ||
] | ||
), | ||
.target( | ||
name: "PropertyName", | ||
dependencies: ["Implementation"] | ||
), | ||
// A client of the library, which is able to use the macro in its own code. | ||
.executableTarget( | ||
name: "PropertyNameMacroClient", | ||
dependencies: [ | ||
"PropertyName", | ||
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"), | ||
] | ||
), | ||
.testTarget( | ||
name: "ImplementationMacroTests", | ||
dependencies: [ | ||
"Implementation", | ||
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"), | ||
] | ||
), | ||
] | ||
) |
This file contains 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,73 @@ | ||
# swift-property-name | ||
|
||
 | ||
 | ||
|
||
This Swift macro simplifies the process of obtaining property names as strings in Swift. By annotating a struct or class with `@PropertyNameAccessible`, it automatically generates extensions and methods that provide access to property names as strings. | ||
|
||
## Quick start | ||
|
||
To get started, import: `import PropertyName`, annotate your struct or class with `@PropertyNameAccessible`: | ||
|
||
```swift | ||
import PropertyName | ||
|
||
@PropertyNameAccessible | ||
struct Person { | ||
let name: String | ||
let age: Int | ||
} | ||
``` | ||
|
||
This will automatically generate an extension with a `propertyName(for:)` function. | ||
|
||
```swift | ||
extension Person { | ||
static func propertyName(for keyPath: PartialKeyPath<Self>) -> String { | ||
switch keyPath { | ||
case \.name: | ||
return "name" | ||
case \.age: | ||
return "age" | ||
default: | ||
fatalError() | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Installation | ||
|
||
### For Xcode | ||
|
||
If you are using [GUI to set up Package Dependencies in Xcode](https://developer.apple.com/documentation/xcode/adding-package-dependencies-to-your-app), add the URL in Package Dependencies. | ||
|
||
``` | ||
https://github.com/CuriositySoftware/swift-property-name | ||
``` | ||
|
||
### For Package.swift | ||
|
||
If you are using Package.swift add: | ||
|
||
```swift | ||
.package( | ||
url: "https://github.com/CuriositySoftware/swift-property-name/", | ||
.upToNextMajor(from: "0.1.0") | ||
) | ||
``` | ||
|
||
and then add the product to any target that needs access to the macro: | ||
|
||
```swift | ||
.target( | ||
name: "YourTarget", | ||
dependencies: [ | ||
.product( | ||
name: "PropertyName", | ||
package: "swift-property-name" | ||
) | ||
] | ||
) | ||
``` | ||
|
This file contains 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,27 @@ | ||
import SwiftDiagnostics | ||
|
||
private let domain = "PropertyName" | ||
|
||
enum Diagnostics: String, Error { | ||
case appliedTypeFail | ||
} | ||
|
||
extension Diagnostics: DiagnosticMessage { | ||
var diagnosticID: MessageID { | ||
MessageID(domain: domain, id: rawValue) | ||
} | ||
|
||
var message: String { | ||
switch self { | ||
case .appliedTypeFail: | ||
"This macro can be applied to a class or struct or actor" | ||
} | ||
} | ||
|
||
var severity: DiagnosticSeverity { | ||
switch self { | ||
case .appliedTypeFail: | ||
.error | ||
} | ||
} | ||
} |
This file contains 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,78 @@ | ||
import SwiftSyntax | ||
import SwiftSyntaxMacros | ||
import Foundation | ||
|
||
public enum PropertyNameMacro: ExtensionMacro { | ||
public static func expansion( | ||
of node: SwiftSyntax.AttributeSyntax, | ||
attachedTo declaration: some SwiftSyntax.DeclGroupSyntax, | ||
providingExtensionsOf type: some SwiftSyntax.TypeSyntaxProtocol, | ||
conformingTo protocols: [SwiftSyntax.TypeSyntax], | ||
in context: some SwiftSyntaxMacros.MacroExpansionContext | ||
) throws -> [SwiftSyntax.ExtensionDeclSyntax] { | ||
guard | ||
declaration.is(StructDeclSyntax.self) || | ||
declaration.is(ClassDeclSyntax.self) || | ||
declaration.is(ActorDeclSyntax.self) | ||
else { | ||
throw Diagnostics.appliedTypeFail | ||
} | ||
|
||
let variableDeclarations = declaration.memberBlock.members | ||
.compactMap { $0.decl.as(VariableDeclSyntax.self) } | ||
|
||
guard !variableDeclarations.isEmpty else { | ||
return [] | ||
} | ||
|
||
return [ | ||
ExtensionDeclSyntax( | ||
modifiers: declaration.modifiers, | ||
extendedType: type, | ||
memberBlock: try MemberBlockSyntax { | ||
try FunctionDeclSyntax( | ||
""" | ||
static func propertyName(for keyPath: PartialKeyPath<Self>) -> String { | ||
switch keyPath { | ||
\(raw: variables(with: variableDeclarations)) | ||
default: | ||
fatalError() | ||
} | ||
} | ||
""" | ||
) | ||
} | ||
) | ||
] | ||
} | ||
} | ||
|
||
private extension PropertyNameMacro { | ||
static func makeExtensionSyntax( | ||
extendedType: TypeSyntaxProtocol, | ||
modifiers: DeclModifierListSyntax, | ||
functionDeclSyntax: FunctionDeclSyntax | ||
) -> ExtensionDeclSyntax { | ||
.init( | ||
modifiers: modifiers, | ||
extendedType: extendedType, | ||
memberBlock: MemberBlockSyntax { | ||
functionDeclSyntax | ||
} | ||
) | ||
} | ||
|
||
static func variables( | ||
with variableDeclarations: [VariableDeclSyntax] | ||
) -> String { | ||
variableDeclarations | ||
.flatMap { $0.bindings } | ||
.compactMap { $0.pattern.as(IdentifierPatternSyntax.self) } | ||
.map { $0.identifier.text } | ||
.reduce("") { result, text in | ||
result + """ | ||
case \\.\(text): return "\(text)" | ||
""" | ||
} | ||
} | ||
} |
This file contains 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,11 @@ | ||
#if canImport(SwiftCompilerPlugin) | ||
import SwiftCompilerPlugin | ||
import SwiftSyntaxMacros | ||
|
||
@main | ||
struct PropertyFatalErrorCompilerPlugin: CompilerPlugin { | ||
let providingMacros: [Macro.Type] = [ | ||
PropertyNameMacro.self | ||
] | ||
} | ||
#endif |
This file contains 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,6 @@ | ||
|
||
@attached(extension, names: arbitrary) | ||
public macro PropertyNameAccessible() = #externalMacro( | ||
module: "Implementation", | ||
type: "PropertyNameMacro" | ||
) |
This file contains 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,8 @@ | ||
import PropertyName | ||
import Foundation | ||
|
||
@PropertyNameAccessible | ||
struct Person { | ||
let name: String | ||
let age: Int | ||
} |
Oops, something went wrong.