Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support mutability on property wrapper resolution #7

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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