-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: @wrapper, @DependencyValue(s) 매크로 구현
- Loading branch information
Showing
21 changed files
with
843 additions
and
32 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
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
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 |
---|---|---|
@@ -1,13 +1,25 @@ | ||
<img src="https://github.com/rlarjsdn3/swift-macro-playground/assets/21079970/03ea4a38-951a-450f-9f38-36520b994e18" align="center" width="150" height="150"> </br> | ||
|
||
Bibbi Package는 삐삐(Bibbi) 앱을 개발하는 데 유용한 `매크로`가 포함되어 있습니다. 해당 리포지토리는 구현된 매크로의 기본 컨셉과 매크로를 활용한 적용법을 명시하고 있습니다. 매크로 예제 코드는 [Playground](Sources/Macros/Playground)에서 찾아보실 수 있으며, 자세한 사용법은 각 매크로에 `Option`키를 누르면 확인할 수 있습니다. </br> | ||
**Bibbi Package**는 삐삐(Bibbi)를 개발하는 데 유용한 `라이브러리`와 `매크로`가 포함되어 있습니다. 이 리포지토리는 구현된 매크로의 기본 지식과 활용 방법을 설명하고 있습니다. </br> | ||
|
||
아래는 구현된 매크로의 목록을 보여줍니다. | ||
## Featrues | ||
|
||
## Table Of Macros | ||
### Macros | ||
|
||
* [#URL]() | ||
- [v] [@Codable]() | ||
- [v] [@CodableKey(_:)]() | ||
- [v] [@Deprecated]() | ||
- [v] [@DependencyValue(for:)]() | ||
- [v] [@DependencyValues]() | ||
- [v] [@Wrapper]() | ||
|
||
* [@Codable]() | ||
* [@CodableKey]() | ||
* [@Deprecated]() | ||
## Requirements | ||
|
||
* iOS 15.0+ / macOS 10.15+ | ||
* Swift 5.0+ | ||
|
||
## ChangeLog | ||
|
||
| 버전 | 내용 | | ||
| :----: | :------------------: | | ||
| - | - | |
33 changes: 33 additions & 0 deletions
33
Sources/Macros/Helper/Extensions/AttributeListDecl+Ext.swift
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,33 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by 김건우 on 6/1/24. | ||
// | ||
|
||
import SwiftSyntax | ||
|
||
public extension AttributeListSyntax { | ||
|
||
/// 특정 속성이 적용되었는지 확인합니다. | ||
/// | ||
/// - Parameters: | ||
/// - attributeName: 속성 이름 | ||
/// - Returns: 속성이 있으면 true, 없으면 false | ||
/// | ||
/// - Author: 김소월 | ||
/// | ||
func isAttributeApplied(_ attributeName: String) -> Bool { | ||
guard | ||
let _ = self.first(where: { attribute in | ||
attribute | ||
.as(AttributeSyntax.self)? | ||
.attributeName | ||
.as(IdentifierTypeSyntax.self)? | ||
.name.text == attributeName | ||
}) | ||
else { return false } | ||
return true | ||
} | ||
|
||
} |
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 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by 김건우 on 6/1/24. | ||
// | ||
|
||
import Foundation | ||
|
||
public extension String { | ||
|
||
/// 문자열의 첫 문자를 대문자로 바꿉니다. | ||
/// | ||
/// - Returns: String | ||
/// - Author: 김소월 | ||
/// | ||
func capitalizeFirstLetter() -> String { | ||
return prefix(1).uppercased() + dropFirst() | ||
} | ||
|
||
} |
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,66 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by 김건우 on 6/1/24. | ||
// | ||
|
||
import SwiftSyntax | ||
|
||
public extension VariableDeclSyntax { | ||
|
||
/// 프로퍼티에 초기값이 있는지 확인합니다. | ||
/// | ||
/// - Returns: 초기값이 있으면 true, 없으면 false | ||
/// - Author: 김소월 | ||
/// | ||
var hasIntializer: Bool { | ||
// 라인 하나에 바인딩 하나 | ||
if bindings.count != 1 { | ||
return false | ||
} | ||
|
||
let binding = bindings.first | ||
guard | ||
let _ = binding?.initializer?.value | ||
else { return true } | ||
return false | ||
} | ||
|
||
/// 저장 프로퍼티인지 확인합니다. | ||
/// | ||
/// - Returns: 저장 프로퍼티라면 true, 아니라면 false | ||
/// - Author: 김소월 | ||
/// | ||
var isStoredProperty: Bool { | ||
// 라인 하나에 바인딩 하나 | ||
if bindings.count != 1 { | ||
return false | ||
} | ||
|
||
let binding = bindings.first | ||
switch binding?.accessorBlock?.accessors { | ||
case .none: | ||
return true | ||
|
||
case let .accessors(accessors): | ||
for accessor in accessors { | ||
switch accessor.accessorSpecifier.tokenKind { | ||
case .keyword(.willSet), .keyword(.didSet): | ||
// willset, didset 옵저버는 저장 프로퍼티 | ||
break | ||
|
||
default: | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
|
||
case .getter: | ||
// | ||
return false | ||
} | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
Sources/Macros/Implementation/Accessor/DependencyValueMacro.swift
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,38 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by 김건우 on 5/31/24. | ||
// | ||
|
||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SwiftSyntaxMacros | ||
|
||
public struct DependencyValueMacro: 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 expression = arguments.first? | ||
.expression | ||
else { | ||
throw MacroError.message("유효하지 않은 인자입니다.") | ||
} | ||
|
||
return [ | ||
""" | ||
get { self[\(expression)] } | ||
""", | ||
""" | ||
set { self[\(expression)] = newValue } | ||
""" | ||
] | ||
} | ||
|
||
} | ||
|
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
Oops, something went wrong.