Skip to content

Commit 7fdc7f8

Browse files
committed
fix
1 parent 9ae97be commit 7fdc7f8

File tree

4 files changed

+34
-27
lines changed

4 files changed

+34
-27
lines changed

Sources/VDFlow/EmptyStep.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ public struct EmptyStepsCollection: StepsCollection {
1919
get { .none }
2020
set {}
2121
}
22-
23-
public let none = EmptyStep()
22+
2423
public init() {}
2524

2625
public static func step<T>(for keyPath: WritableKeyPath<Self, StepID<T>>) -> AllSteps? {
2726
AllSteps.none
2827
}
2928

30-
public static func keyPath(for step: AllSteps) -> PartialKeyPath<EmptyStepsCollection> {
31-
\.none
29+
public static func keyPath(for step: AllSteps) -> PartialKeyPath<EmptyStepsCollection>? {
30+
nil
3231
}
3332

3433
public enum AllSteps: Codable, Hashable, Sendable, CaseIterable {
@@ -47,5 +46,5 @@ extension Never: StepsCollection {
4746
set {}
4847
}
4948
public static func step<T>(for keyPath: WritableKeyPath<Self, StepID<T>>) -> AllSteps? { nil }
50-
public static func keyPath(for step: Never) -> PartialKeyPath<Never> { }
49+
public static func keyPath(for step: Never) -> PartialKeyPath<Never>? { }
5150
}

Sources/VDFlow/StepWrapper.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ public struct StepWrapper<Parent: StepsCollection, Value>: Identifiable, StepWra
9696
.map { (id, $0) }
9797
.first
9898
}
99+
100+
/// The key path for this step within the parent collection.
101+
public var keyPath: WritableKeyPath<Parent, Value> {
102+
Parent.keyPath(for: id)! as! WritableKeyPath<Parent, Value>
103+
}
99104
}
100105

101106
public extension StepWrapper where Value == EmptyStep {

Sources/VDFlow/StepsCollection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public protocol StepsCollection {
3737
static func step<T>(for keyPath: WritableKeyPath<Self, StepID<T>>) -> AllSteps?
3838

3939
/// Retrieves the key path for a specific step in the collection.
40-
static func keyPath(for step: AllSteps) -> PartialKeyPath<Self>
40+
static func keyPath(for step: AllSteps) -> PartialKeyPath<Self>?
4141
}
4242

4343
extension StepsCollection {

Sources/VDFlowMacros/StepsMacro.swift

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,30 @@ public struct StepsMacro: MemberAttributeMacro, ExtensionMacro, MemberMacro, Acc
369369
"""
370370
result.append(_lastMutateStepID)
371371

372+
let stepFunc: DeclSyntax =
373+
"""
374+
public static func step<T>(for keyPath: WritableKeyPath<Self, StepID<T>>) -> AllSteps? {
375+
switch keyPath {
376+
\(raw: cases.map { "case \\.$\($0), \\._\($0): return .\($0)" }.joined(separator: "\n"))
377+
default:
378+
return nil
379+
}
380+
}
381+
"""
382+
result.append(stepFunc)
383+
384+
let needDefaultNil = !cases.contains("none") && isOptional
385+
let keyPathFunc: DeclSyntax =
386+
"""
387+
public static func keyPath(for step: AllSteps) -> PartialKeyPath<Self>? {
388+
switch step {
389+
\(raw: cases.map { "case .\($0): return \\.\($0)" }.joined(separator: "\n"))
390+
\(raw: needDefaultNil ? "default: return nil" : "")
391+
}
392+
}
393+
"""
394+
result.append(keyPathFunc)
395+
372396
guard canInitWithSelected else { return result }
373397

374398
let initialSelected: DeclSyntax =
@@ -403,27 +427,6 @@ public struct StepsMacro: MemberAttributeMacro, ExtensionMacro, MemberMacro, Acc
403427
funcString += "}"
404428
return DeclSyntax(stringLiteral: funcString)
405429
}
406-
let stepFunc: DeclSyntax =
407-
"""
408-
public static func step<T>(for keyPath: WritableKeyPath<Self, StepID<T>>) -> AllSteps? {
409-
switch keyPath {
410-
\(raw: cases.map { "case \\.$\($0), \\._\($0): return .\($0)" }.joined(separator: "\n"))
411-
default:
412-
return nil
413-
}
414-
}
415-
"""
416-
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)
427430
return result
428431
}
429432
}

0 commit comments

Comments
 (0)