|
11 | 11 | /// - Parameter keyPath: A case key path to a specific associated value.
|
12 | 12 | /// - Returns: A new binding.
|
13 | 13 | public subscript<Member>(
|
14 |
| - dynamicMember keyPath: CaseKeyPath<Value, Member> |
| 14 | + dynamicMember keyPath: KeyPath<Value.AllCasePaths, AnyCasePath<Value, Member>> |
15 | 15 | ) -> Binding<Member>?
|
16 | 16 | where Value: CasePathable {
|
17 |
| - Binding<Member>( |
| 17 | + let casePath = Value.allCasePaths[keyPath: keyPath] |
| 18 | + return Binding<Member>( |
18 | 19 | unwrapping: Binding<Member?>(
|
19 |
| - get: { self.wrappedValue[case: keyPath] }, |
| 20 | + get: { casePath.extract(from: self.wrappedValue) }, |
20 | 21 | set: { newValue, transaction in
|
21 | 22 | guard let newValue else { return }
|
22 |
| - self.transaction(transaction).wrappedValue[case: keyPath] = newValue |
| 23 | + self.transaction(transaction).wrappedValue = casePath.embed(newValue) |
23 | 24 | }
|
24 | 25 | )
|
25 | 26 | )
|
|
31 | 32 | ///
|
32 | 33 | /// - Parameter keyPath: A case key path to a specific associated value.
|
33 | 34 | /// - Returns: A new binding.
|
34 |
| - public subscript<Enum, AssociatedValue>( |
35 |
| - dynamicMember keyPath: CaseKeyPath<Enum, AssociatedValue> |
36 |
| - ) -> Binding<AssociatedValue?> |
| 35 | + public subscript<Enum: CasePathable, Member>( |
| 36 | + dynamicMember keyPath: KeyPath<Enum.AllCasePaths, AnyCasePath<Enum, Member>> |
| 37 | + ) -> Binding<Member?> |
37 | 38 | where Value == Enum? {
|
38 |
| - return Binding<AssociatedValue?>( |
39 |
| - get: { self.wrappedValue[case: (\Enum?.Cases.some).appending(path: keyPath)] }, |
| 39 | + let casePath = Enum.allCasePaths[keyPath: keyPath] |
| 40 | + return Binding<Member?>( |
| 41 | + get: { |
| 42 | + guard let wrappedValue = self.wrappedValue else { return nil } |
| 43 | + return casePath.extract(from: wrappedValue) |
| 44 | + }, |
40 | 45 | set: { newValue, transaction in
|
41 | 46 | guard let newValue else {
|
42 | 47 | self.transaction(transaction).wrappedValue = nil
|
43 | 48 | return
|
44 | 49 | }
|
45 |
| - self.transaction(transaction).wrappedValue[ |
46 |
| - case: (\Enum?.Cases.some).appending(path: keyPath) |
47 |
| - ] = newValue |
| 50 | + self.transaction(transaction).wrappedValue = casePath.embed(newValue) |
48 | 51 | }
|
49 | 52 | )
|
50 | 53 | }
|
|
0 commit comments