Skip to content

Commit 8f3c947

Browse files
committed
Swift6 대응
1 parent 3362c96 commit 8f3c947

File tree

14 files changed

+80
-46
lines changed

14 files changed

+80
-46
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.9
1+
// swift-tools-version: 6.0
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription

Sources/DIContainer/Container/Container.swift

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import Foundation
22

33
public class Container {
4-
static var root = Container()
4+
@MainActor
5+
private(set) static var root = Container()
56

67
/// Stored object instance factories.
78
var modules: [String: Module] = [:]
@@ -29,11 +30,12 @@ public extension Container {
2930
}
3031
}
3132

33+
@MainActor
3234
extension Container {
3335
/// Resolves through inference and returns an instance of the given type from the current default container.
3436
///
3537
/// If the dependency is not found, an exception will occur.
36-
static func resolve<T>(for type: AnyObject.Type) -> T {
38+
static func resolve<T, U: InjectionKeyType>(for type: U.Type) -> T {
3739
guard let component: T = weakResolve(for: type) else {
3840
fatalError("Dependency '\(T.self)' not resolved!")
3941
}
@@ -44,19 +46,20 @@ extension Container {
4446
/// Resolves through inference and returns an instance of the given type from the current default container.
4547
///
4648
/// If the dependency is not found, return nil
47-
static func weakResolve<T>(for type: AnyObject.Type) -> T? {
49+
static func weakResolve<T, U: InjectionKeyType>(for type: U.Type) -> T? {
4850
root.module(type)?.resolve() as? T
4951
}
5052

51-
func module(_ type: AnyObject.Type) -> Module? {
53+
func module<T: InjectionKeyType>(_ type: T.Type) -> Module? {
5254
let keyName = KeyName(type).name
5355
return modules[keyName]
5456
}
5557
}
5658

5759
public extension Container {
5860
/// DSL for declaring modules within the container dependency initializer.
59-
@resultBuilder enum ContainerBuilder {
61+
@resultBuilder
62+
enum ContainerBuilder {
6063
public static func buildBlock(_ modules: Module...) -> [Module] { modules }
6164
public static func buildBlock(_ module: Module) -> Module { module }
6265
}
@@ -78,10 +81,23 @@ public extension Container {
7881
self.init()
7982
register(contentsOf: [module()])
8083
}
84+
}
8185

86+
@MainActor
87+
public extension Container {
8288
/// Assigns the current container to the composition root.
8389
func build() {
8490
// Used later in property wrapper
8591
Self.root = self
8692
}
8793
}
94+
95+
#if DEBUG
96+
@MainActor
97+
public extension Container {
98+
static func clear() {
99+
root.modules.removeAll()
100+
}
101+
}
102+
103+
#endif
File renamed without changes.

Sources/DIContainer/Key/InjectionKeyType.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import Foundation
22

3+
@MainActor
34
public protocol InjectionKeyType: AnyObject {
45
associatedtype Value
56
static var value: Value { get }
67
static var weakValue: Value? { get }
78
}
89

10+
@MainActor
911
public extension InjectionKeyType {
1012
static var value: Value {
1113
Container.resolve(for: Self.self)
1214
}
13-
15+
1416
static var weakValue: Value? {
1517
Container.weakResolve(for: Self.self)
1618
}

Sources/DIContainer/PropertyWrapper/Inject.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22

3+
@MainActor
34
@propertyWrapper
45
public class Inject<Value> {
56
private let lazyValue: () -> Value

Sources/DIContainer/PropertyWrapper/WeakInject.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22

3+
@MainActor
34
@propertyWrapper
45
public class WeakInject<Value> {
56
private let lazyValue: () -> Value?

Sources/DIContainer/Scan/ModuleScanner.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ public struct ModuleScanner {
6363
// }
6464
// }
6565

66-
67-
#if DEBUG
6866
print("""
6967
┌───── \(Self.self) \(#function) ──────
7068
│ Duration : \((Date().timeIntervalSince(start) * 1000).rounded())ms
@@ -74,7 +72,6 @@ public struct ModuleScanner {
7472
│ - \(keys)
7573
└────────────────────────────────────────────────
7674
""")
77-
#endif
7875
return keys
7976
}
8077

@@ -116,8 +113,6 @@ public struct ModuleScanner {
116113
// }
117114
// }
118115

119-
120-
#if DEBUG
121116
print("""
122117
┌───── \(Self.self) \(#function) ─────────
123118
│ Duration : \((Date().timeIntervalSince(start) * 1000).rounded())ms
@@ -127,7 +122,6 @@ public struct ModuleScanner {
127122
│ - \(keys)")
128123
└────────────────────────────────────────────────
129124
""")
130-
#endif
131125

132126
return keys
133127
}

Tests/DIContainerTests/Helper/Helper.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Foundation
33
@testable import DIContainer
44

55
extension InjectionKeyType {
6+
@MainActor
67
static var module: Module? {
78
return Container.root.module(Self.self)
89
}

Tests/DIContainerTests/Mock/MockService.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import DIContainer
22
import Foundation
33

4+
@MainActor
45
final class MockServiceKey: InjectionKey {
56
typealias Value = MockService
67
}
78

9+
@MainActor
810
protocol MockService {
911
func doSomething()
1012
}
1113

14+
@MainActor
1215
final class MockServiceImpl: AutoModule, MockService {
1316
typealias ModuleKeyType = MockServiceKey
1417

Tests/DIContainerTests/Tests/InjectTestsTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import XCTest
22

33
@testable import DIContainer
44

5+
@MainActor
56
class ContainerTests: XCTestCase {
67
func test_container1() {
78
testContainer(withSetup: .init {

0 commit comments

Comments
 (0)