Skip to content

Commit

Permalink
Support arguments for providers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ивлев Александр Евгеньевич committed Sep 27, 2018
1 parent 30ad91e commit 49e7567
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v3.5.1
* Support arguments for providers
* Add empty Lazy/Provider initialization with fatalError

# v3.5.0
* Support container Hierarchy

Expand Down
2 changes: 1 addition & 1 deletion DITranquillity.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = 'DITranquillity'
s.version = '3.5.0'
s.version = '3.5.1'
s.summary = 'DITranquillity - Dependency injection for iOS/macOS/tvOS (Swift) '

s.description = <<-DESC
Expand Down
2 changes: 1 addition & 1 deletion Sources/Core/Private/ProtocolMagic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Weak<T> {
/// Delay types (lazy, provider)

protocol DelayMaker: WrappedType {
init(_ factory: @escaping () -> Any?)
init(_ container: DIContainer, _ factory: @escaping () -> Any?)
}

////// For remove optional type
Expand Down
2 changes: 1 addition & 1 deletion Sources/Core/Private/Resolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class Resolver {
if let delayMaker = asDelayMaker(type) {
let saveGraph = cache.graph

return delayMaker.init({ () -> Any? in
return delayMaker.init(container, { () -> Any? in
return self.mutex.sync {
self.cache.graph = saveGraph
return self.make(by: type, isMany: isMany, components: components, use: object)
Expand Down
108 changes: 106 additions & 2 deletions Sources/Extensions/SwiftLazy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
import SwiftLazy

extension Lazy: DelayMaker {
convenience init(_ factory: @escaping () -> Any?) {
public convenience init(file: String = #file, line: UInt = #line) {
self.init { () -> Value in
let name = (file as NSString).lastPathComponent
fatalError("Please inject this property from DI in file: \(name) on line: \(line). Lazy type: \(Value.self) ")
}
}

convenience init(_ container: DIContainer, _ factory: @escaping () -> Any?) {
self.init { () -> Value in
return gmake(by: factory())
}
Expand All @@ -21,7 +28,14 @@ extension Lazy: DelayMaker {
}

extension Provider: DelayMaker {
convenience init(_ factory: @escaping () -> Any?) {
public convenience init(file: String = #file, line: UInt = #line) {
self.init { () -> Value in
let name = (file as NSString).lastPathComponent
fatalError("Please inject this property from DI in file: \(name) on line: \(line). Provider type: \(Value.self) ")
}
}

convenience init(_ container: DIContainer, _ factory: @escaping () -> Any?) {
self.init { () -> Value in
return gmake(by: factory())
}
Expand All @@ -32,3 +46,93 @@ extension Provider: DelayMaker {
}
}

// Providers with args
extension Provider1: DelayMaker {
public convenience init(file: String = #file, line: UInt = #line) {
self.init { _ -> Value in
let name = (file as NSString).lastPathComponent
fatalError("Please inject this property from DI in file: \(name) on line: \(line). Provider type: \(Value.self) ")
}
}

static var type: DIAType { return Value.self }

convenience init(_ container: DIContainer, _ factory: @escaping () -> Any?) {
self.init { (arg1) -> Value in
container.extensions(for: Value.self)?.setArgs(arg1)
return gmake(by: factory())
}
}
}

extension Provider2: DelayMaker {
public convenience init(file: String = #file, line: UInt = #line) {
self.init { _, _ -> Value in
let name = (file as NSString).lastPathComponent
fatalError("Please inject this property from DI in file: \(name) on line: \(line). Provider type: \(Value.self) ")
}
}

static var type: DIAType { return Value.self }

convenience init(_ container: DIContainer, _ factory: @escaping () -> Any?) {
self.init { (arg1, arg2) -> Value in
container.extensions(for: Value.self)?.setArgs(arg1, arg2)
return gmake(by: factory())
}
}
}

extension Provider3: DelayMaker {
public convenience init(file: String = #file, line: UInt = #line) {
self.init { _, _, _ -> Value in
let name = (file as NSString).lastPathComponent
fatalError("Please inject this property from DI in file: \(name) on line: \(line). Provider type: \(Value.self) ")
}
}

static var type: DIAType { return Value.self }

convenience init(_ container: DIContainer, _ factory: @escaping () -> Any?) {
self.init { (arg1, arg2, arg3) -> Value in
container.extensions(for: Value.self)?.setArgs(arg1, arg2, arg3)
return gmake(by: factory())
}
}
}

extension Provider4: DelayMaker {
public convenience init(file: String = #file, line: UInt = #line) {
self.init { _, _, _, _ -> Value in
let name = (file as NSString).lastPathComponent
fatalError("Please inject this property from DI in file: \(name) on line: \(line). Provider type: \(Value.self) ")
}
}

static var type: DIAType { return Value.self }

convenience init(_ container: DIContainer, _ factory: @escaping () -> Any?) {
self.init { (arg1, arg2, arg3, arg4) -> Value in
container.extensions(for: Value.self)?.setArgs(arg1, arg2, arg3, arg4)
return gmake(by: factory())
}
}
}

extension Provider5: DelayMaker {
public convenience init(file: String = #file, line: UInt = #line) {
self.init { _, _, _, _, _ -> Value in
let name = (file as NSString).lastPathComponent
fatalError("Please inject this property from DI in file: \(name) on line: \(line). Provider type: \(Value.self) ")
}
}

static var type: DIAType { return Value.self }

convenience init(_ container: DIContainer, _ factory: @escaping () -> Any?) {
self.init { (arg1, arg2, arg3, arg4, arg5) -> Value in
container.extensions(for: Value.self)?.setArgs(arg1, arg2, arg3, arg4, arg5)
return gmake(by: factory())
}
}
}
75 changes: 75 additions & 0 deletions Tests/DITranquillityTest/DITranquillityTests_SwiftLazy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,33 @@ private class ProviderInjectA {
var inject: Provider<ServiceProtocol>!
}

private class A1 {
fileprivate let value1: Int
init(_ value1: Int) { self.value1 = value1 }
}
private class Provider1InjectA {
var inject: Provider1<A1, Int> = Provider1()
}

private class A2 {
fileprivate let value1: Int
fileprivate let value2: Double
init(_ value1: Int, _ value2: Double) { self.value1 = value1; self.value2 = value2 }
}
private class Provider2InjectA {
var inject: Provider2<A2, Int, Double> = Provider2()
}

private class A3 {
fileprivate let value1: Int
fileprivate let value2: Double
fileprivate let value3: String
init(_ value1: Int, _ value2: Double, _ value3: String) { self.value1 = value1; self.value2 = value2; self.value3 = value3 }
}
private class Provider3InjectA {
var inject: Provider3<A3, Int, Double, String> = Provider3()
}


class DITranquillityTests_SwiftLazy: XCTestCase {
override func setUp() {
Expand Down Expand Up @@ -239,4 +266,52 @@ class DITranquillityTests_SwiftLazy: XCTestCase {
XCTAssertEqual(test.inject.value.foo(), "foo")
}

func test11_Provider1() {
let container = DIContainer()

container.register1{ A1(arg($0)) }

container.register(Provider1InjectA.init)
.injection(\.inject)

let test: Provider1InjectA = *container


XCTAssertEqual(test.inject.value(10).value1, 10)
XCTAssertEqual(test.inject.value(12).value1, 12)
}

func test11_Provider2() {
let container = DIContainer()

container.register{ A2(arg($0), arg($1)) }

container.register(Provider2InjectA.init)
.injection(\.inject)

let test: Provider2InjectA = *container


let a = test.inject.value(10, 15.0)
XCTAssertEqual(a.value1, 10)
XCTAssertEqual(a.value2, 15.0)
}

func test11_Provider3() {
let container = DIContainer()

container.register{ A3(arg($0), arg($1), arg($2)) }

container.register(Provider3InjectA.init)
.injection(\.inject)

let test: Provider3InjectA = *container


let a = test.inject.value(11, 12.0, "a")
XCTAssertEqual(a.value1, 11)
XCTAssertEqual(a.value2, 12.0)
XCTAssertEqual(a.value3, "a")
}

}

0 comments on commit 49e7567

Please sign in to comment.