Skip to content

Commit

Permalink
Merge pull request #7 from willowtreeapps/feature/support-mutability-…
Browse files Browse the repository at this point in the history
…on-prop-wrapper-resolution

Support mutability on property wrapper resolution
  • Loading branch information
rafcabezas authored Apr 9, 2024
2 parents be2770f + e4879dd commit 1259603
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Grove is available through the Swift Package Manager. To install it, simply add

```swift
dependencies: [
.package(url: "https://github.com/willowtreeapps/grove.git", from: "1.1.0")
.package(url: "https://github.com/willowtreeapps/grove.git", from: "1.1.1")
]
```

Expand Down
30 changes: 18 additions & 12 deletions Sources/Grove/PropertyWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,26 @@ public class Resolve<Dependency>: @unchecked Sendable {
}

public var wrappedValue: Dependency {
switch container.scope(for: Dependency.self) {
case .singleton:
return container.resolve()
case .transient:
transientInstanceLock.lock()
defer {
transientInstanceLock.unlock()
}
guard let transientInstance else {
let transientInstance = (container.resolve() as Dependency)
self.transientInstance = transientInstance
get {
switch container.scope(for: Dependency.self) {
case .singleton:
return container.resolve()
case .transient:
transientInstanceLock.lock()
defer {
transientInstanceLock.unlock()
}
guard let transientInstance else {
let transientInstance = (container.resolve() as Dependency)
self.transientInstance = transientInstance
return transientInstance
}
return transientInstance
}
return transientInstance
}

set {
/* No-Op */
}
}
}
32 changes: 28 additions & 4 deletions Tests/GroveTests/GrovePropertyWrapperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,27 @@
import XCTest
@testable import Grove

private final class TestClass: TestProtocol {
fileprivate struct TestStruct {
var value: Int
}

fileprivate protocol TestProtocol {
var type: TestStruct { get set }
var value: Int { get }
func increment()
}

private final class TestClass: TestProtocol {
var type: TestStruct

var value: Int { type.value }

init(value: Int = 0) {
self.value = value
self.type = TestStruct(value: value)
}

func increment() {
value += 1
type.value += 1
}
}

Expand Down Expand Up @@ -49,10 +61,22 @@ final class GrovePropertyWrapperTests: XCTestCase {
XCTAssertEqual(testClass.value, 103)
XCTAssertEqual(testClass2.value, 100)
}

func testWritingToPropertyDirectly() {
// Given
Grove.defaultContainer.register(as: TestProtocol.self, scope: .singleton, TestClass(value: 10))
@Resolve(TestProtocol.self) var testClass

// When
testClass.type.value = 20

// Then
XCTAssertEqual(testClass.value, 20)
}
}

final class GrovePropertyWrapperAsClassTests: XCTestCase {
@Resolve(TestProtocol.self) var testClass
@Resolve(TestProtocol.self) private var testClass

func testUpdatedRegistration() {
// Given
Expand Down
2 changes: 1 addition & 1 deletion Tests/GroveTests/GroveTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import XCTest
@testable import Grove

protocol TestProtocol {
private protocol TestProtocol {
var value: Int { get }
func increment()
}
Expand Down

0 comments on commit 1259603

Please sign in to comment.