Skip to content

Commit 8aa5f16

Browse files
committed
update
1 parent ea44e5e commit 8aa5f16

File tree

6 files changed

+89
-17
lines changed

6 files changed

+89
-17
lines changed

Sources/Implementation/Member/LoggingMacro.swift

+16-1
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,28 @@ public struct LoggingMacro: MemberMacro {
3131
throw MacroExpansionErrorMessage(msg)
3232
}
3333

34+
let category = extractCategory(from: node).map { "\"\($0)\"" } ?? "String(describing: Self.self)"
35+
3436
return [
3537
DeclSyntax(
3638
"""
3739
lazy var logger: OSLog = {
38-
LoggingMacroHelper.generateOSLog(category: String(describing: Self.self))
40+
LoggingMacroHelper.generateOSLog(category: \(raw: category))
3941
}()
4042
"""),
4143
]
4244
}
45+
46+
private static func extractCategory(from node: AttributeSyntax) -> String? {
47+
node.arguments?.as(LabeledExprListSyntax.self)?
48+
.lazy
49+
.compactMap { labeledExprSyntax -> String? in
50+
labeledExprSyntax.expression
51+
.as(StringLiteralExprSyntax.self)?
52+
.segments.first?
53+
.as(StringSegmentSyntax.self)?
54+
.content.text
55+
}
56+
.first
57+
}
4358
}

Sources/Implementation/Support/SyntaxHelpers.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ extension VariableDeclSyntax {
6363
var isComputedProperty: Bool {
6464
guard
6565
self.bindings.count == 1,
66-
let binding = self.bindings.first?.as(PatternBindingSyntax.self)
66+
let binding = self.bindings.first
6767
else { return false }
6868

6969
return self.bindingSpecifier.tokenKind == .keyword(.var) && binding.isComputedProperty

Sources/Interface/MemberMacros.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public macro CaseDetection(_ accessLevel: CaseDetectionAccessLevelConfig? = nil)
1717
// MARK: - Logging
1818

1919
@attached(member, names: named(logger))
20-
public macro Logging() = #externalMacro(module: "Macros", type: "LoggingMacro")
20+
public macro Logging(category: String? = nil) = #externalMacro(module: "Macros", type: "LoggingMacro")
2121

2222
public enum LoggingMacroHelper {
2323
public static func generateOSLog(_ fileID: String = #fileID, category: String) -> OSLog {
@@ -36,6 +36,7 @@ public enum AssociatedValuesAccessLevelConfig {
3636
case `private`
3737
case `fileprivate`
3838
case `internal`
39+
case `package`
3940
case `public`
4041
}
4142

Sources/Playground/Run/MemberMacrosPlayground.swift

+29-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@ import Foundation
1414
import MacroKit
1515

1616
func runMemberMacrosPlayground() {
17-
// MARK: MemberMacros
17+
runCaseDetection()
1818

19+
runLogging()
20+
21+
runAssociatedValues()
22+
}
23+
24+
// MARK: MemberMacros
25+
26+
private func runCaseDetection() {
1927
@CaseDetection(.fileprivate)
2028
enum Animal1 {
2129
case dog
@@ -33,8 +41,10 @@ func runMemberMacrosPlayground() {
3341

3442
let cat = Animal2.cat(curious: true)
3543
print("cat is \(cat.isCat)")
44+
}
3645

37-
@Logging
46+
private func runLogging() {
47+
@Logging()
3848
class LogModel {
3949
func log() {
4050
Logger(logger).trace("log messsage")
@@ -44,9 +54,22 @@ func runMemberMacrosPlayground() {
4454
#log(level: .error, "log messsage")
4555
}
4656
}
57+
@Logging(category: "UI")
58+
class LogUIModel {
59+
func log() {
60+
Logger(logger).trace("log messsage")
61+
os_log(.info, log: logger, "log messsage")
62+
#log(level: .info, "log messsage")
63+
#log(level: .debug, "log messsage")
64+
#log(level: .error, "log messsage")
65+
}
66+
}
4767

4868
LogModel().log()
69+
LogUIModel().log()
70+
}
4971

72+
private func runAssociatedValues() {
5073
@AssociatedValues(.fileprivate)
5174
enum SomeEnum {
5275
case none
@@ -56,6 +79,8 @@ func runMemberMacrosPlayground() {
5679
case closure(() -> Void)
5780
}
5881

59-
let someEnum = SomeEnum.labeledValue(a: "a", b: "b")
60-
print(someEnum.labeledValue)
82+
print(SomeEnum.labeledValue(a: "a", b: "b").labeledValue!)
83+
print(SomeEnum.value(10).value!)
84+
print(SomeEnum.optional("Hello").optional!!)
85+
SomeEnum.closure({ print("Hello MacroKit World") }).closure!()
6186
}

Sources/Playground/Run/PeerMacrosPlayground.swift

+14-10
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,42 @@ import Foundation
1414
import MacroKit
1515

1616
func runPeerMacrosPlayground() {
17-
// MARK: - Add Async
18-
17+
runAddAsync()
18+
19+
runAddCompletionHandler()
20+
}
21+
22+
// MARK: - Add Async
23+
private func runAddAsync() {
1924
struct MyStruct {
2025
@AddAsync
2126
func c(a: Int, for b: String, _ value: Double, completionBlock: @escaping (Result<String, Error>) -> Void) -> Void {
2227
completionBlock(.success("a: \(a), b: \(b), value: \(value)"))
2328
}
24-
29+
2530
@AddAsync
2631
func d(a: Int, for b: String, _ value: Double, completionBlock: @escaping (Bool) -> Void) -> Void {
2732
completionBlock(true)
2833
}
2934
}
30-
35+
3136
Task {
3237
let myStruct = MyStruct()
3338
_ = try? await myStruct.c(a: 5, for: "Test", 20)
34-
39+
3540
_ = await myStruct.d(a: 10, for: "value", 40)
3641
}
37-
38-
// MARK: - Add Completion Handler
39-
42+
}
43+
44+
private func runAddCompletionHandler() {
4045
class MyClass {
4146
@AddCompletionHandler
4247
func f(a: Int, for b: String, _ value: Double) async -> String {
4348
return b
4449
}
4550
}
46-
51+
4752
MyClass().f(a: 1, for: "hello", 3.14159) { result in
4853
print("Eventually received \(result + "!")")
4954
}
5055
}
51-

Tests/MacroKitTests/Member/LoggingTests.swift

+27
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,31 @@ final class LoggingTests: XCTestCase {
4949
macros: ["Logging": LoggingMacro.self]
5050
)
5151
}
52+
53+
func testLoggingMacroExpansion3() throws {
54+
assertMacroExpansion(
55+
"""
56+
@Logging(category: "UI")
57+
struct TestingView: View {
58+
59+
var body: some View {
60+
Text("Hello, world!")
61+
}
62+
}
63+
""",
64+
expandedSource: """
65+
struct TestingView: View {
66+
67+
var body: some View {
68+
Text("Hello, world!")
69+
}
70+
71+
lazy var logger: OSLog = {
72+
LoggingMacroHelper.generateOSLog(category: "UI")
73+
}()
74+
}
75+
""",
76+
macros: ["Logging": LoggingMacro.self]
77+
)
78+
}
5279
}

0 commit comments

Comments
 (0)