Skip to content

Commit 098fd00

Browse files
committed
apply subscript code
1 parent d857cd8 commit 098fd00

File tree

13 files changed

+135
-26
lines changed

13 files changed

+135
-26
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ Then get an instance from the container.
5151
@Inject(AnimalKey.self)
5252
var cat: Meow
5353
cat.doSomething() // prints "Meow.."
54+
// or
55+
Container[AnimalKey.self].doSomething() // prints "Meow.."
5456
```
5557

5658
Where definitions of the protocols and struct are
@@ -113,4 +115,5 @@ The DIContainer are inspired by:
113115
* [mikeash.com - Friday Q&A 2014-08-08: Swift Name Mangling](https://mikeash.com/pyblog/friday-qa-2014-08-15-swift-name-mangling.html)
114116
* [Wikipedia - Name mangling](https://en.wikipedia.org/wiki/Name_mangling#Swift)
115117
* [Github - DerekSelander/dsdump](https://github.com/DerekSelander/dsdump)
118+
* [Github - Apple/swift-service-context](https://github.com/apple/swift-service-context)
116119
* [Building a class-dump in 2020](https://derekselander.github.io/dsdump/)

Sources/DIContainer/Container/Container.swift

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ public class Container {
55
private(set) static var root = Container()
66

77
/// Stored object instance factories.
8-
var modules: [String: Module] = [:]
8+
var _storage: [String: Module] = [:]
99

1010
public init() {}
11-
deinit { modules.removeAll() }
11+
deinit { _storage.removeAll() }
1212
}
1313

1414
public extension Container {
1515
/// Registers a specific type and its instantiating factory.
1616
@discardableResult
1717
func register(module: Module) -> Self {
1818
let key = module.name
19-
if let _ = modules[key] {
19+
if let _ = _storage[key] {
2020
assertionFailure("\(key) Key is existed. Please check module \(module)")
2121
}
22-
modules[key] = module
22+
_storage[key] = module
2323
return self
2424
}
2525

@@ -49,15 +49,15 @@ extension Container {
4949
static func weakResolve<T, U: InjectionKeyType>(for type: U.Type) -> T? {
5050
root.module(type)?.resolve() as? T
5151
}
52-
52+
5353
/// Check if the dependency is registered in the container.
5454
static func isRegistered<T: InjectionKeyType>(_ type: T.Type) -> Bool {
5555
root.module(type) != nil
5656
}
5757

5858
func module<T: InjectionKeyType>(_ type: T.Type) -> Module? {
5959
let keyName = KeyName(type).name
60-
return modules[keyName]
60+
return _storage[keyName]
6161
}
6262
}
6363

@@ -95,6 +95,18 @@ public extension Container {
9595
// Used later in property wrapper
9696
Self.root = self
9797
}
98+
99+
static subscript<T: InjectionKeyType>(_ type: T.Type) -> T.Value {
100+
resolve(for: type)
101+
}
102+
103+
var count: Int {
104+
_storage.count
105+
}
106+
107+
var isEmpty: Bool {
108+
_storage.isEmpty
109+
}
98110
}
99111

100112
#if DEBUG
@@ -103,6 +115,14 @@ public extension Container {
103115
static func clear() {
104116
root = .init()
105117
}
118+
119+
static func register(_ module: Module) {
120+
root.register(module: module)
121+
}
122+
123+
static func register(contentsOf newElements: [Module]) {
124+
root.register(contentsOf: newElements)
125+
}
106126
}
107127

108128
#endif
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import Foundation
22

3+
@MainActor
34
struct KeyName {
45
let name: String
5-
init(_ type: AnyObject.Type) {
6-
name = NSStringFromClass(type)
6+
init<T: InjectionKeyType> (_ type: T.Type) {
7+
name = type.nameOverride ?? NSStringFromClass(type)
78
}
89
}

Sources/DIContainer/Key/InjectionKeyType.swift

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@ import Foundation
33
@MainActor
44
public protocol InjectionKeyType: AnyObject {
55
associatedtype Value
6-
static var value: Value { get }
7-
static var weakValue: Value? { get }
6+
7+
/// The human-readable name of this key.
8+
/// This name will be used instead of the type name when a value is printed.
9+
///
10+
/// It MAY also be picked up by an instrument (from Swift Tracing) which serializes context items and e.g. used as
11+
/// header name for carried metadata. Though generally speaking header names are NOT required to use the nameOverride,
12+
/// and MAY use their well known names for header names etc, as it depends on the specific transport and instrument used.
13+
///
14+
/// For example, a context key representing the W3C "trace-state" header may want to return "trace-state" here,
15+
/// in order to achieve a consistent look and feel of this context item throughout logging and tracing systems.
16+
///
17+
/// Defaults to `nil`.
18+
static var nameOverride: String? { get }
819
}
920

10-
@MainActor
11-
public extension InjectionKeyType {
12-
static var value: Value {
13-
Container.resolve(for: Self.self)
14-
}
15-
16-
static var weakValue: Value? {
17-
Container.weakResolve(for: Self.self)
18-
}
21+
extension InjectionKeyType {
22+
public static var nameOverride: String? { nil }
1923
}
2024

2125
/// DO NOT USE THIS CODE DIRECTLY

Sources/DIContainer/Module/AutoModule.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ public protocol AutoModulable: AnyObject {
1515
public typealias AutoModule = AutoModulable & AutoModuleBase
1616

1717
#if DEBUG
18+
@MainActor
1819
extension AutoModulable where Self: AutoModuleBase {
1920
static var module: Module? {
2021
Self().module
2122
}
2223
}
2324

2425
extension AutoModulable {
26+
@MainActor
2527
private var module: Module? {
2628
guard
2729
let instance = self as? ModuleKeyType.Value,

Sources/DIContainer/Module/Module.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import Foundation
22

33
/// A type that contributes to the object graph.
4+
@preconcurrency
45
public struct Module: Hashable {
56
let name: String
67
let resolve: () -> Any
78

9+
@MainActor
810
public init<T: InjectionKeyType, U>(
911
_ keyType: T.Type,
1012
_ resolve: @escaping () -> U
@@ -13,13 +15,15 @@ public struct Module: Hashable {
1315
self.init(name: name, resolve: resolve)
1416
}
1517

18+
@MainActor
1619
public init<T: AutoModule>(_ moduleType: T.Type) {
1720
let name = KeyName(moduleType.ModuleKeyType.self).name
1821
self.init(name: name, resolve: {
1922
moduleType.init()
2023
})
2124
}
2225

26+
@MainActor
2327
init(name: String, resolve: @escaping () -> Any) {
2428
self.name = name
2529
self.resolve = resolve

Sources/DIContainer/PropertyWrapper/Inject.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class Inject<Value> {
1616

1717
public init<K>(_ key: K.Type) where K: InjectionKeyType, Value == K.Value {
1818
lazyValue = {
19-
key.value
19+
Container.resolve(for: K.self)
2020
}
2121
}
2222
}

Sources/DIContainer/PropertyWrapper/WeakInject.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class WeakInject<Value> {
1616

1717
public init<K>(_ key: K.Type) where K: InjectionKeyType, Value == K.Value {
1818
lazyValue = {
19-
key.weakValue
19+
Container.weakResolve(for: K.self)
2020
}
2121
}
2222
}

Sources/DIContainer/Scanner/MachOLoader/MachOLoader.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public extension MachOLoader {
5858
return results
5959
}
6060

61+
@MainActor
6162
var scanModuleList: [Module] {
6263
scanModuleTypeList
6364
.compactMap { ($0 as? any AutoModule.Type)?.module }

Sources/DIContainer/Scanner/ObjcRuntime/ModuleScanner.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public struct ModuleScanner {
126126
return modules
127127
}
128128

129+
@MainActor
129130
public var scanModuleList: [Module] {
130131
scanModuleTypeList
131132
.compactMap { ($0 as? any AutoModule.Type)?.module }

0 commit comments

Comments
 (0)