Skip to content

Commit 9ae97be

Browse files
committed
add key path func
1 parent 06c5fcf commit 9ae97be

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

Sources/VDFlow/EmptyStep.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ public struct EmptyStepsCollection: StepsCollection {
2020
set {}
2121
}
2222

23+
public let none = EmptyStep()
2324
public init() {}
2425

2526
public static func step<T>(for keyPath: WritableKeyPath<Self, StepID<T>>) -> AllSteps? {
2627
AllSteps.none
2728
}
2829

30+
public static func keyPath(for step: AllSteps) -> PartialKeyPath<EmptyStepsCollection> {
31+
\.none
32+
}
33+
2934
public enum AllSteps: Codable, Hashable, Sendable, CaseIterable {
3035

3136
case none
@@ -36,11 +41,11 @@ public struct EmptyStepsCollection: StepsCollection {
3641

3742
@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *)
3843
extension Never: StepsCollection {
39-
4044
public var _lastMutateID: MutateID? { nil }
4145
public var selected: Never {
4246
get { fatalError() }
4347
set {}
4448
}
4549
public static func step<T>(for keyPath: WritableKeyPath<Self, StepID<T>>) -> AllSteps? { nil }
50+
public static func keyPath(for step: Never) -> PartialKeyPath<Never> { }
4651
}

Sources/VDFlow/StepsCollection.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public protocol StepsCollection {
3535

3636
/// Retrieves the step ID for a specific step key path.
3737
static func step<T>(for keyPath: WritableKeyPath<Self, StepID<T>>) -> AllSteps?
38+
39+
/// Retrieves the key path for a specific step in the collection.
40+
static func keyPath(for step: AllSteps) -> PartialKeyPath<Self>
3841
}
3942

4043
extension StepsCollection {

Sources/VDFlowMacros/StepsMacro.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,16 @@ public struct StepsMacro: MemberAttributeMacro, ExtensionMacro, MemberMacro, Acc
414414
}
415415
"""
416416
result.append(stepFunc)
417+
418+
let keyPathFunc: DeclSyntax =
419+
"""
420+
public static func keyPath(for step: AllSteps) -> PartialKeyPath<Self> {
421+
switch step {
422+
\(raw: cases.map { "case .\($0): return \\.\($0)" }.joined(separator: "\n"))
423+
}
424+
}
425+
"""
426+
result.append(keyPathFunc)
417427
return result
418428
}
419429
}

0 commit comments

Comments
 (0)