From efba1336af2ffb0cc64a18ad9a2978d5fa7a2ecd Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Wed, 29 Nov 2023 11:50:46 -0800 Subject: [PATCH] Fix `BindingAction`'s case key path for extraction (#2600) --- .../ComposableArchitecture/SwiftUI/Binding.swift | 2 +- .../Reducers/BindingReducerTests.swift | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Sources/ComposableArchitecture/SwiftUI/Binding.swift b/Sources/ComposableArchitecture/SwiftUI/Binding.swift index e6b6de2bf0c5..ff15a29b6652 100644 --- a/Sources/ComposableArchitecture/SwiftUI/Binding.swift +++ b/Sources/ComposableArchitecture/SwiftUI/Binding.swift @@ -159,7 +159,7 @@ public struct BindingAction: CasePathable, Equatable, @unchecked Sendable ) -> AnyCasePath { AnyCasePath( embed: { .set(keyPath, $0) }, - extract: { $0.keyPath == keyPath ? $0.value as? Value : nil } + extract: { $0.keyPath == keyPath ? $0.value.base as? Value : nil } ) } } diff --git a/Tests/ComposableArchitectureTests/Reducers/BindingReducerTests.swift b/Tests/ComposableArchitectureTests/Reducers/BindingReducerTests.swift index c862e43d3d67..c2bf747ace5d 100644 --- a/Tests/ComposableArchitectureTests/Reducers/BindingReducerTests.swift +++ b/Tests/ComposableArchitectureTests/Reducers/BindingReducerTests.swift @@ -126,4 +126,18 @@ final class BindingTests: BaseTCATestCase { } _ = AnyCasePath(unsafe: Foo.bar).extract(from: .bar(.buzz(true))) } + + struct TestMatching { + struct CounterState { + @BindingState var count = 0 + } + @CasePathable + enum CounterAction: CasePathable { + case binding(BindingAction) + } + } + func testMatching() { + let action = TestMatching.CounterAction.binding(.set(\.$count, 42)) + XCTAssertEqual(action[case: \.binding.$count], 42) + } }