Skip to content

Commit 5f20d0d

Browse files
committed
feat(Configurator): Scope support for immutable values of classes
1 parent 3f30e81 commit 5f20d0d

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

Sources/FunctionalConfigurator/Configurator.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,17 @@ extension Configurator {
195195
var configurator: Configurator
196196
var keyPath: FunctionalKeyPath<Base, Value>
197197

198+
public func scope(
199+
_ configuration: @escaping (Configurator<Value>) -> Configurator<Value>
200+
) -> Configurator where Value: AnyObject {
201+
configurator.appendingConfiguration { base in
202+
keyPath.embed(
203+
modification(of: keyPath.extract(from: base), with: configuration),
204+
in: base
205+
)
206+
}
207+
}
208+
198209
public subscript<LocalValue>(
199210
dynamicMember keyPath: WritableKeyPath<Value, LocalValue>
200211
) -> CallableBlock<LocalValue> where Value: AnyObject {

Tests/DeclarativeConfigurationTests/ConfiguratorTests.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,24 @@ final class ConfiguratorTests: XCTestCase {
123123
}
124124

125125
func testScope() {
126-
struct Container: ConfigInitializable, Equatable {
127-
struct Content: Equatable {
126+
struct Container: ConfigInitializable {
127+
class Content {
128128
var a: Int = 0
129129
var b: Int = 0
130130
var c: Int = 0
131+
132+
init() {}
131133
}
132-
var content: Content = .init()
134+
135+
let content: Content = .init()
136+
}
137+
138+
let expected = Container { $0
139+
.content.a(1)
140+
.content.b(2)
141+
.content.c(3)
133142
}
134143

135-
let expected = Container(content: .init(a: 1, b: 2, c: 3))
136144
let actual = Container { $0
137145
.content.scope { $0
138146
.a(1)
@@ -141,6 +149,8 @@ final class ConfiguratorTests: XCTestCase {
141149
}
142150
}
143151

144-
XCTAssertEqual(actual, expected)
152+
XCTAssertEqual(actual.content.a, expected.content.a)
153+
XCTAssertEqual(actual.content.b, expected.content.b)
154+
XCTAssertEqual(actual.content.c, expected.content.c)
145155
}
146156
}

0 commit comments

Comments
 (0)